in reply to Re: Interesting Use for "state" Variables
in thread Interesting Use for "state" Variables
Talking about state, you know your code may not do what you expect it to do, don't you? 'each' is an iterator which keeps state on the hash itself. Which means that if the loop exits prematurely (which could even happen in this code if do_something_with contains a 'last') the next time around, it will iterate over the entire hash.for (1..2) { while (($k,$v) = each %hash) { do_something_with($k, $v); } }
You may want to develop the habit to write keys %hash; in void or scalar context right before each each. (It's not an easy habit to get used to - each isn't common enough for that - half of the time, I forget it my own good advice).
|
|---|