rovf has asked for the wisdom of the Perl Monks concerning the following question:
The code below gives error Global symbol "$x" requires explicit package name.
Now I admit that this is a somewhat unconventional construct, but taking the perl docs at face value, I thought this should work. For instance, my saysuse strict; use warnings; sub f { $_[0]==15 } if((my $x=15) && f($x)) { print "OK\n" }'
A "my" declares the listed variables to be local (lexically) to the enclosing block, file, or "eval".which means that $x should be interpreted as lexical, not global, in the call of f. Further, && guarantees left-to-right evaluation, so $x should be defined when f is called, so I had expected the output of the program being OK. Of course I can easily rewrite it by declaring $x one line earlier, but I wonder why it does not work the way I tried it.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Declaring a lexical within a 'if' condition
by kennethk (Abbot) on Mar 03, 2010 at 16:04 UTC | |
by LanX (Saint) on Mar 03, 2010 at 16:33 UTC |