in reply to Re^2: $_ functions vs argument-using functions
in thread $_ functions vs argument-using 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){ ... }
if you are interested in functional programming, try having a look into *Higher Order Perl* - you can download it for free.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: $_ functions vs argument-using functions
by tobyink (Canon) on Sep 09, 2013 at 21:26 UTC | |
by LanX (Saint) on Sep 09, 2013 at 21:44 UTC | |
by tobyink (Canon) on Sep 09, 2013 at 22:40 UTC | |
by Laurent_R (Canon) on Sep 09, 2013 at 22:36 UTC | |
|
Re^4: $_ functions vs argument-using functions
by pldanutz (Acolyte) on Sep 09, 2013 at 19:23 UTC | |
by LanX (Saint) on Sep 09, 2013 at 19:31 UTC |