in reply to Howto avoid large hard-coded else-if function calls

Here's another way. Its suitability is something you'd have to decide...

$func_com =~ y/ABCDE//cd; $func_com =~ s/(.)/func_$1();/g; eval $func_com;
A word spoken in Mind will reach its own level, in the objective world, by its own weight

Replies are listed 'Best First'.
Re^2: Howto avoid large hard-coded else-if function calls
by jdporter (Paladin) on Jun 25, 2007 at 13:13 UTC

    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.

    A word spoken in Mind will reach its own level, in the objective world, by its own weight
Re^2: Howto avoid large hard-coded else-if function calls
by BrowserUk (Patriarch) on Jun 25, 2007 at 12:15 UTC