Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Dynamically calling different functions?

by tadman (Prior)
on Jun 30, 2003 at 20:07 UTC ( [id://270298]=note: print w/replies, xml ) Need Help??


in reply to Dynamically calling different functions?

Like what ctilmes suggested, but with a twist:
# Create a HASH which stores a mapping between function names # and the associated function references (\&). Add any other # functions to the quoted-word listing my %commands = map { $_ => \&$_ } qw[ set add mul ]; # ... while (<STDIN>) { my ($command, $param) = split(" ", $_); # Check that the function exists before calling it, could have a # bad $command being passed. Perhaps add a warning in an # else block? if ($command{$command}}) { $register = $command{$command}->($register, $param); } }
It might look a bit weird at first, but the map call works on a single listing that doesn't have duplication. I've also used the $ref->() style of calling which doesn't use an ampersand, but is functionally the same as &$ref().

Log In?
Username:
Password:

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

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

    No recent polls found