If you do not know what a closure is, the following explanation probably won't help much.
That is, the value of the variable within the string is not interpolated until the subroutine is run, and by the time it is run, $i retains the last value it had when the loop completed -- Ie. 10 -- for all instances of the subroutine.
Thus each instance of the subroutine is closing over an alias to a different constant value, 1 .. 10.
In reply to Re: Problem understanding why for loop won't work when foreach is working
by BrowserUk
in thread Problem understanding why for loop won't work when foreach is working
by PatGro
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |