> (e.g. map prefers such functions)

thats a misunderstanding, because the normal (ok nowadays recommended) way to use map is with a block using the default var. So "map prefers blocks"!

From this point of view writing map { f($_) } 1..3 is the canonical approach!

But this means overhead, because map calls now two functions in a row (the block means an anonymous function).

To avoid this, some experts prefer using the EXPR syntax  map f, 1..3 which is magic (which means a parsing exception introduced for DWIM and syntactic sugar).

Because a bareword in Perl almost always means a function call², if you need the function reference of f you need to write \&f, but some builtins like map and grep magically (sic) accept expressions.

Since you are replacing the block with f you have to mimic the behaviour to use $_ - a localized global variable - in the function body.¹

Your problems understanding all of this derive from the fact that you are starting with the rare syntax exceptions.

Clearer now?

(BTW: I always wanted to write a cross-manual explaining Perl vs JS, to highlight the details in both languages, but somehow all people told me there is no need... )

Cheers Rolf

( addicted to the Perl Programming Language)

¹) The trick with the (_) prototype is rather new, it passes $_ into @_ if an argument is missing.

²) opposed to languages like JavaScript where a bare f is ALWAYS the reference of function f(a,b,c){ ... }

update

if you are interested in functional programming, try having a look into *Higher Order Perl* - you can download it for free.


In reply to Re^3: $_ functions vs argument-using functions by LanX
in thread $_ functions vs argument-using functions by pldanutz

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.