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.
لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: foreach-loop-local var in sub
by warshall (Novice) on Jan 21, 2013 at 16:09 UTC | |
by eyepopslikeamosquito (Archbishop) on Jan 21, 2013 at 20:08 UTC | |
by choroba (Cardinal) on Jan 21, 2013 at 16:15 UTC | |
by Anonymous Monk on Jan 22, 2013 at 13:11 UTC |
In Section
Seekers of Perl Wisdom