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.
In reply to Re: Re: Easy dispatch tables.
by jryan
in thread Easy dispatch tables.
by jryan
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |