in reply to scoping inside the loop
Generally better use prefix-constructs like
while () {...}, until () {...} or for (;;) {...}
e.g. Case 3:
use strict; use warnings; my $i=42; print "outside: $i\n"; for (my $i=5; $i>0; $i--) { print "inside: $i\n"; }; print "outside: $i\n"; my $j=666; print "outside: $j\n"; { my $j=5; do { print"inside: $j\n"; $j--; } until($j<=0) } print "outside: $j\n";
prints:
outside: 42 inside: 5 inside: 4 inside: 3 inside: 2 inside: 1 outside: 42 outside: 666 inside: 5 inside: 4 inside: 3 inside: 2 inside: 1 outside: 666
As you can see, does the postfix construction force you to an extra block to limit the scope of the inner $j.
Cheers Rolf
( addicted to the Perl Programming Language)
expanded code example
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: scoping inside the loop
by Anonymous Monk on May 17, 2013 at 18:36 UTC | |
by LanX (Saint) on May 17, 2013 at 18:40 UTC |