Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

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

by oko1 (Deacon)
on Feb 27, 2012 at 01:03 UTC ( #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!
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
Node Status?
node history
Node Type: note [id://956353]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (10)
As of 2014-12-19 00:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (69 votes), past polls