lib/Attribute/Exporter.pm

filter: Statementstmt 87.5% 162 / 185 Branchbran 63.6% 42 / 66 Conditioncond 53.8% 7 / 13 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 90.6% 29 / 32 Podpod 0.0% 0 / 2 totaltotal 79.0% 242 / 306 CC 42 SCAR 18.6
CC
42
Cov
80%
import
12.2
_alias_symbol
10.7
_indirect_symbol
9.7
CRAP 6.4
↑ Summary
1 package Attribute::Exporter;
2  
3 1 ▶use 5.6.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 my %exports;
11 my %export_ok;
12 1 my $export_tags;
13  
14 1 ▶our $Verbose ||= 0;
Condition50%1/2
our $Verbose ||= 0
l!l
10
MC/DC0%0/1
our $Verbose ||= 0
our $Verbose
Truth table50%1/2
A
A= our $Verbose
Aresult
11
00
15 1 our $VERSION = '0.02';
16 1 ▶our $use_indirection ||= 0;
Condition50%1/2
our $use_indirection ||= 0
l!l
01
MC/DC0%0/1
our $use_indirection ||= 0
our $use_indirection
Truth table50%1/2
A
A= our $use_indirection
Aresult
11
00
17  
18 16 ▶sub export_constant {
Subroutine export_constant: called CC=5 SCAR=16.3
Pod undocumented
19 16 my $src_pkg = shift;
20 16 for my $arg (@_) {
21 16 ▶ if (ref($arg)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg) 8 8 16
22 8 ▶ if (!defined(&$arg)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined &$arg) 0 8 8
23 0 die("undefined subroutine ${src_pkg}::$arg requested for export");
24 }
25  
26 8 push(@{$exports{$src_pkg}}, $arg);
27 8 next;
28 }
29  
30 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
31 8 my $ref = *{"${src_pkg}::$arg"}{CODE};
32 1 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 8 ▶ if (!defined($ref)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ref) 0 8 8
34 0 die("unknown constant ${src_pkg}::$arg requested for export");
35 }
36  
37 8 push(@{$exports{$src_pkg}}, $ref);
38 }
39 }
40  
41 13 ▶sub _export_symbol {
Subroutine _export_symbol: called CC=2 SCAR=6.9
42 13 ▶ $use_indirection ? _indirect_symbol(@_) : _alias_symbol(@_);
Branch: 1 branch
BranchTrueFalseTotal
$Attribute::Exporter::use_indirection ? : 4 9 13
43 }
44  
45 4 ▶sub _indirect_symbol {
Subroutine _indirect_symbol: called CC=7 SCAR=22.7
46 4 my ($src_pkg, $dest_pkg, $name, $type) = @_;
47  
48 4 ▶ $Verbose && warn "${src_pkg}::$name => indirect(${dest_pkg}::$name, $type)\n";
Branch: 1 branch
BranchTrueFalseTotal
$Attribute::Exporter::Verbose and warn "${src_pkg}::$name => indirect(${dest_pkg}::$name, $type)\n" 4 0 4
49  
50 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
51 4 ▶ if ($type eq 'CODE') {
Branch: 5 branches
BranchTrueFalseTotal
if ($type eq 'CODE') { } 2 2 4
elsif ($type eq 'SCALAR') { } 2 0 2
elsif ($type eq 'ARRAY') { } 0 0 0
elsif ($type eq 'HASH') { } 0 0 0
elsif ($type eq 'REF') { } 0 0 0
52 2 ▶ *{"${dest_pkg}::$name"} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
53 3 local *sym = *{"${src_pkg}::${name}"};
54 3 &sym(@_);
55 2 };
56 } elsif ($type eq 'SCALAR') {
57 2 tie my $scalar, 'Attribute::Exporter::TieScalar', $src_pkg, $name;
58 2 *{"${dest_pkg}::$name"} = \$scalar;
59 } elsif ($type eq 'ARRAY') {
60 0 die "not yet implemented";
61 } elsif ($type eq 'HASH') {
62 0 die "not yet implemented";
63 } elsif ($type eq 'REF') {
64 0 die "not yet implemented";
65 }
66 1 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
67 }
68  
69 9 ▶sub _alias_symbol {
Subroutine _alias_symbol: called CC=7 SCAR=23.7
70 9 my ($src_pkg, $dest_pkg, $name, $type) = @_;
71  
72 9 ▶ $Verbose && warn "${src_pkg}::$name => ${dest_pkg}::$name $type\n";
Branch: 1 branch
BranchTrueFalseTotal
$Attribute::Exporter::Verbose and warn "${src_pkg}::$name => ${dest_pkg}::$name $type\n" 9 0 9
73  
74 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
75 9 ▶ if ($type eq 'ARRAY') {
Branch: 5 branches
BranchTrueFalseTotal
if ($type eq 'ARRAY') { } 0 9 9
elsif ($type eq 'HASH') { } 0 9 9
elsif ($type eq 'SCALAR') { } 1 8 9
elsif ($type eq 'REF') { } 0 8 8
elsif ($type eq 'CODE') { } 8 0 8
76 0 *{"${dest_pkg}::$name"} = \@{"${src_pkg}::$name"};
77 } elsif ($type eq 'HASH') {
78 0 *{"${dest_pkg}::$name"} = \%{"${src_pkg}::$name"};
79 } elsif ($type eq 'SCALAR') {
80 1 *{"${dest_pkg}::$name"} = \${"${src_pkg}::$name"};
81 } elsif ($type eq 'REF') {
82 0 *{"${dest_pkg}::$name"} = \${"${src_pkg}::$name"};
83 } elsif ($type eq 'CODE') {
84 8 *{"${dest_pkg}::$name"} = \&{"${src_pkg}::$name"};
85 }
86 1 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
87 }
88  
89 37 ▶sub find_symbol_name {
Subroutine find_symbol_name: called CC=6 SCAR=18.1
Pod undocumented
90 37 my ($src_pkg, $ref) = @_;
91  
92 37 my $reftype = ref($ref);
93  
94 37 ▶ if ($reftype eq 'REF') {
Branch: 1 branch
BranchTrueFalseTotal
if ($reftype eq "REF") 0 37 37
95 0 $reftype = 'SCALAR';
96 }
97  
98 37 my $ret = undef;
99  
100 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
101 37 for my $name (keys(%{"${src_pkg}::"})) {
102 700 ▶ if ($name =~ m/::$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /::$/) 0 700 700
103 0 next;
104 }
105 700 local *sym = *{"${src_pkg}::$name"};
106  
107 700 ▶ if (*sym{$reftype} && *sym{$reftype} eq $ref) {
Branch: 1 branch
BranchTrueFalseTotal
if (*sym{$reftype} and *sym{$reftype} eq $ref) 8 692 700
Condition100%3/3
*sym{$reftype} and *sym{$reftype} eq $ref
!ll&&!rl&&r
4252678
MC/DC100%2/2
*sym{$reftype} and *sym{$reftype} eq $ref
*sym{$reftype} *sym{$reftype} eq $ref
Truth table100%3/3
A and B
A= *sym{$reftype}B= *sym{$reftype} eq $ref
ABresult
0X0
100
111
108 8 $ret = $name;
109 8 last;
110 }
111 }
112 1 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
113  
114 37 return $ret;
115 }
116  
117 8 ▶sub import {
Subroutine import: called CC=12 SCAR=25.0
118 8 my $src_pkg = shift;
119  
120 8 my ($dst_pkg) = caller(0);
121  
122 # Export all the default exports to the caller
123 8 ▶ if (!@_) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@_) 2 6 8
124 2 ▶ $Verbose && carp "importing all default symbols to $src_pkg";
Branch: 1 branch
BranchTrueFalseTotal
$Attribute::Exporter::Verbose and carp("importing all default symbols to $src_pkg") 2 0 2
125  
126 2 for my $ref (@{$exports{$src_pkg}}) {
127 27 my $name = find_symbol_name($src_pkg, $ref);
128 27 ▶ if (!$name) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($name) 21 6 27
129 21 ▶ $Verbose && warn "unable to find symbol name for ${src_pkg}::$ref";
Branch: 1 branch
BranchTrueFalseTotal
$Attribute::Exporter::Verbose and warn "unable to find symbol name for ${src_pkg}::$ref" 21 0 21
130 21 next;
131 }
132 6 _export_symbol($src_pkg, $dst_pkg, $name, ref($ref));
133 }
134  
135 2 return;
136 }
137  
138 $Verbose &&
139 6 ▶ carp "importing ${src_pkg}::" . join(',', @_) . " symbols into $dst_pkg";
Branch: 1 branch
BranchTrueFalseTotal
$Attribute::Exporter::Verbose and carp("importing ${src_pkg}::" . join(",", @_) . " symbols into $dst_pkg") 6 0 6
140  
141 # Export named exports to the caller
142 6 for my $import (@_) {
143 # Process tags
144 7 ▶ if ($import =~ m/^:(.+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($import =~ /^:(.+)$/) 2 5 7
145 map {
146 10 my $ref = $_;
147 10 my $name = find_symbol_name($src_pkg, $ref);
148  
149 10 ▶ if (!$name) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $name) { } 8 2 10
150 8 ▶ $Verbose && carp "unable to find symbol name for ${src_pkg}::$ref";
Condition33%1/3
$Attribute::Exporter::Verbose && carp("unable to find symbol name for ${src_pkg}::$ref")
!ll&&!rl&&r
008
MC/DC0%0/2
$Attribute::Exporter::Verbose && carp("unable to find symbol name for ${src_pkg}::$ref")
$Attribute::Exporter::Verbose carp("unable to find symbol name for ${src_pkg}::$ref")
Truth table33%1/3
A && B
A= $Attribute::Exporter::VerboseB= carp("unable to find symbol name for ${src_pkg}::$ref")
ABresult
0X0
100
111
151 } else {
152 2 _export_symbol($src_pkg, $dst_pkg, $name, ref($ref));
153 }
154 2 } @{$export_tags->{$src_pkg}->{$1}};
155  
156 2 next;
157 }
158  
159 # Process individual items requested for import into the caller
160 5 my %types = (
161 '$' => 'SCALAR',
162 '@' => 'ARRAY',
163 '%' => 'HASH',
164 '&' => 'CODE',
165 );
166  
167 5 my ($sigil, $name) = $import =~ m/^(\$|\@|\%|\&)?(.+)$/;
168  
169 5 ▶ my $type = $sigil ? $types{$sigil} : 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
$sigil ? : 3 2 5
170  
171 5 _export_symbol($src_pkg, $dst_pkg, $name, $type);
172 }
173 }
174  
175 0 ▶sub MODIFY_HASH_ATTRIBUTES {
Subroutine MODIFY_HASH_ATTRIBUTES: not called CC=1 SCAR=6.9
176 0 goto &_MODIFY_ATTRIBUTES;
177 }
178  
179 9 ▶sub MODIFY_SCALAR_ATTRIBUTES {
Subroutine MODIFY_SCALAR_ATTRIBUTES: called CC=1 SCAR=0.0
180 9 goto &_MODIFY_ATTRIBUTES;
181 }
182  
183 0 ▶sub MODIFY_ARRAY_ATTRIBUTES {
Subroutine MODIFY_ARRAY_ATTRIBUTES: not called CC=1 SCAR=6.9
184 0 goto &_MODIFY_ATTRIBUTES;
185 }
186  
187 32 ▶sub MODIFY_CODE_ATTRIBUTES {
Subroutine MODIFY_CODE_ATTRIBUTES: called CC=1 SCAR=0.0
188 32 goto &_MODIFY_ATTRIBUTES;
189 }
190  
191 7 ▶sub MODIFY_REF_ATTRIBUTES {
Subroutine MODIFY_REF_ATTRIBUTES: called CC=1 SCAR=0.0
192 7 goto &_MODIFY_ATTRIBUTES;
193 }
194  
195 48 ▶sub _MODIFY_ATTRIBUTES {
Subroutine _MODIFY_ATTRIBUTES: called CC=9 SCAR=22.4
196 48 my ($src_pkg, $ref, @attrs) = @_;
197  
198 48 my $reftype = ref($ref);
199  
200 48 ▶ $Verbose == 2 && warn "args " . join(',', @_) . "\n";
Branch: 1 branch
BranchTrueFalseTotal
$Attribute::Exporter::Verbose == 2 and warn 'args ' . join(",", @_) . "\n" 48 0 48
201 48 ▶ $Verbose == 2 &&
Branch: 1 branch
BranchTrueFalseTotal
$Attribute::Exporter::Verbose == 2 and warn "processing $reftype ($ref) with attrs " . join(",", @attrs) . "\n" 48 0 48
202 warn "processing $reftype ($ref) with attrs " . join(',', @attrs) . "\n";
203  
204 48 my %attrs = map { $_ => 1 } @attrs;
205  
206 # strip the handled attributes
207 48 my $export_def_attr = delete($attrs{export_def});
208 48 my $export_ok_attr = delete($attrs{export_ok});
209 48 my @export_tag_attrs;
210 48 for my $attr (keys(%attrs)) {
211 8 ▶ if (my ($tag) = $attr =~ m/^export_tag\(([^)]+)\)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($tag) = $attr =~ /^export_tag\(([^)]+)\)$/) 8 0 8
212 8 ▶ if ($tag eq 'DEFAULT' || $tag eq 'OK') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag eq "DEFAULT" or $tag eq "OK") 0 8 8
Condition33%1/3
$tag eq "DEFAULT" or $tag eq "OK"
l!l&&r!l&&!r
008
MC/DC0%0/2
$tag eq "DEFAULT" or $tag eq "OK"
$tag eq "DEFAULT" $tag eq "OK"
Truth table33%1/3
A or B
A= $tag eq "DEFAULT"B= $tag eq "OK"
ABresult
1X1
011
000
213 0 carp "using $tag as an export tag clobbers internal $tag";
214 }
215 8 push(@export_tag_attrs, split(m/,/, $tag));
216 8 delete($attrs{$attr})
217 }
218 }
219  
220 # save the unhandled attributes to pass back
221 48 my @unhandled = keys(%attrs);
222  
223 48 ▶ if ($export_def_attr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($export_def_attr) 8 40 48
224 8 push(@{$exports{$src_pkg}}, $ref);
225 8 push(@export_tag_attrs, 'DEFAULT');
226 }
227  
228 48 ▶ if ($export_ok_attr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($export_ok_attr) 32 16 48
229 32 push(@{$export_ok{$src_pkg}}, $ref);
230 32 push(@export_tag_attrs, 'OK');
231 }
232  
233 map {
234 48 push(@{$export_tags->{$src_pkg}->{$_}}, $ref);
235 } @export_tag_attrs;
236  
237 48 return @unhandled;
238 }
239  
240 package Attribute::Exporter::TieScalar;
241  
242 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
243 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
244  
245 2 ▶sub TIESCALAR {
Subroutine TIESCALAR: called CC=1 SCAR=0.0
246 2 my ($class, $src_pkg, $symbol) = @_;
247 2 return bless({ src_pkg => $src_pkg, symbol => $symbol });
248 }
249  
250 4 ▶sub FETCH {
Subroutine FETCH: called CC=1 SCAR=0.0
251 4 my ($self) = @_;
252 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
253 4 my $ret = ${$self->{src_pkg} . '::' . $self->{symbol}};
254 1 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
255 4 return $ret;
256 }
257  
258 0 ▶sub STORE {
Subroutine STORE: not called CC=1 SCAR=6.9
259 0 my ($self, $store) = @_;
260 0 ${$self->{src_pkg} . '::' . $self->{symbol}} = $store;
261 0 return $store;
262 }
263  
264 1 1;
265  
266 __END__
↑ Summary