Thanks for spotting my error. I had confused dynamic and lexical. I'll go re-read Variables and Scoping now.
As with the foreach loop, if you add a continue block (even an empty one) the behavior through the main loop block changes.
my $i=1; $i=~/(.)/; while ($i++<=4) { print "\$i = $i\n"; print "first: \$1 = $1\n"; $i=~/(3)/; print "second: \$1 = $1\n"; }continue{} print "after: \$1 = $1\n";
gives
$i = 2 first: $1 = 1 second: $1 = 1 $i = 3 first: $1 = 1 second: $1 = 3 $i = 4 first: $1 = 1 second: $1 = 1 $i = 5 first: $1 = 1 second: $1 = 1 after: $1 = 1
I find the behavior without the continue block quite surprising but it might be intentional and a beneficial "feature" in some cases. If it's an intentional feature, it should be documented but there is no mention of this behavior in the obvious places. Thus my bug report.
At least the behavior is consistent between while and for loops.
In reply to Re^4: Problem with regexp or saved match variable.
by ig
in thread Problem with regexp or saved match variable.
by steve077
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |