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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Ouch! Each! Reentrant it is not
by eric256 (Parson) on Jul 15, 2005 at 16:03 UTC | |
by BrowserUk (Patriarch) on Jul 15, 2005 at 17:14 UTC | |
|
Re^2: Ouch! Each! Reentrant it is not
by truedfx (Monk) on Jul 15, 2005 at 19:36 UTC | |
by BrowserUk (Patriarch) on Jul 15, 2005 at 20:31 UTC |