blib/lib/Role/Tiny.pm

filter: Statementstmt 96.4% 295 / 306 Branchbran 84.3% 86 / 102 Conditioncond 59.7% 58 / 97 MC/DCmcdc 32.6% 17 / 52 Subroutinesub 96.0% 48 / 50 Podpod 50.0% 6 / 12 totaltotal 82.3% 510 / 619 CC 99 SCAR 17.4
CC
99
Cov
87%
BEGIN
15.4
apply_roles_to_package
11.0
_install_methods
10.3
CRAP 5.7
↑ Summary blib/lib/Role/Tiny/With.pm »
1 package Role::Tiny;
2 27 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 27 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 27 our $VERSION = '2.002005';
6 27 $VERSION =~ tr/_//d;
7  
8 27 our %INFO;
9 27 our %APPLIED_TO;
10 27 our %COMPOSED;
11 27 our %COMPOSITE_INFO;
12 27 our @ON_ROLE_CREATE;
13  
14 # Module state workaround totally stolen from Zefram's Module::Runtime.
15  
16 27 ▶BEGIN {
Subroutine BEGIN: called CC=7 SCAR=27.3
17 27 ▶ *_WORK_AROUND_BROKEN_MODULE_STATE = "$]" < 5.009 ? sub(){1} : sub(){0};
Branch: 1 branch
BranchTrueFalseTotal
"$]" < 5.009 ? : 0 27 27
18 *_WORK_AROUND_HINT_LEAKAGE
19 = "$]" < 5.011 && !("$]" >= 5.009004 && "$]" < 5.010001)
20 27 ▶ ? sub(){1} : sub(){0};
Branch: 1 branch
BranchTrueFalseTotal
"$]" < 5.011 && !("$]" >= 5.009004 && "$]" < 5.010001) ? : 0 27 27
Condition16%1/6
"$]" < "5.011" and not "$]" >= "5.009004" && "$]" < "5.010001"
!ll&&!rl&&r
2700
"$]" >= 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
21 27 ▶ *_CONSTANTS_DEFLATE = "$]" >= 5.012 && "$]" < 5.020 ? sub(){1} : sub(){0};
Branch: 1 branch
BranchTrueFalseTotal
"$]" >= 5.012 && "$]" < 5.02 ? : 0 27 27
Condition33%1/3
"$]" >= "5.012" and "$]" < "5.02"
!ll&&!rl&&r
0270
MC/DC0%0/2
"$]" >= "5.012" and "$]" < "5.02"
"$]" >= "5.012" "$]" < "5.02"
Truth table33%1/3
A and B
A= "$]" >= "5.012"B= "$]" < "5.02"
ABresult
0X0
100
111
22 }
23  
24 27 ▶sub _getglob { no strict 'refs'; \*{$_[0]} }
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine _getglob: called CC=1 SCAR=0.0
25 27 ▶sub _getstash { no strict 'refs'; \%{"$_[0]::"} }
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine _getstash: called CC=1 SCAR=0.0
26  
27 8 ▶sub croak {
Subroutine croak: called CC=1 SCAR=0.0
Pod undocumented
28 8 require Carp;
29 27 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
30 8 *croak = \&Carp::croak;
31 8 goto &Carp::croak;
32 }
33  
34 0 ▶sub Role::Tiny::__GUARD__::DESTROY {
Subroutine DESTROY: not called CC=2 SCAR=17.9
35 0 ▶ delete $INC{$_[0]->[0]} if @{$_[0]};
Branch: 1 branch
BranchTrueFalseTotal
if @{$_[0];} 0 0 0
36 }
37  
38 279 ▶sub _load_module {
Subroutine _load_module: called CC=3 SCAR=11.0
39 279 my ($module) = @_;
40 279 (my $file = "$module.pm") =~ s{::}{/}g;
41 return 1
42 279 ▶ if $INC{$file};
Branch: 1 branch
BranchTrueFalseTotal
if $INC{$file} 13 266 279
43  
44 # can't just ->can('can') because a sub-package Foo::Bar::Baz
45 # creates a 'Baz::' key in Foo::Bar's symbol table
46 return 1
47 266 ▶ if grep !/::\z/, keys %{_getstash($module)};
Branch: 1 branch
BranchTrueFalseTotal
if grep((!/::\z/), keys %{_getstash($module);}) 262 4 266
48 4 my $guard = _WORK_AROUND_BROKEN_MODULE_STATE
49 && bless([ $file ], 'Role::Tiny::__GUARD__');
50 4 local %^H if _WORK_AROUND_HINT_LEAKAGE;
51 4 require $file;
52 1 pop @$guard if _WORK_AROUND_BROKEN_MODULE_STATE;
53 1 return 1;
54 }
55  
56 275 ▶sub _require_module {
Subroutine _require_module: called CC=1 SCAR=0.0
57 275 _load_module($_[1]);
58 }
59  
60 312 ▶sub _all_subs {
Subroutine _all_subs: called CC=1 SCAR=0.0
61 312 my ($me, $package) = @_;
62 312 my $stash = _getstash($package);
63 return {
64 map {;
65 27 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
66 # this is an ugly hack to populate the scalar slot of any globs, to
67 # prevent perl from converting constants back into scalar refs in the
68 # stash when they are used (perl 5.12 - 5.18). scalar slots on their own
69 # aren't detectable through pure perl, so this seems like an acceptable
70 # compromise.
71 1237 ${"${package}::${_}"} = ${"${package}::${_}"}
72 if _CONSTANTS_DEFLATE;
73 1237 $_ => \&{"${package}::${_}"}
74 }
75 312 grep exists &{"${package}::${_}"},
76 grep !/::\z/,
77 keys %$stash
78 };
79 }
80  
81 91 ▶sub import {
Subroutine import: called CC=1 SCAR=0.0
82 91 my $target = caller;
83 91 my $me = shift;
84 91 strict->import;
85 91 warnings->import;
86 91 $me->init_role($target, @_);
87 }
88  
89 91 ▶sub init_role {
Subroutine init_role: called CC=3 SCAR=11.0
Pod undocumented
90 91 my ($me, $target, @args) = @_;
91  
92 91 my $non_methods = $me->_non_methods($target);
93 91 $me->_install_subs($target, @args);
94 91 $me->make_role($target);
95 91 ▶ $me->_mark_new_non_methods($target, $non_methods)
Branch: 1 branch
BranchTrueFalseTotal
if $non_methods and %$non_methods 2 89 91
Condition66%2/3
$non_methods and %$non_methods
!ll&&!rl&&r
0892
MC/DC50%1/2
$non_methods and %$non_methods
$non_methods %$non_methods
Truth table66%2/3
A and B
A= $non_methodsB= %$non_methods
ABresult
0X0
100
111
96 if $non_methods && %$non_methods;
97 91 return;
98 }
99  
100 2 ▶sub _mark_new_non_methods {
Subroutine _mark_new_non_methods: called CC=4 SCAR=13.9
101 2 my ($me, $target, $old_non_methods) = @_;
102  
103 2 my $non_methods = $INFO{$target}{non_methods};
104  
105 2 my $subs = $me->_all_subs($target);
106 2 for my $sub (keys %$subs) {
107 6 ▶ if ( exists $old_non_methods->{$sub} && $non_methods->{$sub} != $subs->{$sub} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $old_non_methods->{$sub} and $non_methods->{$sub} != $subs->{$sub}) 5 1 6
Condition66%2/3
exists $old_non_methods->{$sub} and $non_methods->{$sub} != $subs->{$sub}
!ll&&!rl&&r
105
MC/DC50%1/2
exists $old_non_methods->{$sub} and $non_methods->{$sub} != $subs->{$sub}
exists $old_non_methods->{$sub} $non_methods->{$sub} != $subs->{$sub}
Truth table66%2/3
A and B
A= exists $old_non_methods->{$sub}B= $non_methods->{$sub} != $subs->{$sub}
ABresult
0X0
100
111
108 5 $non_methods->{$sub} = $subs->{$sub};
109 }
110 }
111  
112 2 return;
113 }
114  
115 92 ▶sub make_role {
Subroutine make_role: called CC=3 SCAR=11.0
Pod documented
116 92 my ($me, $target) = @_;
117  
118 92 ▶ return if $me->is_role($target);
Branch: 1 branch
BranchTrueFalseTotal
if $me->is_role($target) 2 90 92
119 90 $INFO{$target}{is_role} = 1;
120  
121 90 my $non_methods = $me->_all_subs($target);
122 90 delete @{$non_methods}{grep /\A\(/, keys %$non_methods};
123 90 $INFO{$target}{non_methods} = $non_methods;
124  
125 # a role does itself
126 90 $APPLIED_TO{$target} = { $target => undef };
127 90 foreach my $hook (@ON_ROLE_CREATE) {
128 2 $hook->($target);
129 }
130 }
131  
132 91 ▶sub _install_subs {
Subroutine _install_subs: called CC=3 SCAR=11.0
133 91 my ($me, $target) = @_;
134 91 ▶ return if $me->is_role($target);
Branch: 1 branch
BranchTrueFalseTotal
if $me->is_role($target) 1 90 91
135 90 my %install = $me->_gen_subs($target);
136 450 *{_getglob("${target}::${_}")} = $install{$_}
137 90 for sort keys %install;
138 90 return;
139 }
140  
141 90 ▶sub _gen_subs {
Subroutine _gen_subs: called CC=1 SCAR=0.0
142 90 my ($me, $target) = @_;
143 (
144 (map {;
145 270 my $type = $_;
146 15 ▶ $type => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
147 15 my $code = pop;
148 15 ▶ my @names = ref $_[0] eq 'ARRAY' ? @{ $_[0] } : @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 1 14 15
149 15 ▶ push @{$INFO{$target}{modifiers}||=[]}, [ $type, @names, $code ];
Condition100%2/2
$INFO{$target}{"modifiers"} ||= []
l!l
213
MC/DC100%1/1
$INFO{$target}{"modifiers"} ||= []
$INFO{$target}{"modifiers"}
Truth table100%2/2
A
A= $INFO{$target}{"modifiers"}
Aresult
11
00
150 15 return;
151 270 };
152 } qw(before after around)),
153 13 ▶ requires => sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
154 13 ▶ push @{$INFO{$target}{requires}||=[]}, @_;
Condition50%1/2
$INFO{$target}{"requires"} ||= []
l!l
013
MC/DC0%0/1
$INFO{$target}{"requires"} ||= []
$INFO{$target}{"requires"}
Truth table50%1/2
A
A= $INFO{$target}{"requires"}
Aresult
11
00
155 13 return;
156 },
157 22 ▶ with => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
158 22 $me->apply_roles_to_package($target, @_);
159 21 return;
160 },
161 90 );
162 }
163  
164 121 ▶sub role_application_steps {
Subroutine role_application_steps: called CC=1 SCAR=0.0
Pod undocumented
165 121 qw(
166 _install_methods
167 _check_requires
168 _install_modifiers
169 _copy_applied_list
170 );
171 }
172  
173 130 ▶sub _copy_applied_list {
Subroutine _copy_applied_list: called CC=2 SCAR=6.9
174 130 my ($me, $to, $role) = @_;
175 # copy our role list into the target's
176 130 ▶ @{$APPLIED_TO{$to}||={}}{keys %{$APPLIED_TO{$role}}} = ();
Condition100%2/2
$APPLIED_TO{$to} ||= {}
l!l
4981
MC/DC100%1/1
$APPLIED_TO{$to} ||= {}
$APPLIED_TO{$to}
Truth table100%2/2
A
A= $APPLIED_TO{$to}
Aresult
11
00
177 }
178  
179 43 ▶sub apply_roles_to_object {
Subroutine apply_roles_to_object: called CC=1 SCAR=0.0
Pod documented
180 43 my ($me, $object, @roles) = @_;
181 43 my $class = ref($object);
182 # on perl < 5.8.9, magic isn't copied to all ref copies. bless the parameter
183 # directly, so at least the variable passed to us will get any magic applied
184 43 bless($_[1], $me->create_class_with_roles($class, @roles));
185 }
186  
187 27 my $role_suffix = 'A000';
188 53 ▶sub _composite_name {
Subroutine _composite_name: called CC=3 SCAR=11.1
189 53 my ($me, $superclass, @roles) = @_;
190  
191 53 my $new_name = $superclass . '__WITH__' . join '__AND__', @roles;
192  
193 53 ▶ if (length($new_name) > 252) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $new_name > 252) 7 46 53
194 7 ▶ $new_name = $COMPOSED{abbrev}{$new_name} ||= do {
Condition33%1/3
$COMPOSED{"abbrev"}{$new_name} ||= do { my $abbrev = substr($new_name, 0, 250 - length($role_suffix)); $abbrev =~ s/(?<!:):$//; $abbrev . '__' . $role_suffix++ }
l!l&&r!l&&!r
070
MC/DC0%0/2
$COMPOSED{"abbrev"}{$new_name} ||= do { my $abbrev = substr($new_name, 0, 250 - length($role_suffix)); $abbrev =~ s/(?<!:):$//; $abbrev . '__' . $role_suffix++ }
$COMPOSED{"abbrev"}{$new_name} do { my $abbrev = substr($new_name, 0, 250 - length($role_suffix)); $abbrev =~ s/(?<!:):$//; $abbrev . '__' . $role_suffix++ }
Truth table33%1/3
A ||= B
A= $COMPOSED{"abbrev"}{$new_name}B= do { my $abbrev = substr($new_name, 0, 250 - length($role_suffix)); $abbrev =~ s/(?<!:):$//; $abbrev . '__' . $role_suffix++ }
ABresult
1X1
011
000
195 7 my $abbrev = substr $new_name, 0, 250 - length $role_suffix;
196 7 $abbrev =~ s/(?<!:):$//;
197 7 $abbrev.'__'.$role_suffix++;
198 };
199 }
200 53 return $new_name;
201 }
202  
203 57 ▶sub create_class_with_roles {
Subroutine create_class_with_roles: called CC=2 SCAR=6.9
Pod documented
204 57 my ($me, $superclass, @roles) = @_;
205  
206 57 $me->_require_module($superclass);
207 57 $me->_check_roles(@roles);
208  
209 53 my $new_name = $me->_composite_name($superclass, @roles);
210  
211 return $new_name
212 53 ▶ if $COMPOSED{class}{$new_name};
Branch: 1 branch
BranchTrueFalseTotal
if $COMPOSED{"class"}{$new_name} 3 50 53
213  
214 50 return $me->_build_class_with_roles($new_name, $superclass, @roles);
215 }
216  
217 50 ▶sub _build_class_with_roles {
Subroutine _build_class_with_roles: called CC=1 SCAR=0.0
218 50 my ($me, $new_name, $superclass, @roles) = @_;
219  
220 50 $COMPOSED{base}{$new_name} = $superclass;
221 50 @{*{_getglob("${new_name}::ISA")}} = ( $superclass );
222 50 $me->apply_roles_to_package($new_name, @roles);
223 45 $COMPOSED{class}{$new_name} = 1;
224 45 return $new_name;
225 }
226  
227 183 ▶sub _check_roles {
Subroutine _check_roles: called CC=5 SCAR=16.1
228 183 my ($me, @roles) = @_;
229 183 ▶ croak "No roles supplied!" unless @roles;
Branch: 1 branch
BranchTrueFalseTotal
unless @roles 2 181 183
230  
231 181 my %seen;
232 181 ▶ if (my @dupes = grep 1 == $seen{$_}++, @roles) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@dupes) = grep((1 == $seen{$_}++), @roles)) 1 180 181
233 1 croak "Duplicated roles: ".join(', ', @dupes);
234 }
235  
236 180 foreach my $role (@roles) {
237 215 $me->_require_module($role);
238 214 ▶ croak "${role} is not a ${me}" unless $me->is_role($role);
Branch: 1 branch
BranchTrueFalseTotal
unless $me->is_role($role) 2 212 214
239 }
240 }
241  
242 27 our %BACKCOMPAT_HACK;
243 27 $BACKCOMPAT_HACK{+__PACKAGE__} = 0;
244 121 ▶sub _want_backcompat_hack {
Subroutine _want_backcompat_hack: called CC=3 SCAR=11.0
245 121 my $me = shift;
246 return $BACKCOMPAT_HACK{$me}
247 121 ▶ if exists $BACKCOMPAT_HACK{$me};
Branch: 1 branch
BranchTrueFalseTotal
if exists $Role::Tiny::BACKCOMPAT_HACK{$me} 118 3 121
248 27 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
249 3 ▶ $BACKCOMPAT_HACK{$me} =
Condition100%3/3
$me->can('apply_single_role_to_package') != \&Role::Tiny::apply_single_role_to_package && $me->can('role_application_steps') == \&Role::Tiny::role_application_steps
!ll&&!rl&&r
111
MC/DC100%2/2
$me->can('apply_single_role_to_package') != \&Role::Tiny::apply_single_role_to_package && $me->can('role_application_steps') == \&Role::Tiny::role_application_steps
$me->can('apply_single_role_to_package') != \&Role::Tiny::apply_single_role_to_package $me->can('role_application_steps') == \&Role::Tiny::role_application_steps
Truth table100%3/3
A && B
A= $me->can('apply_single_role_to_package') != \&Role::Tiny::apply_single_role_to_packageB= $me->can('role_application_steps') == \&Role::Tiny::role_application_steps
ABresult
0X0
100
111
250 $me->can('apply_single_role_to_package') != \&apply_single_role_to_package
251 && $me->can('role_application_steps') == \&role_application_steps
252 }
253  
254 27 our $IN_APPLY_ROLES;
255 2 ▶sub apply_single_role_to_package {
Subroutine apply_single_role_to_package: called CC=2 SCAR=11.6
Pod undocumented
256 return
257 2 ▶ if $IN_APPLY_ROLES;
Branch: 1 branch
BranchTrueFalseTotal
if $Role::Tiny::IN_APPLY_ROLES 2 0 2
258 0 local $IN_APPLY_ROLES = 1;
259  
260 0 my ($me, $to, $role) = @_;
261 0 $me->apply_roles_to_package($to, $role);
262 }
263  
264 9 ▶sub apply_role_to_package {
Subroutine apply_role_to_package: called CC=1 SCAR=0.0
Pod undocumented
265 9 my ($me, $to, $role) = @_;
266 9 $me->apply_roles_to_package($to, $role);
267 }
268  
269 127 ▶sub apply_roles_to_package {
Subroutine apply_roles_to_package: called CC=11 SCAR=24.0
Pod documented
270 127 my ($me, $to, @roles) = @_;
271 127 ▶ croak "Can't apply roles to object with apply_roles_to_package"
Branch: 1 branch
BranchTrueFalseTotal
if ref $to 1 126 127
272 if ref $to;
273  
274 126 $me->_check_roles(@roles);
275  
276 124 my @have_conflicts;
277 my %role_methods;
278  
279 124 ▶ if (@roles > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@roles > 1) 19 105 124
280 19 my %conflicts = %{$me->_composite_info_for(@roles)->{conflicts}};
281 19 @have_conflicts = grep $to->can($_), keys %conflicts;
282 19 delete @conflicts{@have_conflicts};
283  
284 19 ▶ if (keys %conflicts) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %conflicts) 3 16 19
285 3 ▶ my $class = $COMPOSED{base}{$to} || $to;
Condition66%2/3
$COMPOSED{'base'}{$to} || $to
l!l&&r!l&&!r
120
MC/DC0%0/2
$COMPOSED{'base'}{$to} || $to
$COMPOSED{'base'}{$to} $to
Truth table66%2/3
A || B
A= $COMPOSED{'base'}{$to}B= $to
ABresult
1X1
011
000
286 my $fail =
287 join "\n",
288 map {
289 3 "Due to a method name conflict between roles "
290 3 .join(' and ', map "'$_'", sort values %{$conflicts{$_}})
291 .", the method '$_' must be implemented by '$class'"
292 } sort keys %conflicts;
293 3 croak $fail;
294 }
295  
296 16 %role_methods = map +($_ => $me->_concrete_methods_of($_)), @roles;
297 }
298  
299 121 ▶ if (!$IN_APPLY_ROLES and _want_backcompat_hack($me)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $Role::Tiny::IN_APPLY_ROLES and _want_backcompat_hack($me)) 1 120 121
Condition66%2/3
not $Role::Tiny::IN_APPLY_ROLES and _want_backcompat_hack($me)
!ll&&!rl&&r
01201
MC/DC50%1/2
not $Role::Tiny::IN_APPLY_ROLES and _want_backcompat_hack($me)
not $Role::Tiny::IN_APPLY_ROLES _want_backcompat_hack($me)
Truth table66%2/3
not(A) and B
A= not $Role::Tiny::IN_APPLY_ROLESB= _want_backcompat_hack($me)
ABresult
0X0
100
111
300 1 local $IN_APPLY_ROLES = 1;
301 1 foreach my $role (@roles) {
302 2 $me->apply_single_role_to_package($to, $role);
303 }
304 }
305  
306 121 my $role_methods;
307 121 foreach my $step ($me->role_application_steps) {
308 468 foreach my $role (@roles) {
309 # conflicting methods are supposed to be treated as required by the
310 # composed role. we don't have an actual composed role, but because
311 # we know the target class already provides them, we can instead
312 # pretend that the roles don't do for the duration of application.
313 $role_methods = $role_methods{$role} and (
314 140 (local @{$role_methods}{@have_conflicts}),
315 544 ▶ (delete @{$role_methods}{@have_conflicts}),
Branch: 1 branch
BranchTrueFalseTotal
$role_methods = $role_methods{$role} and local @{$role_methods;}{@have_conflicts}, delete @{$role_methods;}{@have_conflicts} 140 404 544
316 );
317  
318 544 $me->$step($to, $role);
319 }
320 }
321 111 $APPLIED_TO{$to}{join('|',@roles)} = 1;
322 }
323  
324 19 ▶sub _composite_info_for {
Subroutine _composite_info_for: called CC=5 SCAR=16.1
325 19 my ($me, @roles) = @_;
326 19 ▶ $COMPOSITE_INFO{join('|', sort @roles)} ||= do {
Condition66%2/3
$COMPOSITE_INFO{join "|", sort(@roles)} ||= do { my %methods; foreach my $role (@roles) { my $this_methods = $me->_concrete_methods_of($role); $methods{$_}{$this_methods->{$_}} = $role foreach (keys %$this_methods); } ; delete $methods{$_} foreach (grep((keys %{$methods{$_};} == 1), keys %methods)); +{"conflicts", \%methods} }
l!l&&r!l&&!r
3160
MC/DC0%0/2
$COMPOSITE_INFO{join "|", sort(@roles)} ||= do { my %methods; foreach my $role (@roles) { my $this_methods = $me->_concrete_methods_of($role); $methods{$_}{$this_methods->{$_}} = $role foreach (keys %$this_methods); } ; delete $methods{$_} foreach (grep((keys %{$methods{$_};} == 1), keys %methods)); +{"conflicts", \%methods} }
$COMPOSITE_INFO{join "|", sort(@roles)} do { my %methods; foreach my $role (@roles) { my $this_methods = $me->_concrete_methods_of($role); $methods{$_}{$this_methods->{$_}} = $role foreach (keys %$this_methods); } ; delete $methods{$_} foreach (grep((keys %{$methods{$_};} == 1), keys %methods)); +{"conflicts", \%methods} }
Truth table66%2/3
A ||= B
A= $COMPOSITE_INFO{join "|", sort(@roles)}B= do { my %methods; foreach my $role (@roles) { my $this_methods = $me->_concrete_methods_of($role); $methods{$_}{$this_methods->{$_}} = $role foreach (keys %$this_methods); } ; delete $methods{$_} foreach (grep((keys %{$methods{$_};} == 1), keys %methods)); +{"conflicts", \%methods} }
ABresult
1X1
011
000
327 16 my %methods;
328 16 foreach my $role (@roles) {
329 35 my $this_methods = $me->_concrete_methods_of($role);
330 35 $methods{$_}{$this_methods->{$_}} = $role for keys %$this_methods;
331 }
332 16 delete $methods{$_} for grep keys(%{$methods{$_}}) == 1, keys %methods;
333 16 +{ conflicts => \%methods }
334 };
335 }
336  
337 140 ▶sub _check_requires {
Subroutine _check_requires: called CC=6 SCAR=18.0
338 140 my ($me, $to, $name, $requires) = @_;
339 140 ▶ $requires ||= $INFO{$name}{requires} || [];
Condition60%3/5
$requires ||= $INFO{$name}{"requires"} || []
l!l&&r!l&&!r
01400
$INFO{$name}{'requires'} || []
l!l
27113
MC/DC0%0/2
$requires ||= $INFO{$name}{"requires"} || []
$requires $INFO{$name}{'requires'}
Truth table33%1/3
A ||= B
A= $requiresB= $INFO{$name}{'requires'}
ABresult
1X1
011
000
340 140 ▶ if (my @requires_fail = grep !$to->can($_), @$requires) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@requires_fail) = grep((!$to->can($_)), @$requires)) 13 127 140
341 # role -> role, add to requires, role -> class, error out
342 13 ▶ if (my $to_info = $INFO{$to}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $to_info = $INFO{$to}) { } 5 8 13
343 5 ▶ push @{$to_info->{requires}||=[]}, @requires_fail;
Condition100%2/2
$to_info->{"requires"} ||= []
l!l
14
MC/DC100%1/1
$to_info->{"requires"} ||= []
$to_info->{"requires"}
Truth table100%2/2
A
A= $to_info->{"requires"}
Aresult
11
00
344 } else {
345 8 croak "Can't apply ${name} to ${to} - missing ".join(', ', @requires_fail);
346 }
347 }
348 }
349  
350 170 ▶sub _non_methods {
Subroutine _non_methods: called CC=7 SCAR=19.5
351 170 my ($me, $role) = @_;
352 170 ▶ my $info = $INFO{$role} or return {};
Branch: 1 branch
BranchTrueFalseTotal
my $info = $INFO{$role} or return {} 80 90 170
353  
354 80 ▶ my %non_methods = %{ $info->{non_methods} || {} };
Condition50%1/2
$info->{'non_methods'} || {}
l!l
800
MC/DC0%0/1
$info->{'non_methods'} || {}
$info->{'non_methods'}
Truth table50%1/2
A
A= $info->{'non_methods'}
Aresult
11
00
355  
356 # this is only for backwards compatibility with older Moo, which
357 # reimplements method tracking rather than calling our method
358 80 ▶ my %not_methods = reverse %{ $info->{not_methods} || {} };
Condition100%2/2
$info->{'not_methods'} || {}
l!l
179
MC/DC100%1/1
$info->{'not_methods'} || {}
$info->{'not_methods'}
Truth table100%2/2
A
A= $info->{'not_methods'}
Aresult
11
00
359 80 ▶ return \%non_methods unless keys %not_methods;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %not_methods 79 1 80
360  
361 1 my $subs = $me->_all_subs($role);
362 1 for my $sub (grep !/\A\(/, keys %$subs) {
363 6 my $code = $subs->{$sub};
364 6 ▶ if (exists $not_methods{$code}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $not_methods{$code}) 1 5 6
365 1 $non_methods{$sub} = $code;
366 }
367 }
368  
369 1 return \%non_methods;
370 }
371  
372 215 ▶sub _concrete_methods_of {
Subroutine _concrete_methods_of: called CC=7 SCAR=19.5
373 215 my ($me, $role) = @_;
374 215 my $info = $INFO{$role};
375  
376 return $info->{methods}
377 215 ▶ if $info && $info->{methods};
Branch: 1 branch
BranchTrueFalseTotal
if $info and $info->{"methods"} 136 79 215
Condition100%3/3
$info and $info->{"methods"}
!ll&&!rl&&r
178136
MC/DC100%2/2
$info and $info->{"methods"}
$info $info->{"methods"}
Truth table100%3/3
A and B
A= $infoB= $info->{"methods"}
ABresult
0X0
100
111
378  
379 79 my $non_methods = $me->_non_methods($role);
380  
381 79 my $subs = $me->_all_subs($role);
382 79 for my $sub (keys %$subs) {
383 476 ▶ if ( exists $non_methods->{$sub} && $non_methods->{$sub} == $subs->{$sub} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $non_methods->{$sub} and $non_methods->{$sub} == $subs->{$sub}) 398 78 476
Condition66%2/3
exists $non_methods->{$sub} and $non_methods->{$sub} == $subs->{$sub}
!ll&&!rl&&r
780398
MC/DC50%1/2
exists $non_methods->{$sub} and $non_methods->{$sub} == $subs->{$sub}
exists $non_methods->{$sub} $non_methods->{$sub} == $subs->{$sub}
Truth table66%2/3
A and B
A= exists $non_methods->{$sub}B= $non_methods->{$sub} == $subs->{$sub}
ABresult
0X0
100
111
384 398 delete $subs->{$sub};
385 }
386 }
387  
388 79 ▶ if ($info) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info) 78 1 79
389 78 $info->{methods} = $subs;
390 }
391 79 return $subs;
392 }
393  
394 3 ▶sub methods_provided_by {
Subroutine methods_provided_by: called CC=3 SCAR=11.0
Pod undocumented
395 3 my ($me, $role) = @_;
396 3 $me->_require_module($role);
397 3 ▶ croak "${role} is not a ${me}" unless $me->is_role($role);
Branch: 1 branch
BranchTrueFalseTotal
unless $me->is_role($role) 1 2 3
398 2 ▶ sort (keys %{$me->_concrete_methods_of($role)}, @{$INFO{$role}->{requires}||[]});
Condition50%1/2
$INFO{$role}{'requires'} || []
l!l
02
MC/DC0%0/1
$INFO{$role}{'requires'} || []
$INFO{$role}{'requires'}
Truth table50%1/2
A
A= $INFO{$role}{'requires'}
Aresult
11
00
399 }
400  
401 140 ▶sub _install_methods {
Subroutine _install_methods: called CC=10 SCAR=23.3
402 140 my ($me, $to, $role) = @_;
403  
404 140 my $methods = $me->_concrete_methods_of($role);
405  
406 140 my %existing_methods;
407 140 @existing_methods{keys %{ $me->_all_subs($to) }} = ();
408  
409 # _concrete_methods_of caches its result on roles. that cache needs to be
410 # invalidated after applying roles
411 140 ▶ delete $INFO{$to}{methods} if $INFO{$to};
Branch: 1 branch
BranchTrueFalseTotal
if $INFO{$to} 25 115 140
412  
413 140 foreach my $i (keys %$methods) {
414 next
415 106 ▶ if exists $existing_methods{$i};
Branch: 1 branch
BranchTrueFalseTotal
if exists $existing_methods{$i} 21 85 106
416  
417 85 my $glob = _getglob "${to}::${i}";
418 85 *$glob = $methods->{$i};
419  
420 # overloads using method names have the method stored in the scalar slot
421 # and &overload::nil in the code slot.
422 next
423 unless $i =~ /^\(/
424 && ((defined &overload::nil && $methods->{$i} == \&overload::nil)
425 85 ▶ || (defined &overload::_nil && $methods->{$i} == \&overload::_nil));
Branch: 1 branch
BranchTrueFalseTotal
unless $i =~ /^\(/ and defined &overload::nil && $methods->{$i} == \&overload::nil || defined &overload::_nil && $methods->{$i} == \&overload::_nil 76 9 85
Condition66%8/12
$i =~ /^\(/ and defined &overload::nil && $methods->{$i} == \&overload::nil || defined &overload::_nil && $methods->{$i} == \&overload::_nil
!ll&&!rl&&r
6979
defined &overload::nil and $methods->{$i} == \&overload::nil or defined &overload::_nil and $methods->{$i} == \&overload::_nil
l!l&&r!l&&!r
907
defined &overload::nil and $methods->{$i} == \&overload::nil
!ll&&!rl&&r
079
defined &overload::_nil and $methods->{$i} == \&overload::_nil
!ll&&!rl&&r
700
MC/DC40%2/5
$i =~ /^\(/ and defined &overload::nil && $methods->{$i} == \&overload::nil || defined &overload::_nil && $methods->{$i} == \&overload::_nil
$i =~ /^\(/ defined &overload::nil $methods->{$i} == \&overload::nil defined &overload::_nil $methods->{$i} == \&overload::_nil
Truth table37%3/8
A and B and C or D and E
A= $i =~ /^\(/B= defined &overload::nilC= $methods->{$i} == \&overload::nilD= defined &overload::_nilE= $methods->{$i} == \&overload::_nil
ABCDEresult
0XXXX0
10X0X0
10X100
1100X0
110100
111XX1
10X111
110111
426  
427 9 my $overload = ${ _getglob "${role}::${i}" };
428 next
429 9 ▶ unless defined $overload;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $overload 0 9 9
430  
431 9 *$glob = \$overload;
432 }
433  
434 140 $me->_install_does($to);
435 }
436  
437 132 ▶sub _install_modifiers {
Subroutine _install_modifiers: called CC=7 SCAR=19.5
438 132 my ($me, $to, $name) = @_;
439 132 ▶ return unless my $modifiers = $INFO{$name}{modifiers};
Branch: 1 branch
BranchTrueFalseTotal
unless my $modifiers = $INFO{$name}{"modifiers"} 99 33 132
440 33 my $info = $INFO{$to};
441 33 ▶ my $existing = ($info ? $info->{modifiers} : $COMPOSED{modifiers}{$to}) ||= [];
Branch: 1 branch
BranchTrueFalseTotal
$info ? : 7 26 33
Condition100%2/2
$info ? $info->{"modifiers"} : $COMPOSED{"modifiers"}{$to} ||= []
l!l
1122
MC/DC100%1/1
$info ? $info->{"modifiers"} : $COMPOSED{"modifiers"}{$to} ||= []
$info ? $info->{"modifiers"} : $COMPOSED{"modifiers"}{$to}
Truth table100%2/2
A
A= $info ? $info->{"modifiers"} : $COMPOSED{"modifiers"}{$to}
Aresult
11
00
442 my @modifiers = grep {
443 35 my $modifier = $_;
444 35 !grep $_ == $modifier, @$existing;
445 33 ▶ } @{$modifiers||[]};
Condition50%1/2
$modifiers || []
l!l
330
MC/DC0%0/1
$modifiers || []
$modifiers
Truth table50%1/2
A
A= $modifiers
Aresult
11
00
446 33 push @$existing, @modifiers;
447  
448 33 ▶ if (!$info) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $info) 26 7 33
449 26 foreach my $modifier (@modifiers) {
450 26 $me->_install_single_modifier($to, @$modifier);
451 }
452 }
453 }
454  
455 27 my $vcheck_error;
456  
457 26 ▶sub _install_single_modifier {
Subroutine _install_single_modifier: called CC=4 SCAR=14.0
458 26 my ($me, @args) = @_;
459 26 ▶ defined($vcheck_error) or $vcheck_error = do {
Branch: 1 branch
BranchTrueFalseTotal
defined $vcheck_error or $vcheck_error = do { local $@; eval { do { require Class::Method::Modifiers; "Class::Method::Modifiers"->VERSION("1.05"); 1 } } ? 0 : $@ } 20 6 26
460 6 local $@;
461 6 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require Class::Method::Modifiers; 'Class::Method::Modifiers'->VERSION(1.05); 1 } } ? : 6 0 6
462 6 require Class::Method::Modifiers;
463 6 Class::Method::Modifiers->VERSION(1.05);
464 6 1;
465 } ? 0 : $@;
466 };
467 26 ▶ $vcheck_error and die $vcheck_error;
Branch: 1 branch
BranchTrueFalseTotal
$vcheck_error and die $vcheck_error 0 26 26
468 26 Class::Method::Modifiers::install_modifier(@args);
469 }
470  
471 0 ▶my $FALLBACK = sub { 0 };
Subroutine __ANON__: not called CC=1 SCAR=1.2
472 140 ▶sub _install_does {
Subroutine _install_does: called CC=8 SCAR=21.5
473 140 my ($me, $to) = @_;
474  
475 # only add does() method to classes
476 140 ▶ return if $me->is_role($to);
Branch: 1 branch
BranchTrueFalseTotal
if $me->is_role($to) 25 115 140
477  
478 115 my $does = $me->can('does_role');
479 # add does() only if they don't have one
480 115 ▶ *{_getglob "${to}::does"} = $does unless $to->can('does');
Branch: 1 branch
BranchTrueFalseTotal
unless $to->can("does") 60 55 115
481  
482 return
483 115 ▶ if $to->can('DOES') and $to->can('DOES') != (UNIVERSAL->can('DOES') || 0);
Branch: 1 branch
BranchTrueFalseTotal
if $to->can("DOES") and $to->can("DOES") != ("UNIVERSAL"->can("DOES") || 0) 56 59 115
Condition60%3/5
$to->can("DOES") and $to->can("DOES") != ("UNIVERSAL"->can("DOES") || 0)
!ll&&!rl&&r
05956
'UNIVERSAL'->can('DOES') || 0
l!l
1150
MC/DC50%1/2
$to->can("DOES") and $to->can("DOES") != ("UNIVERSAL"->can("DOES") || 0)
$to->can("DOES") $to->can("DOES") != ("UNIVERSAL"->can("DOES") || 0)
MC/DC0%0/1
'UNIVERSAL'->can('DOES') || 0
'UNIVERSAL'->can('DOES')
Truth table66%2/3
A and B
A= $to->can("DOES")B= $to->can("DOES") != ("UNIVERSAL"->can("DOES") || 0)
ABresult
0X0
100
111
Truth table50%1/2
A
A= 'UNIVERSAL'->can('DOES')
Aresult
11
00
484  
485 59 ▶ my $existing = $to->can('DOES') || $to->can('isa') || $FALLBACK;
Condition33%2/6
$to->can('DOES') || $to->can('isa') || $FALLBACK
l!l&&r!l&&!r
5900
$to->can("DOES") or $to->can("isa")
l!l&&r!l&&!r
5900
MC/DC0%0/3
$to->can('DOES') || $to->can('isa') || $FALLBACK
$to->can("DOES") $to->can("isa") $FALLBACK
Truth table25%1/4
A or B || C
A= $to->can("DOES")B= $to->can("isa")C= $FALLBACK
ABCresult
1XX1
01X1
0011
0000
486 5 ▶ my $new_sub = sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
487 5 my ($proto, $role) = @_;
488 5 ▶ $proto->$does($role) or $proto->$existing($role);
Branch: 1 branch
BranchTrueFalseTotal
$proto->$does($role) or $proto->$existing($role) 5 0 5
489 59 };
490 27 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
491 59 return *{_getglob "${to}::DOES"} = $new_sub;
492 }
493  
494 # optimize for newer perls
495 require mro
496 27 ▶ if "$]" >= 5.009_005;
Branch: 1 branch
BranchTrueFalseTotal
if "$]" >= "5.009005" 27 0 27
497  
498 sub _linear_isa($;$);
499  
500 27 ▶if (defined &mro::get_linear_isa) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined &mro::get_linear_isa) { } 27 0 27
501 27 ▶ no warnings 'prototype';
Subroutine BEGIN: called CC=1 SCAR=0.0
502 27 *_linear_isa = \&mro::get_linear_isa;
503 }
504 else {
505 0 my $e;
506 {
507 0 local $@;
508 # this routine is simplified and not fully compatible with mro::get_linear_isa
509 # but for our use the order doesn't matter, so we don't need to care
510 0 ▶ eval <<'END_CODE' or $e = $@;
Branch: 1 branch
BranchTrueFalseTotal
eval "sub _linear_isa(\$;\$) {\n if (defined &mro::get_linear_isa) {\n no warnings 'redefine', 'prototype';\n *_linear_isa = \\&mro::get_linear_isa;\n goto &mro::get_linear_isa;\n }\n\n my \@check = shift;\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
511 sub _linear_isa($;$) {
512 if (defined &mro::get_linear_isa) {
513 no warnings 'redefine', 'prototype';
514 *_linear_isa = \&mro::get_linear_isa;
515 goto &mro::get_linear_isa;
516 }
517  
518 my @check = shift;
519 my @lin;
520  
521 my %found;
522 while (defined(my $check = shift @check)) {
523 push @lin, $check;
524 no strict 'refs';
525 unshift @check, grep !$found{$_}++, @{"$check\::ISA"};
526 }
527  
528 return \@lin;
529 }
530  
531 1;
532 END_CODE
533 }
534 0 ▶ die $e if defined $e;
Branch: 1 branch
BranchTrueFalseTotal
if defined $e 0 0 0
535 }
536  
537 38 ▶sub does_role {
Subroutine does_role: called CC=4 SCAR=13.9
Pod documented
538 38 my ($proto, $role) = @_;
539 38 ▶ foreach my $class (@{_linear_isa(ref($proto)||$proto)}) {
Condition66%2/3
ref $proto || $proto
l!l&&r!l&&!r
9290
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table66%2/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
540 41 ▶ return 1 if exists $APPLIED_TO{$class}{$role};
Branch: 1 branch
BranchTrueFalseTotal
if exists $APPLIED_TO{$class}{$role} 28 13 41
541 }
542 10 return 0;
543 }
544  
545 541 ▶sub is_role {
Subroutine is_role: called CC=5 SCAR=20.9
Pod documented
546 541 my ($me, $role) = @_;
547 return !!($INFO{$role} && (
548 $INFO{$role}{is_role}
549 # these are for backward compatibility with older Moo that overrode some
550 # methods without calling the originals, thus not getting is_role set
551 || $INFO{$role}{requires}
552 || $INFO{$role}{not_methods}
553 || $INFO{$role}{non_methods}
554 541 ▶ ));
Condition41%5/12
$INFO{$role} && ($INFO{$role}{'is_role'} || $INFO{$role}{'requires'} || $INFO{$role}{'not_methods'} || $INFO{$role}{'non_methods'})
!ll&&!rl&&r
2970244
$INFO{$role}{"is_role"} or $INFO{$role}{"requires"} or $INFO{$role}{"not_methods"} or $INFO{$role}{"non_methods"}
l!l&&r!l&&!r
24400
$INFO{$role}{"is_role"} or $INFO{$role}{"requires"} or $INFO{$role}{"not_methods"}
l!l&&r!l&&!r
24400
$INFO{$role}{"is_role"} or $INFO{$role}{"requires"}
l!l&&r!l&&!r
24400
MC/DC20%1/5
$INFO{$role} && ($INFO{$role}{'is_role'} || $INFO{$role}{'requires'} || $INFO{$role}{'not_methods'} || $INFO{$role}{'non_methods'})
$INFO{$role} $INFO{$role}{"is_role"} $INFO{$role}{"requires"} $INFO{$role}{"not_methods"} $INFO{$role}{"non_methods"}
Truth table33%2/6
A && B or C or D or E
A= $INFO{$role}B= $INFO{$role}{"is_role"}C= $INFO{$role}{"requires"}D= $INFO{$role}{"not_methods"}E= $INFO{$role}{"non_methods"}
ABCDEresult
0XXXX0
100000
11XXX1
101XX1
1001X1
100011
555 }
556  
557 27 1;
558 __END__
↑ Summary blib/lib/Role/Tiny/With.pm »