In the so-called good example, both local $_ could easily have been replaced with my. You gain the readability of a named variable at the tiny cost of having to specify the topic of two operations.
my @a = map { s/\s*(#.*)?$//g; $_ } <DATA>;
my @b;
while (my $group = shift @a) {
if ($group =~ m/\D(\d+)-(\d+)(?:\D|$)/) {
my $range = "$1-$2";
foreach my $i ($1..$2) {
(my $addr = $group) =~ s/(\D)$range(\D|$)/$1$i$2/;
push @a, $addr;
}
} else {
push @b, $group;
}
}
print join("\n", @b);
There are cases where using $_ helps, but I don't think this is one of them.
Note: The localization of $1 and $2 is unnecessary. It's already being done by the for.
Note: local $_ doesn't preserve $_ in all situations, unfortunately. for ($var) is a far better way of aliasing $_.
|