in reply to map sub to list?

chr defaults to using $_, so use: @a = map f( $_ ), @numbers;.

Or define f() to default to $_:

sub f(_){ $_[0]+1 };

Now my @a = map f, @numbers; works.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^2: map sub to list?
by pldanutz (Acolyte) on Sep 09, 2013 at 17:15 UTC
    Thanks to everybody who replied! I think I've got it. When writing @a = map f( $_ ), @numbers; this is a case of map BLOCK LIST (as opposed to map EXPR LIST) right?
      No, using the comma makes it a case of EXPR, LIST.

        I can't seem to be able to reply to other replies (too much nesting probably). Perhaps I'll try the chatterbox or a separate question.

        I was trying to understand if the Perl compiler parses "f ($_)" in "map f ($_), @numbers" as a lambda. If it's not a block, but an expression, it must be either an arithmetic expression, a lambda expression etc. My question was from a syntactic/compiler point of view.

        And as to eager evaluation, obviously this is a counterexample, since map f($_) @nums doesn't immediately, eagerly evaluate f on the current $_, but instead passed a lambda to map()

        Where can I read about functions that have comma and non-comma parsings? This confuses me. Also, what expression is f ($_)? Is it a new function, i.e. a shorthand for sub f2 (_) { f ($_) }