aijin has asked for the wisdom of the Perl Monks concerning the following question:
$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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Passing references to subroutines with parameters
by suaveant (Parson) on Jul 12, 2001 at 22:35 UTC | |
|
Re: Passing references to subroutines with parameters
by no_slogan (Deacon) on Jul 12, 2001 at 22:21 UTC | |
by John M. Dlugosz (Monsignor) on Jul 12, 2001 at 23:07 UTC | |
by no_slogan (Deacon) on Jul 13, 2001 at 00:17 UTC | |
|
Re: Passing references to subroutines with parameters
by lhoward (Vicar) on Jul 12, 2001 at 22:21 UTC | |
|
Re: Passing references to subroutines with parameters
by runrig (Abbot) on Jul 12, 2001 at 22:29 UTC | |
by clscott (Friar) on Jul 13, 2001 at 18:53 UTC |