in reply to Hash option/menu loop wierd or usefull?
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 | |
by Limbic~Region (Chancellor) on Dec 07, 2005 at 18:14 UTC | |
|
Re^2: Hash option/menu loop weird or useful?
by grinder (Bishop) on Dec 08, 2005 at 12:40 UTC |