Another alternative to the ones suggested. Put the functions in their own package, and have them all become methods. (The difference being that the method will get the calling object as its first argument. If this doesn't make sense to you, then start with perlboot.)

The reason to do that is that if $meth has the name of a method, then $obj->$meth(@args) will dynamically look up the method on the fly.

You don't actually have to be using OO to do that, you can just use the fact that Perl's facilities for OO support don't tell functions and methods apart very well. The following bad hack should work just fine (albeit fairly slowly) for normal functions:

use Carp; # time passes... sub get_func { my $func_name = shift; my $pack = caller(); UNIVERSAL::can($pack, $func_name) || croak("Function '$func_name' not found in package '$pack'"); } # elsewhere what you wanted above becomes $register = get_func($command)->($register, $param);

In reply to Re: Dynamically calling different functions? by tilly
in thread Dynamically calling different functions? by Anonymous Monk

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.