in reply to Re^2: Balancing Coding Time And Code Quality
in thread Balancing Coding Time And Code Quality

The Blue Camel has this to say about map on page 741:

Because $_ is an alias (implicit reference) into the list's values, this variable can be used to modify the elements of the array.

So you'll be modifying the orginal value, weather it was a list of references or not. Mearly chomping the value, as the code posted does, is unlikely to do real harm, so I wouldn't consider it a problem in this case.

----
I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
-- Schemer

: () { :|:& };:

Note: All code is untested, unless otherwise stated

Replies are listed 'Best First'.
Re^4: Balancing Coding Time And Code Quality
by Coruscate (Sexton) on Dec 04, 2003 at 19:50 UTC

    Aha, I didn't realize that $_ was an alias within a map (greps as well then). Which makes a lot of sense if you take even half a second to think about it. I even threw together an example to prove it to myself.

    # because $_ is an alias, @foo is also modified my @foo = qw/one two three four five six/; my @bar = map { s/[aeiou]//g; $_ } @foo; print join(', ', @foo), $/; # because we localize $_, @foo is not modified my @foo = qw/one two three four five six/; my @bar = map { local $_ = $_; s/[aeiou]//g; $_ } @foo; print join(', ', @foo), $/; # not to mention even when you loop through an array my @foo = qw/one two three four five six/; s/[aeiou]//g for @foo; print join(', ', @foo), $/;

Re: Re: Re^2: Balancing Coding Time And Code Quality
by parv (Parson) on Dec 04, 2003 at 22:18 UTC

    I thought that was common knowledge; guess not...

    In any case, if the input list will not be used again after the map, i see no reason to create a local copy of $_.