Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

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

by oko1 (Deacon)
on Feb 27, 2012 at 00:50 UTC ( #956352=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 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"


Comment on Re^6: Help! My variables are jumping off a cliff!
Re^7: Help! My variables are jumping off a cliff!
by Your Mother (Canon) on Feb 27, 2012 at 01:22 UTC
    • 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.
Re^7: Help! My variables are jumping off a cliff!
by JavaFan (Canon) on Feb 27, 2012 at 08:45 UTC
    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://956352]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2014-10-23 06:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (124 votes), past polls