Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

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

by BrowserUk (Patriarch)
on Feb 26, 2012 at 04:25 UTC ( [id://956181]=note: print w/replies, xml ) Need Help??


in reply to Help! My variables are jumping off a cliff!

What's so hard to understand?

Strict ensures that variables are declared:

C:\test>perl -Mstrict -E"$x = 1; $x = 10" Global symbol "$x" requires explicit package name at -e line 1. Global symbol "$x" requires explicit package name at -e line 1. Execution of -e aborted due to compilation errors. C:\test>perl -Mstrict -E"my $x = 1; my $x = 10"

Warnings will tell you if you did it twice at in the same scope:

C:\test>perl -Mstrict -wE"my $x = 1; my $x = 10" "my" variable $x masks earlier declaration in same scope at -e line 1.

Declaring a variable twice isn't an error, just unnecessary, hence a warning rather than a fatal error.


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?

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

    Declaring a variable twice isn't an error, just unnecessary

    Declaring a variable twice in the same scope is indeed an error - and of exactly the same type as 'strict' is supposed to prevent. Perl may not throw a warning about it as a default behavior, but that does not make it not an error. Is there ever a situation in which declaring a variable twice in the same scope is useful? Conversely, is there ever a time when doing so will not cause problems?

    It seems to me that reasonable answers to both of the above questions imply that this should indeed be a feature in "strict" - if not in Perl itself. It would have rather obvious positive effects, and no negative ones that I can think of.

    -- 
    I hate storms, but calms undermine my spirits.
     -- Bernard Moitessier, "The Long Way"
      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.

        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"
      Declaring a variable twice in the same scope is indeed an error ...

      By who's definition? Certainly not Perl's. Or strict's.

      and of exactly the same type as 'strict' is supposed to prevent.

      And yet -- it doesn't. Which could mean one of two things:

      1. The strict module has had a bug all these years and nobody noticed until you did.
      2. You've misunderstood.

      I'll let you decide which you want to believe.

      And while you do that, I'll continue to believe the evidence of the implementation, and bet you that nothing you say or do will make it change in the future.


      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?

      A reply falls below the community's threshold of quality. You may see it by logging in.

      Is there ever a situation in which declaring a variable twice in the same scope is useful? Conversely, is there ever a time when doing so will not cause problems?

      Yes - when a variable is no longer needed, then there's no harm in redeclaring it and using it for a different purpose.

      For example, imagine the following test script:

      use Test::More; use strict; no warnings; diag "Testing that 'uc' works."; plan tests => 3; my $result = uc 'foo'; is $result, 'FOO'; my $result = uc 'bar'; is $result, 'BAR'; my $result = uc 'baz'; is $result, 'BAZ';

      Certainly different variables could be used for each test, or the my could be dropped from the second and third tests, but why should Perl insist upon it. The code above is perfectly clear, and runs fine.

        Yes, it's possible to do. I already knew that (and had, in fact, demonstrated it in my very first post.) That's not what I was asking about.

        Because, you see, I have also shown that it can be, and usually is, an error - one so bad that it already has a name in common usage (variable suicide.) My contention is that errors of that sort need to be highlighted very strongly - and most other languages do so, generally by failing at compile time. Perl itself does nothing about it. In fact, even the additional warnings mechanism considers it a 'misc'-category warning; for example, using common::sense, which selects a "sensible set of warnings", eliminates that warning entirely.

        You have re-demonstrated that it can be done. That's not the same thing as showing that it is useful - and even if it was... smoke alarms are installed in bedrooms and plane lavatories for a very good reason, one that the majority of us humans agree with: that it can do much more damage than any good that it prevents.

        My contention is that this type of error should be taken much more seriously than it is, for that same reason.

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

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

      No, it's not. The following code works just fine:

      my $x = 1; say $x; my $x = 2; say $x;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (8)
As of 2024-04-23 10:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found