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

Re: Why does each() always re-evaluate its argument?

by ikegami (Patriarch)
on Dec 06, 2023 at 21:27 UTC ( [id://11156145] : note . print w/replies, xml ) Need Help??


in reply to Why does each() always re-evaluate its argument?

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.