blib/lib/Export/Attrs.pm

filter: Statementstmt 75.4% 117 / 155 Branchbran 30.4% 14 / 46 Conditioncond 31.8% 7 / 22 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 94.1% 16 / 17 Podpod n/a totaltotal 60.8% 154 / 253 CC 41 SCAR 35.9
CC
41
Cov
62%
_generic_import
79.4
_get_lexical_name
24.4
_generic_handler
12.3
CRAP 36.1
↑ Summary
1 package Export::Attrs;
2  
3 2 our $VERSION = 'v0.1.0';
4  
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Attribute::Handlers;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use PadWalker qw( var_name peek_my );
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 my %IMPORT_for;
12  
13 2 ▶sub import {
Subroutine import: called CC=2 SCAR=6.9
14 2 my $caller = caller;
15 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 *{$caller.'::import'} = \&_generic_import;
17 0 ▶ *{$caller.'::IMPORT'} = sub (&) { $IMPORT_for{$caller} = shift };
Subroutine __ANON__: not called CC=1 SCAR=0.4
18 2 for my $var_type (qw( SCALAR ARRAY HASH CODE )) {
19 8 *{$caller.'::MODIFY_'.$var_type.'_ATTRIBUTES'} = \&_generic_handler;
20 }
21 2 return;
22 }
23  
24 my %tagsets_for;
25 2 my %is_exported_from;
26 2 my %named_tagsets_for;
27 2 my %decl_loc_for;
28 2 my %name_of;
29  
30 2 my $IDENT = '[^\W\d]\w*';
31  
32 1 ▶sub _generic_handler {
Subroutine _generic_handler: called CC=9 SCAR=25.1
33 1 my ($package, $referent, @attrs) = @_;
34  
35 ATTR:
36 1 for my $attr (@attrs) {
37  
38 1 ▶ ($attr||=q{}) =~ s/\A Export (?: \( (.*) \) )? \z/$1||q{}/exms
Branch: 1 branch
BranchTrueFalseTotal
($attr ||= "") =~ s/\A Export (?: \( (.*) \) )? \z/$1 or "";/emsx or next ATTR 1 0 1
Condition50%2/4
$attr ||= ""
l!l
10
$1 || ''
l!l
01
MC/DC0%0/1
$attr ||= ""
$attr
MC/DC0%0/1
$1 || ''
$1
Truth table50%1/2
A
A= $attr
Aresult
11
00
Truth table50%1/2
A
A= $1
Aresult
11
00
39 or next ATTR;
40  
41 0 my @tagsets = grep {length $_} split m/ \s+,?\s* | ,\s* /xms, $attr;
42  
43 1 my (undef, $file, $line) = caller(1);
44 1 $file =~ s{.*/}{}xms;
45  
46 0 ▶ if (my @bad_tags = grep {!m/\A :$IDENT \z/xms} @tagsets) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@bad_tags) = grep({not /\A :$IDENT \z/msx;} @tagsets)) 0 1 1
47 0 ▶ die 'Bad tagset',
Branch: 1 branch
BranchTrueFalseTotal
@bad_tags == 1 ? : 0 0 0
48 (@bad_tags==1?' ':'s '),
49 "in :Export attribute at '$file' line $line: [@bad_tags]\n";
50 }
51  
52 1 ▶ my $tagsets = $tagsets_for{$package} ||= {};
Condition50%1/2
$tagsets_for{$package} ||= {}
l!l
01
MC/DC0%0/1
$tagsets_for{$package} ||= {}
$tagsets_for{$package}
Truth table50%1/2
A
A= $tagsets_for{$package}
Aresult
11
00
53  
54 1 for my $tagset (@tagsets) {
55 0 push @{ $tagsets->{$tagset} }, $referent;
56 }
57 1 push @{ $tagsets->{':ALL'} }, $referent;
58  
59 1 $is_exported_from{$package}{$referent} = 1;
60 1 $decl_loc_for{$referent} = "$file line $line";
61 1 $name_of{$referent} = _get_lexical_name($referent);
62  
63 1 undef $attr;
64  
65 }
66  
67 1 return grep {defined $_} @attrs;
68 }
69  
70 2 my %desc_for = (
71 SCALAR => 'lexical scalar variable',
72 ARRAY => 'lexical array variable',
73 HASH => 'lexical hash variable',
74 CODE => 'anonymous subroutine',
75 );
76  
77 2 my %hint_for = (
78 SCALAR => "(declare the variable with 'our' instead of 'my')",
79 ARRAY => "(declare the variable with 'our' instead of 'my')",
80 HASH => "(declare the variable with 'our' instead of 'my')",
81 CODE => "(specify a name after the 'sub' keyword)",
82 );
83  
84 1 ▶sub _get_lexical_name {
Subroutine _get_lexical_name: called CC=6 SCAR=32.0
85 1 my ($var_ref) = @_;
86 1 ▶ return if ref $var_ref eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $var_ref eq "CODE" 1 0 1
87  
88 SEARCH:
89 0 for my $up_level (1..(~0>>1)-1) {
90 0 ▶ my $sym_tab_ref = eval { peek_my($up_level) }
Branch: 1 branch
BranchTrueFalseTotal
my $sym_tab_ref = eval { do { peek_my($up_level) } } or last SEARCH 0 0 0
91 or last SEARCH;
92  
93 0 for my $var_name (keys %{$sym_tab_ref}) {
94 0 ▶ return $var_name if $var_ref == $sym_tab_ref->{$var_name};
Branch: 1 branch
BranchTrueFalseTotal
if $var_ref == $sym_tab_ref->{$var_name} 0 0 0
95 }
96 }
97 0 return;
98 }
99  
100 1 ▶sub _invert_tagset {
Subroutine _invert_tagset: called CC=6 SCAR=18.4
101 1 my ($package, $tagset) = @_;
102 1 my %inverted_tagset;
103  
104 1 for my $tag (keys %{$tagset}) {
105 1 for my $sub_ref (@{$tagset->{$tag}}) {
106 1 my $type = ref $sub_ref;
107 my $sym = Attribute::Handlers::findsym($package, $sub_ref, $type)
108 1 ▶ || $name_of{$sub_ref}
Branch: 1 branch
BranchTrueFalseTotal
my $sym = Attribute::Handlers::findsym($package, $sub_ref, $type) || $name_of{$sub_ref} or die "Can't export $desc_for{$type} ", "at $decl_loc_for{$sub_ref}\n$hint_for{$type}\n" 1 0 1
Condition33%1/3
Attribute::Handlers::findsym($package, $sub_ref, $type) || $name_of{$sub_ref}
l!l&&r!l&&!r
100
MC/DC0%0/2
Attribute::Handlers::findsym($package, $sub_ref, $type) || $name_of{$sub_ref}
Attribute::Handlers::findsym($package, $sub_ref, $type) $name_of{$sub_ref}
Truth table33%1/3
A || B
A= Attribute::Handlers::findsym($package, $sub_ref, $type)B= $name_of{$sub_ref}
ABresult
1X1
011
000
109 or die "Can't export $desc_for{$type} ",
110 "at $decl_loc_for{$sub_ref}\n$hint_for{$type}\n";
111 1 ▶ if (ref $sym) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $sym) 1 0 1
112 1 $sym = *{$sym}{NAME};
113 }
114 1 $inverted_tagset{$tag}{$sym} = $sub_ref;
115 }
116 }
117  
118 1 return \%inverted_tagset;
119 }
120  
121 2 my %type_for = qw( $ SCALAR @ ARRAY % HASH );
122  
123 # Reusable import() subroutine for all packages...
124 1 ▶sub _generic_import {
Subroutine _generic_import: called CC=22 SCAR=43.7
125 1 my $package = shift;
126  
127 my $tagset
128 = $named_tagsets_for{$package}
129 1 ▶ ||= _invert_tagset($package, $tagsets_for{$package});
Condition33%1/3
$named_tagsets_for{$package} ||= _invert_tagset($package, $tagsets_for{$package})
l!l&&r!l&&!r
010
MC/DC0%0/2
$named_tagsets_for{$package} ||= _invert_tagset($package, $tagsets_for{$package})
$named_tagsets_for{$package} _invert_tagset($package, $tagsets_for{$package})
Truth table33%1/3
A ||= B
A= $named_tagsets_for{$package}B= _invert_tagset($package, $tagsets_for{$package})
ABresult
1X1
011
000
130  
131 1 my $is_exported = $is_exported_from{$package};
132  
133 1 my $errors;
134  
135 my %request;
136 1 my $subs_ref;
137  
138 1 my $args_supplied = @_;
139  
140 1 my $argno = 0;
141 REQUEST:
142 1 ▶ while ($argno < @_) {
Branch: 1 branch
BranchTrueFalseTotal
if ($argno < @_) 1 1 2
143 1 my $request = $_[$argno];
144 1 ▶ if (my ($sub_name) = $request =~ m/\A &? ($IDENT) (?:\(\))? \z/xms) {
Branch: 3 branches
BranchTrueFalseTotal
if (my($sub_name) = $request =~ /\A &? ($IDENT) (?:\(\))? \z/msx) { } 1 0 1
elsif (() = $request =~ /\A ([\$\@%])($IDENT) \z/msx) { } 0 0 0
elsif ($request =~ /\A :$IDENT \z/msx and $subs_ref = $tagset->{$request}) { } 0 0 0
145 1 ▶ if (exists $request{$sub_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $request{$sub_name}) 0 1 1
146 0 splice @_, $argno, 1;
147 0 next REQUEST;
148 }
149 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
150 2 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
151 1 ▶ if (my $sub_ref = *{$package.'::'.$sub_name}{CODE}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sub_ref = *{$package . '::' . $sub_name;}{"CODE"}) 1 0 1
152 1 ▶ if ($is_exported->{$sub_ref}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_exported->{$sub_ref}) 1 0 1
153 1 $request{$sub_name} = $sub_ref;
154 1 splice @_, $argno, 1;
155 1 next REQUEST;
156 }
157 }
158 }
159 elsif (my ($sigil, $name) = $request =~ m/\A ([\$\@%])($IDENT) \z/xms) {
160 0 ▶ next REQUEST if exists $request{$sigil.$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $request{$sigil . $name} 0 0 0
161 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
162 2 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
163 0 ▶ if (my $var_ref = *{$package.'::'.$name}{$type_for{$sigil}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $var_ref = *{$package . '::' . $name;}{$type_for{$sigil}}) 0 0 0
164 0 ▶ if ($is_exported->{$var_ref}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_exported->{$var_ref}) 0 0 0
165 0 $request{$sigil.$name} = $var_ref;
166 0 splice @_, $argno, 1;
167 0 next REQUEST;
168 }
169 }
170 }
171 ▶ elsif ($request =~ m/\A :$IDENT \z/xms
Condition0%0/3
$request =~ /\A :$IDENT \z/msx and $subs_ref = $tagset->{$request}
!ll&&!rl&&r
000
MC/DC0%0/2
$request =~ /\A :$IDENT \z/msx and $subs_ref = $tagset->{$request}
$request =~ /\A :$IDENT \z/msx $subs_ref = $tagset->{$request}
Truth table0%0/3
A and B
A= $request =~ /\A :$IDENT \z/msxB= $subs_ref = $tagset->{$request}
ABresult
0X0
100
111
172 and $subs_ref = $tagset->{$request}) {
173 0 @request{keys %{$subs_ref}} = values %{$subs_ref};
174 0 splice @_, $argno, 1;
175 0 next REQUEST;
176 }
177 0 $errors .= " $request";
178 0 $argno++;
179 }
180  
181 # Report unexportable requests...
182 1 my $real_import = $IMPORT_for{$package};
183  
184 1 ▶ croak "$package does not export:$errors\nuse $package failed"
Branch: 1 branch
BranchTrueFalseTotal
if $errors and not $real_import 0 1 1
Condition33%1/3
$errors and not $real_import
!ll&&!rl&&r
100
MC/DC0%0/2
$errors and not $real_import
$errors not $real_import
Truth table33%1/3
A and not(B)
A= $errorsB= not $real_import
ABresult
0X0
100
111
185 if $errors && !$real_import;
186  
187 1 ▶ if (!$args_supplied) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args_supplied) 0 1 1
188 0 ▶ %request = %{$tagset->{':DEFAULT'}||={}}
Condition0%0/2
$tagset->{":DEFAULT"} ||= {}
l!l
00
MC/DC0%0/1
$tagset->{":DEFAULT"} ||= {}
$tagset->{":DEFAULT"}
Truth table0%0/2
A
A= $tagset->{":DEFAULT"}
Aresult
11
00
189 }
190  
191 1 ▶ my $mandatory = $tagset->{':MANDATORY'} ||= {};
Condition50%1/2
$tagset->{":MANDATORY"} ||= {}
l!l
01
MC/DC0%0/1
$tagset->{":MANDATORY"} ||= {}
$tagset->{":MANDATORY"}
Truth table50%1/2
A
A= $tagset->{":MANDATORY"}
Aresult
11
00
192 1 @request{ keys %{$mandatory} } = values %{$mandatory};
193  
194 1 my $caller = caller;
195  
196 1 for my $sub_name (keys %request) {
197 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
198 1 my ($sym_name) = $sub_name =~ m{\A [\$\@&%]? (.*)}xms;
199 1 *{$caller.'::'.$sym_name} = $request{$sub_name};
200 }
201  
202 1 ▶ if ($real_import) {
Branch: 1 branch
BranchTrueFalseTotal
if ($real_import) 0 1 1
203 0 my $idx=0;
204 0 ▶ while ($idx < @_) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idx < @_) 0 0 0
205 0 ▶ if (defined $_[$idx]) { $idx++ }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[$idx]) { } 0 0 0
206 0 else { splice @_, $idx, 1 }
207 }
208 0 goto &{$real_import};
209 }
210 1 return;
211 }
212  
213 2 1; # Magic true value required at end of module
214 __END__
↑ Summary