to find all the elements $_ of @list for which $code->($_) is true, for a hypothetical mygrep (&@), one would callgrep &$code, @list
to achieve the same goal.mygrep \&$code, @list
ikegami pointed out yet another subtlety, which is that grep cannot accept a ‘calculated’ first argument. Thus,
(While I'm at it, ikegami also pointed out that my first example could be written as grep $code->(), @list, which I find completely bizarre.)sub mygrep (&@) { my ( $code, @list ) = @_; return grep &$code, @list +} sub gt { my ( $test ) = @_; return sub { $_ > $test } } grep &{ gt 1 }, qw/1 2 3 1 4/; => () mygrep \&{ gt 1 }, qw/1 2 3 1 4/; => ( 2, 3, 4 )
UPDATE: Actually, having just run the code, it seems to me that the grep and mygrep invocations actually return the same thing (namely, (2, 3, 4)), so I must have misunderstood. Can anyone (like ikegami :-) ) clarify for me?
In reply to Re: coderefs and (&) prototypes
by JadeNB
in thread coderefs and (&) prototypes
by LanX
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |