blib/lib/Xporter.pm

filter: Statementstmt 71.0% 86 / 121 Branchbran 41.0% 23 / 56 Conditioncond 31.7% 20 / 63 MC/DCmcdc 15.1% 5 / 33 Subroutinesub 82.3% 14 / 17 Podpod 0.0% 0 / 4 totaltotal 50.3% 148 / 294 CC 57 SCAR 37.3
CC
57
Cov
54%
cmp_ver
110.0
_version_specified
70.9
import
46.3
CRAP 41.6
↑ Summary
1 #!/usr/bin/perl
2 7 ▶BEGIN { require $_.".pm" && $_->import for qw(strict warnings) }
Subroutine BEGIN: called CC=3 SCAR=12.0
Branch: 1 branch
BranchTrueFalseTotal
require $_ . '.pm' and $_->import 14 0 14
3 # vim=:SetNumberAndWidth
4 =encoding utf-8
5  
6 =head1 NAME
7  
8 Xporter - Alternative Exporter with persistant defaults & auto-ISA
9  
10 =head1 VERSION
11  
12 Version "0.1.1"
13  
14 =cut
15  
16 7 { package Xporter;
17 7 ▶ BEGIN { require $_.".pm" && $_->import for qw(strict warnings) }
Subroutine BEGIN: called CC=3 SCAR=12.0
Branch: 1 branch
BranchTrueFalseTotal
require $_ . '.pm' and $_->import 14 0 14
18 7 our $VERSION='0.1.2';
19 7 our @CARP_NOT;
20 7 ▶ use mem(@CARP_NOT=(__PACKAGE__));
Subroutine BEGIN: called CC=1 SCAR=0.0
21 # 0.1.2 - Bad version check found in self-testing;
22 # Added test against 4 version formats
23 # 0.1.1 - Bad use of modern proptype (_) for old perls
24 # 0.1.0 - Bugfix: only match user input after stripping sigels or "nots" (!^-)
25 # - Feature addition, in addition to a global, (solo) 'not'
26 # at the beginning of a list to zero the default exports,
27 # individual items in EXPORTS can be excluded by prefixing them
28 # with a negating prefix (!^-);
29 # - Added new test case for specific exclusion
30 # - NOTE: blocking an export will ignore type as will asking for a non-dflt
31 # 0.0.14 - Documentation update
32 # 0.0.13 - Bug fix in string version compare -- didn't add leading
33 # zeros for numeric compares;
34 # 0.0.12 - Add version tests to test 3 forms of version: v-string,
35 # numeric version, and string-based version.
36 # If universal method $VERSION doesn't exist, call our own
37 # method.
38 # 0.0.11 - Add a Configure_depends to see if that satisfies the one
39 # test client that is broken (sigh)
40 # 0.0.10 - Remove P from another test (missed one); Having to use
41 # replacement lang features is torture on my RSI
42 # 0.0.9 - add alternate version format for ExtMM(this system sucks)
43 # - remove diagnostic messages from tests (required P)
44 # 0.0.8 - add current dep for BUILD_REQ of ExtMM
45 # 0.0.7 - 'require' version# bugfix
46 # 0.0.6 - comment cleanup; Change CONFIGURE_REQUIRES to TEST_REQUIRES
47 # 0.0.5 - export inheritance test written to highlight a problem area
48 # - problem area addessed; converted to use efficient jump table
49 # 0.0.4 - documentation additions;
50 # - added tests & corrected any found problems
51 # 0.0.3 - added auto-ISA-adding (via push) when this mod is used.
52 # - added check for importing 'import' to disable auto-ISA-add
53 # 0.0.2 - Allow for "!" as first arg to import to turn off default export
54 # NOTE: defaults are defaults when using EXPORT_OK as well;
55 # One must specifically disable defaults to turn them off.
56 # 0.0.1 - Initial split of code from iomon
57 #
58 #require 5.8.0;
59
60 # Alternate export-import method that doesn't undefine defaults by
61 # default
62  
63 7 ▶ sub add_to_caller_ISA($$) {
Subroutine add_to_caller_ISA: called CC=3 SCAR=11.1
Pod undocumented
64 7 my ($pkg, $caller) = @_;
65
66 7 ▶ if ($pkg eq __PACKAGE__) { no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if ($pkg eq __PACKAGE__) 7 0 7
67 7 ▶ unshift @{$caller."::ISA"}, $pkg unless grep /$pkg/, @{$caller."::ISA"};
Branch: 1 branch
BranchTrueFalseTotal
unless grep /$pkg/, @{$caller . '::ISA';} 7 0 7
68 }
69 }
70  
71 # adapted from Core::Types to avoid circular include
72 46 ▶ sub _EhV($*) { my ($arg, $field) = @_;
Subroutine _EhV: called CC=5 SCAR=18.6
73 (ref $arg && 'HASH' eq ref $arg) &&
74 46 ▶ defined $field && exists $arg->{$field} ? $arg->{$field} : undef
Branch: 1 branch
BranchTrueFalseTotal
ref $arg && 'HASH' eq ref $arg && defined $field && exists $arg->{$field} ? : 15 31 46
Condition44%4/9
ref $arg and "HASH" eq ref $arg and defined $field and exists $arg->{$field}
!ll&&!rl&&r
03115
ref $arg and "HASH" eq ref $arg and defined $field
!ll&&!rl&&r
0046
ref $arg and "HASH" eq ref $arg
!ll&&!rl&&r
0046
MC/DC25%1/4
ref $arg and "HASH" eq ref $arg and defined $field and exists $arg->{$field}
ref $arg "HASH" eq ref $arg defined $field exists $arg->{$field}
Truth table40%2/5
A and B and C and D
A= ref $argB= "HASH" eq ref $argC= defined $fieldD= exists $arg->{$field}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
75 }
76  
77 0 ▶ sub cmp_ver($$) {
Subroutine cmp_ver: not called CC=10 SCAR=47.0
Pod undocumented
78 0 my ($v1, $v2) = @_;
79 0 ▶ for (my $i=0; $i<@$v2 && $i<@$v1; ++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$v2 and $i < @$v1) 0 0 0
Condition0%0/3
$i < @$v2 and $i < @$v1
!ll&&!rl&&r
000
MC/DC0%0/2
$i < @$v2 and $i < @$v1
$i < @$v2 $i < @$v1
Truth table0%0/3
A and B
A= $i < @$v2B= $i < @$v1
ABresult
0X0
100
111
80 0 my ($v1p, $v1_num, $v1s) = ($v1->[$i] =~ /^([^\d]*)(\d+)([^\d]*)$/);
81 0 my ($v2p, $v2_num, $v2s) = ($v2->[$i] =~ /^([^\d]*)(\d+)([^\d]*)$/);
82 0 ▶ my $maxlen = $v1_num > $v2_num ? $v1_num : $v2_num;
Branch: 1 branch
BranchTrueFalseTotal
$v1_num > $v2_num ? : 0 0 0
83 0 ▶ my $r = sprintf("%s%0*d%s", $v1p||"", $maxlen, $v1_num, $v1s||"") cmp
Condition0%0/8
$v1p || ''
l!l
00
$v1s || ''
l!l
00
$v2p || ''
l!l
00
$v2s || ''
l!l
00
MC/DC0%0/1
$v1p || ''
$v1p
MC/DC0%0/1
$v1s || ''
$v1s
MC/DC0%0/1
$v2p || ''
$v2p
MC/DC0%0/1
$v2s || ''
$v2s
Truth table0%0/2
A
A= $v1p
Aresult
11
00
Truth table0%0/2
A
A= $v1s
Aresult
11
00
Truth table0%0/2
A
A= $v2p
Aresult
11
00
Truth table0%0/2
A
A= $v2s
Aresult
11
00
84 sprintf("%s%0*d%s", $v2p||"", $maxlen, $v2_num, $v2s||"");
85 0 ▶ return -1 if $r<0;
Branch: 1 branch
BranchTrueFalseTotal
if $r < 0 0 0 0
86 0 ▶ return 0 if $r>0;
Branch: 1 branch
BranchTrueFalseTotal
if $r > 0 0 0 0
87 }
88 0 return 0;
89 }
90  
91  
92 0 ▶ sub _version_specified($$;$) {
Subroutine _version_specified: not called CC=9 SCAR=42.6
93 0 my ($pkg, $requires) = @_;
94 0 my $pkg_ver;
95 0 ▶ { no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.2
96 0 ▶ $pkg_ver = ${$pkg."::VERSION"} || '(undef)';
Condition0%0/2
${$pkg . '::VERSION';} || '(undef)'
l!l
00
MC/DC0%0/1
${$pkg . '::VERSION';} || '(undef)'
${$pkg . '::VERSION';}
Truth table0%0/2
A
A= ${$pkg . '::VERSION';}
Aresult
11
00
97 }
98 0 my @v1=split /_|\./, $pkg_ver;
99 0 my @v2=split /_|\./, $requires;
100 0 ▶ if (@v1>2 || @v2>2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@v1 > 2 or @v2 > 2) { } 0 0 0
Condition0%0/3
@v1 > 2 or @v2 > 2
l!l&&r!l&&!r
000
MC/DC0%0/2
@v1 > 2 or @v2 > 2
@v1 > 2 @v2 > 2
Truth table0%0/3
A or B
A= @v1 > 2B= @v2 > 2
ABresult
1X1
011
000
101 0 ▶ return if cmp_ver(\@v1, \@v2) >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if &cmp_ver(\@v1, \@v2) >= 0 0 0 0
102 } else {
103 0 ▶ return if $pkg_ver && ($pkg_ver cmp $requires)>=0;
Branch: 1 branch
BranchTrueFalseTotal
if $pkg_ver and ($pkg_ver cmp $requires) >= 0 0 0 0
Condition0%0/3
$pkg_ver and ($pkg_ver cmp $requires) >= 0
!ll&&!rl&&r
000
MC/DC0%0/2
$pkg_ver and ($pkg_ver cmp $requires) >= 0
$pkg_ver ($pkg_ver cmp $requires) >= 0
Truth table0%0/3
A and B
A= $pkg_verB= ($pkg_ver cmp $requires) >= 0
ABresult
0X0
100
111
104 0 ▶ return if $pkg_ver ne '(undef)' && $pkg_ver >= $requires;
Branch: 1 branch
BranchTrueFalseTotal
if $pkg_ver ne "(undef)" and $pkg_ver >= $requires 0 0 0
Condition0%0/3
$pkg_ver ne "(undef)" and $pkg_ver >= $requires
!ll&&!rl&&r
000
MC/DC0%0/2
$pkg_ver ne "(undef)" and $pkg_ver >= $requires
$pkg_ver ne "(undef)" $pkg_ver >= $requires
Truth table0%0/3
A and B
A= $pkg_ver ne "(undef)"B= $pkg_ver >= $requires
ABresult
0X0
100
111
105 }
106 0 require Carp;
107 0 Carp::croak(sprintf "module %s %s required. This is only %s",
108 $pkg, $requires, $pkg_ver);
109 }
110  
111  
112 7 our %exporters;
113  
114  
115 7 our $tc2proto = {'&' => '&', '$' => '$', '@' => '@', '%' => '%',
116 '*' => '*', '!' => '!', '-' => '!', '^' => '!'};
117  
118 0 ▶ sub list(;*) { return @_ }
Subroutine list: not called CC=1 SCAR=6.9
Pod undocumented
119  
120 sub op_prefix;
121 46 ▶ sub op_prefix {
Subroutine op_prefix: called CC=5 SCAR=16.7
Pod undocumented
122 46 ▶ return ($_, undef) unless $_;
Branch: 1 branch
BranchTrueFalseTotal
unless $_ 0 46 46
123 46 my $type = substr $_, 0, 1;
124 46 my $mapped_op = _EhV $tc2proto, $type;
125 46 ▶ if ($mapped_op) {
Branch: 2 branches
BranchTrueFalseTotal
if ($mapped_op) { } 15 31 46
elsif ($type =~ /\w/) { } 31 0 31
126 15 $_ = substr($_,1);
127 15 ▶ if ($mapped_op eq '!') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mapped_op eq "!") 0 15 15
128 0 ($_, $type, undef ) = op_prefix() }
129 31 } elsif ($type =~ /\w/) { $mapped_op=$type='&' }
130 46 ($_, $type, $mapped_op);
131
132 }
133 16 ▶ sub import {
Subroutine import: called CC=26 SCAR=38.4
134 16 my $pkg = shift;
135 16 my ($caller, $fl, $ln) = (caller);
136 7 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
137  
138  
139 #*{$caller."::import"}=
140 #\&{__PACKAGE__."::import"} if !exists ${$caller."::import"}->{CODE};
141  
142 16 ▶ if (@_ && $_[0] && $_[0] =~ /^(v?[\d\._]+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ and $_[0] and $_[0] =~ /^(v?[\d\._]+)$/) 0 16 16
Condition66%4/6
@_ and $_[0] and $_[0] =~ /^(v?[\d\._]+)$/
!ll&&!rl&&r
4120
@_ and $_[0]
!ll&&!rl&&r
4012
MC/DC0%0/3
@_ and $_[0] and $_[0] =~ /^(v?[\d\._]+)$/
@_ $_[0] $_[0] =~ /^(v?[\d\._]+)$/
Truth table50%2/4
A and B and C
A= @_B= $_[0]C= $_[0] =~ /^(v?[\d\._]+)$/
ABCresult
0XX0
10X0
1100
1111
143 0 my @t=split /\./, $_[0];
144 7 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
145 0 ▶ if ($pkg->can("VERSION") && @t<3 && $1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pkg->can('VERSION') and @t < 3 and $1) { } 0 0 0
Condition0%0/6
$pkg->can("VERSION") and @t < 3 and $1
!ll&&!rl&&r
000
$pkg->can("VERSION") and @t < 3
!ll&&!rl&&r
000
MC/DC0%0/3
$pkg->can("VERSION") and @t < 3 and $1
$pkg->can("VERSION") @t < 3 $1
Truth table0%0/4
A and B and C
A= $pkg->can("VERSION")B= @t < 3C= $1
ABCresult
0XX0
10X0
1100
1111
146 0 $pkg->VERSION($1) }
147 else {
148 0 _version_specified($pkg, $1); }
149 0 shift;
150 }
151  
152 16 ▶ if ($pkg eq __PACKAGE__) { # we are exporting
Branch: 1 branch
BranchTrueFalseTotal
if ($pkg eq __PACKAGE__) 8 8 16
153 8 ▶ if (@_ && $_[0] eq q(import)) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ and $_[0] eq 'import') { } 1 7 8
Condition100%3/3
@_ and $_[0] eq "import"
!ll&&!rl&&r
251
MC/DC100%2/2
@_ and $_[0] eq "import"
@_ $_[0] eq "import"
Truth table100%3/3
A and B
A= @_B= $_[0] eq "import"
ABresult
0X0
100
111
154 7 ▶ no strict q(refs);
Subroutine BEGIN: called CC=1 SCAR=0.0
155 1 *{$caller."::import"} = \*{$pkg."::import"};
156 } else {
157 7 add_to_caller_ISA($pkg, $caller);
158 }
159 8 $exporters{$caller} = 1;
160 8 return 1;
161 }
162  
163 8 my ($export, $exportok, $exporttags);
164  
165 7 ▶ { no strict q(refs);
Subroutine BEGIN: called CC=1 SCAR=0.0
166 8 ▶ $export = \@{$pkg."::"."EXPORT"} || [];
Condition50%1/2
\@{$pkg . '::' . 'EXPORT';} || []
l!l
80
MC/DC0%0/1
\@{$pkg . '::' . 'EXPORT';} || []
\@{$pkg . '::' . 'EXPORT';}
Truth table50%1/2
A
A= \@{$pkg . '::' . 'EXPORT';}
Aresult
11
00
167 8 ▶ $exportok = \@{$pkg."::"."EXPORT_OK"} || [];
Condition50%1/2
\@{$pkg . '::' . 'EXPORT_OK';} || []
l!l
80
MC/DC0%0/1
\@{$pkg . '::' . 'EXPORT_OK';} || []
\@{$pkg . '::' . 'EXPORT_OK';}
Truth table50%1/2
A
A= \@{$pkg . '::' . 'EXPORT_OK';}
Aresult
11
00
168 8 $exporttags = \%{$pkg."::"."EXPORT_TAGS"};
169 }
170
171 8 my @allowed_exports = (@$export, @$exportok);
172  
173 8 ▶ if (@_ and $_[0] and $_[0] eq '!' || $_[0] eq '-' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ and $_[0] and $_[0] eq "!" || $_[0] eq "-") 1 7 8
Condition77%7/9
@_ and $_[0] and $_[0] eq "!" || $_[0] eq "-"
!ll&&!rl&&r
251
@_ and $_[0]
!ll&&!rl&&r
206
$_[0] eq "!" or $_[0] eq "-"
l!l&&r!l&&!r
105
MC/DC50%2/4
@_ and $_[0] and $_[0] eq "!" || $_[0] eq "-"
@_ $_[0] $_[0] eq "!" $_[0] eq "-"
Truth table60%3/5
A and B and C or D
A= @_B= $_[0]C= $_[0] eq "!"D= $_[0] eq "-"
ABCDresult
0XXX0
10XX0
11000
111X1
11011
174 1 printf("Export RESET\n");
175 1 $export=[];
176 1 shift @_;
177 }
178  
179 8 for my $pat (@_) { # filter individual params
180 7 $_ = $pat; # passed to op_prefix
181 7 my ($name, $type, $mapped_op ) = op_prefix();
182 7 ▶ if ($mapped_op eq '!') {
Branch: 2 branches
BranchTrueFalseTotal
if ($mapped_op eq '!') { } 0 7 7
elsif (grep /$name/, @allowed_exports) { } 7 0 7
183 0 ▶ if (grep /$name/, @$export) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep /$name/, @$export) 0 0 0
184 0 my @new_export = grep { !/$name/ } @$export;
185 0 $export=\@new_export;
186 }
187 } elsif (grep /$name/, @allowed_exports) {
188 #printf("allowing export of %s\n", $pat);
189 7 push @$export, $pat ;
190 }
191 }
192  
193  
194 8 for(@$export) {
195 39 my ($type, $mapped_op);
196 #printf("_=%s:", $_||"undef");
197 39 ($_, $type, $mapped_op) = op_prefix;
198 #printf("_=%s, t=%s, mapped=%s\n", $_||"undef", $type||"undef", $mapped_op||"undef");
199 39 ▶ if ($mapped_op) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mapped_op) { } 39 0 39
200 39 ▶ print "skip exp of $_\n" if $mapped_op eq '!';
Branch: 1 branch
BranchTrueFalseTotal
if $mapped_op eq "!" 0 39 39
201 39 ▶ next if $mapped_op eq '!';
Branch: 1 branch
BranchTrueFalseTotal
if $mapped_op eq "!" 0 39 39
202 } else {
203 0 require Carp;
204 0 ▶ Carp::croak("Unknown type ". ($type||"(undef)") . " in " . ($_||"(undef)"));
Condition0%0/4
$type || '(undef)'
l!l
00
$_ || '(undef)'
l!l
00
MC/DC0%0/1
$type || '(undef)'
$type
MC/DC0%0/1
$_ || '(undef)'
$_
Truth table0%0/2
A
A= $type
Aresult
11
00
Truth table0%0/2
A
A= $_
Aresult
11
00
205 }
206 39 my $colon_name = "::" . $_ ;
207 39 my ($exf, $imf) = ( $pkg . $colon_name, $caller . $colon_name);
208 7 ▶ no strict q(refs);
Subroutine BEGIN: called CC=1 SCAR=0.0
209 39 my $case = {
210 '&' => \&$exf,
211 '$' => \$$exf,
212 '@' => \@$exf,
213 '%' => \%$exf,
214 '*' => *$exf};
215 39 *$imf = $case->{$type};
216 }
217 }
218 7 1}
219  
220  
221 =head1 SYNOPIS
222  
223 In the "Exporting" module:
224  
225 { package module_adder [optional version];
226 use warnings; use strict;
227 use mem; # to allow using module in same file
228 our (@EXPORT, @EXPORT_OK);
229 our $lastsum;
230 our @lastargs;
231 use Xporter(@EXPORT=qw(adder $lastsum @lastargs),
232 @EXPORT_OK=qw(print_last_result));
233  
234 sub adder($$) {@lastargs=@_; $lastsum=$_[0]+$_[1]}
235 sub print_last_result () {
236 use P; # using P allows answer printed or as string
237 if (@lastargs && defined $lastsum){
238 P "%s = %s\n", (join ' + ' , @lastargs), $lastsum;
239 }
240 }
241 }
242  
243 In C<use>-ing module (same or different file)
244  
245 package main; use warnings; use strict;
246 use module_adder qw(print_last_result);
247  
248 adder 4,5;
249  
250 Printing output:
251  
252 print_last_result();
253  
254 #Result:
255
256 4 + 5 = 9
257  
258 (Or in a test:)
259
260 ok(print_last_result eq "4 + 5 = 9", "a pod test");
261  
262 =head1 DESCRIPTION
263  
264 C<Xporter> provides C<EXPORT> functionality similar to L<Exporter> with
265 some different rules to simplify common cases.
266  
267 The primary difference, in C<Xporter> is that the default C<EXPORT> list
268 remains the default C<EXPORT> list unless the user specifically asks for it
269 to not be included, whereas in L<Exporter>, asking for any additional
270 exports from the C<EXPORT_OK> list, clears the default C<EXPORT> list.
271  
272 C<Xporter> makes it easy to reset or clear the default so that choice
273 is left to the user.
274  
275 To reset the default C<EXPORT> list to empty, a bare I<minus> ('-') or
276 I<logical-not> sign ('!') is placed as the first parameter in the client's import
277 list.
278  
279 =head3 Example
280  
281 Suppose a module has exports:
282  
283 our (@EXPORT, @EXPORT_OK);
284 use Xporter(@EXPORT=qw(one $two %three @four),
285 @EXPORT_OK=qw(&five));
286  
287 In the using module, to only import symbols 'two' and 'five',
288 one would use:
289  
290 =head3 Example
291  
292 use MODULENAME qw(! $two five);
293  
294 That negates the default C<EXPORT> list, and allows selective import
295 of the values wanted from either, the default C<EXPORT> or the
296 C<EXPORT_OK> lists. I<Note:> modules in the default list don't need
297 to be reiterated in the OK list as they are already assumed to be
298 "OK" to export having been in the default list.
299  
300 (New in 0.1) It is also possible to negate only 1 item from the
301 default C<EXPORT> list, as well as import optional symbols in
302 1 statement.
303  
304 =head3 Example
305  
306 use MODULENAME qw(!$two five); #or
307 use MODULENAME qw(!two five);
308  
309 Only export C<two> from the default export list will be
310 excluded. Whereas export C<five> will be added to the list
311 of items to import.
312  
313 Other functions of Exporter are not currently implemented, though
314 certainly requests and code donations made via the CPAN issue database
315 will be considered if possible.
316  
317 =head2 Types and Type Export
318  
319 Listing the EXPORT and EXPORT_OK assignments as params to Xporter will
320 allow their types to be available to importing modules at compile time.
321 the L<mem> module was provided as a generic way to force declarations
322 into memory during Perl's initial BEGIN phase so they will be in effect
323 when the program runs.
324  
325 =head2 Version Strings
326  
327 Version strings in the form of a decimal fraction, (0.001001), a
328 V-String (v1.2.1 with no quotes), or a version string
329 ('1.1.1' or 'v1.1.1') are supported, though note, versions in
330 different formats are not interchangeable. The format specified
331 in a module's documentation should be used.
332  
333  
334  
335  
336  
337  
↑ Summary