http://www.perlmonks.org?node_id=22611

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

From the perldoc page for map:

<cite> Note that, because C<$_> is a reference into the list value, it can be used to modify the elements of the array. While this is useful and supported, it can cause bizarre results if the LIST is not a named array. Using a regular C<foreach> loop for this purpose would be clearer in most cases. </cite>

so now, what are the cases where map { s/foo/bar/ } @goo is different from for (@goo) { s/foo/bar/ }? what "bizarre results" is it talking about?

Replies are listed 'Best First'.
(chromatic) Re: map and modifying $_
by chromatic (Archbishop) on Jul 14, 2000 at 22:56 UTC
    @goo is a named array. However, if you use a list of some sort: map { s/\d/\\d/ } (1, 2, 3, 4); you'll get happy error messages like Modification of a read-only value attempted at - line 1.
Re: map and modifying $_
by Abigail (Deacon) on Jul 14, 2000 at 23:52 UTC
    I don't understand your question. @goo is a named array, and for (@goo) is not a C-style loop.

    Note that perl5.6 broke all programs that did something like map {s/foo/bar/} "foobar";. What has been gained by this breakage is totally unclear to me.

    -- Abigail

Re: map and modifying $_
by Anonymous Monk on Jul 16, 2000 at 18:13 UTC
    use of map in void context deprecated...