There are just two cases to be concerned about: 1/ $var is local to the loop, 2/ $var is global to the loop.
1/ If $var is local to the loop the desired behaviour is that you get a fresh version of $var each time through the loop. In that case use a my declaration where the first value is assigned to $var within the loop.
2/ If you want $var to retain some previous value from one iteration of the loop to the next (maybe you are looking for a largest value or something) then $var should be global to the loop. In that case use a my declaration just before the loop and assign an appropriate initial value if required.
True laziness is hard work
| [reply] |
Yes, my $var will go out of scope after each iteration. If you want it to stay, you must declare it outside of your for loop.
You also might find this illuminating: Lexical scoping like a fox. Someone else shared it with regard to another thread today.
| [reply] [d/l] |
In modern versions of Perl (I use 5.12) you can use the state keyword.From the docs:
state declares a lexically scoped variable, just like my does. However, those variables will never be reinitialized, contrary to lexical variables that are reinitialized each time their enclosing block is entered.
state variables are enabled only when the use feature "state" pragma is in effect. But take care: even if you leave the loop entirely and re-enter it later, the state variable will still have maintained its last value!
CountZero A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James
| [reply] [d/l] [select] |
Using a state variable doesn't seem appropriate here. What the right thing to do here depends on "whatever". If "whatever" is the same all the time, and the value of the variable doesn't change, moving it outside of the loop is the right thing to do. Otherwise, it should be left as is. It's a common case, and perl actually optimizes this - from a language POV, you get a new variable each time. Internally, structures are reused if possible.
| [reply] |
That code is perfectly fine. In effect, a new var is created every time. As an optimisation, the same variable is reused when possible. | [reply] |