blib/lib/Exporter/Dispatch.pm

filter: Statementstmt 66.6% 20 / 30 Branchbran 50.0% 4 / 8 Conditioncond 66.6% 6 / 9 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 80.0% 4 / 5 Podpod 100% 1 / 1 totaltotal 64.9% 37 / 57 CC 9 SCAR 18.5
CC
9
Cov
64%
import
10.6
create_dptable
5.1
__ANON__
2.0
CRAP 6.4
↑ Summary
1 package Exporter::Dispatch;
2 1 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 our $VERSION = 2.10;
4
5 1 ▶sub import {
Subroutine import: called CC=5 SCAR=23.6
6 1 my $pkg = (caller)[0];
7 1 ▶ if (@_ > 2) {
Branch: 4 branches
BranchTrueFalseTotal
if (@_ > 2) { } 0 1 1
elsif ($_[-1] eq 'create_dptable') { } 0 1 1
elsif ($_[-1] eq 'dptable_alias') { } 0 1 1
elsif (@_ == 2) { } 0 1 1
8 0 croak 'Incorrect import list for Exporter::Dispatch';
9 }
10 elsif ($_[-1] eq 'create_dptable') {
11 0 *{"${pkg}::create_dptable"} = \&create_dptable;
12 return
13 0 }
14 elsif ($_[-1] eq 'dptable_alias') {
15 0 ▶ *{"${pkg}::dptable_alias"} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
16 0 *{"${pkg}::$_[1]"} = *{"${pkg}::$_[0]"}
17 }
18 0 }
19 elsif (@_ == 2) {
20 0 croak 'Incorrect import list for Exporter::Dispatch';
21 }
22 1 ▶ *{"${pkg}::create_dptable"} = sub { create_dptable($pkg) };
Subroutine __ANON__: called CC=1 SCAR=0.0
23 }
24
25 1 ▶sub create_dptable {
Subroutine create_dptable: called CC=5 SCAR=16.3
Pod documented
26 1 my $pkg = shift;
27 1 my %dispatch;
28 my @oksymbols = grep { !/^_/
29 && !/^dptable_alias$/
30 && !/^create_dptable$/
31 3 ▶ && defined *{"${pkg}::$_"}{CODE} }
Condition66%6/9
!(/^_/ || /^dptable_alias$/ || /^create_dptable$/) && defined *{"${pkg}::$_";}{'CODE'}
!ll&&!rl&&r
111
/^_/ || /^dptable_alias$/ || /^create_dptable$/
l!l&&r!l&&!r
012
/^_/ or /^dptable_alias$/
l!l&&r!l&&!r
003
MC/DC50%2/4
!(/^_/ || /^dptable_alias$/ || /^create_dptable$/) && defined *{"${pkg}::$_";}{'CODE'}
/^_/ /^dptable_alias$/ /^create_dptable$/ defined *{"${pkg}::$_";}{'CODE'}
Truth table60%3/5
not(A or B || C) && D
A= /^_/B= /^dptable_alias$/C= /^create_dptable$/D= defined *{"${pkg}::$_";}{'CODE'}
ABCDresult
1XXX0
01XX0
001X0
00000
00011
32 1 keys %{*{"${pkg}::"}};
33 1 $dispatch{$_} = *{"${pkg}::$_"}{CODE}
34 1 foreach ( @oksymbols );
35 1 return \%dispatch
36 };
37
38 1 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<use Exporter::Dispatch;>, and a C<create_dptable> 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<Exporter::Dispatch> 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<create_dptable>
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<create_dptable("PacakgeName")>
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<dptable_alias("sub_name", "sub_name_alias")>
119
120 Typeglobbing for dummies. Automatically imported into the calling package.
121 C<dptable_alias> 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 <ryan.311@osu.edu>
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.
↑ Summary