Hacker News new | comments | ask | show | jobs | submit login
Examples of using dispatch tables in perl (pin21.com)
36 points by f00li5h on Oct 13, 2010 | hide | past | web | favorite | 13 comments

This looks more like perl scripting than perl programming.

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.

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.

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).

Also note that if you need more power than string dispatch and are already using Moose, there are match_on_type[1], MooseX-Types-VariantTable[2] and MooseX-MultiMethods[3] that already go a long way.

[1] http://search.cpan.org/dist/Moose/lib/Moose/Util/TypeConstra...

[2] http://search.cpan.org/dist/MooseX-Types-VariantTable/

[3] http://search.cpan.org/dist/MooseX-MultiMethods/

I did cover "with some objects" ... although it was far from a complete treatment of all the things one can do with an object system, that's for sure.

I'm really not sure what the difference between "scripting" and "programming" is ... could you point me at a rant on the topic?

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)

  # 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')}){

Two quick questions... Why is the name for the function "controller_edit" instead of "controller_save"? Why is post_only checked as part of the dispatch and not inside the two functions?

Quickie older-style perl alternative structure where I use AUTOLOAD to figure out the exists/can situation:

   use strict;
   use warnings;
   package Controller;
   sub search   {...}
   sub view     {...}
   sub save     {...}
   sub delete   {...}
   sub AUTOLOAD {
       # Do index stuff
   package main;
   my $method = $q->param('do');

Beware this could have a huge security hole if you are taking method names from the user like that -- they can include a qualifying package name (normally you see SUPER:: used in this context, but it could be anything loaded).

In your example not so much, but if the params to that call were also under the user's control. Say IO::File was loaded and I went to 'do=IO::File::open', I might be able to read/write files.

I agree wholeheartedly. Never trust input.

I grabbed that out of an older project, most of my newer ones only have a save.

I didn't know AUTOLOAD could do that... neat!

urls full of ?do=! very tidy example none the less.

(running commands was not the best example, i've gotta admit)

Thanks for the feedback!

yep, HoP is much better than my writing is now ...

nothing wrong with helping others up onto the shoulders of giants though...

Applications are open for YC Summer 2019

Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact