in reply to What I Most Recently Learned in Perl, But Should Have Already Known
#!/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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: What I Most Recently Learned in Perl, But Should Have Already Known
by pKai (Priest) on Aug 17, 2006 at 22:55 UTC |