Exporter::Dispatch
Simple and modular creation of dispatch tables.
package TestPkg; use Exporter::Dispatch qw(dptable_alias); dptable_alias("sub_a", "sub_aa"); # typeglobbing for dummies; sub sub_a { ... } sub sub_b { ... } sub sub_c { ... } sub _sub_c_helper { # not part of the table! # ... } package main; my $table = create_dptable TestPkg; # or TestPkg::create_dptable(); $table->{'sub_c'}->("Hello!"); # ------------------------------------------------------ # or package TestPkg; sub sub_a { ... } sub sub_b { ... } sub sub_c { ... } sub _sub_c_helper { # not part of the table! # ... } package main; use Exporter::Dispatch; my $table = create_dptable 'TestPkg'; # Please know what you are doing here. $table->{'sub_c'}->("Hello!");
Dispatch tables are great and convienient, but sometimes can be a bit of a pain to write. You have references flying over here and closures flying over there; yuck! Thats much too complicated for so simple of an idea. Wouldn't it be great if you could say "Ok, I have a set of subs here, and I want a dispatch table that maps each subname to each sub... Go do it, Perl genie!" With this short snippet of a module, now you can. Just throw your subs in a module, use Exporter::Dispatch;, and a create_dptable subroutine that (surpise!) creates a dispatch table that maps each subname in the package to its corresponding sub will magically appear to serve you.
use Exporter::Dispatch;
create_dptable
In a more serious tone, Exporter::Dispatch essentially creats a subroutine (named create_dptable) in namespaces it is imported to. This subroutine, when called, returns a hashref that maps a string of each subname to the corresponding subroutine. Subroutines that begin with an underscore are not added to the returned table, so they can be used as "helper" routines.
Indirect object syntax version; is automatically imported into the calling package unless the functional form of create_dptable is exported. Please note that this form ofcreate_dptable takes no arguments; the version used in the first part of the synopsis uses Perl's indirect object syntax.
Functional version of create_dptable; this is a version that can create a dispatch table based on any package. Please note that you should only use this form when creating a dispatch table based on a package that you have control of. (i.e., that you wrote)
Typeglobbing for dummies. Automatically imported into the calling package. dptable_alias will create an entry in the symbol table that maps "sub_name" to "sub_name_alias"
dptable_alias
If you find any bugs or oddities, please do inform me.
See perlmodinstall for information and options on installing Perl modules.
The latest version of this module is available from the Comprehensive Perl Archive Network (CPAN) (http://search.cpan.org/CPAN/). Or see http://search.cpan.org/author/JRYAN/.
This document describes version 2.10 of Exporter::Dispatch.
Joseph F. Ryan <ryan.311@osu.edu>
Copyright 2004 Joseph F. Ryan. All rights reserved.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Exporter::Dispatch, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Exporter::Dispatch
CPAN shell
perl -MCPAN -e shell install Exporter::Dispatch
For more information on module installation, please visit the detailed CPAN module installation guide.