It's not a difference between a foreach loop and each; it's not a difference between a foreach loop and a while loop.

A foreach loop is used when we need to iterate a number of times which is known up front.

On the other hand, a while loop allows us to loop a number of times that isn't known ahead of time. An expression is repeatedly evaluated to determine if the loop should be entered again. It MUST be evaluated each time. It wouldn't work otherwise.


So why does following work:

while ( my ( $k, $v ) = each( %h ) ) { ... }

Each array and hash has an iterator associated with it which is used by each, keys and values. Each call to each fetches one element from that iterator (resetting the iterator once the end is reached).

So then what about the following:

while ( my ( $k, $v ) = each( %{ create_new_hash() } ) ) { ... }

A different, newly-created hash is passed to each each time. Each of those hashes has a fresh iterator. Each of those iterator returns the first element of that hash.


Giving each array and hash its own iterator allows us to use

while ( my ( $k, $v ) = each( %h ) ) { ... }

instead of having to use

my $iter = get_iter( \%hash ); while ( my ( $k, $v ) = $iter->() ) { ... }

What some other newer languages have done instead is provide syntactical support for iterators. The equivalent of the previous snippet is what's executed.

using ( var iter = dict.GetEnumerator() ) { while ( iter.MoveNext() ) { var entry = iter.Current; ... } }

But a shorthand is provided by the language.

foreach ( var entry in dict ) { ... }

You can build iterators in Perl, but it lacks the syntactical support shown here.


In reply to Re: Why does each() always re-evaluate its argument? by ikegami
in thread Why does each() always re-evaluate its argument? by Darkwing

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.