Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

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

by oko1 (Deacon)
on Feb 26, 2012 at 15:28 UTC ( #956272=note: print w/ replies, xml ) Need Help??


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

my does two things. During compilation, it creates an association between a variable name and its appropriate lexical scope. [...] The strict pragma only cares about the former

Agreed (and thank you! - having my point addressed directly is like a breath of fresh air.) So, given that 'strict' "cares" about the association between a variable name and its lexical scope... doesn't it make sense that it should check that scope for that name already existing in it?

With that said, you can't unilaterally assume that all instances of the double-declaration have unintended runtime consequences.

But I don't assume that. I'm saying that this is usually a dangerous error that could be caught by 'strict' - and if, for whatever reason, you actually need this to happen in your code, we have the 'no' pragma. This is a well-established practice in Perl for a variety of errors of this sort, and it just seems to me that this type of error would fit very well into that process, without creating any problems. I'd be willing to bet significant amounts of money - say, a nickel :) - that doing so would A) not harm anyone's correctly-written code and B) perhaps discover buried errors.

It seems to me that this would be an obviously Good Thing with no negative consequences. Hence my surprise at its absence.

-- 
I hate storms, but calms undermine my spirits.
 -- Bernard Moitessier, "The Long Way"


Comment on Re^4: Help! My variables are jumping off a cliff!
Re^5: Help! My variables are jumping off a cliff!
by chromatic (Archbishop) on Feb 26, 2012 at 22:13 UTC
    ... doesn't it make sense that it should check that scope for that name already existing in it?

    I don't see how that follows. To me, that's like saying that exists on a hash key should return a different answer depending on how many different values that hash has had associated with the key.

    You can convince me that having to enable both strict and warnings is silly busywork (because it is), but adding features from warnings to strict makes little sense to me.

      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"
        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.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (9)
As of 2014-08-20 13:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (114 votes), past polls