Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: 'my' with 'if 0' retains old value

by hubb0r (Pilgrim)
on Apr 21, 2006 at 03:52 UTC ( [id://544770]=note: print w/replies, xml ) Need Help??


in reply to 'my' with 'if 0' retains old value

I know that there is a statement about this in one of the perl docs, although I cannot find it now.

From perlsyn:

NOTE: The behaviour of a "my" statement modified with a statement mod +ifier conditional or loop construct (e.g. "my $x if ...") is undefine +d. The value of the "my" variable may be "undef", any previously ass +igned value, or possibly anything else. Don't rely on it. Future ve +rsions of perl might do something different from the version of perl +you try it out on. Here be dragons.
So the following:
my $v2 = -2 if 0;

Will have undefined and probably unexp(?:exted|licible) behavior. Don't do it! Don't use a my assignment with a conditional!

Define your variable first, then set it using the conditional.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-03-29 13:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found