in reply to Re: Howto avoid large hard-coded else-if function calls
in thread Howto avoid large hard-coded else-if function calls
Here's another technique which is kind of like a combination of both the dispatch table, as presented elsewhere in this thread, and interpreting the 'ABC' string directly as code. I think it's generally better, especially in an application architecture which is already object oriented.
{ package Funcs; use Carp; sub A { print "A!\n" } sub B { print "B!\n" } sub C { print "C!\n" } sub D { print "D!\n" } sub E { print "E!\n" } sub AUTOLOAD { our $AUTOLOAD; carp "no $AUTOLOAD"; } } # now, wherever $func_com comes from... Funcs->$_() for split //, $func_com;
Of course, you may have other ways of parsing $func_com, rather than just by single characters; and other ways of validating it in addition to making the calls and barfing on non-existent methods.
|
|---|