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

That's how each works. It works the same way on arrays:
#! /usr/bin/perl use warnings; use strict; use feature qw{ say }; my @a = (a => 1, b => 2, c => 3); sub clone_a { print "cloning \%a\n"; return [@a] } while (my ($index, $val) = each @{ clone_a() }) { print "$index => $val\n"; }

Perl stores the iterator for each in the hash or array structure. As the anonymous hash/array is a different one each time, it has a different iterator.

for (or foreach for those who like to type more) just loops over a list of values. That's why you can for qw( a b c ) - no object needed to store an iterator.

Update: BTW, where's any eval anywhere?

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Replies are listed 'Best First'.
Re^2: Why does each() always re-evaluate its argument?
by Darkwing (Scribe) on Dec 06, 2023 at 13:08 UTC
    Thanks, the "eval", of course, was a typo.
      Please, fix it, so we don't confuse further readers.

      map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]