I'd consider using a namespace (package) rather than a dispatch table (hash).
The concept is the same, but you get name checking for free and it's probably faster.
use strict; use warnings;
{
package Dispatch;
sub render_template {
my ($action, $params) = @_;
print "render_template( action => $action, params => $params )
+\n";
}
*home =
*donate =
*news = sub {
my $pkg = shift;
my ($action, $params) = @_;
return render_template($action, $params);
};
sub samples {
my ($dbh) = shift;
print "samples( dbh = $dbh )\n";
}
sub samp {
my $pkg = shift;
samples('$DBH');
}
sub AUTOLOAD {
my $pkg = shift;
our $AUTOLOAD;
print "Error: no such function $AUTOLOAD ( @_ )\n";
}
}
# test:
for my $act ( qw( home donate news samp bogus ))
{
Dispatch->$act($act,"PARAMS");
}
Note - You'll get a "used only once" warning on the assignments to *home etc.
To squelch it, you can enclose those assignments in a block like so:
{
no warnings 'once';
. . .
}
A word spoken in Mind will reach its own level, in the objective world, by its own weight