in reply to Declaring a code ref with arguments
The simple answer is that you cannot store a coderef with arguments. What would that mean if you could?
sub test{ print @_; } ## THIS DOESN'T WORK, BUT IF IT DID... my $coderef = \&test( 'this is the argument' ); $coderef->(); # would print 'this is the argument' ); $coderef->( 'a different argument' ); # Would print 'this is the argument'!!
If you really want the sub to always have the same argument(s), then they aren't argument(s)--they are constant(s) :) -- so code the sub that way.
sub test{ print 'this is the argument'; } my $coderef = \&test; $coderef->(); # prints 'this is the argument' ); $coderef->( 'a different argument' ); # Prints 'this is the argument'!!
Problem solved:)
However, if you want to pass different arguments when you invoke the coderef, do so.
my $coderef = sub{ print @_; }; $coderef->( 'Some', 'args' ); # Prints 'someargs'
|
|---|