blib/lib/Moo/_Utils.pm

filter: Statementstmt 93.7% 149 / 159 Branchbran 77.9% 53 / 68 Conditioncond 65.1% 28 / 43 MC/DCmcdc 38.1% 8 / 21 Subroutinesub 90.9% 30 / 33 Podpod n/a totaltotal 82.7% 268 / 324 CC 53 SCAR 19.6
CC
53
Cov
85%
BEGIN
16.7
BEGIN
13.1
_load_module
11.3
CRAP 7.1
« blib/lib/Moo/Role.pm ↑ Summary blib/lib/Method/Generate/Constructor.pm »
1 package Moo::_Utils;
2 114 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 114 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 {
6 114 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 114 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4253 ▶ sub _getglob { \*{$_[0]} }
Subroutine _getglob: called CC=1 SCAR=0.0
9 898 ▶ sub _getstash { \%{"$_[0]::"} }
Subroutine _getstash: called CC=1 SCAR=0.0
10 }
11  
12 114 ▶BEGIN {
Subroutine BEGIN: called CC=14 SCAR=28.1
13 114 my ($su, $sn);
14 $su = $INC{'Sub/Util.pm'} && defined &Sub::Util::set_subname
15 or $sn = $INC{'Sub/Name.pm'}
16 102 or $su = eval { require Sub::Util; } && defined &Sub::Util::set_subname
17 114 ▶ or $sn = eval { require Sub::Name; };
Branch: 1 branch
BranchTrueFalseTotal
$su = $INC{"Sub/Util.pm"} && defined &Sub::Util::set_subname or $sn = $INC{"Sub/Name.pm"} or $su = eval { do { require Sub::Util } } && defined &Sub::Util::set_subname or $sn = eval { do { require Sub::Name } } 112 2 114
Condition75%9/12
$su = $INC{"Sub/Util.pm"} && defined &Sub::Util::set_subname or $sn = $INC{"Sub/Name.pm"} or $su = eval { do { require Sub::Util } } && defined &Sub::Util::set_subname
l!l&&r!l&&!r
121002
$su = $INC{"Sub/Util.pm"} && defined &Sub::Util::set_subname or $sn = $INC{"Sub/Name.pm"}
l!l&&r!l&&!r
120102
$INC{'Sub/Util.pm'} && defined &Sub::Util::set_subname
!ll&&!rl&&r
102012
eval { do { require Sub::Util } } && defined &Sub::Util::set_subname
!ll&&!rl&&r
20100
MC/DC40%2/5
$su = $INC{"Sub/Util.pm"} && defined &Sub::Util::set_subname or $sn = $INC{"Sub/Name.pm"} or $su = eval { do { require Sub::Util } } && defined &Sub::Util::set_subname
$INC{'Sub/Util.pm'} defined &Sub::Util::set_subname $sn = $INC{"Sub/Name.pm"} eval { do { require Sub::Util } } defined &Sub::Util::set_subname
Truth table33%3/9
A && B or C or D && E
A= $INC{'Sub/Util.pm'}B= defined &Sub::Util::set_subnameC= $sn = $INC{"Sub/Name.pm"}D= eval { do { require Sub::Util } }E= defined &Sub::Util::set_subname
ABCDEresult
11XXX1
0X1XX1
101XX1
0X0111
100111
0X00X0
0X0100
1000X0
100100
18  
19 *_subname = $su ? \&Sub::Util::set_subname
20 : $sn ? \&Sub::Name::subname
21 114 ▶ : sub { $_[1] };
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 2 branches
BranchTrueFalseTotal
$su ? : 112 2 114
$sn ? : 1 1 2
22 114 ▶ *_CAN_SUBNAME = ($su || $sn) ? sub(){1} : sub(){0};
Branch: 1 branch
BranchTrueFalseTotal
$su || $sn ? : 113 1 114
Condition100%3/3
$su or $sn
l!l&&r!l&&!r
11211
MC/DC100%2/2
$su or $sn
$su $sn
Truth table100%3/3
A or B
A= $suB= $sn
ABresult
1X1
011
000
23  
24 114 ▶ *_WORK_AROUND_BROKEN_MODULE_STATE = "$]" < 5.009 ? sub(){1} : sub(){0};
Branch: 1 branch
BranchTrueFalseTotal
"$]" < 5.009 ? : 0 114 114
25 *_WORK_AROUND_HINT_LEAKAGE
26 = "$]" < 5.011 && !("$]" >= 5.009004 && "$]" < 5.010001)
27 114 ▶ ? sub(){1} : sub(){0};
Branch: 1 branch
BranchTrueFalseTotal
"$]" < 5.011 && !("$]" >= 5.009004 && "$]" < 5.010001) ? : 0 114 114
Condition16%1/6
"$]" < "5.011" and not "$]" >= "5.009004" && "$]" < "5.010001"
!ll&&!rl&&r
11400
"$]" >= 5.009004 && "$]" < 5.010001
!ll&&!rl&&r
000
MC/DC0%0/3
"$]" < "5.011" and not "$]" >= "5.009004" && "$]" < "5.010001"
"$]" < "5.011" "$]" >= 5.009004 "$]" < 5.010001
Truth table25%1/4
A and not(B && C)
A= "$]" < "5.011"B= "$]" >= 5.009004C= "$]" < 5.010001
ABCresult
0XX0
1110
10X1
1101
28  
29 114 my $module_name_rx = qr/\A(?!\d)\w+(?:::\w+)*\z/;
30 0 ▶ *_module_name_rx = sub(){$module_name_rx};
Subroutine __ANON__: not called CC=1 SCAR=1.2
31 }
32  
33 114 ▶use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
34 114 ▶BEGIN { *import = \&Exporter::import }
Subroutine BEGIN: called CC=1 SCAR=0.0
35 114 ▶use Config ();
Subroutine BEGIN: called CC=1 SCAR=0.0
36 114 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
37 114 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 # this should be empty, but some CPAN modules expect these
40 114 our @EXPORT = qw(
41 _install_coderef
42 _load_module
43 );
44  
45 114 our @EXPORT_OK = qw(
46 _check_tracked
47 _getglob
48 _getstash
49 _install_coderef
50 _install_modifier
51 _install_tracked
52 _load_module
53 _maybe_load_module
54 _module_name_rx
55 _name_coderef
56 _set_loaded
57 _unimport_coderefs
58 _linear_isa
59 _in_global_destruction
60 _in_global_destruction_code
61 );
62  
63 114 my %EXPORTS;
64  
65 34 ▶sub _install_modifier {
Subroutine _install_modifier: called CC=7 SCAR=19.5
66 34 my $target = $_[0];
67 34 my $type = $_[1];
68 34 my $code = $_[-1];
69 34 my @names = @_[2 .. $#_ - 1];
70  
71 34 ▶ @names = @{ $names[0] }
Branch: 1 branch
BranchTrueFalseTotal
if ref $names[0] eq "ARRAY" 1 33 34
72 if ref($names[0]) eq 'ARRAY';
73  
74 34 my @tracked = _check_tracked($target, \@names);
75  
76 34 ▶ if ($INC{'Sub/Defer.pm'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($INC{"Sub/Defer.pm"}) 30 4 34
77 30 for my $name (@names) {
78 # CMM will throw for us if it doesn't exist
79 31 ▶ if (my $to_modify = $target->can($name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $to_modify = $target->can($name)) 30 1 31
80 30 Sub::Defer::undefer_sub($to_modify);
81 }
82 }
83 }
84  
85 34 require Class::Method::Modifiers;
86 34 Class::Method::Modifiers::install_modifier(@_);
87  
88 33 ▶ if (@tracked) {
Branch: 1 branch
BranchTrueFalseTotal
if (@tracked) 3 30 33
89 3 my $exports = $EXPORTS{$target};
90 weaken($exports->{$_} = $target->can($_))
91 3 for @tracked;
92 }
93  
94 33 return;
95 }
96  
97 2886 ▶sub _install_tracked {
Subroutine _install_tracked: called CC=1 SCAR=0.0
98 2886 my ($target, $name, $code) = @_;
99 2886 my $from = caller;
100 2886 weaken($EXPORTS{$target}{$name} = $code);
101 2886 _install_coderef("${target}::${name}", "${from}::${name}", $code);
102 }
103  
104 0 ▶sub Moo::_Util::__GUARD__::DESTROY {
Subroutine DESTROY: not called CC=2 SCAR=17.9
105 0 ▶ delete $INC{$_[0]->[0]} if @{$_[0]};
Branch: 1 branch
BranchTrueFalseTotal
if @{$_[0];} 0 0 0
106 }
107  
108 134 ▶sub _require {
Subroutine _require: called CC=3 SCAR=11.0
109 134 my ($file) = @_;
110 134 my $guard = _WORK_AROUND_BROKEN_MODULE_STATE
111 && bless([ $file ], 'Moo::_Util::__GUARD__');
112 134 local %^H if _WORK_AROUND_HINT_LEAKAGE;
113 134 ▶ if (!eval { require $file; 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { require $file; 1 } }) 34 100 134
114 34 ▶ my $e = $@ || "Can't locate $file";
Condition50%1/2
$@ || "Can't locate $file"
l!l
340
MC/DC0%0/1
$@ || "Can't locate $file"
$@
Truth table50%1/2
A
A= $@
Aresult
11
00
115 34 my $me = __FILE__;
116 34 $e =~ s{ at \Q$me\E line \d+\.\n\z}{};
117 34 return $e;
118 }
119 100 pop @$guard if _WORK_AROUND_BROKEN_MODULE_STATE;
120 100 return undef;
121 }
122  
123 303 ▶sub _load_module {
Subroutine _load_module: called CC=11 SCAR=24.2
124 303 my ($module) = @_;
125 303 ▶ croak qq{"$module" is not a module name!}
Branch: 1 branch
BranchTrueFalseTotal
unless $module =~ /\A(?!\d)\w+(?:::\w+)*\z/ 1 302 303
126 unless $module =~ _module_name_rx;
127 302 (my $file = "$module.pm") =~ s{::}{/}g;
128 return 1
129 302 ▶ if $INC{$file};
Branch: 1 branch
BranchTrueFalseTotal
if $INC{$file} 264 38 302
130  
131 38 my $e = _require $file;
132 38 ▶ return 1
Branch: 1 branch
BranchTrueFalseTotal
unless defined $e 6 32 38
133 if !defined $e;
134  
135 32 ▶ croak $e
Branch: 1 branch
BranchTrueFalseTotal
unless $e =~ /\ACan't locate \Q$file\E / 2 30 32
136 if $e !~ /\ACan't locate \Q$file\E /;
137  
138 # can't just ->can('can') because a sub-package Foo::Bar::Baz
139 # creates a 'Baz::' key in Foo::Bar's symbol table
140 30 ▶ my $stash = _getstash($module)||{};
Condition50%1/2
_getstash($module) || {}
l!l
300
MC/DC0%0/1
_getstash($module) || {}
_getstash($module)
Truth table50%1/2
A
A= _getstash($module)
Aresult
11
00
141 114 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
142 30 ▶ return 1 if grep +exists &{"${module}::$_"}, grep !/::\z/, keys %$stash;
Branch: 1 branch
BranchTrueFalseTotal
if grep exists(&{"${module}::$_";}), grep((!/::\z/), keys %$stash) 24 6 30
143 return 1
144 6 ▶ if $INC{"Moose.pm"} && Class::MOP::class_of($module)
Branch: 1 branch
BranchTrueFalseTotal
if $INC{"Moose.pm"} and Class::MOP::class_of($module) or "Mouse::Util"->can("find_meta") and Mouse::Util::find_meta($module) 1 5 6
Condition55%5/9
$INC{"Moose.pm"} and Class::MOP::class_of($module) or "Mouse::Util"->can("find_meta") and Mouse::Util::find_meta($module)
l!l&&r!l&&!r
105
$INC{"Moose.pm"} and Class::MOP::class_of($module)
!ll&&!rl&&r
501
"Mouse::Util"->can("find_meta") and Mouse::Util::find_meta($module)
!ll&&!rl&&r
500
MC/DC25%1/4
$INC{"Moose.pm"} and Class::MOP::class_of($module) or "Mouse::Util"->can("find_meta") and Mouse::Util::find_meta($module)
$INC{"Moose.pm"} Class::MOP::class_of($module) "Mouse::Util"->can("find_meta") Mouse::Util::find_meta($module)
Truth table28%2/7
A and B or C and D
A= $INC{"Moose.pm"}B= Class::MOP::class_of($module)C= "Mouse::Util"->can("find_meta")D= Mouse::Util::find_meta($module)
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
145 or Mouse::Util->can('find_meta') && Mouse::Util::find_meta($module);
146  
147 5 croak $e;
148 }
149  
150 114 our %MAYBE_LOADED;
151 97 ▶sub _maybe_load_module {
Subroutine _maybe_load_module: called CC=4 SCAR=13.9
152 97 my $module = $_[0];
153 return $MAYBE_LOADED{$module}
154 97 ▶ if exists $MAYBE_LOADED{$module};
Branch: 1 branch
BranchTrueFalseTotal
if exists $Moo::_Utils::MAYBE_LOADED{$module} 1 96 97
155 96 (my $file = "$module.pm") =~ s{::}{/}g;
156  
157 96 my $e = _require $file;
158 96 ▶ if (!defined $e) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $e) { } 94 2 96
elsif (not $e =~ /\ACan't locate \Q$file\E /) { } 1 1 2
159 94 return $MAYBE_LOADED{$module} = 1;
160 }
161 elsif ($e !~ /\ACan't locate \Q$file\E /) {
162 1 warn "$module exists but failed to load with error: $e";
163 }
164 2 return $MAYBE_LOADED{$module} = 0;
165 }
166  
167 114 ▶BEGIN {
Subroutine BEGIN: called CC=5 SCAR=25.7
168 # optimize for newer perls
169 require mro
170 114 ▶ if "$]" >= 5.009_005;
Branch: 1 branch
BranchTrueFalseTotal
if "$]" >= "5.009005" 114 0 114
171  
172 114 ▶ if (defined &mro::get_linear_isa) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined &mro::get_linear_isa) { } 114 0 114
173 114 *_linear_isa = \&mro::get_linear_isa;
174 }
175 else {
176 0 my $e;
177 {
178 0 local $@;
179 0 ▶ eval <<'END_CODE' or $e = $@;
Branch: 1 branch
BranchTrueFalseTotal
eval "sub _linear_isa(\$;\$) {\n my \$class = shift;\n my \$type = shift || exists \$Class::C3::MRO{\$class} ? 'c3' : 'dfs';\n\n if (\$type eq 'c3') {\n require Class::C3;\n return [Class::C3::calculateMRO(\$class)];\n }\n\n my \@check = (\$class);\n my \@lin;\n\n my %found;\n while (defined(my \$check = shift \@check)) {\n push \@lin, \$check;\n no strict 'refs';\n unshift \@check, grep !\$found{\$_}++, \@{\"\$check\\::ISA\"};\n }\n\n return \\\@lin;\n}\n\n1;\n" or $e = $@ 0 0 0
180 sub _linear_isa($;$) {
181 my $class = shift;
182 my $type = shift || exists $Class::C3::MRO{$class} ? 'c3' : 'dfs';
183  
184 if ($type eq 'c3') {
185 require Class::C3;
186 return [Class::C3::calculateMRO($class)];
187 }
188  
189 my @check = ($class);
190 my @lin;
191  
192 my %found;
193 while (defined(my $check = shift @check)) {
194 push @lin, $check;
195 no strict 'refs';
196 unshift @check, grep !$found{$_}++, @{"$check\::ISA"};
197 }
198  
199 return \@lin;
200 }
201  
202 1;
203 END_CODE
204 }
205 0 ▶ die $e if defined $e;
Branch: 1 branch
BranchTrueFalseTotal
if defined $e 0 0 0
206 }
207 }
208  
209 114 ▶BEGIN {
Subroutine BEGIN: called CC=4 SCAR=17.1
210 114 ▶ my $gd_code
Branch: 2 branches
BranchTrueFalseTotal
"$]" >= 5.014 ? : 114 0 114
_maybe_load_module('Devel::GlobalDestruction::XS') ? : 0 0 0
211 = "$]" >= 5.014
212 ? q[${^GLOBAL_PHASE} eq 'DESTRUCT']
213 : _maybe_load_module('Devel::GlobalDestruction::XS')
214 ? 'Devel::GlobalDestruction::XS::in_global_destruction()'
215 : 'do { use B (); ${B::main_cv()} == 0 }';
216 0 ▶ *_in_global_destruction_code = sub () { $gd_code };
Subroutine __ANON__: not called CC=1 SCAR=1.2
217 114 ▶ eval "sub _in_global_destruction () { $gd_code }; 1"
Subroutine _in_global_destruction: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
eval "sub _in_global_destruction () { $gd_code }; 1" or die $@ 114 0 114
218 or die $@;
219 }
220  
221 513 ▶sub _set_loaded {
Subroutine _set_loaded: called CC=2 SCAR=7.1
222 513 (my $file = "$_[0].pm") =~ s{::}{/}g;
223 513 ▶ $INC{$file} ||= $_[1];
Condition66%2/3
$INC{$file} ||= $_[1]
l!l&&r!l&&!r
1084050
MC/DC0%0/2
$INC{$file} ||= $_[1]
$INC{$file} $_[1]
Truth table66%2/3
A ||= B
A= $INC{$file}B= $_[1]
ABresult
1X1
011
000
224 }
225  
226 2891 ▶sub _install_coderef {
Subroutine _install_coderef: called CC=2 SCAR=6.9
227 2891 my ($glob, $code) = (_getglob($_[0]), _name_coderef(@_));
228 114 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
229 2891 ▶ if (*{$glob}{CODE}) {
Branch: 1 branch
BranchTrueFalseTotal
if (*{$glob;}{'CODE'}) { } 12 2879 2891
230 12 *{$glob} = $code;
231 }
232 # perl will sometimes warn about mismatched prototypes coming from the
233 # inheritance cache, so disable them if we aren't redefining a sub
234 else {
235 114 ▶ no warnings 'prototype';
Subroutine BEGIN: called CC=1 SCAR=0.0
236 2879 *{$glob} = $code;
237 }
238 }
239  
240 2958 ▶sub _name_coderef {
Subroutine _name_coderef: called CC=2 SCAR=6.9
241 2958 ▶ shift if @_ > 2; # three args is (target, name, sub)
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 2 2886 72 2958
242 2958 _CAN_SUBNAME ? _subname(@_) : $_[1];
243 }
244  
245 269 ▶sub _check_tracked {
Subroutine _check_tracked: called CC=5 SCAR=16.1
246 269 my ($target, $names) = @_;
247 269 my $stash = _getstash($target);
248 269 ▶ my $exports = $EXPORTS{$target}
Branch: 1 branch
BranchTrueFalseTotal
my $exports = $EXPORTS{$target} or return 155 114 269
249 or return;
250  
251 155 ▶ $names = [keys %$exports]
Branch: 1 branch
BranchTrueFalseTotal
unless $names 11 144 155
252 if !$names;
253 my %rev =
254 map +($exports->{$_} => $_),
255 155 grep defined $exports->{$_},
256 keys %$exports;
257  
258 return
259 grep {
260 155 my $g = $stash->{$_};
261 1152 ▶ $g && defined &$g && exists $rev{\&$g};
Condition100%6/6
$g && defined &$g && exists $rev{\&$g}
!ll&&!rl&&r
11374767
$g and defined &$g
!ll&&!rl&&r
651141
MC/DC100%3/3
$g && defined &$g && exists $rev{\&$g}
$g defined &$g exists $rev{\&$g}
Truth table100%4/4
A and B && C
A= $gB= defined &$gC= exists $rev{\&$g}
ABCresult
0XX0
10X0
1100
1111
262 }
263 @$names;
264 }
265  
266 13 ▶sub _unimport_coderefs {
Subroutine _unimport_coderefs: called CC=4 SCAR=13.9
267 13 my ($target) = @_;
268  
269 13 my $stash = _getstash($target);
270 13 my @exports = _check_tracked($target);
271  
272 13 foreach my $name (@exports) {
273 56 my $old = delete $stash->{$name};
274 56 my $full_name = join('::',$target,$name);
275 # Copy everything except the code slot back into place (e.g. $has)
276 56 foreach my $type (qw(SCALAR HASH ARRAY IO)) {
277 224 ▶ next unless defined(*{$old}{$type});
Branch: 1 branch
BranchTrueFalseTotal
unless defined *{$old;}{$type} 168 56 224
278 114 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
279 56 *$full_name = *{$old}{$type};
280 }
281 }
282 }
283  
284 114 ▶if ($Config::Config{useithreads}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Config::Config{"useithreads"}) 0 114 114
285 0 require Moo::HandleMoose::_TypeMap;
286 }
287  
288 114 1;
« blib/lib/Moo/Role.pm ↑ Summary blib/lib/Method/Generate/Constructor.pm »