I have something similar in a quick & dirty piece of code that wraps some version control system code. I give it the verb on the command line as an argument, such as "get" or "co" (for checkout). It jumps to a handler for each known verb exactly like you are asking.

I don't remember which of the many possible ways I used to turn the variable into an actual function call. I think today $action->() would do the trick.

But, this is a problem with release code because the user could trigger a jump to any function in the code, not just those you are intending! So to make it more robust I named the option functions something distinct, with a prefix on the verb. E.g. "get" would call sub option_get. If the user typed foo instead, it would complain about the lack of an option_foo, rather than jump to sub foo. Only the intended targets had names of this form.

—John


In reply to Re: Naming Subs by John M. Dlugosz
in thread Naming Subs by eoin

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.