in reply to Re^3: map sub to list?
in thread map sub to list?

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 ($_) }

Replies are listed 'Best First'.
Re^5: map sub to list?
by Corion (Patriarch) on Sep 09, 2013 at 17:39 UTC

    Your use of syntax leaves me with the impression that before Perl you only knew Haskell, or some other, lazy / currying functional language.

    The major difference between Perl and these others is that Perl is eager, so any unadorned mention of a function will simply call it. Unless you use built-in functions or fancy syntax tricks, all function parameters are passed in parentheses.

    Creating/returning new functions is quite possible in Perl, but the usual approach is more eager in the sense of passing around values instead of passing around functions that, at some time in the future will be called to produce the final output of the program.

Re^5: map sub to list? (glossary)
by LanX (Saint) on Sep 09, 2013 at 17:44 UTC

    > Where can I read about functions that have comma and non-comma parsings?

    in my own words:

    BLOCK means { code } in curlies, like map { $_ +1 } 1..3, so no comma

    DB<124> map { $_ +1 } 1..3 => (2, 3, 4)

    EXPR can be any "atomar" code-snippet.

    DB<128> map sqrt , 1..3 => (1, "1.4142135623731", "1.73205080756888") DB<129> map 5 , 1..3 => (5, 5, 5)

    in both cases the list values are passed by setting a special variable (aka default variable) $_

    according to perlglossary

    BLOCK A syntactic construct consisting of a sequence of Perl stat +ements that is delimited by braces. The "if" and "while" statemen +ts are defined in terms of BLOCKs, for instance. Sometimes we als +o say "block" to mean a lexical scope; that is, a sequence of sta +tements that act like a "BLOCK", such as within an eval or a file, +even though the statements aren’t delimited by braces. ... expression Anything you can legally say in a spot where a "value" is required. Typically composed of literals, variables, opera +tors, functions, and "subroutine" calls, not necessarily in that +order.

    Cheers Rolf

    ( addicted to the Perl Programming Language)

Re^5: map sub to list?
by toolic (Bishop) on Sep 09, 2013 at 17:34 UTC
    f($_) means that you are calling the function, f, and passing it a scalar value contained in the $_ variable. See also perldoc perlsub