$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.
In reply to Passing references to subroutines with parameters by aijin
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |