MOP/Role.pm

filter: Statementstmt 100% 314 / 314 Branchbran 82.3% 163 / 198 Conditioncond 53.7% 29 / 54 MC/DCmcdc 31.0% 9 / 29 Subroutinesub 100% 45 / 45 Podpod 100% 37 / 37 totaltotal 88.1% 597 / 677 CC 122 SCAR 16.8
CC
122
Cov
89%
delete_method
8.2
get_method
8.0
add_slot
7.5
CRAP 5.3
« MOP/Internal/Util.pm ↑ Summary MOP/Slot.pm »
1 package MOP::Role;
2 # ABSTRACT: A representation of a role
3  
4 28 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 28 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 28 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 28 ▶use MOP::Method;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 28 ▶use MOP::Slot;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 28 ▶use MOP::Internal::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 28 our $VERSION = '0.14';
15 28 our $AUTHORITY = 'cpan:STEVAN';
16  
17 28 ▶use parent 'UNIVERSAL::Object::Immutable';
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 115 ▶sub BUILDARGS {
Subroutine BUILDARGS: called CC=7 SCAR=19.6
Pod documented
20 115 my $class = shift;
21 115 my %args;
22  
23 115 ▶ if ( scalar( @_ ) == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ == 1) { } 16 99 115
24 16 ▶ if ( ref $_[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0]) { } 4 12 16
25 4 ▶ if ( ref $_[0] eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq "HASH") 4 0 4
26 4 ▶ if ( MOP::Internal::Util::IS_STASH_REF( $_[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (MOP::Internal::Util::IS_STASH_REF($_[0])) { } 2 2 4
27 # if it is a stash, grab the name
28 2 %args = (
29 name => MOP::Internal::Util::GET_NAME( $_[0] ),
30 stash => $_[0]
31 );
32 }
33 else {
34 # just plain old HASH ref ...
35 2 %args = %{ $_[0] };
36 }
37 }
38 }
39 else {
40 # assume it is a single package name ...
41 12 %args = ( name => $_[0] );
42 }
43 }
44 else {
45 # assume we got key/value pairs ...
46 99 %args = @_;
47 }
48  
49 Carp::confess('[ARGS] You must specify a package name')
50 115 ▶ unless $args{name};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"name"} 0 115 115
51  
52 Carp::confess('[ARGS] You must specify a valid package name, not `'.$_[0].'`')
53 115 ▶ unless MOP::Internal::Util::IS_VALID_MODULE_NAME( $args{name} );
Branch: 1 branch
BranchTrueFalseTotal
unless MOP::Internal::Util::IS_VALID_MODULE_NAME($args{"name"}) 0 115 115
54  
55 115 return \%args;
56 }
57  
58 115 ▶sub CREATE {
Subroutine CREATE: called CC=2 SCAR=6.9
Pod documented
59 115 my ($class, $args) = @_;
60  
61 # intiialize the stash ...
62 115 my $stash = $args->{stash};
63  
64 # if we have it, otherwise get it ...
65 115 ▶ unless ( $stash ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($stash) 113 2 115
66 # get a ref to to the stash itself ...
67 28 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
68 113 $stash = \%{ $args->{name} . '::' };
69 }
70 # and then a ref to that, because we
71 # eventually will need to bless it and
72 # we do not want to bless the actual
73 # stash because that persists beyond
74 # the lifetime of this object, so we
75 # bless a ref of a ref then ...
76 115 return \$stash;
77 }
78  
79 # stash
80  
81 2104 ▶sub stash {
Subroutine stash: called CC=1 SCAR=0.0
Pod documented
82 2104 my ($self) = @_;
83 2104 return $$self; # returns the direct HASH ref of the stash
84 }
85  
86 # identity
87  
88 1133 ▶sub name {
Subroutine name: called CC=1 SCAR=0.0
Pod documented
89 1133 my ($self) = @_;
90 1133 return MOP::Internal::Util::GET_NAME( $self->stash );
91 }
92  
93 8 ▶sub version {
Subroutine version: called CC=2 SCAR=7.0
Pod documented
94 8 my ($self) = @_;
95 8 my $version = MOP::Internal::Util::GET_GLOB_SLOT( $self->stash, 'VERSION', 'SCALAR' );
96 8 ▶ return unless $version;
Branch: 1 branch
BranchTrueFalseTotal
unless $version 0 8 8
97 8 return $$version;
98 }
99  
100 8 ▶sub authority {
Subroutine authority: called CC=2 SCAR=7.0
Pod documented
101 8 my ($self) = @_;
102 8 my $authority = MOP::Internal::Util::GET_GLOB_SLOT( $self->stash, 'AUTHORITY', 'SCALAR' );
103 8 ▶ return unless $authority;
Branch: 1 branch
BranchTrueFalseTotal
unless $authority 0 8 8
104 8 return $$authority;
105 }
106  
107 # other roles
108  
109 161 ▶sub roles {
Subroutine roles: called CC=2 SCAR=6.9
Pod documented
110 161 my ($self) = @_;
111 161 my $does = MOP::Internal::Util::GET_GLOB_SLOT( $self->stash, 'DOES', 'ARRAY' );
112 161 ▶ return unless $does;
Branch: 1 branch
BranchTrueFalseTotal
unless $does 109 52 161
113 52 return @$does;
114 }
115  
116 1 ▶sub set_roles {
Subroutine set_roles: called CC=2 SCAR=7.0
Pod documented
117 1 my ($self, @roles) = @_;
118 1 ▶ Carp::confess('[ARGS] You must specify at least one role')
Branch: 1 branch
BranchTrueFalseTotal
if scalar @roles == 0 0 1 1
119 if scalar( @roles ) == 0;
120 1 MOP::Internal::Util::SET_GLOB_SLOT( $self->stash, 'DOES', \@roles );
121 1 return;
122 }
123  
124 23 ▶sub does_role {
Subroutine does_role: called CC=7 SCAR=19.5
Pod documented
125 23 my ($self, $to_test) = @_;
126  
127 23 ▶ Carp::confess('[ARGS] You must specify a role')
Branch: 1 branch
BranchTrueFalseTotal
unless $to_test 0 23 23
128 unless $to_test;
129  
130 23 my @roles = $self->roles;
131  
132 # no roles, will never match ...
133 23 ▶ return 0 unless @roles;
Branch: 1 branch
BranchTrueFalseTotal
unless @roles 6 17 23
134  
135 # try the simple way first ...
136 17 foreach my $role ( @roles ) {
137 20 ▶ return 1 if $role eq $to_test;
Branch: 1 branch
BranchTrueFalseTotal
if $role eq $to_test 13 7 20
138 }
139  
140 # then try the harder way next ...
141 4 foreach my $role ( @roles ) {
142 4 ▶ return 1
Branch: 1 branch
BranchTrueFalseTotal
if "MOP::Role"->new("name", $role)->does_role($to_test) 3 1 4
143 if MOP::Role->new( name => $role )
144 ->does_role( $to_test );
145 }
146  
147 # oh well ...
148 1 return 0;
149 }
150  
151 ## Methods
152  
153 # get them all; regular, aliased & required
154 48 ▶sub all_methods {
Subroutine all_methods: called CC=3 SCAR=11.0
Pod documented
155 48 my $stash = $_[0]->stash;
156 48 my @methods;
157 48 foreach my $candidate ( keys %$stash ) {
158 1230 ▶ if ( my $code = MOP::Internal::Util::GET_GLOB_SLOT( $stash, $candidate, 'CODE' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $code = MOP::Internal::Util::GET_GLOB_SLOT($stash, $candidate, "CODE")) 944 286 1230
159 944 push @methods => MOP::Method->new( body => $code );
160 }
161 }
162 48 return @methods;
163 }
164  
165 # just the local non-required methods
166 21 ▶sub methods {
Subroutine methods: called CC=6 SCAR=18.0
Pod documented
167 21 my $self = shift;
168 21 my $class = $self->name;
169 21 my @roles = $self->roles;
170  
171 21 my @methods;
172 21 foreach my $method ( $self->all_methods ) {
173 # if the method is required, we don't want it
174 464 ▶ next if $method->is_required;
Branch: 1 branch
BranchTrueFalseTotal
if $method->is_required 5 459 464
175  
176 # if the method is not originally from the
177 # class, then we probably don't want it ...
178 459 ▶ if ( $method->origin_stash ne $class ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method->origin_stash ne $class) 3 456 459
179 # if our class has roles, then non-local
180 # methods *might* be valid, so ...
181  
182 # if we don't have roles, then
183 # it can't be valid, so we don't
184 # want it
185 3 ▶ next unless @roles;
Branch: 1 branch
BranchTrueFalseTotal
unless @roles 0 3 3
186  
187 # if we do have roles, but our
188 # method was not aliased from one
189 # of them, then we don't want it.
190 3 ▶ next unless $method->was_aliased_from( @roles );
Branch: 1 branch
BranchTrueFalseTotal
unless $method->was_aliased_from(@roles) 0 3 3
191  
192 # if we are here then we have
193 # a non-required method that is
194 # not from the local class, it
195 # has roles and was aliased from
196 # one of them, which means we want
197 # to keep it, so we let it fall through
198 }
199  
200 # if we are here then we have
201 # a non-required method that is
202 # either from the local class,
203 # or is not from a local class,
204 # but has fallen through our
205 # conditional above.
206  
207 459 push @methods => $method;
208 }
209  
210 21 return @methods;
211 }
212  
213 # just the non-local non-required methods
214 4 ▶sub aliased_methods {
Subroutine aliased_methods: called CC=2 SCAR=6.9
Pod documented
215 4 my $self = shift;
216 4 my $class = $self->name;
217 4 ▶ return grep { (!$_->is_required) && $_->origin_stash ne $class } $self->all_methods
Condition100%3/3
!$_->is_required && $_->origin_stash ne $class
!ll&&!rl&&r
242
MC/DC100%2/2
!$_->is_required && $_->origin_stash ne $class
!$_->is_required $_->origin_stash ne $class
Truth table100%3/3
not(A) && B
A= !$_->is_requiredB= $_->origin_stash ne $class
ABresult
0X0
100
111
218 }
219  
220 # just the required methods (locality be damned)
221 # NOTE:
222 # We don't care where are required method comes from
223 # just that one exists, so aliasing is not part of the
224 # criteria here.
225 # - SL
226 20 ▶sub required_methods {
Subroutine required_methods: called CC=1 SCAR=0.0
Pod documented
227 20 my $self = shift;
228 20 return grep { $_->is_required } $self->all_methods
229 }
230  
231 # required methods
232  
233 # NOTE:
234 # there is no real heavy need to use the MOP::Method API
235 # below because 1) it is not needed, and 2) the MOP::Method
236 # API is really just an information shim, it does not perform
237 # much in the way of actions. From my point of view, the below
238 # operations are mostly stash manipulation functions and so
239 # therefore belong here in the continuim of responsibility/
240 # ownership.
241 #
242 ## The only argument that could likely be made is for the
243 ## MOP::Method API to handle creating the NULL CV for the
244 ## add_required_method, but that would require us to pass in
245 ## a MOP::Method instance, which would be silly since we never
246 ## need it anyway.
247 #
248 # - SL
249  
250 39 ▶sub has_required_method {
Subroutine has_required_method: called CC=3 SCAR=11.0
Pod documented
251 39 my $stash = $_[0]->stash;
252 39 my $name = $_[1];
253  
254 39 ▶ Carp::confess('[ARGS] You must specify the name of the required method to look for')
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 39 39
255 unless $name;
256  
257 39 ▶ return 0 unless exists $stash->{ $name };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $stash->{$name} 3 36 39
258 36 return MOP::Internal::Util::DOES_GLOB_HAVE_NULL_CV( $stash->{ $name } );
259 }
260  
261 # consistency is a good thing ...
262 38 ▶sub requires_method { goto &has_required_method }
Subroutine requires_method: called CC=1 SCAR=0.0
Pod documented
263  
264 20 ▶sub get_required_method {
Subroutine get_required_method: called CC=5 SCAR=16.1
Pod documented
265 20 my $class = $_[0]->name;
266 20 my $stash = $_[0]->stash;
267 20 my $name = $_[1];
268  
269 20 ▶ Carp::confess('[ARGS] You must specify the name of the required method to get')
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 20 20
270 unless $name;
271  
272 # check these two easy cases first ...
273 20 ▶ return unless exists $stash->{ $name };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $stash->{$name} 3 17 20
274 17 ▶ return unless MOP::Internal::Util::DOES_GLOB_HAVE_NULL_CV( $stash->{ $name } );
Branch: 1 branch
BranchTrueFalseTotal
unless MOP::Internal::Util::DOES_GLOB_HAVE_NULL_CV($stash->{$name}) 11 6 17
275  
276 # now we grab the CV ...
277 6 my $method = MOP::Method->new(
278 body => MOP::Internal::Util::GET_GLOB_SLOT( $stash, $name, 'CODE' )
279 );
280 # and make sure it is local, and
281 # then return the method ...
282 6 ▶ return $method if $method->origin_stash eq $class;
Branch: 1 branch
BranchTrueFalseTotal
if $method->origin_stash eq $class 5 1 6
283 # or return nothing ...
284 1 return;
285 }
286  
287 5 ▶sub add_required_method {
Subroutine add_required_method: called CC=6 SCAR=18.0
Pod documented
288 5 my ($self, $name) = @_;
289  
290 5 ▶ Carp::confess('[ARGS] You must specify the name of the required method to add')
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 5 5
291 unless $name;
292  
293 # if we already have a glob there ...
294 5 ▶ if ( my $glob = $self->stash->{ $name } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $glob = $self->stash->{$name}) 3 2 5
295 # and if we have a NULL CV in it, just return
296 3 ▶ return if MOP::Internal::Util::DOES_GLOB_HAVE_NULL_CV( $glob );
Branch: 1 branch
BranchTrueFalseTotal
if MOP::Internal::Util::DOES_GLOB_HAVE_NULL_CV($glob) 1 2 3
297 # and if we don't and we have a CODE slot, we
298 # need to die because this doesn't make sense
299 Carp::confess("[CONFLICT] Cannot add a required method ($name) when there is a regular method already there")
300 if ref \$glob eq 'GLOB'
301 2 ? defined *{ $glob }{CODE}
302 2 ▶ : defined $glob;
Branch: 2 branches
BranchTrueFalseTotal
if ref \$glob eq "GLOB" ? defined *{$glob;}{"CODE"} : defined $glob 1 1 2
ref \$glob eq 'GLOB' ? : 2 0 2
303 }
304  
305 # if we get here, then we
306 # just create a null CV
307 3 MOP::Internal::Util::CREATE_NULL_CV( $self->name, $name );
308  
309 2 return;
310 }
311  
312 4 ▶sub delete_required_method {
Subroutine delete_required_method: called CC=5 SCAR=16.1
Pod documented
313 4 my ($self, $name) = @_;
314  
315 4 ▶ Carp::confess('[ARGS] You must specify the name of the required method to delete')
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 4 4
316 unless $name;
317  
318 # check if we have a stash entry for $name ...
319 4 ▶ if ( my $glob = $self->stash->{ $name } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $glob = $self->stash->{$name}) 3 1 4
320 # and if we have a NULL CV in it, ...
321 3 ▶ if ( MOP::Internal::Util::DOES_GLOB_HAVE_NULL_CV( $glob ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (MOP::Internal::Util::DOES_GLOB_HAVE_NULL_CV($glob)) { } 1 2 3
322 # then we must delete it
323 1 MOP::Internal::Util::REMOVE_CV_FROM_GLOB( $self->stash, $name );
324 1 return;
325 }
326 else {
327 # and if we have a CV slot, but it doesn't have
328 # a NULL CV in it, then we need to die because
329 # this doesn't make sense
330 Carp::confess("[CONFLICT] Cannot delete a required method ($name) when there is a regular method already there")
331 2 ▶ if defined *{ $glob }{CODE};
Branch: 1 branch
BranchTrueFalseTotal
if defined *{$glob;}{"CODE"} 1 1 2
332  
333 # if we have the glob, but no CV slot (NULL or otherwise)
334 # we do nothing ...
335 1 return;
336 }
337 }
338 # if there is no stash entry for $name, we do nothing
339 1 return;
340 }
341  
342 # methods
343  
344 460 ▶sub has_method {
Subroutine has_method: called CC=7 SCAR=19.5
Pod documented
345 460 my $self = $_[0];
346 460 my $class = $self->name;
347 460 my $stash = $self->stash;
348 460 my $name = $_[1];
349  
350 460 ▶ Carp::confess('[ARGS] You must specify the name of the method to look for')
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 460 460
351 unless $name;
352  
353 # check these two easy cases first ...
354 460 ▶ return 0 unless exists $stash->{ $name };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $stash->{$name} 425 35 460
355 35 ▶ return 0 if MOP::Internal::Util::DOES_GLOB_HAVE_NULL_CV( $stash->{ $name } );
Branch: 1 branch
BranchTrueFalseTotal
if MOP::Internal::Util::DOES_GLOB_HAVE_NULL_CV($stash->{$name}) 5 30 35
356  
357 # now we grab the CV and make sure it is
358 # local, and return accordingly
359 30 ▶ if ( my $code = MOP::Internal::Util::GET_GLOB_SLOT( $stash, $name, 'CODE' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $code = MOP::Internal::Util::GET_GLOB_SLOT($stash, $name, "CODE")) 27 3 30
360 27 my $method = MOP::Method->new( body => $code );
361 27 my @roles = $self->roles;
362 # and make sure it is local, and
363 # then return accordingly
364 27 ▶ return $method->origin_stash eq $class
Condition83%5/6
$method->origin_stash eq $class || @roles && $method->was_aliased_from(@roles)
l!l&&r!l&&!r
2214
@roles and $method->was_aliased_from(@roles)
!ll&&!rl&&r
401
MC/DC66%2/3
$method->origin_stash eq $class || @roles && $method->was_aliased_from(@roles)
$method->origin_stash eq $class @roles $method->was_aliased_from(@roles)
Truth table75%3/4
A || B and C
A= $method->origin_stash eq $classB= @rolesC= $method->was_aliased_from(@roles)
ABCresult
1XX1
0111
00X0
0100
365 || (@roles && $method->was_aliased_from( @roles ));
366 }
367  
368 # if there was no CV, return false.
369 3 return 0;
370 }
371  
372 22 ▶sub get_method {
Subroutine get_method: called CC=8 SCAR=20.8
Pod documented
373 22 my $self = $_[0];
374 22 my $class = $self->name;
375 22 my $stash = $self->stash;
376 22 my $name = $_[1];
377  
378 22 ▶ Carp::confess('[ARGS] You must specify the name of the method to get')
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 22 22
379 unless $name;
380  
381 # check the easy cases first ...
382 22 ▶ return unless exists $stash->{ $name };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $stash->{$name} 2 20 22
383 20 ▶ return if MOP::Internal::Util::DOES_GLOB_HAVE_NULL_CV( $stash->{ $name } );
Branch: 1 branch
BranchTrueFalseTotal
if MOP::Internal::Util::DOES_GLOB_HAVE_NULL_CV($stash->{$name}) 3 17 20
384  
385 # now we grab the CV ...
386 17 ▶ if ( my $code = MOP::Internal::Util::GET_GLOB_SLOT( $stash, $name, 'CODE' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $code = MOP::Internal::Util::GET_GLOB_SLOT($stash, $name, "CODE")) 14 3 17
387 14 my $method = MOP::Method->new( body => $code );
388 14 my @roles = $self->roles;
389 # and make sure it is local, and
390 # then return accordingly
391 14 ▶ return $method
Branch: 1 branch
BranchTrueFalseTotal
if $method->origin_stash eq $class or @roles and $method->was_aliased_from(@roles) 13 1 14
Condition83%5/6
$method->origin_stash eq $class or @roles and $method->was_aliased_from(@roles)
l!l&&r!l&&!r
1211
@roles and $method->was_aliased_from(@roles)
!ll&&!rl&&r
101
MC/DC66%2/3
$method->origin_stash eq $class or @roles and $method->was_aliased_from(@roles)
$method->origin_stash eq $class @roles $method->was_aliased_from(@roles)
Truth table75%3/4
A or B and C
A= $method->origin_stash eq $classB= @rolesC= $method->was_aliased_from(@roles)
ABCresult
1XX1
0111
00X0
0100
392 if $method->origin_stash eq $class
393 || (@roles && $method->was_aliased_from( @roles ));
394 }
395  
396 # if there was no CV, return false.
397 4 return;
398 }
399  
400 1 ▶sub add_method {
Subroutine add_method: called CC=4 SCAR=15.2
Pod documented
401 1 my ($self, $name, $code) = @_;
402  
403 1 ▶ Carp::confess('[ARGS] You must specify the name of the method to add')
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 1 1
404 unless $name;
405  
406 1 ▶ Carp::confess('[ARGS] You must specify a CODE reference to add as a method')
Branch: 1 branch
BranchTrueFalseTotal
unless $code and ref $code eq "CODE" 0 1 1
Condition33%1/3
$code and ref $code eq "CODE"
!ll&&!rl&&r
001
MC/DC0%0/2
$code and ref $code eq "CODE"
$code ref $code eq "CODE"
Truth table33%1/3
A and B
A= $codeB= ref $code eq "CODE"
ABresult
0X0
100
111
407 unless $code && ref $code eq 'CODE';
408  
409 1 MOP::Internal::Util::INSTALL_CV( $self->name, $name, $code, set_subname => 1 );
410 1 return;
411 }
412  
413 5 ▶sub delete_method {
Subroutine delete_method: called CC=8 SCAR=21.0
Pod documented
414 5 my ($self, $name) = @_;
415  
416 5 ▶ Carp::confess('[ARGS] You must specify the name of the method to delete')
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 5 5
417 unless $name;
418  
419 # check if we have a stash entry for $name ...
420 5 ▶ if ( my $glob = $self->stash->{ $name } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $glob = $self->stash->{$name}) 4 1 5
421 # and if we have a NULL CV in it, ...
422 4 ▶ if ( MOP::Internal::Util::DOES_GLOB_HAVE_NULL_CV( $glob ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (MOP::Internal::Util::DOES_GLOB_HAVE_NULL_CV($glob)) { } 1 3 4
423 # then we need to die because this
424 # shouldn't happen, we should only
425 # delete regular methods.
426 1 Carp::confess("[CONFLICT] Cannot delete a regular method ($name) when there is a required method already there");
427 }
428 else {
429 # if we don't have a code slot ...
430 3 ▶ return unless defined *{ $glob }{CODE};
Branch: 1 branch
BranchTrueFalseTotal
unless defined *{$glob;}{"CODE"} 1 2 3
431  
432 # we need to make sure it is local, and
433 # otherwise, error accordingly
434 2 my $method = MOP::Method->new( body => *{ $glob }{CODE} );
435 2 my @roles = $self->roles;
436  
437 # if the method has not come from
438 # the local class, we need to see
439 # if it was added from a role
440 2 ▶ if ($method->origin_stash ne $self->name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method->origin_stash ne $self->name) 1 1 2
441  
442 # if it came from a role, then it is
443 # okay to be deleted, but if it didn't
444 # then we have an error cause they are
445 # trying to delete an alias using the
446 # regular method method
447 1 ▶ unless ( @roles && $method->was_aliased_from( @roles ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@roles and $method->was_aliased_from(@roles)) 1 0 1
Condition33%1/3
@roles and $method->was_aliased_from(@roles)
!ll&&!rl&&r
100
MC/DC0%0/2
@roles and $method->was_aliased_from(@roles)
@roles $method->was_aliased_from(@roles)
Truth table33%1/3
A and B
A= @rolesB= $method->was_aliased_from(@roles)
ABresult
0X0
100
111
448 1 Carp::confess("[CONFLICT] Cannot delete a regular method ($name) when there is an aliased method already there")
449 }
450 }
451  
452 # but if we have a regular method, then we
453 # can just delete the CV from the glob
454 1 MOP::Internal::Util::REMOVE_CV_FROM_GLOB( $self->stash, $name );
455 }
456 }
457 # if there is no stash entry for $name, we do nothing
458 2 return;
459 }
460  
461 # aliased methods
462  
463 15 ▶sub get_method_alias {
Subroutine get_method_alias: called CC=6 SCAR=17.9
Pod documented
464 15 my $class = $_[0]->name;
465 15 my $stash = $_[0]->stash;
466 15 my $name = $_[1];
467  
468 15 ▶ Carp::confess('[ARGS] You must specify the name of the method alias to look for')
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 15 15
469 unless $name;
470  
471 # check the easy cases first ...
472 15 ▶ return unless exists $stash->{ $name };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $stash->{$name} 2 13 15
473 13 ▶ return if MOP::Internal::Util::DOES_GLOB_HAVE_NULL_CV( $stash->{ $name } );
Branch: 1 branch
BranchTrueFalseTotal
if MOP::Internal::Util::DOES_GLOB_HAVE_NULL_CV($stash->{$name}) 1 12 13
474  
475 # now we grab the CV ...
476 12 ▶ if ( my $code = MOP::Internal::Util::GET_GLOB_SLOT( $stash, $name, 'CODE' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $code = MOP::Internal::Util::GET_GLOB_SLOT($stash, $name, "CODE")) 9 3 12
477 9 my $method = MOP::Method->new( body => $code );
478 # and make sure it is not local, and
479 # then return accordingly
480 9 ▶ return $method
Branch: 1 branch
BranchTrueFalseTotal
if $method->origin_stash ne $class 7 2 9
481 if $method->origin_stash ne $class;
482 }
483  
484 # if there was no CV, return false.
485 5 return;
486 }
487  
488 # NOTE:
489 # Should aliasing be aloud even after a class is closed?
490 # Probably not, but it might not be a bad idea to at
491 # least discuss in more detail what happens when a class
492 # is actually closed.
493 # - SL
494  
495 421 ▶sub alias_method {
Subroutine alias_method: called CC=4 SCAR=15.2
Pod documented
496 421 my ($self, $name, $code) = @_;
497  
498 421 ▶ Carp::confess('[ARGS] You must specify the name of the method alias to add')
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 421 421
499 unless $name;
500  
501 421 ▶ Carp::confess('[ARGS] You must specify a CODE reference to add as a method alias')
Branch: 1 branch
BranchTrueFalseTotal
unless $code and ref $code eq "CODE" 0 421 421
Condition33%1/3
$code and ref $code eq "CODE"
!ll&&!rl&&r
00421
MC/DC0%0/2
$code and ref $code eq "CODE"
$code ref $code eq "CODE"
Truth table33%1/3
A and B
A= $codeB= ref $code eq "CODE"
ABresult
0X0
100
111
502 unless $code && ref $code eq 'CODE';
503  
504 421 MOP::Internal::Util::INSTALL_CV( $self->name, $name, $code, set_subname => 0 );
505 421 return;
506 }
507  
508 6 ▶sub delete_method_alias {
Subroutine delete_method_alias: called CC=6 SCAR=17.9
Pod documented
509 6 my ($self, $name) = @_;
510  
511 6 ▶ Carp::confess('[ARGS] You must specify the name of the method alias to remove')
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 6 6
512 unless $name;
513  
514 # check if we have a stash entry for $name ...
515 6 ▶ if ( my $glob = $self->stash->{ $name } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $glob = $self->stash->{$name}) 5 1 6
516 # and if we have a NULL CV in it, ...
517 5 ▶ if ( MOP::Internal::Util::DOES_GLOB_HAVE_NULL_CV( $glob ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (MOP::Internal::Util::DOES_GLOB_HAVE_NULL_CV($glob)) { } 1 4 5
518 # then we need to die because this
519 # shouldn't happen, we should only
520 # delete regular methods.
521 1 Carp::confess("[CONFLICT] Cannot delete an aliased method ($name) when there is a required method already there");
522 }
523 else {
524 # if we don't have a code slot ...
525 4 ▶ return unless defined *{ $glob }{CODE};
Branch: 1 branch
BranchTrueFalseTotal
unless defined *{$glob;}{"CODE"} 1 3 4
526  
527 # we need to make sure it is local, and
528 # otherwise, error accordingly
529 3 my $method = MOP::Method->new( body => *{ $glob }{CODE} );
530  
531 3 ▶ Carp::confess("[CONFLICT] Cannot delete an aliased method ($name) when there is a regular method already there")
Branch: 1 branch
BranchTrueFalseTotal
if $method->origin_stash eq $self->name 1 2 3
532 if $method->origin_stash eq $self->name;
533  
534 # but if we have a regular method, then we
535 # can just delete the CV from the glob
536 2 MOP::Internal::Util::REMOVE_CV_FROM_GLOB( $self->stash, $name );
537 }
538 }
539 # if there is no stash entry for $name, we do nothing
540 3 return;
541 }
542  
543 33 ▶sub has_method_alias {
Subroutine has_method_alias: called CC=5 SCAR=16.1
Pod documented
544 33 my $class = $_[0]->name;
545 33 my $stash = $_[0]->stash;
546 33 my $name = $_[1];
547  
548 33 ▶ Carp::confess('[ARGS] You must specify the name of the method alias to look for')
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 33 33
549 unless $name;
550  
551 # check these two easy cases first ...
552 33 ▶ return 0 unless exists $stash->{ $name };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $stash->{$name} 3 30 33
553 30 ▶ return 0 if MOP::Internal::Util::DOES_GLOB_HAVE_NULL_CV( $stash->{ $name } );
Branch: 1 branch
BranchTrueFalseTotal
if MOP::Internal::Util::DOES_GLOB_HAVE_NULL_CV($stash->{$name}) 5 25 30
554  
555 # now we grab the CV and make sure it is
556 # local, and return accordingly
557 25 ▶ if ( my $code = MOP::Internal::Util::GET_GLOB_SLOT( $stash, $name, 'CODE' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $code = MOP::Internal::Util::GET_GLOB_SLOT($stash, $name, "CODE")) 21 4 25
558 21 return MOP::Method->new( body => $code )->origin_stash ne $class;
559 }
560  
561 # if there was no CV, return false.
562 4 return 0;
563 }
564  
565 ## Slots
566  
567 ## FIXME:
568 ## The same problem we had methods needs to be fixed with
569 ## slots, just checking the origin_stash v. class is
570 ## not enough, we need to check aliasing as well.
571 ## - SL
572  
573 # get them all; regular & aliased
574 49 ▶sub all_slots {
Subroutine all_slots: called CC=2 SCAR=6.9
Pod documented
575 49 my $self = shift;
576 49 my $has = MOP::Internal::Util::GET_GLOB_SLOT( $self->stash, 'HAS', 'HASH' );
577 49 ▶ return unless $has;
Branch: 1 branch
BranchTrueFalseTotal
unless $has 25 24 49
578 return map {
579 24 MOP::Slot->new(
580 name => $_,
581 24 initializer => $has->{ $_ }
582 )
583 } keys %$has;
584 }
585  
586 # just the local slots
587 28 ▶sub slots {
Subroutine slots: called CC=3 SCAR=11.6
Pod documented
588 28 my $self = shift;
589 28 my $class = $self->name;
590 28 my @roles = $self->roles;
591 return grep {
592 28 ▶ $_->origin_stash eq $class
Condition50%3/6
$_->origin_stash eq $class || @roles && $_->was_aliased_from(@roles)
l!l&&r!l&&!r
503
@roles and $_->was_aliased_from(@roles)
!ll&&!rl&&r
300
MC/DC33%1/3
$_->origin_stash eq $class || @roles && $_->was_aliased_from(@roles)
$_->origin_stash eq $class @roles $_->was_aliased_from(@roles)
Truth table50%2/4
A || B and C
A= $_->origin_stash eq $classB= @rolesC= $_->was_aliased_from(@roles)
ABCresult
1XX1
0111
00X0
0100
593 ||
594 (@roles && $_->was_aliased_from( @roles ))
595 } $self->all_slots
596 }
597  
598 # just the non-local slots
599 10 ▶sub aliased_slots {
Subroutine aliased_slots: called CC=1 SCAR=0.0
Pod documented
600 10 my $self = shift;
601 10 my $class = $self->name;
602 10 return grep { $_->origin_stash ne $class } $self->all_slots
603 }
604  
605 ## regular ...
606  
607 13 ▶sub has_slot {
Subroutine has_slot: called CC=6 SCAR=18.3
Pod documented
608 13 my $self = $_[0];
609 13 my $name = $_[1];
610 13 my $class = $self->name;
611 13 my $has = MOP::Internal::Util::GET_GLOB_SLOT( $self->stash, 'HAS', 'HASH' );
612  
613 13 ▶ Carp::confess('[ARGS] You must specify the name of the slot to look for')
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 13 13
614 unless $name;
615  
616 13 ▶ return unless $has;
Branch: 1 branch
BranchTrueFalseTotal
unless $has 3 10 13
617 10 ▶ return unless exists $has->{ $name };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $has->{$name} 4 6 10
618  
619 6 my @roles = $self->roles;
620 my $slot = MOP::Slot->new(
621 name => $name,
622 6 initializer => $has->{ $name }
623 );
624  
625 6 ▶ return $slot->origin_stash eq $class
Condition50%3/6
$slot->origin_stash eq $class || @roles && $slot->was_aliased_from(@roles)
l!l&&r!l&&!r
303
@roles and $slot->was_aliased_from(@roles)
!ll&&!rl&&r
300
MC/DC33%1/3
$slot->origin_stash eq $class || @roles && $slot->was_aliased_from(@roles)
$slot->origin_stash eq $class @roles $slot->was_aliased_from(@roles)
Truth table50%2/4
A || B and C
A= $slot->origin_stash eq $classB= @rolesC= $slot->was_aliased_from(@roles)
ABCresult
1XX1
0111
00X0
0100
626 || (@roles && $slot->was_aliased_from( @roles ));
627 }
628  
629 18 ▶sub get_slot {
Subroutine get_slot: called CC=7 SCAR=19.7
Pod documented
630 18 my $self = $_[0];
631 18 my $name = $_[1];
632 18 my $class = $self->name;
633 18 my $has = MOP::Internal::Util::GET_GLOB_SLOT( $self->stash, 'HAS', 'HASH' );
634  
635 18 ▶ Carp::confess('[ARGS] You must specify the name of the slot to get')
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 18 18
636 unless $name;
637  
638 18 ▶ return unless $has;
Branch: 1 branch
BranchTrueFalseTotal
unless $has 3 15 18
639 15 ▶ return unless exists $has->{ $name };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $has->{$name} 4 11 15
640  
641 11 my @roles = $self->roles;
642 my $slot = MOP::Slot->new(
643 name => $name,
644 11 initializer => $has->{ $name }
645 );
646  
647 11 ▶ return $slot
Branch: 1 branch
BranchTrueFalseTotal
if $slot->origin_stash eq $class or @roles and $slot->was_aliased_from(@roles) 9 2 11
Condition50%3/6
$slot->origin_stash eq $class or @roles and $slot->was_aliased_from(@roles)
l!l&&r!l&&!r
902
@roles and $slot->was_aliased_from(@roles)
!ll&&!rl&&r
200
MC/DC33%1/3
$slot->origin_stash eq $class or @roles and $slot->was_aliased_from(@roles)
$slot->origin_stash eq $class @roles $slot->was_aliased_from(@roles)
Truth table50%2/4
A or B and C
A= $slot->origin_stash eq $classB= @rolesC= $slot->was_aliased_from(@roles)
ABCresult
1XX1
0111
00X0
0100
648 if $slot->origin_stash eq $class
649 || (@roles && $slot->was_aliased_from( @roles ));
650  
651 2 return;
652 }
653  
654 3 ▶sub add_slot {
Subroutine add_slot: called CC=7 SCAR=20.2
Pod documented
655 3 my $self = $_[0];
656 3 my $name = $_[1];
657 3 my $initializer = $_[2];
658  
659 3 ▶ Carp::confess('[ARGS] You must specify the name of the slot to add')
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 3 3
660 unless $name;
661  
662 3 ▶ Carp::confess('[ARGS] You must specify an initializer CODE reference to associate with the slot')
Branch: 1 branch
BranchTrueFalseTotal
unless $initializer and ref $initializer eq "CODE" || MOP::Internal::Util::CAN_COERCE_TO_CODE_REF($initializer) 0 3 3
Condition33%2/6
$initializer and ref $initializer eq "CODE" || MOP::Internal::Util::CAN_COERCE_TO_CODE_REF($initializer)
!ll&&!rl&&r
003
ref $initializer eq "CODE" or MOP::Internal::Util::CAN_COERCE_TO_CODE_REF($initializer)
l!l&&r!l&&!r
300
MC/DC0%0/3
$initializer and ref $initializer eq "CODE" || MOP::Internal::Util::CAN_COERCE_TO_CODE_REF($initializer)
$initializer ref $initializer eq "CODE" MOP::Internal::Util::CAN_COERCE_TO_CODE_REF($initializer)
Truth table25%1/4
A and B or C
A= $initializerB= ref $initializer eq "CODE"C= MOP::Internal::Util::CAN_COERCE_TO_CODE_REF($initializer)
ABCresult
0XX0
1000
11X1
1011
663 unless $initializer && (ref $initializer eq 'CODE' || MOP::Internal::Util::CAN_COERCE_TO_CODE_REF( $initializer ));
664  
665 3 my $stash = $self->stash;
666 3 my $class = $self->name;
667 3 my $slot = MOP::Slot->new( name => $name, initializer => $initializer );
668  
669 # just as with add_method, we take ownership
670 # of the initializer and set the COMP STASH
671 # field so that we know it is ours.
672 3 ▶ MOP::Internal::Util::SET_COMP_STASH_FOR_CV( $slot->initializer, $class )
Branch: 1 branch
BranchTrueFalseTotal
if $slot->origin_stash ne $class 1 2 3
673 if $slot->origin_stash ne $class;
674  
675 3 my $has = MOP::Internal::Util::GET_GLOB_SLOT( $stash, 'HAS', 'HASH' );
676 3 ▶ MOP::Internal::Util::SET_GLOB_SLOT( $stash, 'HAS', $has = {} )
Branch: 1 branch
BranchTrueFalseTotal
unless $has 1 2 3
677 unless $has;
678  
679 3 $has->{ $name } = $initializer;
680 3 return;
681 }
682  
683 4 ▶sub delete_slot {
Subroutine delete_slot: called CC=5 SCAR=16.1
Pod documented
684 4 my $self = $_[0];
685 4 my $name = $_[1];
686 4 my $stash = $self->stash;
687 4 my $class = $self->name;
688  
689 4 ▶ Carp::confess('[ARGS] You must specify the name of the slot to delete')
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 4 4
690 unless $name;
691  
692 4 my $has = MOP::Internal::Util::GET_GLOB_SLOT( $stash, 'HAS', 'HASH' );
693  
694 4 ▶ return unless $has;
Branch: 1 branch
BranchTrueFalseTotal
unless $has 1 3 4
695 3 ▶ return unless exists $has->{ $name };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $has->{$name} 1 2 3
696  
697 Carp::confess("[CONFLICT] Cannot delete a regular slot ($name) when there is an aliased slot already there")
698 if MOP::Slot->new(
699 name => $name,
700 2 ▶ initializer => $has->{ $name }
Branch: 1 branch
BranchTrueFalseTotal
if "MOP::Slot"->new("name", $name, "initializer", $has->{$name})->origin_stash ne $class 1 1 2
701 )->origin_stash ne $class;
702  
703 1 delete $has->{ $name };
704  
705 1 return;
706 }
707  
708 9 ▶sub has_slot_alias {
Subroutine has_slot_alias: called CC=4 SCAR=13.9
Pod documented
709 9 my $self = $_[0];
710 9 my $name = $_[1];
711 9 my $class = $self->name;
712 9 my $has = MOP::Internal::Util::GET_GLOB_SLOT( $self->stash, 'HAS', 'HASH' );
713  
714 9 ▶ Carp::confess('[ARGS] You must specify the name of the slot alias to look for')
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 9 9
715 unless $name;
716  
717 9 ▶ return unless $has;
Branch: 1 branch
BranchTrueFalseTotal
unless $has 1 8 9
718 8 ▶ return unless exists $has->{ $name };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $has->{$name} 2 6 8
719  
720 return MOP::Slot->new(
721 name => $name,
722 6 initializer => $has->{ $name }
723 )->origin_stash ne $class;
724 }
725  
726 9 ▶sub get_slot_alias {
Subroutine get_slot_alias: called CC=5 SCAR=16.1
Pod documented
727 9 my $self = $_[0];
728 9 my $name = $_[1];
729 9 my $class = $self->name;
730 9 my $has = MOP::Internal::Util::GET_GLOB_SLOT( $self->stash, 'HAS', 'HASH' );
731  
732 9 ▶ Carp::confess('[ARGS] You must specify the name of the slot alias to get')
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 9 9
733 unless $name;
734  
735 9 ▶ return unless $has;
Branch: 1 branch
BranchTrueFalseTotal
unless $has 1 8 9
736 8 ▶ return unless exists $has->{ $name };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $has->{$name} 1 7 8
737  
738 my $slot = MOP::Slot->new(
739 name => $name,
740 7 initializer => $has->{ $name }
741 );
742  
743 7 ▶ return $slot
Branch: 1 branch
BranchTrueFalseTotal
if $slot->origin_stash ne $class 5 2 7
744 if $slot->origin_stash ne $class;
745  
746 2 return;
747 }
748  
749 3 ▶sub alias_slot {
Subroutine alias_slot: called CC=7 SCAR=20.2
Pod documented
750 3 my $self = $_[0];
751 3 my $name = $_[1];
752 3 my $initializer = $_[2];
753  
754 3 ▶ Carp::confess('[ARGS] You must specify the name of the slot alias to add')
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 3 3
755 unless $name;
756  
757 3 ▶ Carp::confess('[ARGS] You must specify an initializer CODE reference to associate with the slot alias')
Branch: 1 branch
BranchTrueFalseTotal
unless $initializer and ref $initializer eq "CODE" || MOP::Internal::Util::CAN_COERCE_TO_CODE_REF($initializer) 0 3 3
Condition33%2/6
$initializer and ref $initializer eq "CODE" || MOP::Internal::Util::CAN_COERCE_TO_CODE_REF($initializer)
!ll&&!rl&&r
003
ref $initializer eq "CODE" or MOP::Internal::Util::CAN_COERCE_TO_CODE_REF($initializer)
l!l&&r!l&&!r
300
MC/DC0%0/3
$initializer and ref $initializer eq "CODE" || MOP::Internal::Util::CAN_COERCE_TO_CODE_REF($initializer)
$initializer ref $initializer eq "CODE" MOP::Internal::Util::CAN_COERCE_TO_CODE_REF($initializer)
Truth table25%1/4
A and B or C
A= $initializerB= ref $initializer eq "CODE"C= MOP::Internal::Util::CAN_COERCE_TO_CODE_REF($initializer)
ABCresult
0XX0
1000
11X1
1011
758 unless $initializer && (ref $initializer eq 'CODE' || MOP::Internal::Util::CAN_COERCE_TO_CODE_REF( $initializer ));
759  
760 3 my $stash = $self->stash;
761 3 my $class = $self->name;
762 3 my $slot = MOP::Slot->new( name => $name, initializer => $initializer );
763  
764 3 ▶ Carp::confess('[CONFLICT] Slot is from the local class (' . $class . '), it should be from a different class')
Branch: 1 branch
BranchTrueFalseTotal
if $slot->origin_stash eq $class 1 2 3
765 if $slot->origin_stash eq $class;
766  
767 2 my $has = MOP::Internal::Util::GET_GLOB_SLOT( $stash, 'HAS', 'HASH' );
768 2 ▶ MOP::Internal::Util::SET_GLOB_SLOT( $stash, 'HAS', $has = {} )
Branch: 1 branch
BranchTrueFalseTotal
unless $has 1 1 2
769 unless $has;
770  
771 2 $has->{ $name } = $initializer;
772 2 return;
773 }
774  
775 4 ▶sub delete_slot_alias {
Subroutine delete_slot_alias: called CC=5 SCAR=16.1
Pod documented
776 4 my $self = $_[0];
777 4 my $name = $_[1];
778 4 my $stash = $self->stash;
779 4 my $class = $self->name;
780  
781 4 ▶ Carp::confess('[ARGS] You must specify the name of the slot alias to delete')
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 4 4
782 unless $name;
783  
784 4 my $has = MOP::Internal::Util::GET_GLOB_SLOT( $stash, 'HAS', 'HASH' );
785  
786 4 ▶ return unless $has;
Branch: 1 branch
BranchTrueFalseTotal
unless $has 1 3 4
787 3 ▶ return unless exists $has->{ $name };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $has->{$name} 1 2 3
788  
789 Carp::confess("[CONFLICT] Cannot delete an slot alias ($name) when there is an regular slot already there")
790 if MOP::Slot->new(
791 name => $name,
792 2 ▶ initializer => $has->{ $name }
Branch: 1 branch
BranchTrueFalseTotal
if "MOP::Slot"->new("name", $name, "initializer", $has->{$name})->origin_stash eq $class 1 1 2
793 )->origin_stash eq $class;
794  
795 1 delete $has->{ $name };
796  
797 1 return;
798 }
799  
800 28 1;
801  
802 __END__
« MOP/Internal/Util.pm ↑ Summary MOP/Slot.pm »