What you've got there is called a Dispatch Table and it is a useful (and common) technique. I'm sure you can pull up some interesting nodes via Super Search if you look for that term.

I would change this line in your code:

$ret = &{$options{$opt}};

To this:

$ret = $options{$opt}->();

a) Is it worth using?

Definitely. It's a great way to handle a large number of options, IMHO.

b) Is this will be taken as a 'bad style' example?

It's a standard technique, so as long as you've got some comments explaining that it's a dispatch table it's fine.

c) Is if-elsif-elsif-elsif-else chain more efficient than this?

It might be, depending on how many options there are. Hash lookups are roughly constant-time operations, whereas a chain of if-elsif-else blocks would increase in time for every new condition added. Further, dividing separate tasks up into small subs is far better from a maintenance standpoint than a gigantic conditional structure.

Update: Expanded answer a bit.


In reply to Re: Hash option/menu loop wierd or usefull? by friedo
in thread Hash option/menu loop wierd or usefull? by mulander

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.