in reply to Re^2: why not listed foreach and if?
in thread why not listed foreach and if?

perldoc for map starts by saying that map "Evaluates the BLOCK or EXPR for each element of LIST ...". So map is primarily an iterator.

Replies are listed 'Best First'.
Re^4: why not listed foreach and if?
by Fletch (Bishop) on Jun 19, 2024 at 04:08 UTC

    And the very next part of that sentence then reads "and composes a list of the results of each such evaluation." It's not for general purpose iteration.

    Also quoth Perl Best Practices (Chapter 6):

    This function [map] is specifically aimed at those situations when you want to process a list of values, to create some kind of related list.

    In fact for a while (until 5.8-ish? looks like) there actually was a performance hit for (bad) code misusing map because the return value wasn't optimized away in void context so you'd pay to make the list of transformed values even though they weren't used.. Use for (or foreach or while) for general iteration, use map for transformation, or grep for filtering; otherwise you're misleading readers about what code is doing.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.