Clear questions and runnable code
get the best and fastest answer
Re: Why does each() always re-evaluate its argument?by ikegami (Patriarch)
|on Dec 06, 2023 at 21:27 UTC
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:
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:
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
instead of having to use
What some other newer languages have done instead is provide syntactical support for iterators. The equivalent of the previous snippet is what's executed.
But a shorthand is provided by the language.
You can build iterators in Perl, but it lacks the syntactical support shown here.