- 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
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.