This behavior isn't that suprising if you remember that each isn't returning you an iterator which is magically affected by the operations on previously used iterators -- or can magically affect subsequent iterators.

each is advancing the existing iterator contained in the internals of the hash you are working with -- that iterator is shared by everyone who has a refrence to that hash, in the same way the data inside the hash is shared by everyone with a refrence to it.

Put another way: If perl looked more like java, then i'd agree with you that this is a bug...

my $iter = %data.iter; while (my ($key, $val) = each($iter)) { last if ($key eq 'drop_out'); print("$key => $val\n"); }

...but as it stands, this behavior is no more suprising to me then if you were looping over the keys to a hash, and modifying the values -- and then outside of the loop you discovered that the hash had been changed.


In reply to Re: The Anomalous each() by hossman
in thread The Anomalous each() by jdhedden

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, details, 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, summary, 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.