On Nov 12, 2005, at 4:12 PM, Randal L. Schwartz wrote:
"Ken" == Ken Williams <[EMAIL PROTECTED]> writes:
Ken> You want:
Ken> eval "$subroutine_name(\$hashref)";
Only if you also want "slow" and "dangerous'. See the other answers
in this thread for safer faster solutions.
Yeah, I know, but I posted it because I didn't really like the other
solutions; while they use "better" (and more complicated) techniques,
they won't actually solve the OP's problem. The "pretend you have
methods instead of subroutines" solution is just wishful thinking, and
the "dispatch tables" solution seems overengineered and won't work
unless the specific list of allowed functions (rather than just a
naming-scheme pattern or similar) is known to the dispatcher.
James, if you don't want to use an eval, and you need to call functions
by name this way, you can just use a symbolic reference:
&{$subroutine_name}($hashref);
If "use strict;" is in effect (I'm guessing it's not ;-), then preface
that line with "no strict qw(refs);" or else it'll blow up.
Note that under the hood this is a dispatch table just like Sherm's
solution, but it uses Perl's symbol tables for dispatch rather than a
new hash.
I'm assuming you're being careful enough in this code that someone
can't come along and call some function that you don't want them to
call.
-Ken