in reply to Declaring a lexical within a 'if' condition
use strict; (my $x = 15) && $x;
and obviously the following does not throw the error:
use strict; (my $x = 15);$x;
Unfortunately I don't have a good why scoping behaves the way it does in these examples - I would naively expect the my statement to execute before the second term in the conditional was evaluated.
The declared variable is not introduced (is not visible) until after the current statement.
So there you go. I assume the design logic is so the following works-as-hoped:
my $x = 1; for (1 .. 5) { my $x = $x; $x += $_; print $x; } print $x;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Declaring a lexical within a 'if' condition
by LanX (Saint) on Mar 03, 2010 at 16:33 UTC |