Think about Loose Coupling | |
PerlMonks |
Re: foreach-loop-local var in subby choroba (Cardinal) |
on Jan 21, 2013 at 16:02 UTC ( [id://1014476]=note: print w/replies, xml ) | Need Help?? |
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.
لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
In Section
Seekers of Perl Wisdom
|
|