Darkwing has asked for the wisdom of the Perl Monks concerning the following question:
Hi Monks,
During development, I noticed a behavior of eval that I had not expected
This results in an infinite loop, printinguse strict; use warnings; my %h = (a => 1, b => 2, c => 3); sub clone_h { print "cloning \%a\n"; return +{%h}; } while (my ($key, $val) = each(%{clone_h()})) { print("$key => $val\n"); }
cloning %a b => 2 cloning %a c => 3 cloning %a a => 1 cloning %a c => 3 cloning %a a => 1 cloning %a a => 1 cloning %a b => 2 cloning %a c => 3 ...
Obviously, clone_h() is called again and again. Of course, I can work around this by first saving the result of clone_h() in a temp variable and then put the variable into the each(). But why does each() not just evaluate its argument and then work on the result?
I tried this with perl 5.10, 5.14 and 5.38.
But: The corresponding code using an array and foreach() works as expected:
use strict; use warnings; my @a = qw(a b c); sub clone_a { print "cloning \@a\n"; return [@a]; } foreach my $entry (@{clone_a()}) { print "$entry\n"; }
Why does each (but not foreach) it work like that? Why is a temporary variable forced (which seems redundant to me)?
2023-12-06 Retitled by Discipulus, as per Monastery guidelines
Original title: 'Why does eval() always re-evaluate its argument?'
|
---|