Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

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

by oko1 (Deacon)
on Feb 26, 2012 at 15:02 UTC ( #956271=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!

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"


Comment on Re^4: Help! My variables are jumping off a cliff!
Re^5: Help! My variables are jumping off a cliff!
by tobyink (Abbot) on Feb 26, 2012 at 16:07 UTC

    It was certainly not my intention to merely demonstrate that it can be done. That has been established beyond any doubt. You had asked:

    is there ever a time when doing so will not cause problems?

    My example is a case where redeclaring the variable does not cause problems.

Re^5: Help! My variables are jumping off a cliff!
by JavaFan (Canon) on Feb 26, 2012 at 22:29 UTC
    I have also shown that it can be, and usually is, an error
    You haven't. Your OP shows some code, but it's not clear to me whether there's an error. But even I grant you that you have shown that it's possible to make an error, you have utterly failed to even remotely convince me of the "usual" part.

    To go back to your original code:

    my $x = 1; # Variable declaration my $x = 10; # Variable suicide! print $x;
    What's the error here? This prints 10. Should this have printed 1? Do note that if you'd had written:
    my $x = 1; $x = 10; print $x;
    it would have still printed 10.

    You know, it happens probably a few times a week that my code triggers the "masks declaration" warning. I cannot recall a single time where this was actually causing a problem. Just removing the "my" fixes the problem; or a no warnings 'misc';.

      I have indeed shown that it is an error - in the post preceding the one where I made that statement. To recap: you declare a variable in one part of a large program. Later, when adding code, you unintentionally declare the same variable again in another part of the program. You've just smashed the first one, and caused errors in any code that depended on it.

      The error is not that $x now contains 10. The error is that this can happen at all, with minimal (if any, depending on externals) warnings.

      -- 
      I hate storms, but calms undermine my spirits.
       -- Bernard Moitessier, "The Long Way"
        • You should be using warnings and you can easily promote them to errors with FATAL or use something like strictures if its flavor is appealing.
        • This bug has a strong code smell to it. If your code blocks/scopes are large enough that this is happening, they are too large. So I say.
        moo@cow[26]~>perl -Mstrict -le 'my $w = 1; my $w; print "OHAI"' OHAI moo@cow[27]~>perl -Mstrict -wle 'my $w = 1; my $w; print "OHAI"' "my" variable $w masks earlier declaration in same scope at -e line 1. OHAI moo@cow[28]~>perl -Mstrictures -le 'my $w = 1; my $w; print "OHAI"' "my" variable $w masks earlier declaration in same scope at -e line 1. moo@cow[29]~>perl -Mstrict -Mwarnings=FATAL,misc -le 'my $w = 1; my $w +; print "OHAI"' "my" variable $w masks earlier declaration in same scope at -e line 1.
        you declare a variable in one part of a large program. Later, when adding code, you unintentionally declare the same variable again in another part of the program. You've just smashed the first one, and caused errors in any code that depended on it.
        Unfortunally, it's impossible to determine whether any further uses of said variable depend on the original one (then there's an error), or on the new one (then using the same name is harmless). That's why Perl gives you a warning. A warning after all means "heh, coder, I spotted something that may be an error. But I can't know for sure, I'm just carrying on, cause, you know, I'm often quite mistaken about this".

        Just because you made a boo-boo in your code, that you didn't spot because you weren't running with warnings doesn't mean you now have a reason to claim "use strict" is broken. It's not. Your code was.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (12)
As of 2014-07-28 09:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (195 votes), past polls