Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Passing an anonymous block to a method.

by kennethk (Abbot)
on Mar 10, 2011 at 15:35 UTC ( [id://892444]=note: print w/replies, xml ) Need Help??


in reply to Passing an anonymous block to a method.

According to Prototypes in perlsub it's possible:
sub mygrep (&@) mygrep { /foo/ } $a, $b, $c

However, I seem to remember a thread a few months back where a monk said it was not worth the hassle. Example lifted from the docs:

#!/usr/bin/perl use strict; use warnings; sub mygrep (&@) { my $code = shift; my @result; foreach $_ (@_) { push(@result, $_) if &$code; } @result; } print join "\n", mygrep { /a/ } qw(cat hat bed den);

Update: I did some searching , but did not uncover the article I was thinking of. I did come across some thoughts from GrandFather, but they did not contain a compelling argument, at least to my eyes.

Update 2: And disregard the above. Somehow I hadn't processed the word "method" from the above.

Replies are listed 'Best First'.
Re^2: Passing an anonymous block to a method.
by Corion (Patriarch) on Mar 10, 2011 at 15:40 UTC

    Unfortunately not, for two reasons:

    • Prototypes are disregarded when a code block is invoked as a method (see perlsub).
    • & as a prototype only works if it is in the first position. $& does not allow to leave the sub from the second parameter.
Re^2: Passing an anonymous block to a method.
by BrowserUk (Patriarch) on Mar 10, 2011 at 15:42 UTC

    Unfortunately (but very usefully), that is being called as a subroutine, not a method. And method syntax doesn't respect prototypes :(


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-04-18 04:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found