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


in reply to Ouch! Each! Reentrant it is not

That's not really a "lack of reentrancy". You are never reentering.

As you discovered, each is an iterator, and it will not 'reset' until you either 'wrap', having processed the entire list of contents, or you manually 'reset' it, but how could it be different?

In your example you are calling each in a while loop, and expected that the iterator would 'reset' even though the logic of your code ensures that it will only do so 1/n occasions. It is perfectly legitimate to call an iterator outside of a loop construct:

$key1 = each %hash; ... $key2 = each %hash; ... $key3 = each %hash;

which makes it impossible for each to know when it should 'reset' the iterator without the programmer manually intervening.

However, it seems that each is non-reentrant, in as much as, even localising the hash doesn't appear to give you a separate instance of the iterator:

@h{ 'a'..'z' } = 1..26;; print each %h;; w 23 print each %h;; r 18 print each %h;; a 1 { local %h=%h; print "$k => $v" while ($k,$v) = each %h };; w => 23 a => 1 r => 18 d => 4 x => 24 j => 10 y => 25 u => 21 h => 8 k => 11 g => 7 f => 6 i => 9 t => 20 e => 5 n => 14 m => 13 v => 22 s => 19 l => 12 p => 16 c => 3 b => 2 q => 17 z => 26 o => 15 print each %h;; w 23

which is a pain.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.