in reply to Re: Easy dispatch tables. (pointless?)
in thread Easy dispatch tables.
How about this:
use CGI qw(:all); my $op = param('operation'); my $table = create_dptable TestPkg; $table->{$op}->() if exists $table->{$op};
As opposed to this:
use CGI qw(:all); my $op = param('operation'); { no strict 'refs'; TestPkg->$dispatch('Hello!') if defined *{"TestPkg::$dispatch"}{CODE}; }
Or what about this:
my $input = <>; my $table = create_dptable TestPkg; $table->{$_}->($input) foreach (keys $table);
As opposed to this:
my $input = <>; { no strict 'refs'; *{"TestPkg::$_"}{CODE}->($input) foreach( grep { !/^_/ && defined *{"TestPkg::$_"}{CODE} } keys %{*{'TestPkg::'}} ); }
Essentially, the gain of a dispatch table is the gain of all of the advantages and techniques of working with hashes.
Update: Added another example.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Easy dispatch tables. (still easier)
by Aristotle (Chancellor) on Apr 03, 2003 at 02:54 UTC | |
by jryan (Vicar) on Apr 03, 2003 at 06:57 UTC | |
by Aristotle (Chancellor) on Apr 03, 2003 at 08:25 UTC | |
by jryan (Vicar) on Apr 03, 2003 at 21:59 UTC | |
by Aristotle (Chancellor) on Apr 04, 2003 at 15:25 UTC |