Here’s a fun one that caused perl to go into an infinite loop. I suppose being frugal and not assigning things to variables is not necessarily a good idea.
#!/usr/bin/perl
use strict;
use warnings;
my %thingies = %{{qw/a 1 b 2 c 3/}};
my $c=0;
#while (my ($a,$b) = each %thingies ) {
while (my ($a,$b) = each %{{qw/a 1 b 2 c 3/}} ) {
print "$a $b $c\n";
last if ++$c > 4; # stop the madness
}
Uncommenting out the line above where each uses a pre-assigned version of the same data element works fine. Not knowing anything regarding the internals of Perl makes me scratch my head. I’m sure there is a perfectly good explanation for this and maybe it has even been corrected in a more recent version of Perl?
$ perl -v
This is perl, v5.10.1 (*) built for x86_64-linux-thread-multi
I guess perl does not read minds all of the time. In the tradition of do what I want, not what I wrote I had hoped that perl knew that I only wanted to create the data element once, not each time through the loop. My bad.
Moral: Do not use each() - unless you are aware of all of the gotchas and even them don’t use each.