in reply to Hash option/menu loop wierd or usefull?

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.

Replies are listed 'Best First'.
Re^2: Hash option/menu loop wierd or usefull?
by mulander (Monk) on Dec 07, 2005 at 17:47 UTC
    Thanks friedo, I was lacking the name of such hash usage, maybe that's why I didn't manage to find nodes about it. Thanks again.
Re^2: Hash option/menu loop weird or useful?
by grinder (Bishop) on Dec 08, 2005 at 12:40 UTC

    There's another problem with if () elsif () elsif () ... else chains that bears mentioning.

    It's an artifact of the way Perl source code is compiled down into the form used by the bytecode interpreter running underneath. The problem is that if any expression in any of the elsif conditions throws a warning (for instance, performing a comparison with a variable set to undef will emit an "Uninitialized variable" warning), then perl will report that the problem occurred on the line containing the if, not the actual line in question.

    This is, as you can imagine, a royal pain when debugging if you're not aware of the quirk. In fact it causes a steady trickle of bug reports to be filed. The perl5-porters are currently evaluating how much extra memory it would take to save the additional line number information (that is currently being discarded) into the resulting bytecode in order to report the correct line number. So it may be fixed one day, but until then, dispatch tables are a good way of avoiding this gotcha.

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