[Purdue-pm] Perl 5 Dispatch Tables

Mark Senn mark at purdue.edu
Sat May 25 21:21:15 PDT 2019


I blogged about Perl 5 Dispatch Tables at
    https://engineering.purdue.edu/~mark/blog/perl-5-dispatch-tables.pdf

In short, I don't use conventional dispatch tables because they
violate the don't repeat yourself principle.  I'm only dispatching
to subs based on trusted information and the core of the idea is


sub __SUB__add  { return $_[0] + $_[1]; }

my $_ = 'add 1 2';

# Split the line of text into words.
my @word = split /\s+/, $_;

# Get the command name from @word and leave the arguments
# in @word.  Prepend ’__SUB__’ (two underlines before and
# after ’SUB’) to the comand name.
my $sub = ’__SUB__’ . shift @word;

# Is the "__SUB__$sub" name defined?
defined(&$sub)  or  die qq/sub "$sub" is not defined/;

# Do the command.
say $sub->(@word);


See the web page for more information.

-mark


More information about the Purdue-pm mailing list