in reply to Re^3: Closure confusion in Tkx (declarations in loops - inner vs outer)
in thread Closure confusion in Tkx

I agree with LanX's explanation here, but here's something I put together that may provide a different viewpoint.

In the C-style for-loop and in its while-loop equivalent in LanX's examples here, a closure is formed (if that's the correct term) over two variables, $i and $x. The $i variable is created anew in each iteration through each loop (there's a  my $i = ...; within the scope of the loop), but the $x variable is created only once (in each example). Therefore, the closures are over three different versions of a variable named $i, each of which can have a different value, but only over a single version of a variable named $x, which has the value it ends up with at the end of each of the loops.

In the Perl-style loop example here, the for-loop iterator variable $i is successively aliased to three individual values, each of which individually has a closure formed over it and so can reflect those three values independently; you're seeing the alias of $i in each case and not the value of $i.

Or so I think...

Update: And no, the way the C-style for-loop works in Perl is not a bug; it's the way closures work.


Give a man a fish:  <%-{-{-{-<