noelgolding has asked for the wisdom of the Perl Monks concerning the following question:

Not sure if I am asking this properly, so I hope this code snippet will be able to illustrate my desire.
#!/usr/bin/perl use strict; #this works fine my $methodWithoutParam = \&myMethod; &$methodWithoutParam; # however, the next line actually executes the method. Not desired my $methodWithParam = \&myMethod('My Param'); # and the attempt to call the method generates an error. # Not a CODE reference at ... &$methodWithParam; sub myMethod { my ($param) = @_; if ($param) { print "Woohoo, you have successfully passed in a parameter ($p +aram).\n"; } else { print "You have not attempted to pass in a parameter.\n"; } }
  • Comment on how can I store a reference to a method/function along with the parameters?
  • Download Code

Replies are listed 'Best First'.
Re: how can I store a reference to a method/function along with the parameters?
by Skeeve (Parson) on Aug 11, 2011 at 19:34 UTC

    try

    my $meth_with_param= sub { myMethod('My Param'); };


    s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
    +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e
      Or even:
      my $meth_with_param = sub {myMethod('My Param', @_)};
      so you can call $meth_with_param->(1, 2, 3); which then will call myMethod('My Param', 1, 2, 3)
      Thanks, this is exactly what I was looking for. I don't know how to mark this question as answered, and to mark your reply as the solution. But I really appreciate the help.
Re: how can I store a reference to a method/function along with the parameters?
by johngg (Canon) on Aug 11, 2011 at 20:55 UTC

    I wonder why you need one reference to a subroutine without parameters and one with. Do you anticipate many different parameter combinations, each with its corresponding reference? Why not just invoke the reference to the subroutine with or without parameters?

    knoppix@Microknoppix:~$ perl -Mstrict -wE ' > sub meth { say @_ ? qq{Got params: @_} : q{No params} } > > my $refToMeth = \ &meth; > $refToMeth->(); > say q{-} x 20; > $refToMeth->( qw{ a b c } );' No params -------------------- Got params: a b c knoppix@Microknoppix:~$

    Or create the subroutine reference directly in the first place.

    knoppix@Microknoppix:~$ perl -Mstrict -wE ' > my $refToMeth = sub { say @_ ? qq{Got params: @_} : q{No params} }; > > $refToMeth->(); > say q{-} x 20; > $refToMeth->( qw{ a b c } );' No params -------------------- Got params: a b c knoppix@Microknoppix:~$

    I hope this is helpful.

    Cheers,

    JohnGG

Re: how can I store a reference to a method/function along with the parameters?
by cdarke (Prior) on Aug 12, 2011 at 08:38 UTC
    Or you could use a closure:
    sub myMethod { my ($param) = @_; return sub { if ($param) { print "Woohoo, you have successfully passed in a parameter + ($param).\n"; } else { print "You have not attempted to pass in a parameter.\n"; } } } my $methodWithoutParam = myMethod(); $methodWithoutParam->(); my $methodWithParam = myMethod('My Param'); $methodWithParam->();
Re: how can I store a reference to a method/function along with the parameters?
by Arunbear (Prior) on Aug 12, 2011 at 10:26 UTC
    Store them separately? (And for calling a method, you don't really need a reference)
    use strict; sub myMethod { my $self = shift; my ($param) = @_; if ($param) { print "Woohoo, you have successfully passed in a parameter ($p +aram).\n"; } else { print "You have not attempted to pass in a parameter.\n"; } } my ($method, @params) = ('myMethod', 'foo'); my $object = bless {}; $object->$method(@params);
    output:
    :! perl -w meth.pl Woohoo, you have successfully passed in a parameter (foo).