What stops you ...
Nothing stops the beginner from using the first approach or from pitching headlong into the yawning chasm it opens up, for the foreach loop is mutating the @data "source" array. Of course, map also aliases $_ to the items of the list over which it iterates and so presents the same pitfall, but I agree it is the idiomatic and preferable approach — but let beginners beware!
c:\@Work\Perl>perl -wMstrict -MData::Dump -le
"my @data = 1 .. 4;
dd \@data;
;;
my @results;
foreach (@data) {
$_ = $_ * 13;
push @results, $_;
}
;;
dd \@data;
dd \@results;
"
[1 .. 4]
[13, 26, 39, 52]
[13, 26, 39, 52]
|