Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Implementing Dispatch Tables

by Roy Johnson (Monsignor)
on May 12, 2005 at 19:27 UTC ( #456530=perltutorial: print w/ replies, xml ) Need Help??

A dispatch table is just a mapping from strings to actions. If you find yourself lamenting the lack of a C-style switch/case statement in Perl, and you find yourself grumbling that the equivalent series of if statements is a linear search, you probably want a dispatch table. In Perl, the hash is a natural way to implement this.

There is an implementation in CPAN, but I don't think it's what we're looking for. It's certainly not straightforward, and dispatch tables are easy enough to roll your own.

The trivial case

A basic dispatch table might look like this:
my %disp_table = ( something => \&do_something );
You put a string in, you get a code ref out. Then, presumably, you execute the code ref:
$disp_table{'something'}->();
What could be simpler?

Defaulting

That's great if what you're looking for is in the table, but if it's not, you're going to get an error for trying to use an empty string as a subroutine ref, because the hash value came up undef. So you need to have an alternative:
($disp_table{'not there'} || sub {})->();
Now if the hash lookup returns nothing, you fall back on a subroutine that doesn't do anything. Of course, you could use a default that did something if you wanted to.

Fall-through (chaining)

What if you've got a bunch of things that map to the same action? You could duplicate your code for each of them:
one => sub { print "Number!\n" }, two => sub { print "Number!\n" }, three => sub { print "Number!\n" },
which should then raise a red flag that you need to abstract it out into its own subroutine. And if 'one' is supposed to do something before falling through, you can embed that sub call in its custom action:
one => sub { print "The loneliest "; number() }, two => \&number, three => \&number,
By chaining calls this way, you can make things "fall through" to whatever you want them to. They are not limited by what comes next (which is good, because in a hash, what comes next is unpredictable).

Matching patterns and ranges

Up until a few minutes ago, I would have said that using a dispatch table precludes pattern matching. I would have been wrong. Super Search on dispatch table came up with an ingenious post by grinder that shows how to do just that, using Regexp::Assemble. In a sense, the pattern match is still searching all the alternatives, albeit in an optimized way. However, the circumstances in which a dispatch table is most advantageous — where you have a very large number of alternatives — are where a regex is likely to bog down.

Ranges offer a similar challenge. Of course, neither patterns nor ranges are handled by C's switch statement. Modula handles ranges, but it sacrifices fall-through (and may only work on numbers and single characters). You can't have everything. If you can't enumerate all the cases you want to cover, you don't want a dispatch table. Functions that check all the alternatives in order (such as my smatch) can offer a great deal of flexibility.

Having Perl build the table

The only really tricky bit about having a subroutine that translates a list of arguments resembling a C switch statement into a dispatch table is in implementing fall-through. A program doesn't have the luxury of creating new named subroutines, so there's a bit of bookkeeping to handle there.

I have implemented a function that does the translation for all the features described above (except patterns and ranges, for the reasons cited) in the Snippet Switch/case as a dispatch table with C-style fall-through.

Update: I have written a module, Case, that implements dispatch tables (as well as a more complex smart-matching construct).

Comment on Implementing Dispatch Tables
Select or Download Code
Re: Implementing Dispatch Tables
by Anonymous Monk on Jan 10, 2012 at 19:33 UTC
    Just what i was looking for, searched Google for ages for this.
Re: Implementing Dispatch Tables
by deMize (Monk) on Jul 31, 2012 at 13:41 UTC

    This is an old post, but regarding your snippet ($disp_table{'not there'} || sub {})->(); why not do something like $disp_table{$some_key}->() if $some_key;?

    It shouldn't error out for using an empty string. If your key is falsy (0 or undef), then it won't look it up.



    Demize

      TIAS

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perltutorial [id://456530]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2014-10-24 21:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (137 votes), past polls