in reply to Re: Trying to understand closures
in thread Trying to understand closures
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Trying to understand closures
by davido (Cardinal) on Nov 23, 2006 at 07:13 UTC | |
by tye (Sage) on Nov 23, 2006 at 08:02 UTC | |
by ikegami (Patriarch) on Nov 23, 2006 at 09:15 UTC |