Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Passing references to subroutines with parameters

by aijin (Monk)
on Jul 12, 2001 at 22:17 UTC ( #96135=perlquestion: print w/ replies, xml ) Need Help??
aijin has asked for the wisdom of the Perl Monks concerning the following question:

Alright, I know I can do this:

$refsub = \&subfoo; $refsub->('la')->('dee')->('da');

But is it possible to pass a referenced subroutines with it parameters, and then call it with more parameters in the receiving subroutine without losing parameters?

I tried:

#! /usr/bin/perl -w use strict; sub_to_receive('123', \&sub_to_be_passed('xyz')); sub sub_to_be_passed { my ($one) = shift; my ($two) = shift; print "One: $one\n"; print "Two: $two\n"; } sub sub_to_receive { my ($x, $sub_to_run) = @_; $sub_to_run->("lalala"); }

...but it loses the "lalala" parameter and ends up with errors all over the place. Admittedly, I'm not too solid with the whole passing referenced subroutines concept.

What I'm trying to set up is a function which gets passed with a filename as a parameter. This function then gets called with a string over and over by the receiving function, does some nifty manipulation of said string, and then writes the results into the specified file.

I can't change the receiving subroutine, only the referenced one. Does anyone have a solution for me?

-aijin.

Comment on Passing references to subroutines with parameters
Select or Download Code
Re: Passing references to subroutines with parameters
by no_slogan (Deacon) on Jul 12, 2001 at 22:21 UTC
    You want curried functions, which aren't provided for you in perl. Here's a close approximation using an anonymous sub:
    sub_to_receive('123', sub{ sub_to_be_passed('xyz',@_) });
      I understand that curried functions have been accepted for inclusion in Perl 6, though. (see RFC 23)
        Yeah. I can hardly wait for the next Apocalypse.
Re: Passing references to subroutines with parameters
by lhoward (Vicar) on Jul 12, 2001 at 22:21 UTC
    You can do this by changing your call slightly and creating an anonymous subroutine:
    sub_to_receive('123', sub {sub_to_be_passed('xyz',@_)});
    Effectively, you're creating a new (anonymous) subroutine that calls sub_to_be_passed with the first argument already filled in and the others coming from what is passed to the anonymous subroutine when it is called.
Re: Passing references to subroutines with parameters
by runrig (Abbot) on Jul 12, 2001 at 22:29 UTC
    You might do it this way:
    sub_to_receive('123', \&sub_to_be_passed, 'xyz'); sub sub_to_be_passed { my ($one) = shift; my ($two) = shift; print "One: $one\n"; print "Two: $two\n"; } sub sub_to_receive { my ($x, $sub_to_run, @args) = @_; $sub_to_run->(@args, "lalala"); }
      Aijin!

      I would follow runrigs lead. It's flexible and straight forward, and makes it easy to pass multiple arguments to sub_to_pass and the interface is consistent with fun things like  system.

      sub sub_to_receive { # Example usage # sub_to_receive('123', \&sub_to_be_passed, 'arg'); # sub_to_receive('123', \&sub_to_be_passed, 'arg1', 'arg2'); # sub_to_receive('123', \&sub_to_be_passed, @args); # my ($x, $sub_to_run, @args) = @_; $sub_to_run->(@args, "lalala"); }

      --
      Clayton aka "Tex"
Re: Passing references to subroutines with parameters
by suaveant (Parson) on Jul 12, 2001 at 22:35 UTC
    You could do this with a closure, too... and generate as many as you want...
    sub curried { my @args = @_; sub { print(@args,@_); }; } $sub = curried("xyz"); $sub->(abc);
    That will allow you to generate more than one sub but each with their own args prepended. if you don't understand closures here is what is happening... when you call curried it copies @_ into the lexically scoped @args, you then define and return an anonymous subroutine which uses @args... normally @args would go away when curried finishes, but since the anon subroutine still contains it, curried has access to it, and no one else does. Subsequent calls to that sub will always maintain your args, whereas if you call curried again and get another subroutine, the same thing will happen but with the new args, completely separate from the first $sub, which still contains its original... very cool...

    if you want to replace an existing subroutine you can do...

    { my $mysub = \&mysub; *mysub = sub { $mysub->('xyz', @_) }; }
    which will replace an existing sub with a kind of wrapper, again, very cool... let me know if you have any questions
    BTW: This code is actually tested, too :)

    Update if I understand your question, which I read a few more times... you can do this...

    #! /usr/bin/perl -w use strict; sub_to_receive('123', curried(\&sub_to_be_passed,'xyz')); sub sub_to_be_passed { my ($one) = shift; my ($two) = shift; print "One: $one\n"; print "Two: $two\n"; } sub sub_to_receive { my ($x, $sub_to_run) = @_; $sub_to_run->("lalala"); } sub curried { my ($func,@args) = @_; sub { push @args, @_; $func->(@args); } }
    Everytime you run the sub returned by curried, its arguments are pushed onto the list, and just keep building up... is that about what you wanted?

                    - Ant

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://96135]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2014-10-31 07:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (215 votes), past polls