With the help and patience of wfsp, Anno and others on the CB, I was able to trim out some fat in my existing dispatch tables for a site I'm rewriting.

What I'm currently left with (which works, so far), is this:

my %dispatch = ( home => \&home, donate => \&donate, news => \&news, samp => sub { \&samples($dbh) }, ... ); $action = $apr->param('a') || 'home'; $dispatch{$action}->($action); sub render_template { my ($action, $params) = @_; my $template = HTML::Template->new( die_on_bad_params => '0', filename => "$action"); my $content = $template->output; print $content; } sub home { my ($action, $params) = @_; return render_template($action, $params); } sub donate { my ($action, $params) = @_; return render_template($action, $params); } sub news { my ($action, $params) = @_; return render_template($action, $params); } sub samples { my ($dbh) = shift; .... }

Note: There's one small bug I just found when composing this (if I pass an unknown param, it should default to 'home', but it doesn't, it just dies with Can't use string ("") as a subroutine ref while "strict refs" in use). I'll try to fix that shortly.

What I'm wondering now, is with these subs being identical now except for their sub name... can I make these into anonymous subs, and pass the $action down to them, reducing the 20+ identical subs down to 1?


In reply to Reducing the complexity of dispatch tables by hacker

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.