This looks more like perl scripting than perl programming.<p>If I put all those functions into a class and then called $class_object->can($method), I get the same effect, only without the crazy hash. Perl has built-in dispatching.<p>I can add new methods to my perl classes at any time. Better yet, if I do it all with Class::MOP (and/or Moose), I can make it cleaner, since then I can choose to add methods to the object vs the whole class. In addition, my "dispatch tables" get the potential benefits of inheritance, roles, and other abstraction tools.<p>It might be a good starting exercise in rolling your own dispatch tables, but you would probably get a lot more out of reading Art of the Metaobject Protocol and trying to implement a good class system (aka reimplement Moose).
I'm pretty new to perl scripting but most of my cgi's at work look like this near the beginning. How is that crazy bit of code better? (Post_only is just a function that returns the function or a 404 if the request isn't a post)<p><pre><code> # Dispatcher
my %action = (
'search' => \&controller_search,
'view' => \&controller_view,
'save' => post_only(\&controller_edit),
'delete' => post_only(\&controller_delete),
'index' => \&controller_index,
);
if(exists $action{$q->param('do')}){
$action{$q->param('do')}->();
}else{
$action{'index'}->();
}
exit;</code></pre>