http://www.perlmonks.org?node_id=1014476


in reply to foreach-loop-local var in sub

Defining a named sub in a loop is not doing what you think. The sub is defined just once, using the $i of the first iteration. On the next iteration, a new $i is created by my, but the sub is not redefined and still references the previous $i. It is better to use parameters to pass values to subroutines.

If you remove my (and, under strict, replace it with our), the intended behaviour will occur, using a global variable.

لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ