We've now entered the lexical scope in which foo() and bar() already pre-incremented $x when we called them earlier. On the first iteration of the loop we'll pre-increment it again, and print 3. That's probably the most counterintuitive step.
It certainly is counterintuitive! Maybe I'm fuzzy brained tonight, but I am not clear why the first iteration of the loop would be different from the second and third iterations. In other words, I would expect the output to be 12111. Why does a new lexical $x get created the second and third times through the loop, but not the first? The statment my $x; is encountered all three times.
In reply to Re^2: Trying to understand closures
by crashtest
in thread Trying to understand closures
by shine22vn
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |