MOP/Role.pm

filter: Statementstmt 100% 316 / 316 Branchbran 83.0% 166 / 200 Conditioncond 50.9% 26 / 51 MC/DCmcdc 14.8% 4 / 27 Subroutinesub 100% 46 / 46 Podpod 100% 37 / 37 totaltotal 87.8% 595 / 677 CC 121 SCAR 16.6
CC
121
Cov
90%
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 UNIVERSAL::Object::Immutable;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 28 ▶use MOP::Method;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 28 ▶use MOP::Slot;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 28 ▶use MOP::Internal::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 28 our $VERSION = '0.11';
17 28 our $AUTHORITY = 'cpan:STEVAN';
18  
19 28 ▶our @ISA; BEGIN { @ISA = 'UNIVERSAL::Object::Immutable' };
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 sub BUILDARGS {
22 110 ▶ my $class = shift;
Subroutine BUILDARGS: called CC=7 SCAR=19.6
Pod documented
23 110 my %args;
24  
25 110 ▶ if ( scalar( @_ ) == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ == 1) { } 16 94 110
26 16 ▶ if ( ref $_[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0]) { } 4 12 16
27 4 ▶ if ( ref $_[0] eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq "HASH") 4 0 4
28 4 ▶ if ( MOP::Internal::Util::IS_STASH_REF( $_[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (MOP::Internal::Util::IS_STASH_REF($_[0])) { } 2 2 4
29 # if it is a stash, grab the name
30 2 %args = (
31 name => MOP::Internal::Util::GET_NAME( $_[0] ),
32 stash => $_[0]
33 );
34 }
35 else {
36 # just plain old HASH ref ...
37 2 %args = %{ $_[0] };
38 }
39 }
40 }
41 else {
42 # assume it is a single package name ...
43 12 %args = ( name => $_[0] );
44 }
45 }
46 else {
47 # assume we got key/value pairs ...
48 94 %args = @_;
49 }
50  
51 Carp::croak('[ARGS] You must specify a package name')
52 110 ▶ unless $args{name};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"name"} 0 110 110
53  
54 Carp::croak('[ARGS] You must specify a valid package name, not `'.$_[0].'`')
55 110 ▶ 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 110 110
56  
57 110 return \%args;
58 }
59  
60 sub CREATE {
61 110 ▶ my ($class, $args) = @_;
Subroutine CREATE: called CC=2 SCAR=6.9
Pod documented
62  
63 # intiialize the stash ...
64 110 my $stash = $args->{stash};
65  
66 # if we have it, otherwise get it ...
67 110 ▶ unless ( $stash ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($stash) 108 2 110
68 # get a ref to to the stash itself ...
69 28 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
70 108 $stash = \%{ $args->{name} . '::' };
71 }
72 # and then a ref to that, because we
73 # eventually will need to bless it and
74 # we do not want to bless the actual
75 # stash because that persists beyond
76 # the lifetime of this object, so we
77 # bless a ref of a ref then ...
78 110 return \$stash;
79 }
80  
81 # stash
82  
83 sub stash {
84 2058 ▶ my ($self) = @_;
Subroutine stash: called CC=1 SCAR=0.0
Pod documented
85 2058 return $$self; # returns the direct HASH ref of the stash
86 }
87  
88 # identity
89  
90 sub name {
91 1115 ▶ my ($self) = @_;
Subroutine name: called CC=1 SCAR=0.0
Pod documented
92 1115 return MOP::Internal::Util::GET_NAME( $self->stash );
93 }
94  
95 sub version {
96 8 ▶ my ($self) = @_;
Subroutine version: called CC=2 SCAR=7.0
Pod documented
97 8 my $version = MOP::Internal::Util::GET_GLOB_SLOT( $self->stash, 'VERSION', 'SCALAR' );
98 8 ▶ return unless $version;
Branch: 1 branch
BranchTrueFalseTotal
unless $version 0 8 8
99 8 return $$version;
100 }
101  
102 sub authority {
103 8 ▶ my ($self) = @_;
Subroutine authority: called CC=2 SCAR=7.0
Pod documented
104 8 my $authority = MOP::Internal::Util::GET_GLOB_SLOT( $self->stash, 'AUTHORITY', 'SCALAR' );
105 8 ▶ return unless $authority;
Branch: 1 branch
BranchTrueFalseTotal
unless $authority 0 8 8
106 8 return $$authority;
107 }
108  
109 # other roles
110  
111 sub roles {
112 155 ▶ my ($self) = @_;
Subroutine roles: called CC=2 SCAR=6.9
Pod documented
113 155 my $does = MOP::Internal::Util::GET_GLOB_SLOT( $self->stash, 'DOES', 'ARRAY' );
114 155 ▶ return unless $does;
Branch: 1 branch
BranchTrueFalseTotal
unless $does 103 52 155
115 52 return @$does;
116 }
117  
118 sub set_roles {
119 1 ▶ my ($self, @roles) = @_;
Subroutine set_roles: called CC=2 SCAR=7.0
Pod documented
120 1 ▶ Carp::croak('[ARGS] You must specify at least one role')
Branch: 1 branch
BranchTrueFalseTotal
if scalar @roles == 0 0 1 1
121 if scalar( @roles ) == 0;
122 1 MOP::Internal::Util::SET_GLOB_SLOT( $self->stash, 'DOES', \@roles );
123 1 return;
124 }
125  
126 sub does_role {
127 23 ▶ my ($self, $to_test) = @_;
Subroutine does_role: called CC=7 SCAR=19.5
Pod documented
128  
129 23 ▶ Carp::croak('[ARGS] You must specify a role')
Branch: 1 branch
BranchTrueFalseTotal
unless $to_test 0 23 23
130 unless $to_test;
131  
132 23 my @roles = $self->roles;
133  
134 # no roles, will never match ...
135 23 ▶ return 0 unless @roles;
Branch: 1 branch
BranchTrueFalseTotal
unless @roles 6 17 23
136  
137 # try the simple way first ...
138 17 foreach my $role ( @roles ) {
139 20 ▶ return 1 if $role eq $to_test;
Branch: 1 branch
BranchTrueFalseTotal
if $role eq $to_test 13 7 20
140 }
141  
142 # then try the harder way next ...
143 4 foreach my $role ( @roles ) {
144 4 ▶ return 1
Branch: 1 branch
BranchTrueFalseTotal
if "MOP::Role"->new("name", $role)->does_role($to_test) 3 1 4
145 if MOP::Role->new( name => $role )
146 ->does_role( $to_test );
147 }
148  
149 # oh well ...
150 1 return 0;
151 }
152  
153 ## Methods
154  
155 # get them all; regular, aliased & required
156 sub all_methods {
157 46 ▶ my $stash = $_[0]->stash;
Subroutine all_methods: called CC=3 SCAR=11.0
Pod documented
158 46 my @methods;
159 46 foreach my $candidate ( keys %$stash ) {
160 1211 ▶ 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")) 941 270 1211
161 941 push @methods => MOP::Method->new( body => $code );
162 }
163 }
164 46 return @methods;
165 }
166  
167 # just the local non-required methods
168 sub methods {
169 21 ▶ my $self = shift;
Subroutine methods: called CC=6 SCAR=18.0
Pod documented
170 21 my $class = $self->name;
171 21 my @roles = $self->roles;
172  
173 21 my @methods;
174 21 foreach my $method ( $self->all_methods ) {
175 # if the method is required, we don't want it
176 464 ▶ next if $method->is_required;
Branch: 1 branch
BranchTrueFalseTotal
if $method->is_required 5 459 464
177  
178 # if the method is not originally from the
179 # class, then we probably don't want it ...
180 459 ▶ if ( $method->origin_stash ne $class ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method->origin_stash ne $class) 3 456 459
181 # if our class has roles, then non-local
182 # methods *might* be valid, so ...
183  
184 # if we don't have roles, then
185 # it can't be valid, so we don't
186 # want it
187 3 ▶ next unless @roles;
Branch: 1 branch
BranchTrueFalseTotal
unless @roles 0 3 3
188  
189 # if we do have roles, but our
190 # method was not aliased from one
191 # of them, then we don't want it.
192 3 ▶ next unless $method->was_aliased_from( @roles );
Branch: 1 branch
BranchTrueFalseTotal
unless $method->was_aliased_from(@roles) 0 3 3
193  
194 # if we are here then we have
195 # a non-required method that is
196 # not from the local class, it
197 # has roles and was aliased from
198 # one of them, which means we want
199 # to keep it, so we let it fall through
200 }
201  
202 # if we are here then we have
203 # a non-required method that is
204 # either from the local class,
205 # or is not from a local class,
206 # but has fallen through our
207 # conditional above.
208  
209 459 push @methods => $method;
210 }
211  
212 21 return @methods;
213 }
214  
215 # just the non-local non-required methods
216 sub aliased_methods {
217 2 ▶ my $self = shift;
Subroutine aliased_methods: called CC=2 SCAR=6.9
Pod documented
218 2 my $class = $self->name;
219 2 ▶ return grep { (!$_->is_required) && $_->origin_stash ne $class } $self->all_methods
Branch: 1 branch
BranchTrueFalseTotal
not $_->is_required and $_->origin_stash ne $class 3 2 5
220 }
221  
222 # just the required methods (locality be damned)
223 # NOTE:
224 # We don't care where are required method comes from
225 # just that one exists, so aliasing is not part of the
226 # criteria here.
227 # - SL
228 sub required_methods {
229 20 ▶ my $self = shift;
Subroutine required_methods: called CC=1 SCAR=0.0
Pod documented
230 20 return grep { $_->is_required } $self->all_methods
231 }
232  
233 # required methods
234  
235 # NOTE:
236 # there is no real heavy need to use the MOP::Method API
237 # below because 1) it is not needed, and 2) the MOP::Method
238 # API is really just an information shim, it does not perform
239 # much in the way of actions. From my point of view, the below
240 # operations are mostly stash manipulation functions and so
241 # therefore belong here in the continuim of responsibility/
242 # ownership.
243 #
244 ## The only argument that could likely be made is for the
245 ## MOP::Method API to handle creating the NULL CV for the
246 ## add_required_method, but that would require us to pass in
247 ## a MOP::Method instance, which would be silly since we never
248 ## need it anyway.
249 #
250 # - SL
251  
252 sub has_required_method {
253 37 ▶ my $stash = $_[0]->stash;
Subroutine has_required_method: called CC=3 SCAR=11.0
Pod documented
254 37 my $name = $_[1];
255  
256 37 ▶ Carp::croak('[ARGS] You must specify the name of the required method to look for')
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 37 37
257 unless $name;
258  
259 37 ▶ return 0 unless exists $stash->{ $name };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $stash->{$name} 3 34 37
260 34 return MOP::Internal::Util::DOES_GLOB_HAVE_NULL_CV( $stash->{ $name } );
261 }
262  
263 # consistency is a good thing ...
264 36 ▶sub requires_method { goto &has_required_method }
Subroutine requires_method: called CC=1 SCAR=0.0
Pod documented
265  
266 sub get_required_method {
267 20 ▶ my $class = $_[0]->name;
Subroutine get_required_method: called CC=5 SCAR=16.1
Pod documented
268 20 my $stash = $_[0]->stash;
269 20 my $name = $_[1];
270  
271 20 ▶ Carp::croak('[ARGS] You must specify the name of the required method to get')
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 20 20
272 unless $name;
273  
274 # check these two easy cases first ...
275 20 ▶ return unless exists $stash->{ $name };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $stash->{$name} 3 17 20
276 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
277  
278 # now we grab the CV ...
279 6 my $method = MOP::Method->new(
280 body => MOP::Internal::Util::GET_GLOB_SLOT( $stash, $name, 'CODE' )
281 );
282 # and make sure it is local, and
283 # then return the method ...
284 6 ▶ return $method if $method->origin_stash eq $class;
Branch: 1 branch
BranchTrueFalseTotal
if $method->origin_stash eq $class 5 1 6
285 # or return nothing ...
286 1 return;
287 }
288  
289 sub add_required_method {
290 5 ▶ my ($self, $name) = @_;
Subroutine add_required_method: called CC=5 SCAR=16.1
Pod documented
291  
292 5 ▶ Carp::croak('[ARGS] You must specify the name of the required method to add')
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 5 5
293 unless $name;
294  
295 # if we already have a glob there ...
296 5 ▶ if ( my $glob = $self->stash->{ $name } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $glob = $self->stash->{$name}) 3 2 5
297 # and if we have a NULL CV in it, just return
298 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
299 # and if we don't and we have a CODE slot, we
300 # need to die because this doesn't make sense
301 Carp::croak("[CONFLICT] Cannot add a required method ($name) when there is a regular method already there")
302 2 ▶ if defined *{ $glob }{CODE};
Branch: 1 branch
BranchTrueFalseTotal
if defined *{$glob;}{"CODE"} 1 1 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 sub delete_required_method {
313 4 ▶ my ($self, $name) = @_;
Subroutine delete_required_method: called CC=5 SCAR=16.1
Pod documented
314  
315 4 ▶ Carp::croak('[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::croak("[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 sub has_method {
345 460 ▶ my $self = $_[0];
Subroutine has_method: called CC=7 SCAR=19.5
Pod documented
346 460 my $class = $self->name;
347 460 my $stash = $self->stash;
348 460 my $name = $_[1];
349  
350 460 ▶ Carp::croak('[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 sub get_method {
373 22 ▶ my $self = $_[0];
Subroutine get_method: called CC=8 SCAR=20.8
Pod documented
374 22 my $class = $self->name;
375 22 my $stash = $self->stash;
376 22 my $name = $_[1];
377  
378 22 ▶ Carp::croak('[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/DC0%0/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 table50%2/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 sub add_method {
401 1 ▶ my ($self, $name, $code) = @_;
Subroutine add_method: called CC=4 SCAR=15.2
Pod documented
402  
403 1 ▶ Carp::croak('[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::croak('[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 sub delete_method {
414 5 ▶ my ($self, $name) = @_;
Subroutine delete_method: called CC=8 SCAR=21.0
Pod documented
415  
416 5 ▶ Carp::croak('[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::croak("[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::croak("[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 sub get_method_alias {
464 14 ▶ my $class = $_[0]->name;
Subroutine get_method_alias: called CC=6 SCAR=17.9
Pod documented
465 14 my $stash = $_[0]->stash;
466 14 my $name = $_[1];
467  
468 14 ▶ Carp::croak('[ARGS] You must specify the name of the method alias to look for')
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 14 14
469 unless $name;
470  
471 # check the easy cases first ...
472 14 ▶ return unless exists $stash->{ $name };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $stash->{$name} 2 12 14
473 12 ▶ 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 11 12
474  
475 # now we grab the CV ...
476 11 ▶ 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")) 8 3 11
477 8 my $method = MOP::Method->new( body => $code );
478 # and make sure it is not local, and
479 # then return accordingly
480 8 ▶ return $method
Branch: 1 branch
BranchTrueFalseTotal
if $method->origin_stash ne $class 6 2 8
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 sub alias_method {
496 420 ▶ my ($self, $name, $code) = @_;
Subroutine alias_method: called CC=4 SCAR=15.2
Pod documented
497  
498 420 ▶ Carp::croak('[ARGS] You must specify the name of the method alias to add')
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 420 420
499 unless $name;
500  
501 420 ▶ Carp::croak('[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 420 420
Condition33%1/3
$code and ref $code eq "CODE"
!ll&&!rl&&r
00420
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 420 MOP::Internal::Util::INSTALL_CV( $self->name, $name, $code, set_subname => 0 );
505 420 return;
506 }
507  
508 sub delete_method_alias {
509 5 ▶ my ($self, $name) = @_;
Subroutine delete_method_alias: called CC=6 SCAR=17.9
Pod documented
510  
511 5 ▶ Carp::croak('[ARGS] You must specify the name of the method alias to remove')
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 5 5
512 unless $name;
513  
514 # check if we have a stash entry for $name ...
515 5 ▶ if ( my $glob = $self->stash->{ $name } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $glob = $self->stash->{$name}) 4 1 5
516 # and if we have a NULL CV in it, ...
517 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
518 # then we need to die because this
519 # shouldn't happen, we should only
520 # delete regular methods.
521 1 Carp::croak("[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 3 ▶ return unless defined *{ $glob }{CODE};
Branch: 1 branch
BranchTrueFalseTotal
unless defined *{$glob;}{"CODE"} 1 2 3
526  
527 # we need to make sure it is local, and
528 # otherwise, error accordingly
529 2 my $method = MOP::Method->new( body => *{ $glob }{CODE} );
530  
531 2 ▶ Carp::croak("[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 1 2
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 1 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 2 return;
541 }
542  
543 sub has_method_alias {
544 30 ▶ my $class = $_[0]->name;
Subroutine has_method_alias: called CC=5 SCAR=16.1
Pod documented
545 30 my $stash = $_[0]->stash;
546 30 my $name = $_[1];
547  
548 30 ▶ Carp::croak('[ARGS] You must specify the name of the method alias to look for')
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 30 30
549 unless $name;
550  
551 # check these two easy cases first ...
552 30 ▶ return 0 unless exists $stash->{ $name };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $stash->{$name} 2 28 30
553 28 ▶ 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 23 28
554  
555 # now we grab the CV and make sure it is
556 # local, and return accordingly
557 23 ▶ 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")) 20 3 23
558 20 return MOP::Method->new( body => $code )->origin_stash ne $class;
559 }
560  
561 # if there was no CV, return false.
562 3 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 sub all_slots {
575 43 ▶ my $self = shift;
Subroutine all_slots: called CC=2 SCAR=6.9
Pod documented
576 43 my $has = MOP::Internal::Util::GET_GLOB_SLOT( $self->stash, 'HAS', 'HASH' );
577 43 ▶ return unless $has;
Branch: 1 branch
BranchTrueFalseTotal
unless $has 25 18 43
578 return map {
579 18 MOP::Slot->new(
580 name => $_,
581 18 initializer => $has->{ $_ }
582 )
583 } keys %$has;
584 }
585  
586 # just the local slots
587 sub slots {
588 26 ▶ my $self = shift;
Subroutine slots: called CC=3 SCAR=11.3
Pod documented
589 26 my $class = $self->name;
590 26 my @roles = $self->roles;
591 return grep {
592 26 ▶ $_->origin_stash eq $class
Branch: 1 branch
BranchTrueFalseTotal
$_->origin_stash eq $class or @roles and $_->was_aliased_from(@roles) 2 4 6
Condition50%3/6
$_->origin_stash eq $class || @roles && $_->was_aliased_from(@roles)
l!l&&r!l&&!r
402
@roles and $_->was_aliased_from(@roles)
!ll&&!rl&&r
200
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 sub aliased_slots {
600 8 ▶ my $self = shift;
Subroutine aliased_slots: called CC=1 SCAR=0.0
Pod documented
601 8 my $class = $self->name;
602 8 return grep { $_->origin_stash ne $class } $self->all_slots
603 }
604  
605 ## regular ...
606  
607 sub has_slot {
608 11 ▶ my $self = $_[0];
Subroutine has_slot: called CC=6 SCAR=18.3
Pod documented
609 11 my $name = $_[1];
610 11 my $class = $self->name;
611 11 my $has = MOP::Internal::Util::GET_GLOB_SLOT( $self->stash, 'HAS', 'HASH' );
612  
613 11 ▶ Carp::croak('[ARGS] You must specify the name of the slot to look for')
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 11 11
614 unless $name;
615  
616 11 ▶ return unless $has;
Branch: 1 branch
BranchTrueFalseTotal
unless $has 3 8 11
617 8 ▶ return unless exists $has->{ $name };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $has->{$name} 4 4 8
618  
619 4 my @roles = $self->roles;
620 my $slot = MOP::Slot->new(
621 name => $name,
622 4 initializer => $has->{ $name }
623 );
624  
625 4 ▶ 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
202
@roles and $slot->was_aliased_from(@roles)
!ll&&!rl&&r
200
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 sub get_slot {
630 16 ▶ my $self = $_[0];
Subroutine get_slot: called CC=7 SCAR=19.7
Pod documented
631 16 my $name = $_[1];
632 16 my $class = $self->name;
633 16 my $has = MOP::Internal::Util::GET_GLOB_SLOT( $self->stash, 'HAS', 'HASH' );
634  
635 16 ▶ Carp::croak('[ARGS] You must specify the name of the slot to get')
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 16 16
636 unless $name;
637  
638 16 ▶ return unless $has;
Branch: 1 branch
BranchTrueFalseTotal
unless $has 3 13 16
639 13 ▶ return unless exists $has->{ $name };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $has->{$name} 4 9 13
640  
641 9 my @roles = $self->roles;
642 my $slot = MOP::Slot->new(
643 name => $name,
644 9 initializer => $has->{ $name }
645 );
646  
647 9 ▶ return $slot
Branch: 1 branch
BranchTrueFalseTotal
if $slot->origin_stash eq $class or @roles and $slot->was_aliased_from(@roles) 8 1 9
Condition50%3/6
$slot->origin_stash eq $class or @roles and $slot->was_aliased_from(@roles)
l!l&&r!l&&!r
801
@roles and $slot->was_aliased_from(@roles)
!ll&&!rl&&r
100
MC/DC0%0/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 table25%1/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 1 return;
652 }
653  
654 sub add_slot {
655 3 ▶ my $self = $_[0];
Subroutine add_slot: called CC=7 SCAR=20.2
Pod documented
656 3 my $name = $_[1];
657 3 my $initializer = $_[2];
658  
659 3 ▶ Carp::croak('[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::croak('[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
030
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 table0%0/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 sub delete_slot {
684 4 ▶ my $self = $_[0];
Subroutine delete_slot: called CC=5 SCAR=16.1
Pod documented
685 4 my $name = $_[1];
686 4 my $stash = $self->stash;
687 4 my $class = $self->name;
688  
689 4 ▶ Carp::croak('[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::croak("[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 sub has_slot_alias {
709 8 ▶ my $self = $_[0];
Subroutine has_slot_alias: called CC=4 SCAR=13.9
Pod documented
710 8 my $name = $_[1];
711 8 my $class = $self->name;
712 8 my $has = MOP::Internal::Util::GET_GLOB_SLOT( $self->stash, 'HAS', 'HASH' );
713  
714 8 ▶ Carp::croak('[ARGS] You must specify the name of the slot alias to look for')
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 8 8
715 unless $name;
716  
717 8 ▶ return unless $has;
Branch: 1 branch
BranchTrueFalseTotal
unless $has 1 7 8
718 7 ▶ return unless exists $has->{ $name };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $has->{$name} 2 5 7
719  
720 return MOP::Slot->new(
721 name => $name,
722 5 initializer => $has->{ $name }
723 )->origin_stash ne $class;
724 }
725  
726 sub get_slot_alias {
727 8 ▶ my $self = $_[0];
Subroutine get_slot_alias: called CC=5 SCAR=16.1
Pod documented
728 8 my $name = $_[1];
729 8 my $class = $self->name;
730 8 my $has = MOP::Internal::Util::GET_GLOB_SLOT( $self->stash, 'HAS', 'HASH' );
731  
732 8 ▶ Carp::croak('[ARGS] You must specify the name of the slot alias to get')
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 8 8
733 unless $name;
734  
735 8 ▶ return unless $has;
Branch: 1 branch
BranchTrueFalseTotal
unless $has 1 7 8
736 7 ▶ return unless exists $has->{ $name };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $has->{$name} 1 6 7
737  
738 my $slot = MOP::Slot->new(
739 name => $name,
740 6 initializer => $has->{ $name }
741 );
742  
743 6 ▶ return $slot
Branch: 1 branch
BranchTrueFalseTotal
if $slot->origin_stash ne $class 4 2 6
744 if $slot->origin_stash ne $class;
745  
746 2 return;
747 }
748  
749 sub alias_slot {
750 3 ▶ my $self = $_[0];
Subroutine alias_slot: called CC=7 SCAR=20.2
Pod documented
751 3 my $name = $_[1];
752 3 my $initializer = $_[2];
753  
754 3 ▶ Carp::croak('[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::croak('[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
030
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 table0%0/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::croak('[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 sub delete_slot_alias {
776 4 ▶ my $self = $_[0];
Subroutine delete_slot_alias: called CC=5 SCAR=16.1
Pod documented
777 4 my $name = $_[1];
778 4 my $stash = $self->stash;
779 4 my $class = $self->name;
780  
781 4 ▶ Carp::croak('[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::croak("[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 »