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


in reply to Re: Re: Think for yourself.
in thread is the use of map in a void context deprecated ?

Because its name and general usage strongly suggests that it should return something.

But where is this notion coming from? "map" as an operation that maps a function over all the elements of a list doesn't come from Perl (I've been familiar with the operation and the name even before Larry released Perl 1.000), and its name doesn't suggest anything about return values shall be used.

Someone, or something, has seeded this notion that the first argument of map should be side-effect free, and it wasn't Larry - otherwise $_ wouldn't be an alias.

Take a sub like get_value if you saw that in void context wouldn't you wonder what is going on?

Sure, I'd figure that either the caller hadn't understand the working of get_value, or that the name of the function was misleading. But "map" isn't called "get_value". It's just "map". Short for "mapping a function over a list". And that's just what it's doing. It isn't called "construct a list by collecting return values". In fact, if I want to add one to all elements of an array, I find:

map {$_ += 1} @array;
to be far more natural than:
for (@array) {$_ += 1}

I find it more surprising that $_ is an alias for the list element in a 'for' loop than in a 'map' operation.

Let's look at the first sentence of 'perldoc -f map':

Evaluates the BLOCK or EXPR for each element of LIST (locally setting $_ to each element) and returns the list value composed of the results of each such evaluation.
Before it talks about return values, it describes what map does: evaluating a piece of code for each element of the list.

Anyway, you haven't convinced me. I refuse to deal with "map" differently than other functions. And if I can use other functions in void context, I can use "map" in void context.

Abigail

Replies are listed 'Best First'.
Re^?: Think for yourself.
by Aristotle (Chancellor) on Oct 07, 2003 at 22:59 UTC
    IMO the winner is
    ++$_ for @array;
    which reads more naturally than either of your propositions. (Although between the two of yours, I'd prefer the map as well.)
    But where is this notion coming from? "map" as an operation that maps a function over all the elements of a list doesn't come from Perl (I've been familiar with the operation and the name even before Larry released Perl 1.000), and its name doesn't suggest anything about return values shall be used.
    That's you. Most people don't have such a background. For the majority of Perl programmers it is the first list oriented language they have encountered. So maybe it would be more appropriate to say that you already had a preconception that map in void context is fine, which other people don't come with. It certainly explains why you regard map in void context as a completely normal construct and find the opposition to it exasperating.

    Makeshifts last the longest.