in reply to strange scope
The scope of $x is the entire if . . . else construct. Since the condition following if is always evaluated, that is where the lone my declaration belongs.
Consider,
but,$ perl -Mstrict -e'if (my $x = 0) {} print $x,$/' Global symbol "$x" requires explicit package name at -e line 1. Execution of -e aborted due to compilation errors. $
I agree that that scope is not what one expects.$ perl -Mstrict -e'if (my $x = 0) {} elsif (1) { print $x,$/}' 0 $
Update: Two more cases,
but,$ perl -Mstrict -e'if ($\) {print $x,$/} elsif (my $x = 0) {} else { p +rint $x,$/}' Global symbol "$x" requires explicit package name at -e line 1. Execution of -e aborted due to compilation errors. $
showing that $x may be declared in an elsif condition to narrow its scope, if not needed in previous clauses.$ perl -Mstrict -e'if ($\) {} elsif (my $x = 0) {} else { print $x,$/} +' 0 $
After Compline,
Zaxo
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: strange scope
by blazar (Canon) on May 23, 2007 at 09:12 UTC |