in reply to scoping inside the loop
- Case1: you're resetting $i each time at the beginning
- Case3 the $i scoped within the block is differentš from the outer one
(update: and you are resetting the inner one each time again)
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)
Update
expanded code example
Footnotes
1) see Coping with Scoping
In Section
Seekers of Perl Wisdom