MOP/Internal/Util.pm

filter: Statementstmt 93.4% 184 / 197 Branchbran 54.5% 60 / 110 Conditioncond 44.6% 25 / 56 MC/DCmcdc 6.2% 2 / 32 Subroutinesub 100% 35 / 35 Podpod 0.0% 0 / 19 totaltotal 68.1% 306 / 449 CC 88 SCAR 23.7
CC
88
Cov
74%
APPLY_ROLES
33.1
COMPOSE_ALL_ROLE_METHODS
14.6
COMPOSE_ALL_ROLE_SLOTS
14.2
CRAP 10.7
« MOP/Util.pm ↑ Summary MOP/Role.pm »
1 package MOP::Internal::Util;
2 # ABSTRACT: For MOP Internal Use Only
3  
4 35 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 35 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 35 ▶use B (); # nasty stuff, all nasty stuff
Subroutine BEGIN: called CC=1 SCAR=0.0
8 35 ▶use Carp (); # errors and stuff
Subroutine BEGIN: called CC=1 SCAR=0.0
9 35 ▶use Sub::Util (); # handling some sub stuff
Subroutine BEGIN: called CC=1 SCAR=0.0
10 35 ▶use Sub::Metadata (); # handling other sub stuff
Subroutine BEGIN: called CC=1 SCAR=0.0
11 35 ▶use Symbol (); # creating the occasional symbol
Subroutine BEGIN: called CC=1 SCAR=0.0
12 35 ▶use Scalar::Util (); # I think I use blessed somewhere in here ...
Subroutine BEGIN: called CC=1 SCAR=0.0
13 35 ▶use Devel::OverloadInfo (); # Sometimes I need to know about overloading
Subroutine BEGIN: called CC=1 SCAR=0.0
14 35 ▶use Devel::Hook (); # for scheduling UNITCHECK blocks ...
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 35 our $VERSION = '0.14';
17 35 our $AUTHORITY = 'cpan:STEVAN';
18  
19 ## ------------------------------------------------------------------
20 ## Basic Glob access
21 ## ------------------------------------------------------------------
22  
23 117 ▶sub IS_VALID_MODULE_NAME {
Subroutine IS_VALID_MODULE_NAME: called CC=1 SCAR=0.0
Pod undocumented
24 117 my ($name) = @_;
25 117 $name =~ /[A-Z_a-z][0-9A-Z_a-z]*(?:::[0-9A-Z_a-z]+)*/
26 }
27  
28 4 ▶sub IS_STASH_REF {
Subroutine IS_STASH_REF: called CC=3 SCAR=11.0
Pod undocumented
29 4 my ($stash) = @_;
30 4 ▶ Carp::confess('[ARGS] You must specify a stash')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $stash 0 4 4
31 unless defined $stash;
32 4 ▶ if ( my $name = B::svref_2object( $stash )->NAME ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $name = B::svref_2object($stash)->NAME) 2 2 4
33 2 return IS_VALID_MODULE_NAME( $name );
34 }
35 2 return;
36 }
37  
38 1135 ▶sub GET_NAME {
Subroutine GET_NAME: called CC=2 SCAR=7.1
Pod undocumented
39 1135 my ($stash) = @_;
40 1135 ▶ Carp::confess('[ARGS] You must specify a stash')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $stash 0 1135 1135
41 unless defined $stash;
42 1135 B::svref_2object( $stash )->NAME
43 }
44  
45 91 ▶sub GET_STASH_NAME {
Subroutine GET_STASH_NAME: called CC=2 SCAR=7.1
Pod undocumented
46 91 my ($stash) = @_;
47 91 ▶ Carp::confess('[ARGS] You must specify a stash')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $stash 0 91 91
48 unless defined $stash;
49 91 B::svref_2object( $stash )->STASH->NAME
50 }
51  
52 546 ▶sub GET_GLOB_NAME {
Subroutine GET_GLOB_NAME: called CC=2 SCAR=7.1
Pod undocumented
53 546 my ($stash) = @_;
54 546 ▶ Carp::confess('[ARGS] You must specify a stash')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $stash 0 546 546
55 unless defined $stash;
56 546 B::svref_2object( $stash )->GV->NAME
57 }
58  
59 611 ▶sub GET_GLOB_STASH_NAME {
Subroutine GET_GLOB_STASH_NAME: called CC=2 SCAR=7.1
Pod undocumented
60 611 my ($stash) = @_;
61 611 ▶ Carp::confess('[ARGS] You must specify a stash')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $stash 0 611 611
62 unless defined $stash;
63 611 B::svref_2object( $stash )->GV->STASH->NAME
64 }
65  
66 1627 ▶sub GET_GLOB_SLOT {
Subroutine GET_GLOB_SLOT: called CC=8 SCAR=21.4
Pod undocumented
67 1627 my ($stash, $name, $slot) = @_;
68  
69 1627 ▶ Carp::confess('[ARGS] You must specify a stash')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $stash 0 1627 1627
70 unless defined $stash;
71 1627 ▶ Carp::confess('[ARGS] You must specify a name')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 1627 1627
72 unless defined $name;
73 1627 ▶ Carp::confess('[ARGS] You must specify a slot')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $slot 0 1627 1627
74 unless defined $slot;
75  
76 # do my best to not autovivify, and
77 # return undef if not
78 1627 ▶ return unless exists $stash->{ $name };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $stash->{$name} 156 1471 1627
79 # occasionally we need to auto-inflate
80 # the optimized version of a required
81 # method, its annoying, but the XS side
82 # should not have to care about this so
83 # it can be removed eventually.
84 1471 ▶ if (( $slot eq 'CODE' && $stash->{ $name } eq "-1" ) || ref $stash->{ $name } ne 'GLOB') {
Branch: 1 branch
BranchTrueFalseTotal
if ($slot eq "CODE" and $stash->{$name} eq -1 or ref $stash->{$name} ne "GLOB") 1471 0 1471
Condition83%5/6
$slot eq "CODE" and $stash->{$name} eq -1 or ref $stash->{$name} ne "GLOB"
l!l&&r!l&&!r
314680
$slot eq "CODE" and $stash->{$name} eq -1
!ll&&!rl&&r
15113173
MC/DC0%0/3
$slot eq "CODE" and $stash->{$name} eq -1 or ref $stash->{$name} ne "GLOB"
$slot eq "CODE" $stash->{$name} eq -1 ref $stash->{$name} ne "GLOB"
Truth table60%3/5
A and B or C
A= $slot eq "CODE"B= $stash->{$name} eq -1C= ref $stash->{$name} ne "GLOB"
ABCresult
11X1
0X11
1011
0X00
1000
85 1471 B::svref_2object( $stash )->NAME->can( $name );
86 }
87  
88  
89 # return the reference stored in the glob
90 # which might be undef, but that can be
91 # handled by the caller
92 1471 return *{ $stash->{ $name } }{ $slot };
93 }
94  
95 4 ▶sub SET_GLOB_SLOT {
Subroutine SET_GLOB_SLOT: called CC=4 SCAR=14.0
Pod undocumented
96 4 my ($stash, $name, $value_ref) = @_;
97  
98 4 ▶ Carp::confess('[ARGS] You must specify a stash')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $stash 0 4 4
99 unless defined $stash;
100 4 ▶ Carp::confess('[ARGS] You must specify a name')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 4 4
101 unless defined $name;
102 4 ▶ Carp::confess('[ARGS] You must specify a value REF')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value_ref 0 4 4
103 unless defined $value_ref;
104  
105 {
106 35 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
107 35 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
108 # get the name of the stash, we could have
109 # passed this in, but it is easy to get in
110 # XS, and so we can punt that down the road
111 # for the time being
112 4 my $pkg = B::svref_2object( $stash )->NAME;
113 4 *{ $pkg . '::' . $name } = $value_ref;
114 }
115 4 return;
116 }
117  
118 ## ------------------------------------------------------------------
119 ## UNITCHECK hook
120 ## ------------------------------------------------------------------
121  
122 5 ▶sub ADD_UNITCHECK_HOOK {
Subroutine ADD_UNITCHECK_HOOK: called CC=4 SCAR=15.6
Pod undocumented
123 5 my ($cv) = @_;
124 5 ▶ Carp::confess('[ARGS] You must specify a CODE reference')
Branch: 1 branch
BranchTrueFalseTotal
unless $cv 0 5 5
125 unless $cv;
126 5 ▶ Carp::confess('[ARGS] You must specify a CODE reference')
Branch: 1 branch
BranchTrueFalseTotal
unless $cv and ref $cv eq "CODE" 0 5 5
Condition33%1/3
$cv and ref $cv eq "CODE"
!ll&&!rl&&r
005
MC/DC0%0/2
$cv and ref $cv eq "CODE"
$cv ref $cv eq "CODE"
Truth table33%1/3
A and B
A= $cvB= ref $cv eq "CODE"
ABresult
0X0
100
111
127 unless $cv && ref $cv eq 'CODE';
128 5 Devel::Hook->push_UNITCHECK_hook( $cv );
129 }
130  
131 ## ------------------------------------------------------------------
132 ## CV/Glob introspection
133 ## ------------------------------------------------------------------
134  
135 119 ▶sub CAN_COERCE_TO_CODE_REF {
Subroutine CAN_COERCE_TO_CODE_REF: called CC=5 SCAR=19.4
Pod undocumented
136 119 my ($object) = @_;
137 119 ▶ return 0 unless $object && Scalar::Util::blessed( $object );
Branch: 1 branch
BranchTrueFalseTotal
unless $object and builtin::blessed($object) 93 26 119
Condition66%2/3
$object and builtin::blessed($object)
!ll&&!rl&&r
09326
MC/DC50%1/2
$object and builtin::blessed($object)
$object builtin::blessed($object)
Truth table66%2/3
A and B
A= $objectB= builtin::blessed($object)
ABresult
0X0
100
111
138 # might be just a blessed CODE ref ...
139 26 ▶ return 1 if Scalar::Util::reftype( $object ) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if builtin::reftype($object) eq "CODE" 26 0 26
140 # or might be overloaded object ...
141 0 ▶ return 0 unless Devel::OverloadInfo::is_overloaded( $object );
Branch: 1 branch
BranchTrueFalseTotal
unless Devel::OverloadInfo::is_overloaded($object) 0 0 0
142 0 return exists Devel::OverloadInfo::overload_info( $object )->{'&{}'};
143 }
144  
145 929 ▶sub IS_CV_NULL {
Subroutine IS_CV_NULL: called CC=5 SCAR=19.4
Pod undocumented
146 929 my ($cv) = @_;
147 929 ▶ Carp::confess('[ARGS] You must specify a CODE reference')
Branch: 1 branch
BranchTrueFalseTotal
unless $cv 0 929 929
148 unless $cv;
149 929 ▶ Carp::confess('[ARGS] You must specify a CODE reference')
Branch: 1 branch
BranchTrueFalseTotal
unless $cv and ref $cv eq "CODE" or CAN_COERCE_TO_CODE_REF($cv) 0 929 929
Condition33%2/6
$cv and ref $cv eq "CODE" or CAN_COERCE_TO_CODE_REF($cv)
l!l&&r!l&&!r
92900
$cv and ref $cv eq "CODE"
!ll&&!rl&&r
00929
MC/DC0%0/3
$cv and ref $cv eq "CODE" or CAN_COERCE_TO_CODE_REF($cv)
$cv ref $cv eq "CODE" CAN_COERCE_TO_CODE_REF($cv)
Truth table20%1/5
A and B or C
A= $cvB= ref $cv eq "CODE"C= CAN_COERCE_TO_CODE_REF($cv)
ABCresult
11X1
0X11
1011
0X00
1000
150 unless $cv && ref $cv eq 'CODE'
151 || CAN_COERCE_TO_CODE_REF( $cv );
152 929 return Sub::Metadata::sub_body_type( $cv ) eq 'UNDEF';
153 }
154  
155 166 ▶sub DOES_GLOB_HAVE_NULL_CV {
Subroutine DOES_GLOB_HAVE_NULL_CV: called CC=8 SCAR=22.0
Pod undocumented
156 166 my ($glob) = @_;
157 166 ▶ Carp::confess('[ARGS] You must specify a GLOB')
Branch: 1 branch
BranchTrueFalseTotal
unless $glob 0 166 166
158 unless $glob;
159  
160 # The glob may be -1 or a string, which is perl’s way
161 # of optimizing null sub declarations like ‘sub foo;’
162 # and ‘sub bar($);’.
163 166 ▶ return 1 if ref \$glob eq 'SCALAR' && defined $glob;
Branch: 1 branch
BranchTrueFalseTotal
if ref \$glob eq "SCALAR" and defined $glob 1 165 166
Condition66%2/3
ref \$glob eq "SCALAR" and defined $glob
!ll&&!rl&&r
16501
MC/DC50%1/2
ref \$glob eq "SCALAR" and defined $glob
ref \$glob eq "SCALAR" defined $glob
Truth table66%2/3
A and B
A= ref \$glob eq "SCALAR"B= defined $glob
ABresult
0X0
100
111
164 # We may have a reference to a scalar or array, which
165 # represents a constant, so is not a null sub.
166 165 ▶ return 0 if ref $glob and ref $glob ne 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $glob and ref $glob ne "CODE" 0 165 165
Condition33%1/3
ref $glob and ref $glob ne "CODE"
!ll&&!rl&&r
16500
MC/DC0%0/2
ref $glob and ref $glob ne "CODE"
ref $glob ref $glob ne "CODE"
Truth table33%1/3
A and B
A= ref $globB= ref $glob ne "CODE"
ABresult
0X0
100
111
167 # next lets see if we have a CODE slot (or a code
168 # reference instead of a glob) ...
169 165 ▶ if ( my $code = ref $glob ? $glob : *{ $glob }{CODE} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $code = ref $glob ? $glob : *{$glob;}{"CODE"}) 141 24 165
ref $glob ? : 0 165 165
170 141 return Sub::Metadata::sub_body_type( $code ) eq 'UNDEF';
171 }
172  
173 # if we had no CODE slot, it can't be a NULL CV ...
174 24 return 0;
175 }
176  
177 3 ▶sub CREATE_NULL_CV {
Subroutine CREATE_NULL_CV: called CC=4 SCAR=14.0
Pod undocumented
178 3 my ($in_pkg, $name) = @_;
179 3 ▶ Carp::confess('[ARGS] You must specify a package name')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $in_pkg 0 3 3
180 unless defined $in_pkg;
181 3 ▶ Carp::confess('[ARGS] You must specify a name')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 3 3
182 unless defined $name;
183 # this just tries to eval the NULL CV into
184 # place, it is ugly, but works for now
185 3 ▶ eval "sub ${in_pkg}::${name}; 1;" or do { Carp::confess($@) };
Branch: 1 branch
BranchTrueFalseTotal
eval "sub ${in_pkg}::$name; 1;" or do { Carp::confess($@) } 2 1 3
186 2 return;
187 }
188  
189 3 ▶sub SET_COMP_STASH_FOR_CV {
Subroutine SET_COMP_STASH_FOR_CV: called CC=6 SCAR=19.3
Pod undocumented
190 3 my ($cv, $in_pkg) = @_;
191 3 ▶ Carp::confess('[ARGS] You must specify a CODE reference')
Branch: 1 branch
BranchTrueFalseTotal
unless $cv 0 3 3
192 unless $cv;
193 3 ▶ Carp::confess('[ARGS] You must specify a package name')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $in_pkg 0 3 3
194 unless defined $in_pkg;
195 3 ▶ Carp::confess('[ARGS] You must specify a CODE reference')
Branch: 1 branch
BranchTrueFalseTotal
unless $cv and ref $cv eq "CODE" or CAN_COERCE_TO_CODE_REF($cv) 0 3 3
Condition66%4/6
$cv and ref $cv eq "CODE" or CAN_COERCE_TO_CODE_REF($cv)
l!l&&r!l&&!r
120
$cv and ref $cv eq "CODE"
!ll&&!rl&&r
021
MC/DC0%0/3
$cv and ref $cv eq "CODE" or CAN_COERCE_TO_CODE_REF($cv)
$cv ref $cv eq "CODE" CAN_COERCE_TO_CODE_REF($cv)
Truth table40%2/5
A and B or C
A= $cvB= ref $cv eq "CODE"C= CAN_COERCE_TO_CODE_REF($cv)
ABCresult
11X1
0X11
1011
0X00
1000
196 unless $cv && ref $cv eq 'CODE'
197 || CAN_COERCE_TO_CODE_REF( $cv );
198 3 Sub::Metadata::mutate_sub_package( $cv, $in_pkg );
199 }
200  
201 422 ▶sub INSTALL_CV {
Subroutine INSTALL_CV: called CC=8 SCAR=22.0
Pod undocumented
202 422 my ($in_pkg, $name, $cv, %opts) = @_;
203  
204 422 ▶ Carp::confess('[ARGS] You must specify a package name')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $in_pkg 0 422 422
205 unless defined $in_pkg;
206 422 ▶ Carp::confess('[ARGS] You must specify a name')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 422 422
207 unless defined $name;
208 422 ▶ Carp::confess('[ARGS] You must specify a CODE reference')
Branch: 1 branch
BranchTrueFalseTotal
unless $cv and ref $cv eq "CODE" or CAN_COERCE_TO_CODE_REF($cv) 0 422 422
Condition33%2/6
$cv and ref $cv eq "CODE" or CAN_COERCE_TO_CODE_REF($cv)
l!l&&r!l&&!r
42200
$cv and ref $cv eq "CODE"
!ll&&!rl&&r
00422
MC/DC0%0/3
$cv and ref $cv eq "CODE" or CAN_COERCE_TO_CODE_REF($cv)
$cv ref $cv eq "CODE" CAN_COERCE_TO_CODE_REF($cv)
Truth table20%1/5
A and B or C
A= $cvB= ref $cv eq "CODE"C= CAN_COERCE_TO_CODE_REF($cv)
ABCresult
11X1
0X11
1011
0X00
1000
209 unless $cv && ref $cv eq 'CODE'
210 || CAN_COERCE_TO_CODE_REF( $cv );
211 Carp::confess("[ARGS] You must specify a boolean value for `set_subname` option")
212 422 ▶ if not exists $opts{set_subname};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts{"set_subname"} 0 422 422
213  
214 {
215 35 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
216 35 ▶ no warnings 'once', 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
217  
218 422 my $fullname = $in_pkg.'::'.$name;
219 422 ▶ *{$fullname} = $opts{set_subname} ? Sub::Util::set_subname($fullname, $cv) : $cv;
Branch: 1 branch
BranchTrueFalseTotal
$opts{'set_subname'} ? : 1 421 422
220 }
221 422 return;
222 }
223  
224 4 ▶sub REMOVE_CV_FROM_GLOB {
Subroutine REMOVE_CV_FROM_GLOB: called CC=8 SCAR=21.1
Pod undocumented
225 4 my ($stash, $name) = @_;
226  
227 4 ▶ Carp::confess('[ARGS] You must specify a stash')
Branch: 1 branch
BranchTrueFalseTotal
unless $stash and ref $stash eq "HASH" 0 4 4
Condition33%1/3
$stash and ref $stash eq "HASH"
!ll&&!rl&&r
004
MC/DC0%0/2
$stash and ref $stash eq "HASH"
$stash ref $stash eq "HASH"
Truth table33%1/3
A and B
A= $stashB= ref $stash eq "HASH"
ABresult
0X0
100
111
228 unless $stash && ref $stash eq 'HASH';
229 4 ▶ Carp::confess('[ARGS] You must specify a name')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 4 4
230 unless defined $name;
231  
232 # find the glob we are looking for
233 # which might not exist, in which
234 # case we do nothing ....
235 4 ▶ if ( my $glob = $stash->{ $name } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $glob = $stash->{$name}) 4 0 4
236 # once we find it, extract all the
237 # slots we need, note the missing
238 # CODE slot since we don't need
239 # that in our new glob ...
240 4 my %to_save;
241 4 foreach my $slot (qw[ SCALAR ARRAY HASH FORMAT IO ]) {
242 20 ▶ if ( my $val = *{ $glob }{ $slot } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $val = *{$glob;}{$slot}) 4 16 20
243 4 $to_save{ $slot } = $val;
244 }
245 }
246 # replace the old glob with a new one ...
247 4 $stash->{ $name } = Symbol::gensym();
248 # now go about constructing our new
249 # glob by restoring the other slots
250 {
251 35 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
252 35 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
253 # get the name of the stash, we could have
254 # passed this in, but it is easy to get in
255 # XS, and so we can punt that down the road
256 # for the time being
257 4 my $pkg = B::svref_2object( $stash )->NAME;
258 4 foreach my $type ( keys %to_save ) {
259 4 *{ $pkg . '::' . $name } = $to_save{ $type };
260 }
261 }
262 }
263 # ... the end
264 4 return;
265 }
266  
267 ## ------------------------------------------------------------------
268 ## Role application and composition
269 ## ------------------------------------------------------------------
270  
271 16 ▶sub APPLY_ROLES {
Subroutine APPLY_ROLES: called CC=18 SCAR=35.0
Pod undocumented
272 16 my ($meta, $roles) = @_;
273  
274 16 ▶ Carp::confess('[ARGS] You must specify a metaclass to apply roles to')
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($meta) 0 16 16
275 unless Scalar::Util::blessed( $meta );
276 16 ▶ Carp::confess('[ARGS] You must specify a least one roles to apply as an ARRAY ref')
Branch: 1 branch
BranchTrueFalseTotal
unless $roles and ref $roles eq "ARRAY" and scalar @$roles != 0 0 16 16
Condition33%2/6
$roles and ref $roles eq "ARRAY" and scalar @$roles != 0
!ll&&!rl&&r
0016
$roles and ref $roles eq "ARRAY"
!ll&&!rl&&r
0016
MC/DC0%0/3
$roles and ref $roles eq "ARRAY" and scalar @$roles != 0
$roles ref $roles eq "ARRAY" scalar @$roles != 0
Truth table25%1/4
A and B and C
A= $rolesB= ref $roles eq "ARRAY"C= scalar @$roles != 0
ABCresult
0XX0
10X0
1100
1111
277 unless $roles && ref $roles eq 'ARRAY' && scalar( @$roles ) != 0;
278  
279 16 foreach my $r ( $meta->roles ) {
280 Carp::confess("[ERROR] Could not find role ($_) in the set of roles in $meta (" . $meta->name . ")")
281 18 ▶ unless scalar grep { $r eq $_ } @$roles;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar grep({$r eq $_;} @$roles) 0 18 18
282 }
283  
284 16 my @meta_roles = map { MOP::Role->new( name => $_ ) } @$roles;
285  
286 my (
287 16 $slots,
288 $slot_conflicts
289 ) = COMPOSE_ALL_ROLE_SLOTS( @meta_roles );
290  
291 16 ▶ Carp::confess("[CONFLICT] There should be no conflicting slots when composing (" . (join ', ' => @$roles) . ") into (" . $meta->name . ")")
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %$slot_conflicts 0 16 16
292 if scalar keys %$slot_conflicts;
293  
294 16 foreach my $name ( keys %$slots ) {
295 # if we have a slot already by that name ...
296 0 ▶ Carp::confess("[CONFLICT] Role Conflict, cannot compose slot ($name) into (" . $meta->name . ") because ($name) already exists")
Branch: 1 branch
BranchTrueFalseTotal
if $meta->has_slot($name) 0 0 0
297 if $meta->has_slot( $name );
298 # otherwise alias it ...
299 0 $meta->alias_slot( $name, $slots->{ $name } );
300 }
301  
302 my (
303 16 $methods,
304 $method_conflicts,
305 $required_methods
306 ) = COMPOSE_ALL_ROLE_METHODS( @meta_roles );
307  
308 Carp::confess("[CONFLICT] There should be no conflicting methods when composing (" . (join ', ' => @$roles) . ") into (" . $meta->name . ") but instead we found (" . (join ', ' => keys %$method_conflicts) . ")")
309 if (scalar keys %$method_conflicts) # do we have any conflicts ...
310 # and the conflicts are not satisfied by the composing item ...
311 0 ▶ && (scalar grep { !$meta->has_method( $_ ) } keys %$method_conflicts);
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %$method_conflicts and scalar grep({not $meta->has_method($_);} keys %$method_conflicts) 0 16 16
Condition50%1/2
scalar keys %$method_conflicts and scalar grep({not $meta->has_method($_);} keys %$method_conflicts)
!ll
160
MC/DC0%0/1
scalar keys %$method_conflicts and scalar grep({not $meta->has_method($_);} keys %$method_conflicts)
scalar keys %$method_conflicts
Truth table50%1/2
A
A= scalar keys %$method_conflicts
Aresult
00
11
312  
313 # check the required method set and
314 # see if what we are composing into
315 # happens to fulfill them
316 16 foreach my $name ( keys %$required_methods ) {
317 3 ▶ delete $required_methods->{ $name }
Branch: 1 branch
BranchTrueFalseTotal
if $meta->name->can($name) 3 0 3
318 if $meta->name->can( $name );
319 }
320  
321 16 ▶ Carp::confess("[CONFLICT] There should be no required methods when composing (" . (join ', ' => @$roles) . ") into (" . $meta->name . ") but instead we found (" . (join ', ' => keys %$required_methods) . ")")
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %$required_methods 0 16 16
322 if scalar keys %$required_methods; # do we have required methods ...
323  
324 16 foreach my $name ( keys %$methods ) {
325 # if we have a method already by that name ...
326 419 ▶ next if $meta->has_method( $name );
Branch: 1 branch
BranchTrueFalseTotal
if $meta->has_method($name) 0 419 419
327 # otherwise, alias it ...
328 419 $meta->alias_method( $name, $methods->{ $name } );
329 }
330  
331 # if we still have keys in $required, it is
332 # because we are a role (class would have
333 # died above), so we can just stuff in the
334 # required methods ...
335 16 $meta->add_required_method( $_ ) for keys %$required_methods;
336  
337 16 return;
338 }
339  
340 16 ▶sub COMPOSE_ALL_ROLE_SLOTS {
Subroutine COMPOSE_ALL_ROLE_SLOTS: called CC=6 SCAR=26.5
Pod undocumented
341 16 my @roles = @_;
342  
343 16 ▶ Carp::confess('[ARGS] You must specify a least one role to compose slots in')
Branch: 1 branch
BranchTrueFalseTotal
if scalar @roles == 0 0 16 16
344 if scalar( @roles ) == 0;
345  
346 16 my (%slots, %conflicts);
347  
348 16 foreach my $role ( @roles ) {
349 18 foreach my $slot ( $role->slots ) {
350 0 my $name = $slot->name;
351 # if we have one already, but
352 # it is not the same refaddr ...
353 0 ▶ if ( exists $slots{ $name } && $slots{ $name } != $slot->initializer ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $slots{$name} and $slots{$name} != $slot->initializer) { } 0 0 0
Condition0%0/3
exists $slots{$name} and $slots{$name} != $slot->initializer
!ll&&!rl&&r
000
MC/DC0%0/2
exists $slots{$name} and $slots{$name} != $slot->initializer
exists $slots{$name} $slots{$name} != $slot->initializer
Truth table0%0/3
A and B
A= exists $slots{$name}B= $slots{$name} != $slot->initializer
ABresult
0X0
100
111
354 # mark it as a conflict ...
355 0 $conflicts{ $name } = undef;
356 # and remove it from our slot set ...
357 0 delete $slots{ $name };
358 }
359 # if we don't have it already ...
360 else {
361 # make a note of it
362 0 $slots{ $name } = $slot->initializer;
363 }
364 }
365 }
366  
367 16 return \%slots, \%conflicts;
368 }
369  
370  
371 # TODO:
372 # We should track the name of the role
373 # where the required method was composed
374 # from, as well as the two classes in
375 # which a method conflicted.
376 # - SL
377 16 ▶sub COMPOSE_ALL_ROLE_METHODS {
Subroutine COMPOSE_ALL_ROLE_METHODS: called CC=10 SCAR=26.8
Pod undocumented
378 16 my @roles = @_;
379  
380 16 ▶ Carp::confess('[ARGS] You must specify a least one role to compose methods in')
Branch: 1 branch
BranchTrueFalseTotal
if scalar @roles == 0 0 16 16
381 if scalar( @roles ) == 0;
382  
383 16 my (%methods, %conflicts, %required);
384  
385 # flatten the set of required methods ...
386 16 foreach my $r ( @roles ) {
387 18 foreach my $m ( $r->required_methods ) {
388 3 $required{ $m->name } = undef;
389 }
390 }
391  
392 # for every role ...
393 16 foreach my $r ( @roles ) {
394 # and every method in that role ...
395 18 foreach my $m ( $r->methods ) {
396 419 my $name = $m->name;
397 # if we have already seen the method,
398 # but it is not the same refaddr
399 # it is a conflict, which means:
400 419 ▶ if ( exists $methods{ $name } && $methods{ $name } != $m->body ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $methods{$name} and $methods{$name} != $m->body) { } 0 419 419
Condition33%1/3
exists $methods{$name} and $methods{$name} != $m->body
!ll&&!rl&&r
41900
MC/DC0%0/2
exists $methods{$name} and $methods{$name} != $m->body
exists $methods{$name} $methods{$name} != $m->body
Truth table33%1/3
A and B
A= exists $methods{$name}B= $methods{$name} != $m->body
ABresult
0X0
100
111
401 # we need to add it to our required-method map
402 0 $required{ $name } = undef;
403 # and note that it is also a conflict ...
404 0 $conflicts{ $name } = undef;
405 # and remove it from our method map
406 0 delete $methods{ $name };
407 }
408 # if we haven't seen the method ...
409 else {
410 # add it to the method map
411 419 $methods{ $name } = $m->body;
412 # and remove it from the required-method map
413 delete $required{ $name }
414 # if it actually exists in it, and ...
415 if exists $required{ $name }
416 # is not also a conflict ...
417 419 ▶ && !exists $conflicts{ $name };
Branch: 1 branch
BranchTrueFalseTotal
if exists $required{$name} and not exists $conflicts{$name} 0 419 419
Condition33%1/3
exists $required{$name} and not exists $conflicts{$name}
!ll&&!rl&&r
41900
MC/DC0%0/2
exists $required{$name} and not exists $conflicts{$name}
exists $required{$name} not exists $conflicts{$name}
Truth table33%1/3
A and not(B)
A= exists $required{$name}B= not exists $conflicts{$name}
ABresult
0X0
100
111
418 }
419 }
420 }
421  
422 #use Data::Dumper;
423 #warn Dumper [ [ map { $_->name } @roles ], \%methods, \%conflicts, \%required ];
424  
425 16 return \%methods, \%conflicts, \%required;
426 }
427  
428 35 1;
429  
430 __END__
« MOP/Util.pm ↑ Summary MOP/Role.pm »