Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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.

In reply to Re: Ouch! Each! Reentrant it is not by BrowserUk
in thread Ouch! Each! Reentrant it is not by Thilosophy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others taking refuge in the Monastery: (7)
    As of 2020-06-01 06:47 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found

      Notices?