Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Reducing the complexity of dispatch tables

by jdporter (Paladin)
on Aug 31, 2007 at 16:43 UTC ( [id://636376]=note: print w/replies, xml ) Need Help??


in reply to Reducing the complexity of dispatch tables

I'd consider using a namespace (package) rather than a dispatch table (hash). The concept is the same, but you get name checking for free and it's probably faster.

use strict; use warnings; { package Dispatch; sub render_template { my ($action, $params) = @_; print "render_template( action => $action, params => $params ) +\n"; } *home = *donate = *news = sub { my $pkg = shift; my ($action, $params) = @_; return render_template($action, $params); }; sub samples { my ($dbh) = shift; print "samples( dbh = $dbh )\n"; } sub samp { my $pkg = shift; samples('$DBH'); } sub AUTOLOAD { my $pkg = shift; our $AUTOLOAD; print "Error: no such function $AUTOLOAD ( @_ )\n"; } } # test: for my $act ( qw( home donate news samp bogus )) { Dispatch->$act($act,"PARAMS"); }

Note - You'll get a "used only once" warning on the assignments to *home etc. To squelch it, you can enclose those assignments in a block like so:

{ no warnings 'once'; . . . }
A word spoken in Mind will reach its own level, in the objective world, by its own weight

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (6)
As of 2024-04-19 08:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found