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().