Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( [id://3333]=superdoc: 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).


In reply to Implementing Dispatch Tables by Roy Johnson

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2024-03-28 16:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found