lib/Badger/Exporter.pm

filter: Statementstmt 93.8% 228 / 243 Branchbran 80.2% 130 / 162 Conditioncond 59.6% 34 / 57 MC/DCmcdc 29.7% 11 / 37 Subroutinesub 90.3% 28 / 31 Podpod 92.3% 12 / 13 totaltotal 81.5% 443 / 543 CC 106 SCAR 32.4
CC
106
Cov
85%
export
54.5
exportables
27.4
export_tags
11.1
CRAP 25.5
« blib/lib/Badger/Data/Type.pm ↑ Summary blib/lib/Badger/Data/Facets.pm »
1 #========================================================================
2 #
3 # Badger::Exporter
4 #
5 # DESCRIPTION
6 # This module is an OO version of the Exporter module. It
7 # does the same kind of thing but with an OO interface that means
8 # you don't have to go messing around with package variables. It
9 # correctly handles inheritance, exporting not only those symbols
10 # defined by a subclass, but also those of its base classes.
11 #
12 # AUTHOR
13 # Andy Wardley <abw@wardley.org>
14 #
15 #========================================================================
16  
17 package Badger::Exporter;
18  
19 70 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 70 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 70 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 use constant {
23 70 ALL => 'all', # Alas, we can't pull these in from
24 NONE => 'none', # Badger::Constants because it's a
25 DEFAULT => 'default', # subclass of Badger::Exporter which
26 IMPORT => 'import', # gives us a chicken-and-egg dependency
27 IMPORTS => 'imports', # problem.We could pull them into
28 HOOKS => 'hooks', # Badger::Constants though because
29 ARRAY => 'ARRAY', # that's a subclass... hmmm....
30 HASH => 'HASH',
31 CODE => 'CODE',
32 EXPORT_ALL => 'EXPORT_ALL',
33 EXPORT_ANY => 'EXPORT_ANY',
34 EXPORT_TAGS => 'EXPORT_TAGS',
35 EXPORT_FAIL => 'EXPORT_FAIL',
36 EXPORT_HOOKS => 'EXPORT_HOOKS',
37 EXPORT_BEFORE => 'EXPORT_BEFORE',
38 EXPORT_AFTER => 'EXPORT_AFTER',
39 EXPORTABLES => 'EXPORTABLES',
40 ISA => 'ISA',
41 REFS => 'refs',
42 ONCE => 'once',
43 PKG => '::',
44 DELIMITER => qr/(?:,\s*)|\s+/, # match a comma or whitespace
45 MISSING => "Missing value for the '%s' option%s",
46 BAD_HANDLER => "Invalid export %s handler specified: %s",
47 BAD_HOOK => "Invalid export hook handler specified for the '%s' option: %s",
48 WANTED => " (%s wanted, %s specified)",
49 UNDEFINED => " (argument %s of %s is undefined)",
50 70 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 70 our $VERSION = 0.01;
53 70 ▶our $DEBUG = 0 unless defined $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Badger::Exporter::DEBUG 70 0 70
54 70 our $HANDLERS = {
55 all => \&export_all,
56 any => \&export_any,
57 tags => \&export_tags,
58 hooks => \&export_hooks,
59 fail => \&export_fail,
60 before => \&export_before,
61 after => \&export_after,
62 };
63  
64  
65 #-----------------------------------------------------------------------
66 # export declaration methods:
67 # exports( all => [...], any => [...], ...etc... )
68 # export_all('foo bar baz')
69 # export_any('foo bar baz')
70 # export_before( sub { ... } )
71 # export_after( sub { ... } )
72 # export_tags( set1 => 'foo bar baz', set2 => 'wam bam' )
73 # export_hooks( foo => sub { ... }, bar => sub { ... } )
74 # export_fail( sub { ... } )
75 #-----------------------------------------------------------------------
76  
77 sub exports {
78 696 ▶ my $self = shift;
Subroutine exports: called CC=5 SCAR=16.4
Pod documented
79 696 ▶ my $data = @_ == 1 && ref $_[0] eq HASH ? shift : { @_ };
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 && ref $_[0] eq 'HASH' ? : 680 16 696
Condition66%2/3
@_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
160680
MC/DC50%1/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1 ref $_[0] eq "HASH"
Truth table66%2/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
80 696 my $handler;
81  
82 # delegate each key in $data to a handler in $HANDLERS
83 696 ▶ while (my ($key, $value) = each %$data) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$data) 1188 696 1884
84 1188 ▶ $handler = $HANDLERS->{ $key }
Condition33%1/3
$Badger::Exporter::HANDLERS->{$key} || croak("Invalid exports key: $key\n")
l!l&&r!l&&!r
118800
MC/DC0%0/2
$Badger::Exporter::HANDLERS->{$key} || croak("Invalid exports key: $key\n")
$Badger::Exporter::HANDLERS->{$key} croak("Invalid exports key: $key\n")
Truth table33%1/3
A || B
A= $Badger::Exporter::HANDLERS->{$key}B= croak("Invalid exports key: $key\n")
ABresult
1X1
011
000
85 || croak "Invalid exports key: $key\n";
86 1188 $handler->($self, $value);
87 }
88 }
89  
90 sub export_all {
91 75 ▶ my $self = shift;
Subroutine export_all: called CC=3 SCAR=11.0
Pod documented
92 75 ▶ my $args = @_ == 1 ? shift : [ @_ ];
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 74 1 75
93 75 my $list = $self->export_variable( EXPORT_ALL => [ ] );
94 75 ▶ push( @$list, ref $args eq ARRAY ? @$args : split(DELIMITER, $args) );
Branch: 1 branch
BranchTrueFalseTotal
ref $args eq 'ARRAY' ? : 1 74 75
95 }
96  
97 sub export_any {
98 795 ▶ my $self = shift;
Subroutine export_any: called CC=3 SCAR=11.0
Pod documented
99 795 ▶ my $args = @_ == 1 ? shift : [ @_ ];
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 334 461 795
100 795 my $list = $self->export_variable( EXPORT_ANY => [ ] );
101 795 ▶ push( @$list, ref $args eq ARRAY ? @$args : split(DELIMITER, $args) );
Branch: 1 branch
BranchTrueFalseTotal
ref $args eq 'ARRAY' ? : 464 331 795
102 }
103  
104 sub export_before {
105 26 ▶ my $self = shift;
Subroutine export_before: called CC=3 SCAR=11.4
Pod documented
106 26 ▶ my $args = @_ == 1 ? shift : [ @_ ];
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 26 0 26
107 26 my $list = $self->export_variable( EXPORT_BEFORE => [ ] );
108 26 ▶ push( @$list, ref $args eq ARRAY ? @$args : $args );
Branch: 1 branch
BranchTrueFalseTotal
ref $args eq 'ARRAY' ? : 0 26 26
109 }
110  
111 sub export_after {
112 72 ▶ my $self = shift;
Subroutine export_after: called CC=3 SCAR=11.4
Pod documented
113 72 ▶ my $args = @_ == 1 ? shift : [ @_ ];
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 72 0 72
114 72 my $list = $self->export_variable( EXPORT_AFTER => [ ] );
115 72 ▶ push( @$list, ref $args eq ARRAY ? @$args : $args );
Branch: 1 branch
BranchTrueFalseTotal
ref $args eq 'ARRAY' ? : 0 72 72
116 }
117  
118 sub export_tags {
119 287 ▶ my $self = shift;
Subroutine export_tags: called CC=11 SCAR=24.1
Pod documented
120 287 ▶ my $args = (@_ == 1) && (ref $_[0] eq HASH) ? shift : { @_ };
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 && ref $_[0] eq 'HASH' ? : 284 3 287
Condition66%2/3
@_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
30284
MC/DC50%1/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1 ref $_[0] eq "HASH"
Truth table66%2/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
121 287 my $tags = $self->export_variable( EXPORT_TAGS => { } );
122  
123 # Add new tags into $EXPORT_TAGS hash ref
124 @$tags{ keys %$args } = map {
125 # Tags can be defined as hash arrays containing (key => '=value')
126 # declarataions. We upgrade each '=value' to a constant subroutine.
127 287 ▶ if (ref && ref eq HASH) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_ and ref $_ eq "HASH") 4 496 500
Condition100%3/3
ref $_ and ref $_ eq "HASH"
!ll&&!rl&&r
3501464
MC/DC100%2/2
ref $_ and ref $_ eq "HASH"
ref $_ ref $_ eq "HASH"
Truth table100%3/3
A and B
A= ref $_B= ref $_ eq "HASH"
ABresult
0X0
100
111
128 4 ▶ while (my ($key, $value) = each %$_) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$_) 13 4 17
129 13 ▶ if ($value =~ s/^=//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ s/^=//) 4 9 13
130 4 ▶ _debug("export_tags() constructing constant: $key => $value\n") if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Badger::Exporter::DEBUG 0 4 4
131 4 ▶ $_->{ $key } = sub() { $value };
Subroutine __ANON__: not called CC=1 SCAR=1.2
132 }
133 }
134 }
135 500 $_;
136 }
137 values %$args;
138  
139 # all symbols referenced in tagsets (except other tag sets) must be
140 # flagged as exportable
141 $self->export_any(
142 grep {
143 # ignore references to code or other tag sets
144 2702 ▶ not (ref || /^(:|=)/);
Condition66%2/3
ref $_ || /^(:|=)/
l!l&&r!l&&!r
802694
MC/DC50%1/2
ref $_ || /^(:|=)/
ref $_ /^(:|=)/
Truth table66%2/3
A || B
A= ref $_B= /^(:|=)/
ABresult
1X1
011
000
145 }
146 map {
147 # symbols in tagset can be a list ref, hash ref or string
148 287 ▶ ref $_ eq ARRAY ? @$_ :
Branch: 2 branches
BranchTrueFalseTotal
ref $_ eq 'ARRAY' ? : 146 354 500
ref $_ eq 'HASH' ? : 4 350 354
149 ref $_ eq HASH ? %$_ :
150 split DELIMITER
151 }
152 values %$args
153 );
154  
155 287 return $tags;
156 }
157  
158 sub export_hooks {
159 506 ▶ my $self = shift;
Subroutine export_hooks: called CC=3 SCAR=11.0
Pod documented
160 506 ▶ my $args = (@_ == 1) && (ref $_[0] eq HASH) ? shift : { @_ };
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 && ref $_[0] eq 'HASH' ? : 505 1 506
Condition66%2/3
@_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
10505
MC/DC50%1/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1 ref $_[0] eq "HASH"
Truth table66%2/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
161 506 my $hooks = $self->export_variable( EXPORT_HOOKS => { } );
162 506 @$hooks{ keys %$args } = values %$args;
163 506 return $hooks;
164 }
165  
166 sub export_fail {
167 166 ▶ my $self = shift;
Subroutine export_fail: called CC=3 SCAR=11.8
Pod documented
168 166 ▶ my $class = ref $self || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
01660
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
169 70 ▶ no strict REFS;
Subroutine BEGIN: called CC=1 SCAR=0.0
170  
171 # get/set $EXPORT_FAIL
172 return @_
173 166 ? (${$class.PKG.EXPORT_FAIL} = shift)
174 166 ▶ : ${$class.PKG.EXPORT_FAIL};
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 166 0 166
175 }
176  
177  
178 #------------------------------------------------------------------------
179 # import/export methods:
180 # import(@imports)
181 # export($target, @exports)
182 #------------------------------------------------------------------------
183  
184 sub import {
185 3422 ▶ my $class = shift;
Subroutine import: called CC=1 SCAR=0.0
186 3422 my $target = (caller())[0];
187  
188 # enable strict and warnings in the caller - this ensures that every
189 # Badger module (that calls this method - which is pretty much all of
190 # them) has strict/warnings enabled, without having to explicitly write
191 # it. Thx Moose!
192 3422 strict->import;
193 3422 warnings->import;
194  
195 # call in the heavy guns
196 3422 $class->export($target, @_);
197 }
198  
199 sub export {
200 6647 ▶ my $class = shift;
Subroutine export: called CC=45 SCAR=40.0
Pod documented
201 6647 my $target = shift;
202 6647 ▶ my $imports = @_ == 1 ? shift : [ @_ ];
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 2488 4159 6647
203 6647 my ($all, $any, $tags, $hooks, $fails, $before, $after)
204 = $class->exportables;
205 6647 ▶ my $can_hook = (%$hooks ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
%$hooks ? : 4142 2505 6647
206 6647 my $added_all = 0;
207 6647 my $count = 0;
208 6647 my ($symbol, $symbols, $source, $hook, $pkg, $nargs,
209 %done, @args, @errors);
210  
211 70 ▶ no strict REFS;
Subroutine BEGIN: called CC=1 SCAR=0.0
212 70 ▶ no warnings ONCE;
Subroutine BEGIN: called CC=1 SCAR=0.0
213  
214 # imports can be a single whitespace delimited string of symbols
215 6647 ▶ $imports = [ split(DELIMITER, $imports) ]
Branch: 1 branch
BranchTrueFalseTotal
unless ref $imports eq "ARRAY" 2481 4166 6647
216 unless ref $imports eq ARRAY;
217  
218 # default to export_all if list of exports not specified
219 # TODO: what about: use Badger::Example qw(); ? perhaps we should
220 # return unless @_ up above?
221 6647 ▶ @$imports = @$all unless @$imports;
Branch: 1 branch
BranchTrueFalseTotal
unless @$imports 1017 5630 6647
222  
223 6647 foreach $hook (@$before) {
224 28 $hook->($class, $target, $imports);
225 }
226  
227 6647 ▶ SYMBOL: while (@$imports) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$imports) 33804 6644 40448
228 33804 ▶ next unless ($symbol = shift @$imports);
Branch: 1 branch
BranchTrueFalseTotal
unless $symbol = shift @$imports 0 33804 33804
229 33804 ▶ next if $done{ $symbol }++;
Branch: 1 branch
BranchTrueFalseTotal
if $done{$symbol}++ 2 33802 33804
230  
231 # look for :tagset symbols and expand their contents onto @$imports
232 33802 ▶ if ($symbol =~ s/^://) {
Branch: 1 branch
BranchTrueFalseTotal
if ($symbol =~ s/^://) 455 33347 33802
233 455 ▶ if ($symbols = $tags->{ $symbol }) {
Branch: 3 branches
BranchTrueFalseTotal
if ($symbols = $tags->{$symbol}) { } 445 10 455
elsif ($symbol eq 'default') { } 1 9 10
elsif ($symbol eq 'all') { } 9 0 9
234 445 ▶ if (ref $symbols eq ARRAY) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $symbols eq 'ARRAY') { } 8 437 445
elsif (ref $symbols eq 'HASH') { } 4 433 437
235 # expand list of symbols onto @$imports list
236 8 unshift(@$imports, @$symbols);
237 }
238 elsif (ref $symbols eq HASH) {
239 # map hash into [name => $symbol] pairs
240 4 unshift(@$imports, map { [$_ => $symbols->{ $_ }] } keys %$symbols);
241 }
242 else {
243 # string of space-delimited symbols
244 433 unshift(@$imports, split(DELIMITER, $symbols));
245 }
246 }
247 elsif ($symbol eq DEFAULT) {
248 1 unshift(@$imports, @$all);
249 }
250 elsif ($symbol eq ALL) {
251 9 unshift(@$imports, keys %$any);
252 9 $added_all = 1;
253 }
254 else {
255 0 push(@errors, "Invalid import tag: $symbol\n");
256 }
257 455 next SYMBOL;
258 }
259  
260 33347 ▶ if (ref $symbol eq ARRAY) {
Branch: 5 branches
BranchTrueFalseTotal
if (ref $symbol eq 'ARRAY') { } 12 33335 33347
elsif ($can_hook and $hook = $hooks->{$symbol}) { } 14062 19273 33335
elsif ($symbol eq 'imports') { } 0 19273 19273
elsif ($symbol eq 'import') { } 1191 18082 19273
elsif ($symbol eq 'hooks') { } 1191 16891 18082
261 # a pair of [name, $symbol] expanded from a :tag hash set
262 12 ($symbol, $source) = @$symbol;
263 # _debug("expanded export pair: $symbol => $source\n") if $DEBUG;
264 }
265 ▶ elsif ($can_hook && ($hook = $hooks->{ $symbol })) {
Condition100%3/3
$can_hook and $hook = $hooks->{$symbol}
!ll&&!rl&&r
10944832914062
MC/DC100%2/2
$can_hook and $hook = $hooks->{$symbol}
$can_hook $hook = $hooks->{$symbol}
Truth table100%3/3
A and B
A= $can_hookB= $hook = $hooks->{$symbol}
ABresult
0X0
100
111
266 # a hook can be specified as [$code,$nargs] in which case we
267 # generate a closure around the $code which shifts $nargs off
268 # the symbols list and passes them as arguments to $code
269 14062 ▶ $hook = $hooks->{ $symbol } = $class->export_hook_generator($symbol, $hook)
Branch: 1 branch
BranchTrueFalseTotal
unless ref $hook eq "CODE" 467 13595 14062
270 unless ref $hook eq CODE;
271  
272 # fire off handler hooked to this import item
273 14062 &$hook($class, $target, $symbol, $imports);
274  
275 # hooks can be repeated so pretend we haven't done it
276 14059 $done{ $symbol }--;
277 14059 next SYMBOL;
278 }
279 elsif ($symbol eq IMPORTS) {
280 # special 'imports' hook disables any more hooks causing
281 # all remaining arguments to be imported as symbols
282 0 $can_hook = 0;
283 0 next SYMBOL;
284 }
285 elsif ($symbol eq IMPORT) {
286 # 'import' hook accepts the next item as an import list/string
287 # and unpacks it onto the front of the imports list. We disable
288 # hooks for the duration of the import and insert a dummy HOOKS
289 # symbol at the end to re-enable hooks
290 1191 $can_hook = 0;
291 1191 ▶ if ($symbols = shift @$imports) {
Branch: 1 branch
BranchTrueFalseTotal
if ($symbols = shift @$imports) { } 1191 0 1191
292 1191 ▶ $symbols = [ split(DELIMITER, $symbols) ]
Branch: 1 branch
BranchTrueFalseTotal
unless ref $symbols eq "ARRAY" 1191 0 1191
293 unless ref $symbols eq ARRAY;
294 1191 unshift(@$imports, @$symbols, HOOKS);
295 }
296 else {
297 0 push(@errors, "Missing argument for $symbol hook\n");
298 }
299 1191 next SYMBOL;
300 }
301 elsif ($symbol eq HOOKS) {
302 # special 'hooks' item turns hooks back on
303 1191 $can_hook = 1;
304 1191 next SYMBOL;
305 }
306 else {
307 # otherwise the symbol exported is the one requested
308 16891 $source = $symbol;
309 }
310  
311 # check we're allowed to export the symbol requested
312 16903 ▶ if ($pkg = $any->{ $symbol }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pkg = $any->{$symbol}) { } 16708 195 16903
313 # _debug("exporting $symbol from $pkg to $target\n") if $DEBUG;
314 }
315 else {
316 195 foreach $hook (@$fails) {
317 198 ▶ if (&$hook($class, $target, $symbol, $imports)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$hook($class, $target, $symbol, $imports)) 195 3 198
318 # hooks can be repeated so pretend we haven't done it
319 195 $done{ $symbol }--;
320 195 next SYMBOL;
321 }
322 }
323 0 push(@errors, "$symbol is not exported by $class\n");
324 0 next SYMBOL;
325 }
326  
327 16708 ▶ if (ref $source eq CODE) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $source eq 'CODE') { } 8 16700 16708
328 # patch directly into the code ref
329 # _debug("exporting $symbol from code reference\n") if $DEBUG;
330 8 *{ $target.PKG.$symbol } = $source;
331 }
332 else {
333 16700 my $type = "&";
334 16700 $symbol =~ s/^(\W)//;
335 16700 ▶ $source =~ s/^(\W)// and $type = $1;
Branch: 1 branch
BranchTrueFalseTotal
$source =~ s/^(\W)// and $type = $1 58 16642 16700
336 # NOTE: '=value' should *probably* never be found at this point
337 # because we're now upgrading them to constant subroutines in
338 # the import_tags() method. However, I'm leaving this in here
339 # until I've had a chance to properly review the code and convince
340 # myself that this assumption is correct.
341 16700 ▶ _debug("export() constructing constant: $symbol => $source\n")
Branch: 1 branch
BranchTrueFalseTotal
if $Badger::Exporter::DEBUG and $type eq "=" 0 16700 16700
Condition33%1/3
$Badger::Exporter::DEBUG and $type eq "="
!ll&&!rl&&r
1670000
MC/DC0%0/2
$Badger::Exporter::DEBUG and $type eq "="
$Badger::Exporter::DEBUG $type eq "="
Truth table33%1/3
A and B
A= $Badger::Exporter::DEBUGB= $type eq "="
ABresult
0X0
100
111
342 if $DEBUG && $type eq '=';
343 16700 ▶ $source = $pkg.PKG.$source unless $source =~ /::/ or $type eq '=';
Branch: 1 branch
BranchTrueFalseTotal
unless $source =~ /::/ or $type eq "=" 16696 4 16700
Condition66%2/3
$source =~ /::/ or $type eq "="
l!l&&r!l&&!r
4016696
MC/DC50%1/2
$source =~ /::/ or $type eq "="
$source =~ /::/ $type eq "="
Truth table66%2/3
A or B
A= $source =~ /::/B= $type eq "="
ABresult
1X1
011
000
344 16700 ▶ _debug("exporting $type$symbol from $source into $target\n") if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Badger::Exporter::DEBUG 0 16700 16700
345 16700 *{ $target.PKG.$symbol } =
346 16644 ▶ $type eq '&' ? \&{$source} :
Branch: 1 branch
BranchTrueFalseTotal
$type eq '$' ? : 34 22 56
347 34 ▶ $type eq '$' ? \${$source} :
Branch: 1 branch
BranchTrueFalseTotal
$type eq '@' ? : 13 9 22
348 13 ▶ $type eq '@' ? \@{$source} :
Branch: 1 branch
BranchTrueFalseTotal
$type eq '%' ? : 9 0 9
349 9 ▶ $type eq '%' ? \%{$source} :
Branch: 1 branch
BranchTrueFalseTotal
$type eq '*' ? : 0 0 0
350 0 ▶ $type eq '*' ? *{$source} :
Branch: 1 branch
BranchTrueFalseTotal
$type eq '=' ? : 0 0 0
351 0 ▶ $type eq '=' ? sub(){$source} :
Subroutine __ANON__: not called CC=1 SCAR=6.9
352 16700 ▶ do { push(@errors, "Can't export symbol: $type$symbol\n"); next; };
Branch: 1 branch
BranchTrueFalseTotal
$type eq '&' ? : 16644 56 16700
353 }
354 16708 $count++;
355 }
356 continue {
357 # if we're on the last item and we've only processed hooks
358 # (i.e. no real symbols were specified then we export the
359 # default set of symbols instead
360 33801 ▶ unless (@$imports or $count or $added_all) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$imports or $count or $added_all) 631 33170 33801
Condition83%5/6
@$imports or $count or $added_all
l!l&&r!l&&!r
331700631
@$imports or $count
l!l&&r!l&&!r
280725098631
MC/DC66%2/3
@$imports or $count or $added_all
@$imports $count $added_all
Truth table75%3/4
A or B or C
A= @$importsB= $countC= $added_all
ABCresult
1XX1
01X1
0011
0000
361 631 unshift(@$imports, @$all);
362 631 $added_all = 1;
363 }
364 }
365  
366 6644 ▶ if (@errors) {
Branch: 1 branch
BranchTrueFalseTotal
if (@errors) 0 6644 6644
367 0 require Carp;
368 0 Carp::croak("@{errors}Can't continue after import errors");
369 }
370  
371 6644 foreach $hook (@$after) {
372 102 $hook->($class, $target);
373 }
374  
375 6643 return 1;
376 }
377  
378 sub exportables {
379 6647 ▶ my $class = shift;
Subroutine exportables: called CC=26 SCAR=33.1
Pod documented
380 70 ▶ no strict REFS;
Subroutine BEGIN: called CC=1 SCAR=0.0
381 70 ▶ no warnings ONCE;
Subroutine BEGIN: called CC=1 SCAR=0.0
382  
383 6647 ▶ my $cache = ${ $class.PKG.EXPORTABLES } ||= do {
Condition66%2/3
${$class . '::' . "EXPORTABLES";} ||= do { my($pkg, $symbols, %done, @all, %any, %tags, %hooks, @fails, @before, @after); my(@pending) = $class; while ($pkg = shift @pending) { next if $done{$pkg}++; if ($symbols = ${$pkg . '::' . "EXPORT_ANY";}) { $symbols = [split(/(?:,\s*)|\s+/, $symbols, 0)] unless ref $symbols eq "ARRAY"; $any{$_} ||= $pkg foreach (@$symbols); } ; if ($symbols = ${$pkg . '::' . "EXPORT_ALL";}) { $symbols = [split(/(?:,\s*)|\s+/, $symbols, 0)] unless ref $symbols eq "ARRAY"; push @all, map({$any{$_} ||= $pkg; $_;} @$symbols); } ; if ($symbols = ${$pkg . '::' . "EXPORT_TAGS";}) { $tags{$_} ||= $symbols->{$_} foreach (keys %$symbols); } ; if ($symbols = ${$pkg . '::' . "EXPORT_HOOKS";}) { ($any{$_} or $hooks{$_} ||= $symbols->{$_}) foreach (keys %$symbols); } ; if ($symbols = ${$pkg . '::' . "EXPORT_FAIL";}) { push @fails, $symbols; } ; if ($symbols = ${$pkg . '::' . "EXPORT_BEFORE";}) { unshift @before, ref $symbols eq "CODE" ? $symbols : (ref $symbols eq "ARRAY" ? @$symbols : croak(sprintf('Invalid export %s handler specified: %s', "before", $symbols))); } ; if ($symbols = ${$pkg . '::' . "EXPORT_AFTER";}) { unshift @after, ref $symbols eq "CODE" ? $symbols : (ref $symbols eq "ARRAY" ? @$symbols : croak(sprintf('Invalid export %s handler specified: %s', "after", $symbols))); } ; push @pending, @{$pkg . '::' . "ISA";}; } ; [\@all, \%any, \%tags, \%hooks, \@fails, \@before, \@after] }
l!l&&r!l&&!r
554611010
MC/DC0%0/2
${$class . '::' . "EXPORTABLES";} ||= do { my($pkg, $symbols, %done, @all, %any, %tags, %hooks, @fails, @before, @after); my(@pending) = $class; while ($pkg = shift @pending) { next if $done{$pkg}++; if ($symbols = ${$pkg . '::' . "EXPORT_ANY";}) { $symbols = [split(/(?:,\s*)|\s+/, $symbols, 0)] unless ref $symbols eq "ARRAY"; $any{$_} ||= $pkg foreach (@$symbols); } ; if ($symbols = ${$pkg . '::' . "EXPORT_ALL";}) { $symbols = [split(/(?:,\s*)|\s+/, $symbols, 0)] unless ref $symbols eq "ARRAY"; push @all, map({$any{$_} ||= $pkg; $_;} @$symbols); } ; if ($symbols = ${$pkg . '::' . "EXPORT_TAGS";}) { $tags{$_} ||= $symbols->{$_} foreach (keys %$symbols); } ; if ($symbols = ${$pkg . '::' . "EXPORT_HOOKS";}) { ($any{$_} or $hooks{$_} ||= $symbols->{$_}) foreach (keys %$symbols); } ; if ($symbols = ${$pkg . '::' . "EXPORT_FAIL";}) { push @fails, $symbols; } ; if ($symbols = ${$pkg . '::' . "EXPORT_BEFORE";}) { unshift @before, ref $symbols eq "CODE" ? $symbols : (ref $symbols eq "ARRAY" ? @$symbols : croak(sprintf('Invalid export %s handler specified: %s', "before", $symbols))); } ; if ($symbols = ${$pkg . '::' . "EXPORT_AFTER";}) { unshift @after, ref $symbols eq "CODE" ? $symbols : (ref $symbols eq "ARRAY" ? @$symbols : croak(sprintf('Invalid export %s handler specified: %s', "after", $symbols))); } ; push @pending, @{$pkg . '::' . "ISA";}; } ; [\@all, \%any, \%tags, \%hooks, \@fails, \@before, \@after] }
${$class . '::' . "EXPORTABLES";} do { my($pkg, $symbols, %done, @all, %any, %tags, %hooks, @fails, @before, @after); my(@pending) = $class; while ($pkg = shift @pending) { next if $done{$pkg}++; if ($symbols = ${$pkg . '::' . "EXPORT_ANY";}) { $symbols = [split(/(?:,\s*)|\s+/, $symbols, 0)] unless ref $symbols eq "ARRAY"; $any{$_} ||= $pkg foreach (@$symbols); } ; if ($symbols = ${$pkg . '::' . "EXPORT_ALL";}) { $symbols = [split(/(?:,\s*)|\s+/, $symbols, 0)] unless ref $symbols eq "ARRAY"; push @all, map({$any{$_} ||= $pkg; $_;} @$symbols); } ; if ($symbols = ${$pkg . '::' . "EXPORT_TAGS";}) { $tags{$_} ||= $symbols->{$_} foreach (keys %$symbols); } ; if ($symbols = ${$pkg . '::' . "EXPORT_HOOKS";}) { ($any{$_} or $hooks{$_} ||= $symbols->{$_}) foreach (keys %$symbols); } ; if ($symbols = ${$pkg . '::' . "EXPORT_FAIL";}) { push @fails, $symbols; } ; if ($symbols = ${$pkg . '::' . "EXPORT_BEFORE";}) { unshift @before, ref $symbols eq "CODE" ? $symbols : (ref $symbols eq "ARRAY" ? @$symbols : croak(sprintf('Invalid export %s handler specified: %s', "before", $symbols))); } ; if ($symbols = ${$pkg . '::' . "EXPORT_AFTER";}) { unshift @after, ref $symbols eq "CODE" ? $symbols : (ref $symbols eq "ARRAY" ? @$symbols : croak(sprintf('Invalid export %s handler specified: %s', "after", $symbols))); } ; push @pending, @{$pkg . '::' . "ISA";}; } ; [\@all, \%any, \%tags, \%hooks, \@fails, \@before, \@after] }
Truth table66%2/3
A ||= B
A= ${$class . '::' . "EXPORTABLES";}B= do { my($pkg, $symbols, %done, @all, %any, %tags, %hooks, @fails, @before, @after); my(@pending) = $class; while ($pkg = shift @pending) { next if $done{$pkg}++; if ($symbols = ${$pkg . '::' . "EXPORT_ANY";}) { $symbols = [split(/(?:,\s*)|\s+/, $symbols, 0)] unless ref $symbols eq "ARRAY"; $any{$_} ||= $pkg foreach (@$symbols); } ; if ($symbols = ${$pkg . '::' . "EXPORT_ALL";}) { $symbols = [split(/(?:,\s*)|\s+/, $symbols, 0)] unless ref $symbols eq "ARRAY"; push @all, map({$any{$_} ||= $pkg; $_;} @$symbols); } ; if ($symbols = ${$pkg . '::' . "EXPORT_TAGS";}) { $tags{$_} ||= $symbols->{$_} foreach (keys %$symbols); } ; if ($symbols = ${$pkg . '::' . "EXPORT_HOOKS";}) { ($any{$_} or $hooks{$_} ||= $symbols->{$_}) foreach (keys %$symbols); } ; if ($symbols = ${$pkg . '::' . "EXPORT_FAIL";}) { push @fails, $symbols; } ; if ($symbols = ${$pkg . '::' . "EXPORT_BEFORE";}) { unshift @before, ref $symbols eq "CODE" ? $symbols : (ref $symbols eq "ARRAY" ? @$symbols : croak(sprintf('Invalid export %s handler specified: %s', "before", $symbols))); } ; if ($symbols = ${$pkg . '::' . "EXPORT_AFTER";}) { unshift @after, ref $symbols eq "CODE" ? $symbols : (ref $symbols eq "ARRAY" ? @$symbols : croak(sprintf('Invalid export %s handler specified: %s', "after", $symbols))); } ; push @pending, @{$pkg . '::' . "ISA";}; } ; [\@all, \%any, \%tags, \%hooks, \@fails, \@before, \@after] }
ABresult
1X1
011
000
384 1101 my ($pkg, $symbols, %done, @all, %any, %tags, %hooks, @fails, @before, @after);
385 1101 my @pending = ($class);
386 70 ▶ no strict REFS;
Subroutine BEGIN: called CC=1 SCAR=0.0
387  
388 # walk up inheritance tree collecting values from the @$EXPORT_ALL,
389 # @$EXPORT_ANY, %$EXPORT_TAGS, %$EXPORT_HOOKS and $EXPORT_FAIL pkg
390 # variables, then cache them in $EXPORT_CACHE for subsequent use
391  
392 1101 ▶ while ($pkg = shift @pending) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pkg = shift @pending) 3609 1101 4710
393 3609 ▶ next if $done{ $pkg }++;
Branch: 1 branch
BranchTrueFalseTotal
if $done{$pkg}++ 77 3532 3609
394  
395 # TODO: we could optimise here by looking for a previously
396 # computed EXPORTABLES in the base class and merging it in...
397  
398 # $EXPORT_ANY package vars are list references containing symbols,
399 # which we use to populate the %any hash which maps symbols to
400 # their source packages. e.g. { foo => 'My::Package' }
401 # The presence of an entry in this table indicates that the symbol
402 # key can be exported. The corresponding value indicates the
403 # package that it must be exported from. We don't replace any
404 # existing entries in the %any hash because we're working from
405 # sub-class upwards to super-class, . This ensures that the
406 # entries put in first by more specialised sub-classes are used
407 # in preference to those defined by more general super-classes.
408 3532 ▶ if ($symbols = ${ $pkg.PKG.EXPORT_ANY }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($symbols = ${$pkg . '::' . "EXPORT_ANY";}) 825 2707 3532
409 825 ▶ $symbols = [ split(DELIMITER, $symbols) ]
Branch: 1 branch
BranchTrueFalseTotal
unless ref $symbols eq "ARRAY" 0 825 825
410 unless ref $symbols eq ARRAY;
411 $any{ $_ } ||= $pkg
412 825 ▶ for @$symbols;
Condition66%2/3
$any{$_} ||= $pkg
l!l&&r!l&&!r
12678780
MC/DC0%0/2
$any{$_} ||= $pkg
$any{$_} $pkg
Truth table66%2/3
A ||= B
A= $any{$_}B= $pkg
ABresult
1X1
011
000
413 }
414  
415 # $EXPORT_ALL is merged into @all and all symbols are mapped
416 # to their packages in %any
417 3532 ▶ if ($symbols = ${ $pkg.PKG.EXPORT_ALL }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($symbols = ${$pkg . '::' . "EXPORT_ALL";}) 77 3455 3532
418 77 ▶ $symbols = [ split(DELIMITER, $symbols) ]
Branch: 1 branch
BranchTrueFalseTotal
unless ref $symbols eq "ARRAY" 0 77 77
419 unless ref $symbols eq ARRAY;
420 push(
421 @all,
422 77 ▶ map { $any{ $_ } ||= $pkg; $_ }
Condition66%2/3
$any{$_} ||= $pkg
l!l&&r!l&&!r
28520
MC/DC0%0/2
$any{$_} ||= $pkg
$any{$_} $pkg
Truth table66%2/3
A ||= B
A= $any{$_}B= $pkg
ABresult
1X1
011
000
423 @$symbols
424 );
425 }
426  
427 # $EXPORT_TAGS are copied into %tags unless already defined
428 3532 ▶ if ($symbols = ${ $pkg.PKG.EXPORT_TAGS }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($symbols = ${$pkg . '::' . "EXPORT_TAGS";}) 434 3098 3532
429 $tags{ $_ } ||= $symbols->{ $_ }
430 434 ▶ for keys %$symbols;
Condition33%1/3
$tags{$_} ||= $symbols->{$_}
l!l&&r!l&&!r
06540
MC/DC0%0/2
$tags{$_} ||= $symbols->{$_}
$tags{$_} $symbols->{$_}
Truth table33%1/3
A ||= B
A= $tags{$_}B= $symbols->{$_}
ABresult
1X1
011
000
431 }
432  
433 # $EXPORT_HOOKS are copied into %hooks unless already defined
434 # (by a more specific subclass) either as hooks or any/all items
435 3532 ▶ if ($symbols = ${ $pkg.PKG.EXPORT_HOOKS }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($symbols = ${$pkg . '::' . "EXPORT_HOOKS";}) 610 2922 3532
436 $any{ $_ } or $hooks{ $_ } ||= $symbols->{ $_ }
437 610 ▶ for keys %$symbols;
Branch: 1 branch
BranchTrueFalseTotal
$any{$_} or $hooks{$_} ||= $symbols->{$_} 5541 5 5546
Condition33%1/3
$hooks{$_} ||= $symbols->{$_}
l!l&&r!l&&!r
055410
MC/DC0%0/2
$hooks{$_} ||= $symbols->{$_}
$hooks{$_} $symbols->{$_}
Truth table33%1/3
A ||= B
A= $hooks{$_}B= $symbols->{$_}
ABresult
1X1
011
000
438 }
439  
440 # $EXPORT_FAIL has only one value per package, but we can have
441 # several packages in the class ancestry
442 3532 ▶ if ($symbols = ${ $pkg.PKG.EXPORT_FAIL }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($symbols = ${$pkg . '::' . "EXPORT_FAIL";}) 267 3265 3532
443 267 push(@fails, $symbols);
444 }
445  
446 # $EXPORT_BEFORE and $EXPORT_AFTER are references to CODE or
447 # ARRAY refs (of CODE refs, we assume). As we travel up from
448 # subclass to superclass, we unshift() the handlers onto the
449 # start of the @before/@after arrays. This ensures that the base
450 # class handlers get called before subclass handlers.
451 3532 ▶ if ($symbols = ${ $pkg.PKG.EXPORT_BEFORE }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($symbols = ${$pkg . '::' . "EXPORT_BEFORE";}) 28 3504 3532
452 28 ▶ unshift(
Branch: 2 branches
BranchTrueFalseTotal
ref $symbols eq 'CODE' ? : 2 26 28
ref $symbols eq 'ARRAY' ? : 26 0 26
453 @before,
454 ref $symbols eq CODE ? $symbols :
455 ref $symbols eq ARRAY ? @$symbols :
456 croak sprintf(BAD_HANDLER, before => $symbols)
457 );
458 }
459  
460 3532 ▶ if ($symbols = ${ $pkg.PKG.EXPORT_AFTER }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($symbols = ${$pkg . '::' . "EXPORT_AFTER";}) 74 3458 3532
461 74 ▶ unshift(
Branch: 2 branches
BranchTrueFalseTotal
ref $symbols eq 'CODE' ? : 2 72 74
ref $symbols eq 'ARRAY' ? : 72 0 72
462 @after,
463 ref $symbols eq CODE ? $symbols :
464 ref $symbols eq ARRAY ? @$symbols :
465 croak sprintf(BAD_HANDLER, after => $symbols)
466 );
467 }
468  
469 # This is the same depth-first inheritance resolution algorithm
470 # that Perl uses. We can't use the fancy heritage() method in
471 # Badger::Class because of the Chicken-and-Egg dependency problem
472 # between Badger::Exporter and Badger::Class
473 3532 push(@pending, @{$pkg.PKG.ISA});
474 }
475  
476 1101 [\@all, \%any, \%tags, \%hooks, \@fails, \@before, \@after];
477 };
478  
479 return wantarray
480 6647 ▶ ? @$cache
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 6647 0 6647
481 : $cache;
482 }
483  
484 sub export_symbol {
485 304 ▶ my ($self, $target, $symbol, $ref) = @_;
Subroutine export_symbol: called CC=1 SCAR=0.0
Pod documented
486 70 ▶ no strict REFS;
Subroutine BEGIN: called CC=1 SCAR=0.0
487 70 ▶ no warnings ONCE;
Subroutine BEGIN: called CC=1 SCAR=0.0
488 304 *{ $target.PKG.$symbol } = $ref;
489 }
490  
491 sub export_variable {
492 1761 ▶ my ($self, $name, $default) = @_;
Subroutine export_variable: called CC=3 SCAR=11.0
Pod undocumented
493 1761 ▶ my $class = ref $self || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
017610
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
494 1761 my $var = $class.PKG.$name;
495 1761 my $item;
496 70 ▶ no strict REFS;
Subroutine BEGIN: called CC=1 SCAR=0.0
497  
498 1761 ▶ unless (defined ($item = ${$var})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined($item = ${$var;})) 1548 213 1761
499 # install the default value ref into the SCALAR $EXPORT_XXXX var
500 1548 ${$var} = $item = $default;
501 # then poke the symbol table to make Perl notice it's defined
502 1548 *{$var} = \${$var};
503 }
504  
505 1761 return $item;
506 }
507  
508 sub export_hook_generator {
509 467 ▶ my $self = shift;
Subroutine export_hook_generator: called CC=7 SCAR=20.8
Pod documented
510 467 my $name = shift;
511 467 ▶ my $hook = @_ == 1 ? shift : [ @_ ];
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 467 0 467
512  
513 # do nothing if we've already got a code ref that doesn't require args
514 467 ▶ return $hook
Branch: 1 branch
BranchTrueFalseTotal
if ref $hook eq "CODE" 0 467 467
515 if ref $hook eq CODE;
516  
517 # anything else must be a list ref containing [$code_ref, $n_args]
518 467 ▶ croak sprintf(BAD_HOOK, $name, $hook)
Branch: 1 branch
BranchTrueFalseTotal
unless ref $hook eq "ARRAY" 0 467 467
519 unless ref $hook eq ARRAY;
520  
521 467 my ($code, $nargs) = @$hook;
522  
523 # user is trying to confuse us with [$non_code_ref, ...]
524 467 ▶ croak sprintf(BAD_HOOK, $name, $code)
Branch: 1 branch
BranchTrueFalseTotal
unless ref $code eq "CODE" 0 467 467
525 unless ref $code eq CODE;
526  
527 # [$code, 0] or [$code] is fine as just $code, also reject $nargs < 0
528 467 ▶ return $code
Branch: 1 branch
BranchTrueFalseTotal
unless $nargs and $nargs > 0 0 467 467
Condition33%1/3
$nargs and $nargs > 0
!ll&&!rl&&r
00467
MC/DC0%0/2
$nargs and $nargs > 0
$nargs $nargs > 0
Truth table33%1/3
A and B
A= $nargsB= $nargs > 0
ABresult
0X0
100
111
529 unless $nargs && $nargs > 0;
530  
531 # OK it's safe to proceed
532 return sub {
533 5255 ▶ my ($this, $target, $symbol, $symbols) = @_;
Subroutine __ANON__: called CC=3 SCAR=11.2
534 5255 my $n = 1;
535 # check we've got enough arguments
536 5255 ▶ croak sprintf(MISSING, $symbol, sprintf(WANTED, $nargs, scalar @$symbols))
Branch: 1 branch
BranchTrueFalseTotal
if @$symbols < $nargs 0 5255 5255
537 if @$symbols < $nargs;
538  
539 # call the code ref with the first $nargs arguments, making sure
540 # they all have defined values
541 $code->(
542 $this, $target, $symbol,
543 ( map {
544 5255 ▶ croak sprintf(MISSING, $symbol, sprintf(UNDEFINED, $n, $nargs))
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 0 5255 5255
545 unless defined $_;
546 5255 $n++;
547 5255 $_
548 }
549 splice(@$symbols, 0, $nargs)
550 ),
551 $symbols,
552 );
553 }
554 467 }
555  
556 sub _debug {
557 0 ▶ print STDERR @_;
Subroutine _debug: not called CC=1 SCAR=6.9
558 }
559  
560  
561 70 1;
562  
563 __END__
« blib/lib/Badger/Data/Type.pm ↑ Summary blib/lib/Badger/Data/Facets.pm »