Exporter.pm

filter: Statementstmt 93.2% 166 / 178 Branchbran 82.6% 86 / 104 Conditioncond 63.6% 28 / 44 MC/DCmcdc 37.0% 10 / 27 Subroutinesub 100% 8 / 8 Podpod 100% 2 / 2 totaltotal 82.6% 300 / 363 CC 78 SCAR 38.9
CC
78
Cov
86%
do_export
64.1
_list_package_contents
16.5
install_import
4.1
CRAP 48.7
↑ Summary Examples/Export.pm »
1 package Perinci::Exporter;
2  
3 4 our $DATE = '2019-09-11'; # DATE
4 4 our $VERSION = '0.084'; # VERSION
5  
6 # IFUNBUILT
7 # use strict 'subs', 'vars';
8 # use warnings;
9 # END IFUNBUILT
10  
11 # what a generic name, this hash caches the wrapped functions, so that when
12 # importer asks to import a wrapped function with default wrapping options, we
13 # don't have to call wrap_sub again. customly wrapped functions are not cached
14 # though.
15 4 my %pkg_cache;
16  
17 2 ▶sub import {
Subroutine import: called CC=1 SCAR=0.0
18 2 my $package = shift;
19 2 my @caller = caller(0);
20 2 install_import(@_, into => $caller[0]);
21 }
22  
23 27 ▶sub install_import {
Subroutine install_import: called CC=4 SCAR=14.0
Pod documented
24 27 my %instargs = @_;
25 27 ▶ my @caller = caller($instargs{caller_level} || 0);
Condition50%1/2
$instargs{'caller_level'} || 0
l!l
027
MC/DC0%0/1
$instargs{'caller_level'} || 0
$instargs{'caller_level'}
Truth table50%1/2
A
A= $instargs{'caller_level'}
Aresult
11
00
26 27 ▶ my $into = $instargs{into} || $caller[0];
Condition33%1/3
$instargs{'into'} || $caller[0]
l!l&&r!l&&!r
2700
MC/DC0%0/2
$instargs{'into'} || $caller[0]
$instargs{'into'} $caller[0]
Truth table33%1/3
A || B
A= $instargs{'into'}B= $caller[0]
ABresult
1X1
011
000
27 27 ▶ return [400, "Please specify package to install import() to ".
Branch: 1 branch
BranchTrueFalseTotal
unless $into 0 27 27
28 "(either via 'into' or 'caller_level')"]
29 unless $into;
30 28 ▶ my $import = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
31 28 my $package = shift;
32  
33 28 my @caller = caller(0);
34 do_export(
35 {
36 source => $into,
37 target => $caller[0],
38 default_exports => $instargs{default_exports},
39 extra_exports => $instargs{extra_exports},
40 default_wrap => defined($instargs{default_wrap}) ? $instargs{default_wrap} : 0,
41 28 ▶ default_on_clash => defined($instargs{default_on_clash}) ? $instargs{default_on_clash} : 'force',
Branch: 2 branches
BranchTrueFalseTotal
defined $instargs{'default_wrap'} ? : 3 25 28
defined $instargs{'default_on_clash'} ? : 2 26 28
42 },
43 @_,
44 );
45 27 };
46  
47 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
48 27 *{"$into\::import"} = $import;
49 }
50  
51 28 ▶sub do_export {
Subroutine do_export: called CC=61 SCAR=41.6
Pod documented
52 28 my $expopts;
53 28 ▶ if ($_[0] && ref($_[0]) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] and ref $_[0] eq 'HASH') { } 28 0 28
Condition33%1/3
$_[0] and ref $_[0] eq "HASH"
!ll&&!rl&&r
0028
MC/DC0%0/2
$_[0] and ref $_[0] eq "HASH"
$_[0] ref $_[0] eq "HASH"
Truth table33%1/3
A and B
A= $_[0]B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
54 28 $expopts = shift;
55 } else {
56 0 die "First argument to do_export() must be export options";
57 }
58 28 my $source = $expopts->{source};
59 28 my $target = $expopts->{target};
60  
61  
62 # collect what symbols are available for exporting, along with their tags,
63 # etc.
64  
65 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
66  
67 28 my %exports;
68 28 my $metas = \%{"$source\::SPEC"};
69 28 ▶ $metas ||= {};
Condition50%1/2
$metas ||= {}
l!l
280
MC/DC0%0/1
$metas ||= {}
$metas
Truth table50%1/2
A
A= $metas
Aresult
11
00
70 28 for my $k (keys %$metas) {
71 # for now we limit ourselves to subs
72 159 ▶ next unless $k =~ /\A\w+\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $k =~ /\A\w+\z/ 0 159 159
73 159 ▶ my @tags = @{ $metas->{$k}{tags} || [] };
Condition100%2/2
$metas->{$k}{'tags'} || []
l!l
12534
MC/DC100%1/1
$metas->{$k}{'tags'} || []
$metas->{$k}{'tags'}
Truth table100%2/2
A
A= $metas->{$k}{'tags'}
Aresult
11
00
74 159 ▶ next if grep {$_ eq 'export:never'} @tags;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq "export:never";} @tags 25 134 159
75 134 $exports{$k} = {
76 tags => \@tags,
77 };
78 }
79  
80 28 ▶ for my $k (@{$expopts->{default_exports} || []},
Condition100%2/2
$expopts->{'default_exports'} || []
l!l
127
MC/DC100%1/1
$expopts->{'default_exports'} || []
$expopts->{'default_exports'}
Truth table100%2/2
A
A= $expopts->{'default_exports'}
Aresult
11
00
81 28 @{"$source\::EXPORT"}) {
82 54 ▶ if ($exports{$k}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exports{$k}) { } 29 25 54
83 29 push @{$exports{$k}{tags}}, 'export:default';
84 } else {
85 25 $exports{$k} = {
86 tags => [qw/export:default/],
87 };
88 }
89 }
90  
91 28 ▶ for my $k (@{$expopts->{extra_exports} || []},
Condition100%2/2
$expopts->{'extra_exports'} || []
l!l
127
MC/DC100%1/1
$expopts->{'extra_exports'} || []
$expopts->{'extra_exports'}
Truth table100%2/2
A
A= $expopts->{'extra_exports'}
Aresult
11
00
92 28 @{"$source\::EXPORT_OK"}) {
93 54 ▶ if ($exports{$k}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exports{$k}) { } 3 51 54
94 } else {
95 51 $exports{$k} = {
96 tags => [],
97 };
98 }
99 }
100  
101 28 for my $k (keys %exports) {
102 210 push @{$exports{$k}{tags}}, 'all';
103 }
104  
105 # parse import() arguments
106  
107 28 my %impopts; #
108 my @imps; # requested symbols or tags to export, each element is:
109 28 while (1) {
110 55 ▶ last unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 27 28 55
111 28 my $i = shift;
112 28 ▶ if ($i =~ s!^-!!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i =~ s/^-//) { } 4 24 28
113 4 ▶ die "Import option -$i requires argument" unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 1 3 4
114 3 $impopts{$i} = shift;
115 3 next;
116 } else {
117 24 my $el = {};
118 24 ▶ if (@_ && ref($_[0]) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ and ref $_[0] eq "HASH") 11 13 24
Condition100%3/3
@_ and ref $_[0] eq "HASH"
!ll&&!rl&&r
8511
MC/DC100%2/2
@_ and ref $_[0] eq "HASH"
@_ ref $_[0] eq "HASH"
Truth table100%3/3
A and B
A= @_B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
119 11 my $io = shift;
120 11 $el->{$_} = $io->{$_} for keys %$io;
121 };
122 24 $el->{sym} = $i;
123 24 push @imps, $el;
124 }
125 }
126  
127 27 ▶ if (!@imps) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@imps) 5 22 27
128 5 push @imps, {sym=>':default'};
129 }
130  
131 # find out existing symbols on the target package, so we can die on clash,
132 # if that behavior's what the importer wants
133  
134 27 my %existing = _list_package_contents($target);
135  
136 # recap information
137 27 my $recap = {wrapped=>[]};
138  
139 # import!
140  
141 27 ▶ $pkg_cache{$source} ||= {};
Condition100%2/2
$pkg_cache{$source} ||= {}
l!l
252
MC/DC100%1/1
$pkg_cache{$source} ||= {}
$pkg_cache{$source}
Truth table100%2/2
A
A= $pkg_cache{$source}
Aresult
11
00
142  
143 27 for my $imp (@imps) {
144 29 my @ssyms; # symbols from source package
145 29 ▶ if ($imp->{sym} =~ s!^:!!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($imp->{'sym'} =~ s/^://) { } 7 22 29
146 7 @ssyms = grep { grep {
147 107 ▶ "export:$imp->{sym}" eq $_ || $imp->{sym} eq $_
Condition100%3/3
"export:$imp->{'sym'}" eq $_ || $imp->{'sym'} eq $_
l!l&&r!l&&!r
181079
MC/DC100%2/2
"export:$imp->{'sym'}" eq $_ || $imp->{'sym'} eq $_
"export:$imp->{'sym'}" eq $_ $imp->{'sym'} eq $_
Truth table100%3/3
A || B
A= "export:$imp->{'sym'}" eq $_B= $imp->{'sym'} eq $_
ABresult
1X1
011
000
148 51 } @{ $exports{$_}{tags} } }
149 keys %exports;
150 } else {
151 22 @ssyms = ($imp->{sym});
152 }
153  
154 29 for my $ssym (sort @ssyms) {
155  
156 49 ▶ if (!$exports{$ssym}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($exports{$ssym}) 1 48 49
157 1 die "$ssym is not exported by $source";
158 }
159  
160 # export to what target symbol?
161 48 my $tsym;
162 48 ▶ if ($imp->{as}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($imp->{'as'}) { } 1 47 48
163 1 $tsym = $imp->{as};
164 } else {
165 47 $tsym = $ssym;
166 47 ▶ if (my $prefix = defined($imp->{prefix}) ? $imp->{prefix} : $impopts{prefix}) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $prefix = defined $imp->{"prefix"} ? $imp->{"prefix"} : $impopts{"prefix"}) 3 44 47
defined $imp->{'prefix'} ? : 1 46 47
167 3 $tsym = "$prefix$tsym";
168 }
169 47 ▶ if (my $suffix = defined($imp->{suffix}) ? $imp->{suffix} : $impopts{suffix}) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $suffix = defined $imp->{"suffix"} ? $imp->{"suffix"} : $impopts{"suffix"}) 3 44 47
defined $imp->{'suffix'} ? : 1 46 47
170 3 $tsym = "$tsym$suffix";
171 }
172 }
173  
174 # clash?
175 48 ▶ if ($existing{$tsym}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($existing{$tsym}) 2 46 48
176 2 ▶ if ((defined($impopts{on_clash}) ? $impopts{on_clash} : $expopts->{default_on_clash})
Branch: 2 branches
BranchTrueFalseTotal
if ((defined $impopts{"on_clash"} ? $impopts{"on_clash"} : $expopts->{"default_on_clash"}) eq "bail") 2 0 2
defined $impopts{'on_clash'} ? : 1 1 2
177 eq 'bail') {
178 2 ▶ die "Refusing to export ".
Branch: 1 branch
BranchTrueFalseTotal
$tsym eq $ssym ? : 2 0 2
179 ($tsym eq $ssym ? $ssym : "$ssym (as $tsym)").
180 " to an existing symbol in package $target";
181 }
182 }
183  
184 46 my $wrap;
185 SET_WRAP_OPTS: {
186 46 my $default_wrap = ref $expopts->{default_wrap} eq 'HASH' ?
187 0 ▶ {%{ $expopts->{default_wrap} }} : $expopts->{default_wrap};
Branch: 1 branch
BranchTrueFalseTotal
ref $expopts->{'default_wrap'} eq 'HASH' ? : 0 46 46
188 46 my $default_wrap_opts = $default_wrap;
189 46 ▶ $default_wrap_opts = {} if ref $default_wrap_opts ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $default_wrap_opts ne "HASH" 46 0 46
190 46 ▶ $default_wrap = {} if $default_wrap && ref $default_wrap ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if $default_wrap and ref $default_wrap ne "HASH" 6 40 46
Condition66%2/3
$default_wrap and ref $default_wrap ne "HASH"
!ll&&!rl&&r
4006
MC/DC50%1/2
$default_wrap and ref $default_wrap ne "HASH"
$default_wrap ref $default_wrap ne "HASH"
Truth table66%2/3
A and B
A= $default_wrapB= ref $default_wrap ne "HASH"
ABresult
0X0
100
111
191  
192 $wrap = ref $imp->{wrap} eq 'HASH' ?
193 46 ▶ {%{ $imp->{wrap} }} : $imp->{wrap};
Branch: 1 branch
BranchTrueFalseTotal
ref $imp->{'wrap'} eq 'HASH' ? : 1 45 46
194 46 ▶ $wrap = {} if $wrap && ref $wrap ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if $wrap and ref $wrap ne "HASH" 0 46 46
Condition66%2/3
$wrap and ref $wrap ne "HASH"
!ll&&!rl&&r
4510
MC/DC0%0/2
$wrap and ref $wrap ne "HASH"
$wrap ref $wrap ne "HASH"
Truth table66%2/3
A and B
A= $wrapB= ref $wrap ne "HASH"
ABresult
0X0
100
111
195 46 ▶ if (defined $imp->{convert}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $imp->{"convert"}) 1 45 46
196 1 ▶ die "Error when exporting $ssym: 'convert' option needs wrap=1 but wrap is disabled"
Branch: 1 branch
BranchTrueFalseTotal
if defined $wrap and not $wrap 0 1 1
Condition33%1/3
defined $wrap and not $wrap
!ll&&!rl&&r
100
MC/DC0%0/2
defined $wrap and not $wrap
defined $wrap not $wrap
Truth table33%1/3
A and not(B)
A= defined $wrapB= not $wrap
ABresult
0X0
100
111
197 if defined $wrap && !$wrap;
198 1 ▶ $wrap = $default_wrap_opts unless ref $wrap eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $wrap eq "HASH" 1 0 1
199  
200 1 $wrap->{convert} = $imp->{convert};
201 }
202 46 for (qw/args_as result_naked curry timeout/) {
203 183 ▶ if (defined $imp->{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $imp->{$_}) 5 178 183
204 5 ▶ die "Error when exporting $ssym: '$_' option needs wrap=1 but wrap is disabled"
Branch: 1 branch
BranchTrueFalseTotal
if defined $wrap and not $wrap 1 4 5
Condition66%2/3
defined $wrap and not $wrap
!ll&&!rl&&r
401
MC/DC50%1/2
defined $wrap and not $wrap
defined $wrap not $wrap
Truth table66%2/3
A and not(B)
A= defined $wrapB= not $wrap
ABresult
0X0
100
111
205 if defined $wrap && !$wrap;
206 4 ▶ $wrap = $default_wrap_opts unless ref $wrap eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $wrap eq "HASH" 4 0 4
207  
208 4 ▶ $wrap->{convert} ||= {};
Condition50%1/2
$wrap->{"convert"} ||= {}
l!l
04
MC/DC0%0/1
$wrap->{"convert"} ||= {}
$wrap->{"convert"}
Truth table50%1/2
A
A= $wrap->{"convert"}
Aresult
11
00
209 4 $wrap->{convert}{$_} = $imp->{$_};
210 }
211 }
212  
213 45 ▶ $wrap = $default_wrap unless defined $wrap;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $wrap 38 7 45
214 #use DD; dd {ssym=>$ssym, wrap=>$wrap};
215 } # SET_WRAP_OPTS
216  
217 45 my $sub = \&{"$source\::$ssym"};
218 DO_WRAP: {
219 45 ▶ last unless $wrap;
Branch: 1 branch
BranchTrueFalseTotal
unless $wrap 33 12 45
220  
221 12 my $cache;
222 12 ▶ if (keys(%$wrap) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$wrap == 0) 6 6 12
223 # using default wrap options, we store the cached version of
224 # these
225 6 $cache = $pkg_cache{$source}{$ssym}{sub};
226 }
227 12 ▶ if ($cache) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cache) { } 1 11 12
228 1 $sub = $cache;
229 1 push @{ $recap->{wrapped} }, $ssym;
230 } else {
231 11 $sub = \&{"$source\::$ssym"};
232 11 my $meta = $metas->{$ssym};
233 11 ▶ if (!$meta) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $meta) { } 1 10 11
234 #warn "Exporting $ssym to $target\::$tsym unwrapped ".
235 # "because $ssym does not have metadata";
236 } else {
237 10 require Perinci::Sub::Wrapper;
238 10 my $res = Perinci::Sub::Wrapper::wrap_sub(
239 %$wrap,
240 sub_name => "$source\::$ssym",
241 meta => $meta,
242 );
243 10 ▶ die "Can't wrap $ssym for $target: ".
Branch: 1 branch
BranchTrueFalseTotal
unless $res->[0] == 200 0 10 10
244 "$res->[0] - $res->[1]" unless $res->[0] == 200;
245 10 $sub = $res->[2]{sub};
246 10 ▶ $pkg_cache{$source}{$ssym}{sub} = $sub
Branch: 1 branch
BranchTrueFalseTotal
if keys %$wrap == 0 4 6 10
247 if keys(%$wrap) == 0;
248 10 push @{ $recap->{wrapped} }, $ssym;
249 }
250 }
251 } # DO_WRAP
252  
253 # finally, do the actual exporting!
254 #say "Exporting $ssym -> $target\::$tsym"; #DEBUG#
255 45 *{"$target\::$tsym"} = $sub;
256  
257 45 $existing{$tsym}++;
258  
259 } # for @ssyms
260  
261 } # for @imps
262  
263 23 $recap;
264 }
265  
266 # borrowed from Package::MoreUtil. this is actually not a proper implementation,
267 # but since we want to avoid extra footprint by loading Package::Stash, we'll
268 # get by for now.
269 27 ▶sub _list_package_contents {
Subroutine _list_package_contents: called CC=12 SCAR=28.0
270 27 my $pkg = shift;
271  
272 27 ▶ return () unless !length($pkg) || _package_exists($pkg);
Branch: 1 branch
BranchTrueFalseTotal
unless not length $pkg or _package_exists($pkg) 0 27 27
Condition33%1/3
not length $pkg or _package_exists($pkg)
l!l&&r!l&&!r
0270
MC/DC0%0/2
not length $pkg or _package_exists($pkg)
not length $pkg _package_exists($pkg)
Truth table33%1/3
not(A) or B
A= not length $pkgB= _package_exists($pkg)
ABresult
1X1
011
000
273 27 my $symtbl = \%{$pkg . "::"};
274  
275 27 my %res;
276 27 ▶ while (my ($k, $v) = each %$symtbl) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$symtbl) 54 27 81
277 54 ▶ next if $k =~ /::$/; # subpackage
Branch: 1 branch
BranchTrueFalseTotal
if $k =~ /::$/ 0 54 54
278 54 my $n;
279 54 ▶ if ("$v" !~ /^\*/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ("$v" =~ /^\*/) 0 54 54
280 # constant
281 0 $res{$k} = $v;
282 0 next;
283 }
284 54 ▶ if (defined *$v{CODE}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined *$v{"CODE"}) 3 51 54
285 3 $res{$k} = *$v{CODE}; # subroutine
286 3 $n++;
287 }
288 54 ▶ if (defined *$v{HASH}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined *$v{"HASH"}) 0 54 54
289 0 $res{"\%$k"} = \%{*$v}; # hash
290 0 $n++;
291 }
292 54 ▶ if (defined *$v{ARRAY}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined *$v{"ARRAY"}) 24 30 54
293 24 $res{"\@$k"} = \@{*$v}; # array
294 24 $n++;
295 }
296 54 ▶ if (defined(*$v{SCALAR}) # XXX always defined?
Branch: 1 branch
BranchTrueFalseTotal
if (defined *$v{"SCALAR"} and defined ${*$v;}) 0 54 54
Condition33%1/3
defined *$v{"SCALAR"} and defined ${*$v;}
!ll&&!rl&&r
0540
MC/DC0%0/2
defined *$v{"SCALAR"} and defined ${*$v;}
defined *$v{"SCALAR"} defined ${*$v;}
Truth table33%1/3
A and B
A= defined *$v{"SCALAR"}B= defined ${*$v;}
ABresult
0X0
100
111
297 54 && defined(${*$v})) { # currently we filter undef values
298 0 $res{"\$$k"} = \${*$v}; # scalar
299 0 $n++;
300 }
301  
302 54 ▶ if (!$n) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($n) 27 27 54
303 27 $res{"\*$k"} = $v; # glob
304 }
305 }
306  
307 27 %res;
308 }
309  
310 # also borrowed from Package::MoreUtil
311 27 ▶sub _package_exists {
Subroutine _package_exists: called CC=2 SCAR=8.4
312 27 my $pkg = shift;
313  
314 # opt
315 #return unless $pkg =~ /\A\w+(::\w+)*\z/;
316  
317 27 ▶ if ($pkg =~ s!::(\w+)\z!!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pkg =~ s/::(\w+)\z//) { } 0 27 27
318 0 return !!${$pkg . "::"}{$1 . "::"};
319 } else {
320 27 return !!$::{$pkg . "::"};
321 }
322 }
323  
324 4 1;
325 # ABSTRACT: An exporter that groks Rinci metadata
326  
327 __END__
↑ Summary Examples/Export.pm »