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

Declaring a variable twice in the same scope is indeed an error...

my does two things. During compilation, it creates an association between a variable name and its appropriate lexical scope. During runtime, it initializes the appropriate storage for that variable.

The strict pragma only cares about the former; the documentation has long stated:

This generates a compile-time error if you access a variable that wasn't declared via "our" or "use vars", localized via "my()", or wasn't fully qualified.

While you may be right that strict should warn about double declaration, strict 'vars' only concerns itself with an idempotent operation. You can't create that compile-time association more than once in the same scope because that association is an either-or concern.

With that said, you can't unilaterally assume that all instances of the double-declaration have unintended runtime consequences. Those consequences are also well established and long understood. I can imagine code which does this deliberately.

That's not my particular style, but not everything I find confusing is (nor should it be) an error.


Improve your skills with Modern Perl: the free book.

Replies are listed 'Best First'.
Re^4: Help! My variables are jumping off a cliff!
by oko1 (Deacon) on Feb 26, 2012 at 15:28 UTC

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