Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^6: Help! My variables are jumping off a cliff!

by oko1 (Deacon)
on Feb 27, 2012 at 01:03 UTC ( [id://956353]=note: print w/replies, xml ) Need Help??


in reply to Re^5: Help! My variables are jumping off a cliff!
in thread Help! My variables are jumping off a cliff!

I'm sorry, but I just don't see how your point about 'exists' is at all relevant. I think we're all agreed that declaring the same var twice in the same scope is a mistake - that's why "warnings" throws that error after all - and there's no counterpart to anything like that in using 'exists'.

I guess I'm looking at this from some unique perspective, then - well, except for all the other languages that consider double-declaration like this a fatal flaw. Fair enough; this thing that is considered a critically-bad programming practice elsewhere is not considered as such in Perl. Got it.

Thanks very much to all those who have contributed useful and relevant information.

-- 
I hate storms, but calms undermine my spirits.
 -- Bernard Moitessier, "The Long Way"
  • Comment on Re^6: Help! My variables are jumping off a cliff!

Replies are listed 'Best First'.
Re^7: Help! My variables are jumping off a cliff!
by chromatic (Archbishop) on Feb 27, 2012 at 04:32 UTC
    I think we're all agreed that declaring the same var twice in the same scope is a mistake...

    I certainly don't agree that it's necessarily a mistake, even in the case that Dave Mitchell demonstrated, where the compile-time lexical binding may differ across closures. You can't analyze that statically and get the right answer all of the time!

    Separate the act of assignment from the act of declaration and think about the code again.

    Then separate the judgment of "Would I write code this way?" from "This is a practice so worth discouraging that it needs special casing in the compilation process."

    except for all the other languages that consider double-declaration like this a fatal flaw

    Pure functional languages consider mutable variables a fatal flaw. I don't see how that matters either.

      dave_the_m's example leads to a very practical, if slightly contrived use for double declaration.

      You have a sub that returns two subrefs. Those two subrefs are, as you might assume from their being instantiated at the same time, related. And they both rely upon a variable of some domain specific name that they inherit through closure. But they each need a different one:

      sub generator { ... my $frobnosticator = 0; my $coderef1 = sub{ ...; $frobnosticator ^= 1; ... }; my $frobnosticator = 0; my $coderef1 = sub{ ...; $frobnosticator ^= 1; ... }; return $coderef1, $coderef2; }

      There, perfectly sensible :)


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

      The start of some sanity?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://956353]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (6)
As of 2024-04-18 20:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found