in reply to Being impervious to an 'each' reset

You could make your own iterator.

sub gen_each_iter(\%) { my ($hash) = @_; my @keys = keys(%$hash); return sub { return if not @keys; my $key = shift(@keys); return wantarray ? ($key, $hash->{$key}) : $key; }; }; my $each = gen_each_iter(%foo); while (my ($key, $val) = $each->()) { ... }

If you don't want a general case solution, the above simplifies to

my @keys = keys(%foo); my $each = sub { return if not @keys; my $key = shift(@keys); return ($key, $foo{$key}); }; while (my ($key, $val) = $each->()) { ... }