in reply to Why localize $_ in the map block?
in thread how could I use map here?

Without the local the original array is changed. Since $_ is aliased to each element the s// changes the original array. Try:

my @x = qw(foo bar CHOP_ME_OFF_foo); my @y = @x; my @copies = map { local $_ = $_; s/^CHOP_ME_OFF_//; $_ } @x; my @changes = map {s/^CHOP_ME_OFF_//; $_ } @y; print "x @x\ny @y\ncopies @copies\nchanges @changes\n";

Replies are listed 'Best First'.
Re: Re: Why localize $_ in the map block?
by pike (Monk) on Feb 10, 2003 at 16:30 UTC
    Ahh, very interesting. I remember that in the Camel book where it explains the use of grep, there is an example along the lines of:

    @subset = grep {s/x/y/} @set;
    and it points out that this will alter the entries in @set, but it never mentions that you can avoid this by localizing $_...

    pike