atemerev
I'm away from a perl interpreter right now so this little snippet (from the the Perl Cookbook, recipe 11.4) will just have to do:
my %commands = ( "happy" => \&joy, "sad" => \&sullen, "done" => sub { die "See ya!" }, "mad" => \&angry, ); print "How are you? "; chomp($string = <STDIN>); if ($commands{$string}) { $commands{$string}->(); } else { print "No such command: $string\n"; }
The idea here is to store your function/method references in a hash (%commands). Then you call the method by looking it up in the hash, and passing in the params. I don't remember the exact context for this, but, most of the time when you're using eval'd strings or "soft references" you can use a hash to achieve the same results.
Good luck with it!
Kurt
In reply to Re: Calling a method by name without eval()
by whereiskurt
in thread Calling a method by name without eval()
by atemerev
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |