in reply to
What I Most Recently Learned in Perl, But Should Have Already Known
Using a foreach loop on an AoH creates an alias of the hash which goes out of scope after the loop.
#!/usr/bin/perl
use strict;
use warnings;
my $recs = [
# short records for brevity
{id => 1},
{id => 2},
{id => 3},
];
my $rec;
for $rec (@{$recs}){
# $rec is an alias...
if ($rec->{id} == 1){
last;
}
}
# ... which is now out of scope :-(
print "$rec->{id}\n"; # Use of uninitialized value...
# what I should have done
($rec) = grep {$_->{id} == 1} @{$recs};
print "$rec->{id}\n"; # 1