I think a dispatch table will rapidly become unwieldy if you need to consider all possible function call combinations of A, B, C, D and E. Assuming A must always be called first, and C must be called between B and D (if present), and so on, you are looking at the power set of A .. E. And the size of the power set of a set increases exponentially.

Here's all the different ways you can call functions A to E:

perl -MData::PowerSet -le 'my $p = Data::PowerSet->new(qw(A B C D E)); + print "@$_" while $_= $p->next' # produces : A B C D E B C D E A C D E C D E A B D E B D E A D E D E A B C E B C E A C E C E A B E B E A E E A B C D B C D A C D C D A B D B D A D D A B C B C A C C A B B A

Add three more function and the size of your dispatch table grows to 256 entries... and it gets much worse if you can call any of A to E in any order.

I think it would be much better to just split your 'ABC' input into separate characters, and use a simple dispatch to deal with the five (or more) elementary cases:

my %dispatch = ( A => \&func_a, B => \&func_b, C => \&func_c, D => \&func_d, E => \&func_e, ); for my $token (split //, $func_com) { push @all_res, [$dispatch{$token}->(@some_val)]; }

This way it is as trivial to deal with 'ABC', 'AD', 'BCE' as it is to deal with 'AABD' or 'BEAD'.

• another intruder with the mooring in the heart of the Perl


In reply to Re: Howto avoid large hard-coded else-if function calls by grinder
in thread Howto avoid large hard-coded else-if function calls by neversaint

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.