in reply to Re: Trying to understand closures
in thread Trying to understand closures
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Trying to understand closures
by superfrink (Curate) on Nov 24, 2006 at 05:51 UTC | |
by ikegami (Patriarch) on Nov 24, 2006 at 05:54 UTC | |
|
Re^3: Trying to understand closures
by gam3 (Curate) on Nov 24, 2006 at 13:01 UTC |