You could also define an AUTOLOAD in TestPkg and forget about it entirely.use CGI qw(:all); my $op = param('operation'); TestPkg->$dispatch('Hello!') if UNIVERSAL::can('TestPkg', $dispatch);
Or maybemy $input = <>; $_->($input) for map /^_/ ? () : *{$TestPkg::{$_}}{CODE} || (), keys %TestPkg::;
Any way you turn it though, I don't see the advantage ofmy $input = <>; my $sub; !/^_/ && *$sub{CODE} && *$sub{CODE}->($input) while ($_, $sub) = each %TestPkg::;
overpackage Foo; use Dispatch; sub bar { ... } sub baz { ... } package main; my $t = Foo->create_dptable; $t->{bar}->($quux);
my $t = { bar => sub { ... }, baz => sub { ... }, }; $t->{bar}->($quux);
If anything, the latter keeps together things that belong together and is quicker to grasp the purpose of.
Fixed minor errors thanks to jryan
Makeshifts last the longest.
In reply to Re^3: Easy dispatch tables. (still easier)
by Aristotle
in thread Easy dispatch tables.
by jryan
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |