No, no optimization was involved. Don't confuse my $x with undef $x. my $x does NOT change the value of $x. Aside from returning $x's value, all my $x does is set a flag to clear $x at the end of the current scope (i.e. at the end of the loop pass).
Can you guess what the following snippet prints?
for (1..4) { my $x; print ++$x; } print "\n"; for (1..4) { goto SKIP if $_ == 2; my $y; SKIP: print ++$y; } print "\n"; for (1..4) { my $z if $_ != 2; print ++$z; }
At the end of every pass of the first loop, $x gets cleared, so the output of that loop is 1111.
At the end of every pass *except the second* of the second loop, $y gets cleared, so the output of that loop is 1121.
The third loop is identical to the second one. It also outputs 1121.
Updated: Reworded for clarity. No new content.
Updated: Added code.
In reply to Re^2: Trying to understand closures
by ikegami
in thread Trying to understand closures
by shine22vn
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |