Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Ouch! Each! Reentrant it is not

by BrowserUk (Pope)
on Jul 15, 2005 at 03:01 UTC ( #475111=note: print w/replies, xml ) Need Help??


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.

Replies are listed 'Best First'.
Re^2: Ouch! Each! Reentrant it is not
by eric256 (Parson) on Jul 15, 2005 at 16:03 UTC

    It would seem that if the iterator was scoped then this wouldn't be a problem. Falling out of scope (end of the for each, or sub) would cause it to be deleted. The next call would reset it, of course this couldn't be changed now because there is surely code depending on that behaviour. I wonder if perl6 handles this better. Maybe in it hashes can return an iterator (or maybe they should if they don't).


    ___________
    Eric Hodges

      I really expected that localising the hash would localise the iterator. If anything were to be done about the current situation, making it so that it was would seem to be the most transparent way least likely to interfere with existing code use.

      I'd be very surprised if this wasn't fixed in P6.


      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.
Re^2: Ouch! Each! Reentrant it is not
by truedfx (Monk) on Jul 15, 2005 at 19:36 UTC
    Localising does give you a local iterator. Try this code:
    %a = ( A=>0, B=>1, C=>2 ); %b = %a; print each %b, "\n"; { local %b = %a; each %b; } while(($k,$v)=each %b) { print $k, $v, "\n" }
    Then, try this:
    %h = ( A=>0, B=>1, C=>2 ); print each %h, "\n"; () = %h; print each %h, "\n";
    The problem with local %h = %h is not that the local copy shares the iterator, but that the original %h gets its iterator reset by being read. This is mentioned briefly in the documentation for the each function.

      Hmmm. So you would have to localise and copy before using the outer level iterator in order to preserve it across a (possible) inner level iterator? Not very useful.


      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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://475111]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (5)
As of 2020-05-31 11:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If programming languages were movie genres, Perl would be:















    Results (173 votes). Check out past polls.

    Notices?