[Purdue-pm] Perl 5 Dispatch Tables
Joe Kline
gizmo at purdue.edu
Tue May 28 11:17:17 PDT 2019
Mark,
I'm not sure how a typical dispatch table is violating DRY.
What I typically do is something like:
my ($dispatch_table) = create_dispatch_table($config_info_if_needed);
sub create_dispatch_table ($config) {
my $dispatch = {
'thing_one' => \&thing_one,
'thing_two' => \&thing_two,
};
return($dispatch);
}
I typically don't have some subs I'm calling but directly calling an
anonymous sub go in there without creating a defined sub.
I have been using the table something like:
$key = q(some token or task based on a keyword);
if ( exists $dispatch->{$key} ) {
($some_ref) = $dispatch->{$key}->($key, $some_arg, $another_arg, $baz);
}
As near as I can detail I'm not using the same code over again in a copy
and paste fashion.
The only repeating is the variable name the table is hanging out in and
maybe the key used to call the associated sub ref.
joe
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: OpenPGP digital signature
URL: <https://mail.pm.org/pipermail/purdue-pm/attachments/20190528/242a5e0f/attachment.bin>
More information about the Purdue-pm
mailing list