in reply to Why does each() always re-evaluate its argument?
each expects a hash (or array with "newer" perls).
The %{} does dereferencing of the enclosed expression, which needs to be evaluated.
There is no way to tell for %{} if and why the result should be cached.
To elaborate further, each is not a loop construct like foreach
It's more like a method operating on the hash, with no clear entry point.
Think %hash->each() and you can call each anytime outside while.
You may rather want to take a look at this newer (5.36 experimental) syntax:
use experimental "for_list"; foreach my ($key, $value) (%hash) { # iterate over the hash # The hash is immediately copied to a flat list before the loop # starts. The list contains copies of keys but aliases of values. # This is the same behaviour as for $var (%hash) {...} }
Cheers Rolf
(addicted to the Perl Programming Language :)
see Wikisyntax for the Monastery
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Why does each() always re-evaluate its argument? (Updated x2 - experimental "for_list" )
by eyepopslikeamosquito (Archbishop) on Dec 06, 2023 at 23:30 UTC | |
by LanX (Saint) on Dec 07, 2023 at 01:46 UTC | |
by ikegami (Patriarch) on Dec 07, 2023 at 01:58 UTC | |
by LanX (Saint) on Dec 07, 2023 at 02:49 UTC | |
by ikegami (Patriarch) on Dec 07, 2023 at 20:11 UTC | |
| |
|
Re^2: Why does each() always re-evaluate its argument? (Updated x2 - experimental "for_list" )
by Anonymous Monk on Dec 06, 2023 at 15:42 UTC |