MOP/Internal/Util.pm

filter: Statementstmt 93.3% 183 / 196 Branchbran 54.7% 58 / 106 Conditioncond 44.0% 22 / 50 MC/DCmcdc 3.5% 1 / 28 Subroutinesub 100% 35 / 35 Podpod 0.0% 0 / 19 totaltotal 68.8% 299 / 434 CC 84 SCAR 23.6
CC
84
Cov
75%
APPLY_ROLES
33.1
COMPOSE_ALL_ROLE_METHODS
14.6
COMPOSE_ALL_ROLE_SLOTS
14.2
CRAP 10.6
« 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::Name (); # 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.11';
17 35 our $AUTHORITY = 'cpan:STEVAN';
18  
19 ## ------------------------------------------------------------------
20 ## Basic Glob access
21 ## ------------------------------------------------------------------
22  
23 sub IS_VALID_MODULE_NAME {
24 112 ▶ my ($name) = @_;
Subroutine IS_VALID_MODULE_NAME: called CC=1 SCAR=0.0
Pod undocumented
25 112 $name =~ /[A-Z_a-z][0-9A-Z_a-z]*(?:::[0-9A-Z_a-z]+)*/
26 }
27  
28 sub IS_STASH_REF {
29 4 ▶ my ($stash) = @_;
Subroutine IS_STASH_REF: called CC=3 SCAR=11.0
Pod undocumented
30 4 ▶ Carp::croak('[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 sub GET_NAME {
39 1117 ▶ my ($stash) = @_;
Subroutine GET_NAME: called CC=2 SCAR=7.1
Pod undocumented
40 1117 ▶ Carp::croak('[ARGS] You must specify a stash')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $stash 0 1117 1117
41 unless defined $stash;
42 1117 B::svref_2object( $stash )->NAME
43 }
44  
45 sub GET_STASH_NAME {
46 81 ▶ my ($stash) = @_;
Subroutine GET_STASH_NAME: called CC=2 SCAR=7.1
Pod undocumented
47 81 ▶ Carp::croak('[ARGS] You must specify a stash')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $stash 0 81 81
48 unless defined $stash;
49 81 B::svref_2object( $stash )->STASH->NAME
50 }
51  
52 sub GET_GLOB_NAME {
53 546 ▶ my ($stash) = @_;
Subroutine GET_GLOB_NAME: called CC=2 SCAR=7.1
Pod undocumented
54 546 ▶ Carp::croak('[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 sub GET_GLOB_STASH_NAME {
60 605 ▶ my ($stash) = @_;
Subroutine GET_GLOB_STASH_NAME: called CC=2 SCAR=7.1
Pod undocumented
61 605 ▶ Carp::croak('[ARGS] You must specify a stash')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $stash 0 605 605
62 unless defined $stash;
63 605 B::svref_2object( $stash )->GV->STASH->NAME
64 }
65  
66 sub GET_GLOB_SLOT {
67 1587 ▶ my ($stash, $name, $slot) = @_;
Subroutine GET_GLOB_SLOT: called CC=8 SCAR=21.4
Pod undocumented
68  
69 1587 ▶ Carp::croak('[ARGS] You must specify a stash')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $stash 0 1587 1587
70 unless defined $stash;
71 1587 ▶ Carp::croak('[ARGS] You must specify a name')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 1587 1587
72 unless defined $name;
73 1587 ▶ Carp::croak('[ARGS] You must specify a slot')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $slot 0 1587 1587
74 unless defined $slot;
75  
76 # do my best to not autovivify, and
77 # return undef if not
78 1587 ▶ return unless exists $stash->{ $name };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $stash->{$name} 150 1437 1587
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 1437 ▶ 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") 1437 0 1437
Condition83%5/6
$slot eq "CODE" and $stash->{$name} eq -1 or ref $stash->{$name} ne "GLOB"
l!l&&r!l&&!r
314340
$slot eq "CODE" and $stash->{$name} eq -1
!ll&&!rl&&r
13912953
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 1437 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 1437 return *{ $stash->{ $name } }{ $slot };
93 }
94  
95 sub SET_GLOB_SLOT {
96 4 ▶ my ($stash, $name, $value_ref) = @_;
Subroutine SET_GLOB_SLOT: called CC=4 SCAR=14.0
Pod undocumented
97  
98 4 ▶ Carp::croak('[ARGS] You must specify a stash')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $stash 0 4 4
99 unless defined $stash;
100 4 ▶ Carp::croak('[ARGS] You must specify a name')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 4 4
101 unless defined $name;
102 4 ▶ Carp::croak('[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 sub ADD_UNITCHECK_HOOK {
123 5 ▶ my ($cv) = @_;
Subroutine ADD_UNITCHECK_HOOK: called CC=4 SCAR=15.6
Pod undocumented
124 5 ▶ Carp::croak('[ARGS] You must specify a CODE reference')
Branch: 1 branch
BranchTrueFalseTotal
unless $cv 0 5 5
125 unless $cv;
126 5 ▶ Carp::croak('[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 sub CAN_COERCE_TO_CODE_REF {
136 109 ▶ my ($object) = @_;
Subroutine CAN_COERCE_TO_CODE_REF: called CC=5 SCAR=19.4
Pod undocumented
137 109 ▶ return 0 unless $object && Scalar::Util::blessed( $object );
Branch: 1 branch
BranchTrueFalseTotal
unless $object and builtin::blessed($object) 83 26 109
Condition66%2/3
$object and builtin::blessed($object)
!ll&&!rl&&r
08326
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 sub IS_CV_NULL {
146 926 ▶ my ($cv) = @_;
Subroutine IS_CV_NULL: called CC=5 SCAR=19.4
Pod undocumented
147 926 ▶ Carp::croak('[ARGS] You must specify a CODE reference')
Branch: 1 branch
BranchTrueFalseTotal
unless $cv 0 926 926
148 unless $cv;
149 926 ▶ Carp::croak('[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 926 926
Condition33%2/6
$cv and ref $cv eq "CODE" or CAN_COERCE_TO_CODE_REF($cv)
l!l&&r!l&&!r
92600
$cv and ref $cv eq "CODE"
!ll&&!rl&&r
00926
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 926 return Sub::Metadata::sub_body_type( $cv ) eq 'UNDEF';
153 }
154  
155 sub DOES_GLOB_HAVE_NULL_CV {
156 160 ▶ my ($glob) = @_;
Subroutine DOES_GLOB_HAVE_NULL_CV: called CC=4 SCAR=13.9
Pod undocumented
157 160 ▶ Carp::croak('[ARGS] You must specify a GLOB')
Branch: 1 branch
BranchTrueFalseTotal
unless $glob 0 160 160
158 unless $glob;
159 # NOTE:
160 # If the glob eq -1 that means it may well be a null sub
161 # this seems to be some kind of artifact of an optimization
162 # perhaps, I really don't know, it is odd. It should not
163 # need to be dealt with in XS, it seems to be a Perl language
164 # level thing.
165 # - SL
166 160 ▶ return 1 if $glob eq '-1';
Branch: 1 branch
BranchTrueFalseTotal
if $glob eq -1 1 159 160
167 # next lets see if we have a CODE slot ...
168 159 ▶ if ( my $code = *{ $glob }{CODE} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $code = *{$glob;}{"CODE"}) 136 23 159
169 136 return Sub::Metadata::sub_body_type( $code ) eq 'UNDEF';
170 }
171 # if we had no CODE slot, it can't be a NULL CV ...
172 23 return 0;
173 }
174  
175 sub CREATE_NULL_CV {
176 3 ▶ my ($in_pkg, $name) = @_;
Subroutine CREATE_NULL_CV: called CC=4 SCAR=14.0
Pod undocumented
177 3 ▶ Carp::croak('[ARGS] You must specify a package name')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $in_pkg 0 3 3
178 unless defined $in_pkg;
179 3 ▶ Carp::croak('[ARGS] You must specify a name')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 3 3
180 unless defined $name;
181 # this just tries to eval the NULL CV into
182 # place, it is ugly, but works for now
183 3 ▶ eval "sub ${in_pkg}::${name}; 1;" or do { Carp::croak($@) };
Branch: 1 branch
BranchTrueFalseTotal
eval "sub ${in_pkg}::$name; 1;" or do { Carp::croak($@) } 1 2 3
184 2 return;
185 }
186  
187 sub SET_COMP_STASH_FOR_CV {
188 3 ▶ my ($cv, $in_pkg) = @_;
Subroutine SET_COMP_STASH_FOR_CV: called CC=6 SCAR=19.3
Pod undocumented
189 3 ▶ Carp::croak('[ARGS] You must specify a CODE reference')
Branch: 1 branch
BranchTrueFalseTotal
unless $cv 0 3 3
190 unless $cv;
191 3 ▶ Carp::croak('[ARGS] You must specify a package name')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $in_pkg 0 3 3
192 unless defined $in_pkg;
193 3 ▶ Carp::croak('[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
194 unless $cv && ref $cv eq 'CODE'
195 || CAN_COERCE_TO_CODE_REF( $cv );
196 3 Sub::Metadata::mutate_sub_package( $cv, $in_pkg );
197 }
198  
199 sub INSTALL_CV {
200 421 ▶ my ($in_pkg, $name, $cv, %opts) = @_;
Subroutine INSTALL_CV: called CC=8 SCAR=22.0
Pod undocumented
201  
202 421 ▶ Carp::croak('[ARGS] You must specify a package name')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $in_pkg 0 421 421
203 unless defined $in_pkg;
204 421 ▶ Carp::croak('[ARGS] You must specify a name')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 421 421
205 unless defined $name;
206 421 ▶ Carp::croak('[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 421 421
Condition33%2/6
$cv and ref $cv eq "CODE" or CAN_COERCE_TO_CODE_REF($cv)
l!l&&r!l&&!r
42100
$cv and ref $cv eq "CODE"
!ll&&!rl&&r
00421
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
207 unless $cv && ref $cv eq 'CODE'
208 || CAN_COERCE_TO_CODE_REF( $cv );
209 Carp::croak("[ARGS] You must specify a boolean value for `set_subname` option")
210 421 ▶ if not exists $opts{set_subname};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts{"set_subname"} 0 421 421
211  
212 {
213 35 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
214 35 ▶ no warnings 'once', 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
215  
216 421 my $fullname = $in_pkg.'::'.$name;
217 421 ▶ *{$fullname} = $opts{set_subname} ? Sub::Name::subname($fullname, $cv) : $cv;
Branch: 1 branch
BranchTrueFalseTotal
$opts{'set_subname'} ? : 1 420 421
218 }
219 421 return;
220 }
221  
222 sub REMOVE_CV_FROM_GLOB {
223 3 ▶ my ($stash, $name) = @_;
Subroutine REMOVE_CV_FROM_GLOB: called CC=8 SCAR=21.1
Pod undocumented
224  
225 3 ▶ Carp::croak('[ARGS] You must specify a stash')
Branch: 1 branch
BranchTrueFalseTotal
unless $stash and ref $stash eq "HASH" 0 3 3
Condition33%1/3
$stash and ref $stash eq "HASH"
!ll&&!rl&&r
003
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
226 unless $stash && ref $stash eq 'HASH';
227 3 ▶ Carp::croak('[ARGS] You must specify a name')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 3 3
228 unless defined $name;
229  
230 # find the glob we are looking for
231 # which might not exist, in which
232 # case we do nothing ....
233 3 ▶ if ( my $glob = $stash->{ $name } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $glob = $stash->{$name}) 3 0 3
234 # once we find it, extract all the
235 # slots we need, note the missing
236 # CODE slot since we don't need
237 # that in our new glob ...
238 3 my %to_save;
239 3 foreach my $slot (qw[ SCALAR ARRAY HASH FORMAT IO ]) {
240 15 ▶ if ( my $val = *{ $glob }{ $slot } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $val = *{$glob;}{$slot}) 3 12 15
241 3 $to_save{ $slot } = $val;
242 }
243 }
244 # replace the old glob with a new one ...
245 3 $stash->{ $name } = Symbol::gensym();
246 # now go about constructing our new
247 # glob by restoring the other slots
248 {
249 35 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
250 35 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
251 # get the name of the stash, we could have
252 # passed this in, but it is easy to get in
253 # XS, and so we can punt that down the road
254 # for the time being
255 3 my $pkg = B::svref_2object( $stash )->NAME;
256 3 foreach my $type ( keys %to_save ) {
257 3 *{ $pkg . '::' . $name } = $to_save{ $type };
258 }
259 }
260 }
261 # ... the end
262 3 return;
263 }
264  
265 ## ------------------------------------------------------------------
266 ## Role application and composition
267 ## ------------------------------------------------------------------
268  
269 sub APPLY_ROLES {
270 16 ▶ my ($meta, $roles) = @_;
Subroutine APPLY_ROLES: called CC=18 SCAR=35.0
Pod undocumented
271  
272 16 ▶ Carp::croak('[ARGS] You must specify a metaclass to apply roles to')
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($meta) 0 16 16
273 unless Scalar::Util::blessed( $meta );
274 16 ▶ Carp::croak('[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
275 unless $roles && ref $roles eq 'ARRAY' && scalar( @$roles ) != 0;
276  
277 16 foreach my $r ( $meta->roles ) {
278 Carp::croak("[ERROR] Could not find role ($_) in the set of roles in $meta (" . $meta->name . ")")
279 18 ▶ unless scalar grep { $r eq $_ } @$roles;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar grep({$r eq $_;} @$roles) 0 18 18
280 }
281  
282 16 my @meta_roles = map { MOP::Role->new( name => $_ ) } @$roles;
283  
284 my (
285 16 $slots,
286 $slot_conflicts
287 ) = COMPOSE_ALL_ROLE_SLOTS( @meta_roles );
288  
289 16 ▶ Carp::croak("[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
290 if scalar keys %$slot_conflicts;
291  
292 16 foreach my $name ( keys %$slots ) {
293 # if we have a slot already by that name ...
294 0 ▶ Carp::croak("[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
295 if $meta->has_slot( $name );
296 # otherwise alias it ...
297 0 $meta->alias_slot( $name, $slots->{ $name } );
298 }
299  
300 my (
301 16 $methods,
302 $method_conflicts,
303 $required_methods
304 ) = COMPOSE_ALL_ROLE_METHODS( @meta_roles );
305  
306 Carp::croak("[CONFLICT] There should be no conflicting methods when composing (" . (join ', ' => @$roles) . ") into (" . $meta->name . ") but instead we found (" . (join ', ' => keys %$method_conflicts) . ")")
307 if (scalar keys %$method_conflicts) # do we have any conflicts ...
308 # and the conflicts are not satisfied by the composing item ...
309 16 ▶ && (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
310  
311 # check the required method set and
312 # see if what we are composing into
313 # happens to fulfill them
314 16 foreach my $name ( keys %$required_methods ) {
315 3 ▶ delete $required_methods->{ $name }
Branch: 1 branch
BranchTrueFalseTotal
if $meta->name->can($name) 3 0 3
316 if $meta->name->can( $name );
317 }
318  
319 16 ▶ Carp::croak("[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
320 if scalar keys %$required_methods; # do we have required methods ...
321  
322 16 foreach my $name ( keys %$methods ) {
323 # if we have a method already by that name ...
324 419 ▶ next if $meta->has_method( $name );
Branch: 1 branch
BranchTrueFalseTotal
if $meta->has_method($name) 0 419 419
325 # otherwise, alias it ...
326 419 $meta->alias_method( $name, $methods->{ $name } );
327 }
328  
329 # if we still have keys in $required, it is
330 # because we are a role (class would have
331 # died above), so we can just stuff in the
332 # required methods ...
333 16 $meta->add_required_method( $_ ) for keys %$required_methods;
334  
335 16 return;
336 }
337  
338 sub COMPOSE_ALL_ROLE_SLOTS {
339 16 ▶ my @roles = @_;
Subroutine COMPOSE_ALL_ROLE_SLOTS: called CC=6 SCAR=26.5
Pod undocumented
340  
341 16 ▶ Carp::croak('[ARGS] You must specify a least one role to compose slots in')
Branch: 1 branch
BranchTrueFalseTotal
if scalar @roles == 0 0 16 16
342 if scalar( @roles ) == 0;
343  
344 16 my (%slots, %conflicts);
345  
346 16 foreach my $role ( @roles ) {
347 18 foreach my $slot ( $role->slots ) {
348 0 my $name = $slot->name;
349 # if we have one already, but
350 # it is not the same refaddr ...
351 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
352 # mark it as a conflict ...
353 0 $conflicts{ $name } = undef;
354 # and remove it from our slot set ...
355 0 delete $slots{ $name };
356 }
357 # if we don't have it already ...
358 else {
359 # make a note of it
360 0 $slots{ $name } = $slot->initializer;
361 }
362 }
363 }
364  
365 16 return \%slots, \%conflicts;
366 }
367  
368  
369 # TODO:
370 # We should track the name of the role
371 # where the required method was composed
372 # from, as well as the two classes in
373 # which a method conflicted.
374 # - SL
375 sub COMPOSE_ALL_ROLE_METHODS {
376 16 ▶ my @roles = @_;
Subroutine COMPOSE_ALL_ROLE_METHODS: called CC=10 SCAR=26.8
Pod undocumented
377  
378 16 ▶ Carp::croak('[ARGS] You must specify a least one role to compose methods in')
Branch: 1 branch
BranchTrueFalseTotal
if scalar @roles == 0 0 16 16
379 if scalar( @roles ) == 0;
380  
381 16 my (%methods, %conflicts, %required);
382  
383 # flatten the set of required methods ...
384 16 foreach my $r ( @roles ) {
385 18 foreach my $m ( $r->required_methods ) {
386 3 $required{ $m->name } = undef;
387 }
388 }
389  
390 # for every role ...
391 16 foreach my $r ( @roles ) {
392 # and every method in that role ...
393 18 foreach my $m ( $r->methods ) {
394 419 my $name = $m->name;
395 # if we have already seen the method,
396 # but it is not the same refaddr
397 # it is a conflict, which means:
398 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
399 # we need to add it to our required-method map
400 0 $required{ $name } = undef;
401 # and note that it is also a conflict ...
402 0 $conflicts{ $name } = undef;
403 # and remove it from our method map
404 0 delete $methods{ $name };
405 }
406 # if we haven't seen the method ...
407 else {
408 # add it to the method map
409 419 $methods{ $name } = $m->body;
410 # and remove it from the required-method map
411 delete $required{ $name }
412 # if it actually exists in it, and ...
413 if exists $required{ $name }
414 # is not also a conflict ...
415 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
416 }
417 }
418 }
419  
420 #use Data::Dumper;
421 #warn Dumper [ [ map { $_->name } @roles ], \%methods, \%conflicts, \%required ];
422  
423 16 return \%methods, \%conflicts, \%required;
424 }
425  
426 35 1;
427  
428 __END__
« MOP/Util.pm ↑ Summary MOP/Role.pm »