Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re^4: map sub to list?

by pldanutz (Acolyte)
on Sep 09, 2013 at 17:28 UTC ( #1053051=note: print w/replies, xml ) Need Help??

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 (Sage) 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

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1053051]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2022-09-29 05:52 GMT
Find Nodes?
    Voting Booth?
    I prefer my indexes to start at:

    Results (125 votes). Check out past polls.