in reply to Passing references to subroutines with parameters
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...sub curried { my @args = @_; sub { print(@args,@_); }; } $sub = curried("xyz"); $sub->(abc);
if you want to replace an existing subroutine you can do...
which will replace an existing sub with a kind of wrapper, again, very cool... let me know if you have any questions{ my $mysub = \&mysub; *mysub = sub { $mysub->('xyz', @_) }; }
Update if I understand your question, which I read a few more times... you can do this...
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?#! /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); } }
- Ant
|
|---|