File Coverage

blib/lib/Exporter/Dispatch.pm
Criterion Covered Total %
statement 18 28 64.2
branch 6 10 60.0
condition 2 3 66.6
subroutine 4 5 80.0
pod 1 1 100.0
total 31 47 65.9


line stmt bran cond sub pod time code
1             package Exporter::Dispatch;
2 1     1   25628 use Carp qw(croak);
  1         3  
  1         657  
3             our $VERSION = 2.10;
4            
5             sub import {
6 1     1   12 my $pkg = (caller)[0];
7 1 50       15 if (@_ > 2) {
    50          
    50          
    50          
8 0         0 croak 'Incorrect import list for Exporter::Dispatch';
9             }
10             elsif ($_[-1] eq 'create_dptable') {
11 0         0 *{"${pkg}::create_dptable"} = \&create_dptable;
  0         0  
12             return
13 0         0 }
14             elsif ($_[-1] eq 'dptable_alias') {
15 0         0 *{"${pkg}::dptable_alias"} = sub {
16 0     0   0 *{"${pkg}::$_[1]"} = *{"${pkg}::$_[0]"}
  0         0  
  0         0  
17             }
18 0         0 }
19             elsif (@_ == 2) {
20 0         0 croak 'Incorrect import list for Exporter::Dispatch';
21             }
22 1     1   12 *{"${pkg}::create_dptable"} = sub { create_dptable($pkg) };
  1         10891  
  1         938  
23             }
24            
25             sub create_dptable {
26 1     1 1 3 my $pkg = shift;
27 1         2 my %dispatch;
28 2         20 my @oksymbols = grep { !/^_/
  1         9  
29             && !/^dptable_alias$/
30             && !/^create_dptable$/
31 3 100 66     35 && defined *{"${pkg}::$_"}{CODE} }
32 1         2 keys %{*{"${pkg}::"}};
  1         2  
33 1         8 $dispatch{$_} = *{"${pkg}::$_"}{CODE}
34 1         4 foreach ( @oksymbols );
35 1         5 return \%dispatch
36             };
37            
38             1;
39            
40             =head1 NAME
41            
42             Exporter::Dispatch
43            
44             =head1 ABSTRACT
45            
46             Simple and modular creation of dispatch tables.
47            
48             =head1 SYNOPSIS
49            
50             package TestPkg;
51             use Exporter::Dispatch qw(dptable_alias);
52             dptable_alias("sub_a", "sub_aa"); # typeglobbing for dummies;
53            
54             sub sub_a { ... }
55             sub sub_b { ... }
56             sub sub_c { ... }
57             sub _sub_c_helper { # not part of the table!
58             # ...
59             }
60            
61             package main;
62             my $table = create_dptable TestPkg; # or TestPkg::create_dptable();
63             $table->{'sub_c'}->("Hello!");
64            
65             # ------------------------------------------------------
66             # or
67            
68             package TestPkg;
69             sub sub_a { ... }
70             sub sub_b { ... }
71             sub sub_c { ... }
72             sub _sub_c_helper { # not part of the table!
73             # ...
74             }
75            
76             package main;
77             use Exporter::Dispatch;
78             my $table = create_dptable 'TestPkg'; # Please know what you are doing here.
79             $table->{'sub_c'}->("Hello!");
80            
81             =head1 DESCRIPTION
82            
83             Dispatch tables are great and convienient, but sometimes can be a bit of a
84             pain to write. You have references flying over here and closures flying over
85             there; yuck! Thats much too complicated for so simple of an idea. Wouldn't
86             it be great if you could say "Ok, I have a set of subs here, and I want a
87             dispatch table that maps each subname to each sub... Go do it, Perl genie!"
88             With this short snippet of a module, now you can. Just throw your subs in a
89             module, C, and a C subroutine that
90             (surpise!) creates a dispatch table that maps each subname in the package to
91             its corresponding sub will magically appear to serve you.
92            
93             In a more serious tone, C essentially creats a
94             subroutine (named create_dptable) in namespaces it is imported to. This
95             subroutine, when called, returns a hashref that maps a string of each
96             subname to the corresponding subroutine. Subroutines that begin with an
97             underscore are not added to the returned table, so they can be used as
98             "helper" routines.
99            
100             =head2 Exports
101            
102             =over 3
103            
104             =item B
105            
106             Indirect object syntax version; is automatically imported into the calling
107             package unless the functional form of create_dptable is exported. Please note
108             that this form ofcreate_dptable takes no arguments; the version used in the
109             first part of the synopsis uses Perl's indirect object syntax.
110            
111             =item B
112            
113             Functional version of create_dptable; this is a version that can create a
114             dispatch table based on any package. Please note that you should only use this
115             form when creating a dispatch table based on a package that you have control of.
116             (i.e., that you wrote)
117            
118             =item B
119            
120             Typeglobbing for dummies. Automatically imported into the calling package.
121             C will create an entry in the symbol table that maps "sub_name"
122             to "sub_name_alias"
123            
124             =back
125            
126             =head1 BUGS
127            
128             If you find any bugs or oddities, please do inform me.
129            
130             =head1 INSTALLATION
131            
132             See perlmodinstall for information and options on installing Perl modules.
133            
134             =head1 AVAILABILITY
135            
136             The latest version of this module is available from the Comprehensive Perl
137             Archive Network (CPAN) (http://search.cpan.org/CPAN/). Or see
138             http://search.cpan.org/author/JRYAN/.
139            
140             =head1 VERSION
141            
142             This document describes version 2.10 of Exporter::Dispatch.
143            
144             =head1 AUTHOR
145            
146             Joseph F. Ryan
147            
148             =head1 COPYRIGHT
149            
150             Copyright 2004 Joseph F. Ryan. All rights reserved.
151            
152             This library is free software; you can redistribute it and/or modify it under
153             the same terms as Perl itself.