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


In reply to Re: Passing references to subroutines with parameters by suaveant
in thread Passing references to subroutines with parameters by aijin

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.