Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Declaring a code ref with arguments

by broquaint (Abbot)
on Aug 05, 2003 at 09:13 UTC ( [id://280928]=note: print w/replies, xml ) Need Help??


in reply to Declaring a code ref with arguments

The best way to do this generally is to pass in an anonymous sub which executes the sub with the desired arguments e.g
sub print_me { my $arg = shift || "Hello World!\n"; print $arg; } %hash = ( no_arg => \&print_me, with_arg => sub { print_me("Hello World, again!\n") }, ); $hash{no_arg}->(); $hash{with_arg}->(); __output__ Hello World! Hello World, again!
Also, you want parens, not curly braces when assigning to a hash as curly braces will return a single element of an anonymous hash reference, whereas using parens will return a list (in this case) which will duely populate %hash. See. perlreftut and tye's References quick reference for more info.
HTH

_________
broquaint

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://280928]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-04-26 09:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found