Prototyped.pm

filter: Statementstmt 82.3% 730 / 887 Branchbran 68.4% 286 / 418 Conditioncond 64.3% 83 / 129 MC/DCmcdc 35.8% 29 / 81 Subroutinesub 85.8% 91 / 106 Podpod 100% 7 / 7 totaltotal 75.3% 1226 / 1628 CC 278 SCAR 30.9
CC
278
Cov
77%
addSlotsParser
90.4
EXISTS
42.0
deleteSlots
28.0
CRAP 21.9
↑ Summary Prototyped/Graph.pm »
1 #############################################################################
2 #
3 # Class::Prototyped - Fast prototype-based OO programming in Perl
4 #
5 # Author: Ned Konz and Toby Ovod-Everett
6 #############################################################################
7 # Copyright 2001-2024 Ned Konz and Toby Ovod-Everett. All rights reserved.
8 #
9 # This program is free software; you can redistribute it and/or modify it
10 # under the same terms as Perl itself.
11 #
12 # For comments, questions, bugs or general interest, feel free to
13 # contact Toby Ovod-Everett at toby@ovod-everett.org
14 #############################################################################
15  
16 # Class::Prototyped - Fast prototype-based OO programming in Perl
17  
18 package Class::Prototyped::Mirror;
19 sub PREFIX() { 'PKG0x' }
20 sub PREFIX_LENGTH() { 5 }
21  
22  
23 package Class::Prototyped;
24 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 13 ▶use Carp();
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 13 $Class::Prototyped::VERSION = '1.16';
28  
29 13 ▶sub import {
Subroutine import: called CC=9 SCAR=22.0
30 13 ▶ while (my $symbol = shift) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $symbol = shift()) 30 13 43
31 30 ▶ if ($symbol eq ':OVERLOAD') {
Branch: 5 branches
BranchTrueFalseTotal
if ($symbol eq ':OVERLOAD') { } 2 28 30
elsif ($symbol eq ':REFLECT') { } 5 23 28
elsif ($symbol eq ':EZACCESS') { } 5 18 23
elsif ($symbol eq ':SUPER_FAST') { } 0 18 18
elsif ($symbol eq ':NEW_MAIN') { } 4 14 18
32 2 ▶ unless (scalar keys %Class::Prototyped::overloadable_symbols) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar keys %Class::Prototyped::overloadable_symbols) 2 0 2
33 2 ▶ eval "use overload";
Subroutine BEGIN: called CC=1 SCAR=0.0
34 2 @Class::Prototyped::overloadable_symbols{ map { split }
35 values %overload::ops } = undef;
36 }
37 }
38 elsif ($symbol eq ':REFLECT') {
39 *UNIVERSAL::reflect =
40 5 ▶ sub { Class::Prototyped::Mirror->new($_[0]) };
Subroutine __ANON__: called CC=1 SCAR=0.0
41 }
42 elsif ($symbol eq ':EZACCESS') {
43 13 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 5 foreach my $call (
46 qw(addSlot addSlots deleteSlot deleteSlots getSlot getSlots super)
47 ) {
48 35 ▶ *{$call} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
49 116 my $obj = shift->reflect;
50 116 UNIVERSAL::can($obj, $call)->($obj, @_);
51 35 };
52 }
53 }
54 elsif ($symbol eq ':SUPER_FAST') {
55 0 *Class::Prototyped::Mirror::super =
56 \&Class::Prototyped::Mirror::super_fast;
57 }
58 elsif ($symbol eq ':NEW_MAIN') {
59 4 ▶ *main::new = sub { Class::Prototyped->new(@_) };
Subroutine __ANON__: called CC=1 SCAR=0.0
60 }
61 }
62 }
63  
64 # Constructor. Pass in field definitions.
65 93 ▶sub new {
Subroutine new: called CC=2 SCAR=7.1
Pod documented
66 93 my $class = shift;
67  
68 93 ▶ Carp::croak("odd number of slot parameters to new\n") if scalar(@_) % 2;
Branch: 1 branch
BranchTrueFalseTotal
if scalar(@_) % 2 0 93 93
69  
70 93 $class->newCore('new', undef, @_);
71 }
72  
73 1 ▶sub newPackage {
Subroutine newPackage: called CC=2 SCAR=7.0
Pod documented
74 1 my $class = shift;
75 1 my $package = shift;
76  
77 1 ▶ Carp::croak("odd number of slot parameters to newPackage\n") if scalar(@_) % 2;
Branch: 1 branch
BranchTrueFalseTotal
if scalar(@_) % 2 0 1 1
78  
79 1 $class->newCore('newPackage', $package, @_);
80 }
81  
82 # Creates a copy of an object
83 15 ▶sub clone {
Subroutine clone: called CC=2 SCAR=7.1
Pod documented
84 15 my $original = shift;
85  
86 15 ▶ Carp::croak("odd number of slot parameters to clone\n") if scalar(@_) % 2;
Branch: 1 branch
BranchTrueFalseTotal
if scalar(@_) % 2 0 15 15
87  
88 15 $original->newCore('clone', undef, @_);
89 }
90  
91 1 ▶sub clonePackage {
Subroutine clonePackage: called CC=2 SCAR=7.0
Pod documented
92 1 my $original = shift;
93 1 my $package = shift;
94  
95 1 ▶ Carp::croak("odd number of slot parameters to clonePackage\n") if scalar(@_) % 2;
Branch: 1 branch
BranchTrueFalseTotal
if scalar(@_) % 2 0 1 1
96  
97 1 $original->newCore('clonePackage', $package, @_);
98 }
99  
100  
101 110 ▶sub newCore {
Subroutine newCore: called CC=13 SCAR=25.7
Pod documented
102 110 my $class = shift;
103 110 my $caller = shift;
104 110 my $package = shift;
105  
106 110 my $isPackage = substr($caller, -7) eq 'Package';
107 110 my $isNew = substr($caller, 0, 3) eq 'new';
108 110 my $isClone = substr($caller, 0, 5) eq 'clone';
109  
110 110 ▶ Carp::croak("odd number of slot parameters to $caller\n") if scalar(@_) % 2;
Branch: 1 branch
BranchTrueFalseTotal
if scalar(@_) % 2 0 110 110
111  
112 110 my $class_mirror = $class->reflect;
113  
114 110 my($self, $tied);
115 {
116 13 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
117  
118 110 ▶ if ( $isPackage ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($isPackage) { } 2 108 110
119 2 ▶ if (scalar(keys %{"$package\::"})) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %{"${package}::";}) 0 2 2
120 0 Carp::croak(
121 "attempt to use newPackage with already existing package\n"
122 . "package: $package");
123 }
124 2 my %self;
125 2 tie %self, $class_mirror->tiedInterfacePackage();
126 2 $tied = tied %self;
127 2 $Class::Prototyped::Mirror::objects{$package} = $self = \%self;
128 }
129 else {
130 108 $self = {};
131 108 $package = Class::Prototyped::Mirror::PREFIX . substr("$self", 7, -1); # HASH($package)
132  
133 108 tie %$self, $class->reflect->tiedInterfacePackage();
134  
135 108 $tied = tied %$self;
136 108 *{"$package\::DESTROY"} = \&Class::Prototyped::DESTROY;
137 }
138 }
139  
140 110 $tied->package($package);
141 110 @{ $tied->{isa} } = qw(Class::Prototyped);
142 110 $tied->{vivified_parents} = 1;
143 110 $tied->{vivified_methods} = 1;
144 110 $tied->{defaults} = $class_mirror->_defaults;
145  
146 110 bless $self, $package; # in my own package
147  
148 110 ▶ my $parsedSlots = scalar @_ || $isClone ?
Branch: 2 branches
BranchTrueFalseTotal
scalar @_ || $isClone ? : 89 21 110
$isClone ? : 16 73 89
Condition100%3/3
scalar @_ or $isClone
l!l&&r!l&&!r
751421
MC/DC100%2/2
scalar @_ or $isClone
scalar @_ $isClone
Truth table100%3/3
A or B
A= scalar @_B= $isClone
ABresult
1X1
011
000
149 $self->reflect->addSlotsParser( ( $isClone ? $class->reflect->getSlots() : () ), @_ ) :
150 [];
151  
152 110 ▶ if ( $isNew ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($isNew) 94 16 110
153 94 ▶ my $classstar = ( ref($class) &&
Branch: 1 branch
BranchTrueFalseTotal
ref $class && substr(ref $class, 0, 5) ne 'PKG0x' ? : 6 88 94
Condition100%3/3
ref $class and substr(ref $class, 0, 5) ne "PKG0x"
!ll&&!rl&&r
8446
MC/DC100%2/2
ref $class and substr(ref $class, 0, 5) ne "PKG0x"
ref $class substr(ref $class, 0, 5) ne "PKG0x"
Truth table100%3/3
A and B
A= ref $classB= substr(ref $class, 0, 5) ne "PKG0x"
ABresult
0X0
100
111
154 substr(ref($class), 0, Class::Prototyped::Mirror::PREFIX_LENGTH) ne
155 Class::Prototyped::Mirror::PREFIX
156 ) ? ref($class) : $class; # allow object for named class to provide a class name
157  
158 94 ▶ if (!(grep { $_->[0] eq 'class*' } @$parsedSlots) && $classstar ne 'Class::Prototyped')
Branch: 1 branch
BranchTrueFalseTotal
if (not grep({$_->[0] eq "class*";} @$parsedSlots) and $classstar ne "Class::Prototyped") 38 56 94
Condition66%2/3
not grep({$_->[0] eq "class*";} @$parsedSlots) and $classstar ne "Class::Prototyped"
!ll&&!rl&&r
05638
MC/DC50%1/2
not grep({$_->[0] eq "class*";} @$parsedSlots) and $classstar ne "Class::Prototyped"
not grep({$_->[0] eq "class*";} @$parsedSlots) $classstar ne "Class::Prototyped"
Truth table66%2/3
not(A) and B
A= not grep({$_->[0] eq "class*";} @$parsedSlots)B= $classstar ne "Class::Prototyped"
ABresult
0X0
100
111
159 {
160 38 unshift(@$parsedSlots, @{$self->reflect->addSlotsParser('class*' => $classstar)});
161 }
162 }
163  
164 110 ▶ $self->reflect->addParsedSlots($parsedSlots) if scalar(@$parsedSlots);
Branch: 1 branch
BranchTrueFalseTotal
if scalar @$parsedSlots 96 14 110
165  
166 110 return $self;
167 }
168  
169 1115 ▶sub reflect {
Subroutine reflect: called CC=2 SCAR=7.2
Pod documented
170 1115 ▶ return $Class::Prototyped::Mirror::mirrors{ $_[0] } || Class::Prototyped::Mirror->new($_[0]);
Condition66%2/3
$Class::Prototyped::Mirror::mirrors{$_[0]} || 'Class::Prototyped::Mirror'->new($_[0])
l!l&&r!l&&!r
1999160
MC/DC0%0/2
$Class::Prototyped::Mirror::mirrors{$_[0]} || 'Class::Prototyped::Mirror'->new($_[0])
$Class::Prototyped::Mirror::mirrors{$_[0]} 'Class::Prototyped::Mirror'->new($_[0])
Truth table66%2/3
A || B
A= $Class::Prototyped::Mirror::mirrors{$_[0]}B= 'Class::Prototyped::Mirror'->new($_[0])
ABresult
1X1
011
000
171 }
172  
173 105 ▶sub destroy {
Subroutine destroy: called CC=1 SCAR=0.0
Pod documented
174 105 my $self = shift;
175 105 my $mirror = $self->reflect;
176 105 my(@otherOrder) = @{$mirror->_otherOrder};
177 105 $mirror->deleteSlots(@otherOrder);
178 }
179  
180 # Remove my symbol table
181 109 ▶sub DESTROY {
Subroutine DESTROY: called CC=14 SCAR=26.5
182 109 my $self = shift;
183 109 my $package = ref($self);
184 109 ▶ if ((substr($package, 0, Class::Prototyped::Mirror::PREFIX_LENGTH) eq
Branch: 1 branch
BranchTrueFalseTotal
if (substr($package, 0, 5) eq "PKG0x" and $package ne "Class::Prototyped") 108 1 109
Condition66%2/3
substr($package, 0, 5) eq "PKG0x" and $package ne "Class::Prototyped"
!ll&&!rl&&r
10108
MC/DC50%1/2
substr($package, 0, 5) eq "PKG0x" and $package ne "Class::Prototyped"
substr($package, 0, 5) eq "PKG0x" $package ne "Class::Prototyped"
Truth table66%2/3
A and B
A= substr($package, 0, 5) eq "PKG0x"B= $package ne "Class::Prototyped"
ABresult
0X0
100
111
185 Class::Prototyped::Mirror::PREFIX)
186 && ($package ne 'Class::Prototyped'))
187 {
188 13 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
189  
190 108 ▶ my $tied = tied(%$self) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $tied = tied %$self or return 108 0 108
191 108 my $parentOrder = $tied->{parentOrder};
192 108 my $isa = $tied->{isa};
193 108 my $slots = $tied->{slots};
194  
195 108 my (@deadIndices);
196 108 foreach my $i (0 .. $#$parentOrder) {
197 91 my $parent = $slots->{ $parentOrder->[$i] };
198 91 ▶ my $parentPackage = ref($parent) || $parent;
Condition66%2/3
ref $parent || $parent
l!l&&r!l&&!r
55360
MC/DC0%0/2
ref $parent || $parent
ref $parent $parent
Truth table66%2/3
A || B
A= ref $parentB= $parent
ABresult
1X1
011
000
199 push (@deadIndices, $i)
200 91 ▶ unless scalar(keys %{"$parentPackage\::"});
Branch: 1 branch
BranchTrueFalseTotal
unless scalar keys %{"${parentPackage}::";} 0 91 91
201 }
202  
203 108 foreach my $i (@deadIndices) {
204 0 delete($slots->{ $parentOrder->[$i] });
205 0 splice(@$parentOrder, $i, 1);
206 0 splice(@$isa, $i, 1);
207 }
208  
209 # this is required to re-cache @ISA
210 108 delete ${"$package\::"}{'::ISA::CACHE::'};
211 108 @$isa=@$isa;
212  
213 108 my $parent_DESTROY;
214 108 my (@isa_queue) = @{"$package\::ISA"};
215 108 my (%isa_cache);
216 108 ▶ while (my $pkg = shift @isa_queue) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pkg = shift @isa_queue) 233 82 315
217 233 ▶ exists $isa_cache{$pkg} and next;
Branch: 1 branch
BranchTrueFalseTotal
exists $isa_cache{$pkg} and next 61 172 233
218 172 my $code = *{"$pkg\::DESTROY"}{CODE};
219 172 ▶ if (defined $code && $code != \&Class::Prototyped::DESTROY) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $code and $code != \&Class::Prototyped::DESTROY) 26 146 172
Condition100%3/3
defined $code and $code != \&Class::Prototyped::DESTROY
!ll&&!rl&&r
2312326
MC/DC100%2/2
defined $code and $code != \&Class::Prototyped::DESTROY
defined $code $code != \&Class::Prototyped::DESTROY
Truth table100%3/3
A and B
A= defined $codeB= $code != \&Class::Prototyped::DESTROY
ABresult
0X0
100
111
220 26 $parent_DESTROY = $code;
221 26 last;
222 }
223 146 unshift (@isa_queue, @{"$pkg\::ISA"});
224 146 $isa_cache{$pkg} = undef;
225 }
226  
227 108 $self->destroy; # call the user destroy function
228  
229 108 ▶ $parent_DESTROY->($self) if defined $parent_DESTROY;
Branch: 1 branch
BranchTrueFalseTotal
if defined $parent_DESTROY 26 82 108
230  
231 108 $self->reflect->deleteSlots($self->reflect->slotNames('PARENT'));
232  
233 108 foreach my $key (keys %{"$package\::"}) {
234 836 delete ${"$package\::"}{$key};
235 }
236  
237 # this only works because we're not a multi-level package:
238 108 delete($main::{"$package\::"});
239  
240 108 delete($Class::Prototyped::Mirror::parents{$package});
241 }
242 }
243  
244 13 $Class::Prototyped::Mirror::ending = 0;
245 13 ▶sub END { $Class::Prototyped::Mirror::ending = 1 }
Subroutine END: called CC=1 SCAR=0.0
246  
247 package Class::Prototyped::Tied;
248 13 $Class::Prototyped::Tied::VERSION = '1.16';
249 13 @Class::Prototyped::Tied::DONT_LIE_FOR = qw(Data::Dumper);
250  
251 139 ▶sub TIEHASH {
Subroutine TIEHASH: called CC=2 SCAR=6.9
252 139 ▶ bless $_[1] || {
Condition100%2/2
$_[1] || {'package', undef, 'isa', undef, 'parentOrder', [], 'otherOrder', [], 'slots', {}, 'types', {}, 'attribs', {}, 'defaults', undef, 'vivified_parents', 0, 'vivified_methods', 0}
l!l
2137
MC/DC100%1/1
$_[1] || {'package', undef, 'isa', undef, 'parentOrder', [], 'otherOrder', [], 'slots', {}, 'types', {}, 'attribs', {}, 'defaults', undef, 'vivified_parents', 0, 'vivified_methods', 0}
$_[1]
Truth table100%2/2
A
A= $_[1]
Aresult
11
00
253 package => undef,
254 isa => undef,
255 parentOrder => [],
256 otherOrder => [],
257 slots => {},
258 types => {},
259 attribs => {},
260 defaults => undef,
261 vivified_parents => 0,
262 vivified_methods => 0,
263 },
264 $_[0];
265 }
266  
267 20 ▶sub FIRSTKEY {
Subroutine FIRSTKEY: called CC=4 SCAR=13.9
268 20 $_[0]->{dont_lie} = 0;
269 20 my $caller = (caller(0))[0];
270 20 foreach my $i (@Class::Prototyped::Tied::DONT_LIE_FOR) {
271 20 ▶ $_[0]->{dont_lie} = $caller eq $i and last;
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"dont_lie"} = $caller eq $i and last 18 2 20
272 }
273 20 $_[0]->{iter} = 1;
274 20 $_[0]->{cachedOrder} = [@{ $_[0]->{parentOrder} }, @{ $_[0]->{otherOrder} }];
275  
276 20 ▶ unless ($_[0]->{dont_lie}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($_[0]{"dont_lie"}) 2 18 20
277 2 my $slots = $_[0]->{slots};
278 2 @{ $_[0]->{cachedOrder} } =
279 5 grep { !UNIVERSAL::isa($slots->{$_}, 'CODE') }
280 2 @{ $_[0]->{cachedOrder} };
281 }
282 20 return $_[0]->{cachedOrder}->[0];
283 }
284  
285 255 ▶sub NEXTKEY {
Subroutine NEXTKEY: called CC=1 SCAR=0.0
286 255 return $_[0]->{cachedOrder}->[ $_[0]->{iter}++ ];
287 }
288  
289 0 ▶sub EXISTS {
Subroutine EXISTS: not called CC=6 SCAR=37.4
290 0 ▶ exists $_[0]->{slots}->{ $_[1] } or return 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $_[0]{"slots"}{$_[1]} or return 0 0 0 0
291 0 ▶ UNIVERSAL::isa($_[0]->{slots}->{ $_[1] }, 'CODE') or return 1;
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($_[0]{"slots"}{$_[1]}, "CODE") or return 1 0 0 0
292 0 my $dont_lie = 0;
293 0 my $caller = (caller(0))[0];
294 0 foreach my $i (@Class::Prototyped::Tied::DONT_LIE_FOR) {
295 0 ▶ $dont_lie = $caller eq $i and last;
Branch: 1 branch
BranchTrueFalseTotal
$dont_lie = $caller eq $i and last 0 0 0
296 }
297 0 ▶ return $dont_lie ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$dont_lie ? : 0 0 0
298 }
299  
300 1 ▶sub CLEAR {
Subroutine CLEAR: called CC=1 SCAR=0.0
301 1 Carp::croak("attempt to call CLEAR on the hash interface"
302 . " of a Class::Prototyped object\n");
303 }
304  
305 139 ▶sub package {
Subroutine package: called CC=2 SCAR=6.9
306 139 ▶ return $_[0]->{package} unless @_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ > 1 2 137 139
307 13 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
308 137 $_[0]->{isa} = \@{"$_[1]\::ISA"};
309 137 $_[0]->{package} = $_[1];
310 }
311  
312 #### Default Tied implementation
313 package Class::Prototyped::Tied::Default;
314 13 $Class::Prototyped::Tied::Default::VERSION = '1.16';
315 13 @Class::Prototyped::Tied::Default::ISA = qw(Class::Prototyped::Tied);
316  
317 10 ▶sub STORE {
Subroutine STORE: called CC=4 SCAR=13.9
318 10 my $slots = $_[0]->{slots};
319  
320 Carp::croak(
321 "attempt to access non-existent slot through tied hash object interface"
322 )
323 10 ▶ unless exists $slots->{ $_[1] };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$slots{$_[1]} 1 9 10
324  
325 Carp::croak(
326 "attempt to access METHOD slot through tied hash object interface")
327 9 ▶ if UNIVERSAL::isa($slots->{ $_[1] }, 'CODE');
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($$slots{$_[1]}, "CODE") 1 8 9
328  
329 8 ▶ Carp::croak(
Branch: 1 branch
BranchTrueFalseTotal
if substr($_[1], -1) eq "*" 0 8 8
330 "attempt to modify parent slot through the tied hash object interface")
331 if substr($_[1], -1) eq '*';
332  
333 8 $slots->{ $_[1] } = $_[2];
334 }
335  
336 283 ▶sub FETCH {
Subroutine FETCH: called CC=6 SCAR=17.9
337 283 my $slots = $_[0]->{slots};
338  
339 Carp::croak(
340 "attempt to access non-existent slot through tied hash object interface:\n"
341 . "$_[1]")
342 283 ▶ unless exists $slots->{ $_[1] };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$slots{$_[1]} 5 278 283
343  
344 278 ▶ if (UNIVERSAL::isa($slots->{ $_[1] }, 'CODE')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($$slots{$_[1]}, "CODE")) 127 151 278
345 127 my $dont_lie = 0;
346 127 my $caller = (caller(0))[0];
347 127 foreach my $i (@Class::Prototyped::Tied::DONT_LIE_FOR) {
348 127 ▶ $dont_lie = $caller eq $i and last;
Branch: 1 branch
BranchTrueFalseTotal
$dont_lie = $caller eq $i and last 126 1 127
349 }
350 Carp::croak(
351 127 ▶ "attempt to access METHOD slot through tied hash object interface")
Branch: 1 branch
BranchTrueFalseTotal
unless $dont_lie 1 126 127
352 unless $dont_lie;
353 }
354  
355 277 $slots->{ $_[1] };
356 }
357  
358 0 ▶sub DELETE {
Subroutine DELETE: not called CC=1 SCAR=6.9
359 0 Carp::croak "attempt to delete a slot through tied hash object interface";
360 }
361  
362 #### AutoVivifying Tied implementation
363 package Class::Prototyped::Tied::AutoVivify;
364 13 $Class::Prototyped::Tied::AutoVivify::VERSION = '1.16';
365 13 @Class::Prototyped::Tied::AutoVivify::ISA = qw(Class::Prototyped::Tied);
366  
367 3 ▶sub STORE {
Subroutine STORE: called CC=5 SCAR=16.2
368 3 my $slots = $_[0]->{slots};
369  
370 3 ▶ Carp::croak(
Branch: 1 branch
BranchTrueFalseTotal
if substr($_[1], -1) eq "*" 0 3 3
371 "attempt to modify parent slot through the tied hash object interface")
372 if substr($_[1], -1) eq '*';
373  
374 3 ▶ if (exists $slots->{ $_[1] }) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$slots{$_[1]}) { } 1 2 3
375 Carp::croak(
376 "attempt to access METHOD slot through tied hash object interface")
377 1 ▶ if UNIVERSAL::isa($slots->{ $_[1] }, 'CODE');
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($$slots{$_[1]}, "CODE") 0 1 1
378 }
379 else {
380 2 my $slot = $_[1];
381 2 $slots->{ $_[1] } = $_[2];
382 1 ▶ my $implementation = bless sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
383 1 ▶ @_ > 1 ? $slots->{$slot} = $_[1] : $slots->{$slot};
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 0 1 1
384 2 }, 'Class::Prototyped::FieldAccessor';
385 13 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
386 2 local $^W = 0; # suppress redefining messages.
387 2 *{ $_[0]->package . "::$slot" } = $implementation;
388 2 push (@{ $_[0]->{otherOrder} }, $slot);
389 2 $_[0]->{types}->{$slot} = 'FIELD';
390 }
391  
392 Carp::croak(
393 "attempt to access non-existent slot through tied hash object interface"
394 )
395 3 ▶ unless exists $slots->{ $_[1] };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$slots{$_[1]} 0 3 3
396  
397 3 $slots->{ $_[1] } = $_[2];
398 }
399  
400 9 ▶sub FETCH {
Subroutine FETCH: called CC=6 SCAR=18.0
401 9 my $slots = $_[0]->{slots};
402  
403 9 ▶ if (exists $slots->{ $_[1] }
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$slots{$_[1]} and UNIVERSAL::isa($$slots{$_[1]}, "CODE")) 1 8 9
Condition100%3/3
exists $$slots{$_[1]} and UNIVERSAL::isa($$slots{$_[1]}, "CODE")
!ll&&!rl&&r
171
MC/DC100%2/2
exists $$slots{$_[1]} and UNIVERSAL::isa($$slots{$_[1]}, "CODE")
exists $$slots{$_[1]} UNIVERSAL::isa($$slots{$_[1]}, "CODE")
Truth table100%3/3
A and B
A= exists $$slots{$_[1]}B= UNIVERSAL::isa($$slots{$_[1]}, "CODE")
ABresult
0X0
100
111
404 and UNIVERSAL::isa($slots->{ $_[1] }, 'CODE'))
405 {
406 1 my $dont_lie = 0;
407 1 my $caller = (caller(0))[0];
408 1 foreach my $i (@Class::Prototyped::Tied::DONT_LIE_FOR) {
409 1 ▶ $dont_lie = $caller eq $i and last;
Branch: 1 branch
BranchTrueFalseTotal
$dont_lie = $caller eq $i and last 0 1 1
410 }
411 Carp::croak(
412 1 ▶ "attempt to access METHOD slot through tied hash object interface")
Branch: 1 branch
BranchTrueFalseTotal
unless $dont_lie 1 0 1
413 unless $dont_lie;
414 }
415  
416 8 $slots->{ $_[1] };
417 }
418  
419 0 ▶sub EXISTS {
Subroutine EXISTS: not called CC=1 SCAR=6.9
420 0 exists $_[0]->{slots}->{ $_[1] };
421 }
422  
423 0 ▶sub DELETE {
Subroutine DELETE: not called CC=5 SCAR=31.5
424 0 my $slots = $_[0]->{slots};
425  
426 0 ▶ if (UNIVERSAL::isa($slots->{ $_[1] }, 'CODE')
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($$slots{$_[1]}, "CODE") and (caller 0)[0] ne "Data::Dumper") 0 0 0
Condition0%0/3
UNIVERSAL::isa($$slots{$_[1]}, "CODE") and (caller 0)[0] ne "Data::Dumper"
!ll&&!rl&&r
000
MC/DC0%0/2
UNIVERSAL::isa($$slots{$_[1]}, "CODE") and (caller 0)[0] ne "Data::Dumper"
UNIVERSAL::isa($$slots{$_[1]}, "CODE") (caller 0)[0] ne "Data::Dumper"
Truth table0%0/3
A and B
A= UNIVERSAL::isa($$slots{$_[1]}, "CODE")B= (caller 0)[0] ne "Data::Dumper"
ABresult
0X0
100
111
427 && (caller(0))[0] ne 'Data::Dumper')
428 {
429 0 Carp::croak
430 "attempt to delete METHOD slot through tied hash object interface";
431 }
432  
433 0 my $package = $_[0]->package;
434 0 my $slot = $_[1];
435 {
436 0 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.2
437 0 my $name = "$package\::$slot";
438  
439 # save the glob...
440 0 local *old = *{$name};
441  
442 # and restore everything else
443 0 local *new;
444 0 foreach my $type (qw(HASH IO FORMAT SCALAR ARRAY)) {
445 0 my $elem = *old{$type};
446 0 ▶ next if !defined($elem);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $elem 0 0 0
447 0 *new = $elem;
448 }
449 0 *{$name} = *new;
450 }
451 0 my $otherOrder = $_[0]->{otherOrder};
452 0 @$otherOrder = grep { $_ ne $slot } @$otherOrder;
453 0 delete $slots->{$slot}; # and delete the data/sub ref
454 0 delete $_[0]->{types}->{$slot};
455 }
456  
457 # Everything that deals with modifying or inspecting the form
458 # of an object is done through a reflector.
459  
460 package Class::Prototyped::Mirror;
461 13 $Class::Prototyped::Mirror::VERSION = '1.16';
462 13 $Class::Prototyped::Mirror::PROFILE::VERSION = '1.16';
463 13 $Class::Prototyped::Mirror::SUPER::VERSION = '1.16';
464  
465 951 ▶sub new {
Subroutine new: called CC=11 SCAR=24.0
466 951 my $class = shift;
467 951 my($entity) = @_;
468  
469 951 ▶ if ( ref($entity) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $entity) 914 37 951
470 914 ▶ if (substr(ref($entity), 0, Class::Prototyped::Mirror::PREFIX_LENGTH) eq
Branch: 2 branches
BranchTrueFalseTotal
if (substr(ref $entity, 0, 5) eq 'PKG0x') { } 909 5 914
elsif ($Class::Prototyped::Mirror::objects{ref $entity} == $entity) { } 3 2 5
471 Class::Prototyped::Mirror::PREFIX)
472 {
473 909 return bless \$entity, 'Class::Prototyped::Mirror';
474 }
475 elsif ($Class::Prototyped::Mirror::objects{ ref($entity) } == $entity) {
476 3 ▶ return $Class::Prototyped::Mirror::mirrors{ $entity } ||= bless \$entity, 'Class::Prototyped::Mirror';
Condition50%1/2
$Class::Prototyped::Mirror::mirrors{$entity} ||= bless(\$entity, "Class::Prototyped::Mirror")
l!l
03
MC/DC0%0/1
$Class::Prototyped::Mirror::mirrors{$entity} ||= bless(\$entity, "Class::Prototyped::Mirror")
$Class::Prototyped::Mirror::mirrors{$entity}
Truth table50%1/2
A
A= $Class::Prototyped::Mirror::mirrors{$entity}
Aresult
11
00
477 }
478 else {
479 2 return Class::Prototyped::Mirror::Normal->new($entity);
480 }
481 }
482  
483 37 my $object;
484 37 ▶ unless ($object = $Class::Prototyped::Mirror::objects{ $entity }) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($object = $Class::Prototyped::Mirror::objects{$entity}) 27 10 37
485 27 my (%self);
486 my $tiepkg;
487 27 ▶ if ($entity eq 'Class::Prototyped') {
Branch: 1 branch
BranchTrueFalseTotal
if ($entity eq 'Class::Prototyped') { } 13 14 27
488 13 $tiepkg = 'Class::Prototyped::Tied::Default';
489 }
490 else {
491 13 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
492 14 $tiepkg = eval { ${"$entity\::ISA"}[0]->reflect->tiedInterfacePackage() };
493 14 ▶ $tiepkg = Class::Prototyped->reflect->tiedInterfacePackage() if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 2 12 14
494 }
495 27 tie %self, $tiepkg;
496 27 $object = $Class::Prototyped::Mirror::objects{ $entity } = \%self;
497 27 tied(%self)->package($entity);
498  
499 27 my $defaults;
500 27 ▶ if ($entity eq 'Class::Prototyped') {
Branch: 1 branch
BranchTrueFalseTotal
if ($entity eq 'Class::Prototyped') { } 13 14 27
501 13 $defaults = {FIELD => undef, METHOD => undef, PARENT => undef};
502 }
503 else {
504 13 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
505 14 $defaults = eval { ${"$entity\::ISA"}[0]->reflect->_defaults() };
506 14 ▶ $defaults = Class::Prototyped->reflect->_defaults() if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 2 12 14
507 }
508  
509 27 tied(%self)->{defaults} = $defaults;
510  
511 27 bless $object, $entity;
512 }
513 37 ▶ return $Class::Prototyped::Mirror::mirrors{ $entity } ||= bless \$object, 'Class::Prototyped::Mirror';
Condition100%2/2
$Class::Prototyped::Mirror::mirrors{$entity} ||= bless(\$object, "Class::Prototyped::Mirror")
l!l
829
MC/DC100%1/1
$Class::Prototyped::Mirror::mirrors{$entity} ||= bless(\$object, "Class::Prototyped::Mirror")
$Class::Prototyped::Mirror::mirrors{$entity}
Truth table100%2/2
A
A= $Class::Prototyped::Mirror::mirrors{$entity}
Aresult
11
00
514 }
515  
516  
517 #This code exists to support calling ->reflect->super on a "normal" object that
518 #is blessed into a C::P class.
519  
520 package Class::Prototyped::Mirror::Normal;
521 13 $Class::Prototyped::Mirror::Normal::VERSION = '1.16';
522 13 @Class::Prototyped::Mirror::Normal::ISA = qw(Class::Prototyped::Mirror);
523  
524 2 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
525 2 my $class = shift;
526 2 my($entity) = @_;
527  
528 2 my $temp = Class::Prototyped::Mirror->new(ref($entity));
529  
530 2 my $self = bless \(my $o = ${$temp}), $class;
531 2 $Class::Prototyped::Mirror::Normal::superselfs->{$self} = $entity;
532 2 return $self;
533 }
534  
535 1 ▶sub super {
Subroutine super: called CC=1 SCAR=0.0
536 1 my $mirror = shift;
537 1 (bless \$Class::Prototyped::Mirror::Normal::superselfs->{$mirror}, 'Class::Prototyped::Mirror')->super(@_);
538 }
539  
540 2 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
541 2 delete $Class::Prototyped::Mirror::Normal::superselfs->{$_[0]};
542 }
543  
544 package Class::Prototyped::Mirror;
545  
546 #### Interface to tied object
547  
548 0 ▶sub autoloadCall {
Subroutine autoloadCall: not called CC=1 SCAR=2.6
549 0 my $mirror = shift;
550  
551 0 my $package = $mirror->package();
552 13 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
553 0 my $call = ${"$package\::AUTOLOAD"};
554 0 $call =~ s/.*:://;
555 0 return $call;
556 }
557  
558 1 ▶sub package {
Subroutine package: called CC=1 SCAR=0.0
559 1 ref(${ $_[0] });
560 }
561  
562 132 ▶sub tiedInterfacePackage {
Subroutine tiedInterfacePackage: called CC=6 SCAR=20.3
563 132 my $self = shift;
564  
565 132 ▶ if ($_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]) { } 2 130 132
566 my $package = {
567 'default' => 'Class::Prototyped::Tied::Default',
568 'autovivify' => 'Class::Prototyped::Tied::AutoVivify',
569 2 ▶ }->{$_[0]} || $_[0];
Condition33%1/3
{'default', 'Class::Prototyped::Tied::Default', 'autovivify', 'Class::Prototyped::Tied::AutoVivify'}->{$_[0]} || $_[0]
l!l&&r!l&&!r
200
MC/DC0%0/2
{'default', 'Class::Prototyped::Tied::Default', 'autovivify', 'Class::Prototyped::Tied::AutoVivify'}->{$_[0]} || $_[0]
{'default', 'Class::Prototyped::Tied::Default', 'autovivify', 'Class::Prototyped::Tied::AutoVivify'}->{$_[0]} $_[0]
Truth table33%1/3
A || B
A= {'default', 'Class::Prototyped::Tied::Default', 'autovivify', 'Class::Prototyped::Tied::AutoVivify'}->{$_[0]}B= $_[0]
ABresult
1X1
011
000
570  
571 0 ▶ if ($package eq $_[0] && scalar(keys %{"$package\::"}) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($package eq $_[0] and scalar keys %{"${package}::";} == 0) 0 2 2
Condition33%1/3
$package eq $_[0] and scalar keys %{"${package}::";} == 0
!ll&&!rl&&r
200
MC/DC0%0/2
$package eq $_[0] and scalar keys %{"${package}::";} == 0
$package eq $_[0] scalar keys %{"${package}::";} == 0
Truth table33%1/3
A and B
A= $package eq $_[0]B= scalar keys %{"${package}::";} == 0
ABresult
0X0
100
111
572 0 eval "use $package";
573 0 ▶ Carp::croak "attempt to import package for :TIED_INTERFACE failed:\n$package"
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
574 if $@;
575 }
576  
577 2 tie %{ ${ $self } }, $package, tied(%{ ${ $self } });
578 2 return $package;
579 }
580 else {
581 130 return ref(tied(%{ ${ $self } }));
582 }
583 }
584  
585 4 ▶sub defaultAttributes {
Subroutine defaultAttributes: called CC=5 SCAR=16.4
586 4 my $mirror = shift;
587  
588 4 ▶ tied(%{ ${ $mirror } })->{defaults} = $_[0] if scalar(@_);
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ 1 3 4
589 4 my $defaults = $mirror->_defaults;
590  
591 4 my $retval = {};
592 0 ▶ $retval->{FIELD} = defined $defaults->{FIELD} ? {%{$defaults->{FIELD}}} : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $defaults->{'FIELD'} ? : 0 4 4
593 4 ▶ $retval->{METHOD} = defined $defaults->{METHOD} ? {%{$defaults->{METHOD}}} : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $defaults->{'METHOD'} ? : 1 3 4
594 0 ▶ $retval->{PARENT} = defined $defaults->{PARENT} ? {%{$defaults->{PARENT}}} : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $defaults->{'PARENT'} ? : 0 4 4
595 4 return $retval;
596 }
597  
598 0 ▶sub _isa {
Subroutine _isa: not called CC=1 SCAR=6.9
599 0 tied(%{ ${ $_[0] } })->isa;
600 }
601  
602 0 ▶sub _parentOrder {
Subroutine _parentOrder: not called CC=2 SCAR=17.9
603 0 my $tied = tied(%{ ${ $_[0] } });
604 0 ▶ $_[0]->_autovivify_parents unless $tied->{vivified_parents};
Branch: 1 branch
BranchTrueFalseTotal
unless $tied->{"vivified_parents"} 0 0 0
605 0 $tied->{parentOrder};
606 }
607  
608 105 ▶sub _otherOrder {
Subroutine _otherOrder: called CC=2 SCAR=7.0
609 105 my $tied = tied(%{ ${ $_[0] } });
610 105 ▶ $_[0]->_autovivify_methods unless $tied->{vivified_methods};
Branch: 1 branch
BranchTrueFalseTotal
unless $tied->{"vivified_methods"} 0 105 105
611 105 $tied->{otherOrder};
612 }
613  
614 0 ▶sub _slotOrder {
Subroutine _slotOrder: not called CC=3 SCAR=24.8
615 0 my $tied = tied(%{ ${ $_[0] } });
616 0 ▶ $_[0]->_autovivify_parents unless $tied->{vivified_parents};
Branch: 1 branch
BranchTrueFalseTotal
unless $tied->{"vivified_parents"} 0 0 0
617 0 ▶ $_[0]->_autovivify_methods unless $tied->{vivified_methods};
Branch: 1 branch
BranchTrueFalseTotal
unless $tied->{"vivified_methods"} 0 0 0
618 0 [@{ $tied->{parentOrder} }, @{ $tied->{otherOrder} }];
619 }
620  
621 53 ▶sub _slots {
Subroutine _slots: called CC=3 SCAR=11.2
622 53 my $tied = tied(%{ ${ $_[0] } });
623 53 ▶ $_[0]->_autovivify_parents unless $tied->{vivified_parents};
Branch: 1 branch
BranchTrueFalseTotal
unless $tied->{"vivified_parents"} 0 53 53
624 53 ▶ $_[0]->_autovivify_methods unless $tied->{vivified_methods};
Branch: 1 branch
BranchTrueFalseTotal
unless $tied->{"vivified_methods"} 0 53 53
625 53 $tied->{slots};
626 }
627  
628 3 ▶sub _types {
Subroutine _types: called CC=1 SCAR=0.0
629 3 tied(%{ ${ $_[0] } })->{types};
630 }
631  
632 9 ▶sub _attribs {
Subroutine _attribs: called CC=1 SCAR=0.0
633 9 tied(%{ ${ $_[0] } })->{attribs};
634 }
635  
636 138 ▶sub _defaults {
Subroutine _defaults: called CC=1 SCAR=0.0
637 138 tied(%{ ${ $_[0] } })->{defaults};
638 }
639  
640 0 ▶sub _vivified_parents {
Subroutine _vivified_parents: not called CC=2 SCAR=17.9
641 0 @_ > 1 ? tied(%{ ${ $_[0] } })->{vivified_parents} = $_[1] :
642 0 ▶ tied(%{ ${ $_[0] } })->{vivified_parents};
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 0 0 0
643 }
644  
645 1 ▶sub _vivified_methods {
Subroutine _vivified_methods: called CC=2 SCAR=8.4
646 1 @_ > 1 ? tied(%{ ${ $_[0] } })->{vivified_methods} = $_[1] :
647 0 ▶ tied(%{ ${ $_[0] } })->{vivified_methods};
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 1 0 1
648 }
649  
650 #The following returns package, _isa, _parentOrder, _otherOrder,
651 #_slots, _types, _attribs, and _defaults;
652 1064 ▶sub _everything {
Subroutine _everything: called CC=3 SCAR=11.0
653 1064 my $tied = tied(%{ ${ $_[0] } });
654 1064 ▶ $_[0]->_autovivify_parents unless $tied->{vivified_parents};
Branch: 1 branch
BranchTrueFalseTotal
unless $tied->{"vivified_parents"} 6 1058 1064
655 1064 ▶ $_[0]->_autovivify_methods unless $tied->{vivified_methods};
Branch: 1 branch
BranchTrueFalseTotal
unless $tied->{"vivified_methods"} 6 1058 1064
656  
657 return (
658 1064 ref(${ $_[0] }),
659 $tied->{isa},
660 $tied->{parentOrder},
661 $tied->{otherOrder},
662 $tied->{slots},
663 $tied->{types},
664 $tied->{attribs},
665 $tied->{defaults},
666 1064 );
667 }
668  
669 #### Autovivifivation support
670  
671 75 ▶sub _autovivify_parents {
Subroutine _autovivify_parents: called CC=10 SCAR=23.7
672 75 my $tied = tied(%{ ${ $_[0] } });
673 75 ▶ return if $tied->{vivified_parents};
Branch: 1 branch
BranchTrueFalseTotal
if $tied->{"vivified_parents"} 69 6 75
674  
675 6 my $mirror = shift;
676 6 $tied->{vivified_parents} = 1;
677  
678 6 my($package, $isa, $parentOrder, $otherOrder, $slots, $types, $attribs, undef) =
679 $mirror->_everything;
680  
681 6 ▶ if (scalar(grep { UNIVERSAL::isa($_, 'Class::Prototyped') } @$isa)
Branch: 1 branch
BranchTrueFalseTotal
if (scalar grep({UNIVERSAL::isa($_, "Class::Prototyped");} @$isa) and $isa->[-1] ne "Class::Prototyped") 0 6 6
Condition66%2/3
scalar grep({UNIVERSAL::isa($_, "Class::Prototyped");} @$isa) and $isa->[-1] ne "Class::Prototyped"
!ll&&!rl&&r
510
MC/DC0%0/2
scalar grep({UNIVERSAL::isa($_, "Class::Prototyped");} @$isa) and $isa->[-1] ne "Class::Prototyped"
scalar grep({UNIVERSAL::isa($_, "Class::Prototyped");} @$isa) $isa->[-1] ne "Class::Prototyped"
Truth table66%2/3
A and B
A= scalar grep({UNIVERSAL::isa($_, "Class::Prototyped");} @$isa)B= $isa->[-1] ne "Class::Prototyped"
ABresult
0X0
100
111
682 && $isa->[-1] ne 'Class::Prototyped')
683 {
684 0 push (@$isa, 'Class::Prototyped');
685 13 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
686 0 delete ${"$package\::"}{'::ISA::CACHE::'}; # re-cache @ISA
687 0 @$isa=@$isa;
688 }
689  
690 6 ▶ if (@{$parentOrder}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$parentOrder;}) 0 6 6
691 0 Carp::croak("attempt to autovivify in the "
692 . "presence of an existing parentOrder\n" . "package: $package");
693 }
694 6 my @isa = @$isa;
695 6 ▶ pop (@isa) if scalar(@isa) && $isa[-1] eq 'Class::Prototyped';
Branch: 1 branch
BranchTrueFalseTotal
if scalar @isa and $isa[-1] eq "Class::Prototyped" 1 5 6
Condition100%3/3
scalar @isa and $isa[-1] eq "Class::Prototyped"
!ll&&!rl&&r
231
MC/DC100%2/2
scalar @isa and $isa[-1] eq "Class::Prototyped"
scalar @isa $isa[-1] eq "Class::Prototyped"
Truth table100%3/3
A and B
A= scalar @isaB= $isa[-1] eq "Class::Prototyped"
ABresult
0X0
100
111
696  
697 6 foreach my $parentPackage (@isa) {
698 9 my $count = '';
699 9 my $slot = "$parentPackage$count*";
700 9 ▶ while (exists $slots->{$slot} || $slot eq 'self*') {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $slots->{$slot} or $slot eq "self*") 3 9 12
Condition66%2/3
exists $slots->{$slot} or $slot eq "self*"
l!l&&r!l&&!r
309
MC/DC50%1/2
exists $slots->{$slot} or $slot eq "self*"
exists $slots->{$slot} $slot eq "self*"
Truth table66%2/3
A or B
A= exists $slots->{$slot}B= $slot eq "self*"
ABresult
1X1
011
000
701 3 $slot = $parentPackage . (++$count) . '*';
702 }
703 9 push (@$parentOrder, $slot);
704 9 $slots->{$slot} = $parentPackage;
705 9 $types->{$slot} = 'PARENT';
706 }
707 }
708  
709 76 ▶sub _autovivify_methods {
Subroutine _autovivify_methods: called CC=9 SCAR=22.1
710 76 my $tied = tied(%{ ${ $_[0] } });
711 76 ▶ return if $tied->{vivified_methods};
Branch: 1 branch
BranchTrueFalseTotal
if $tied->{"vivified_methods"} 69 7 76
712  
713 7 my $mirror = shift;
714 7 $tied->{vivified_methods} = 1;
715  
716 7 my($package, $isa, $parentOrder, $otherOrder, $slots, $types, $attribs, undef) =
717 $mirror->_everything;
718  
719 13 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
720 7 foreach my $slot (grep { $_ ne 'DESTROY' } keys %{"$package\::"}) {
721 552 ▶ my $code = *{"$package\::$slot"}{CODE} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $code = *{"${package}::$slot";}{"CODE"} or next 481 71 552
722 481 ▶ ref($code) =~ /^Class::Prototyped::FieldAccessor/ and next;
Branch: 1 branch
BranchTrueFalseTotal
ref($code) =~ /^Class::Prototyped::FieldAccessor/ and next 1 480 481
723 480 ▶ Carp::croak("the slot self* is inviolable") if $slot eq 'self*';
Branch: 1 branch
BranchTrueFalseTotal
if $slot eq "self*" 0 480 480
724  
725 480 ▶ if (exists $slots->{$slot}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $slots->{$slot}) { } 1 479 480
726 Carp::croak("you overwrote a slot via an include $slot")
727 if !UNIVERSAL::isa($slots->{$slot}, 'CODE')
728 1 ▶ || $slots->{$slot} != $code;
Branch: 1 branch
BranchTrueFalseTotal
if not UNIVERSAL::isa($slots->{$slot}, "CODE") or $slots->{$slot} != $code 0 1 1
Condition33%1/3
not UNIVERSAL::isa($slots->{$slot}, "CODE") or $slots->{$slot} != $code
l!l&&r!l&&!r
001
MC/DC0%0/2
not UNIVERSAL::isa($slots->{$slot}, "CODE") or $slots->{$slot} != $code
not UNIVERSAL::isa($slots->{$slot}, "CODE") $slots->{$slot} != $code
Truth table33%1/3
not(A) or B
A= not UNIVERSAL::isa($slots->{$slot}, "CODE")B= $slots->{$slot} != $code
ABresult
1X1
011
000
729 }
730 else {
731 479 push (@$otherOrder, $slot);
732 479 $slots->{$slot} = $code;
733 479 $types->{$slot} = 'METHOD';
734 }
735 }
736 }
737  
738 69 ▶sub object {
Subroutine object: called CC=1 SCAR=0.0
739 69 $_[0]->_autovivify_parents;
740 69 $_[0]->_autovivify_methods;
741 69 ${ $_[0] };
742 }
743  
744 3 ▶sub class {
Subroutine class: called CC=1 SCAR=0.0
745 3 return $_[0]->_slots->{'class*'};
746 }
747  
748 0 ▶sub dump {
Subroutine dump: not called CC=2 SCAR=17.9
749 0 ▶ eval "package main; use Data::Dumper;"
Branch: 1 branch
BranchTrueFalseTotal
unless scalar keys %Data::Dumper:: 0 0 0
750 unless (scalar keys(%Data::Dumper::));
751  
752 0 Data::Dumper->Dump([ $_[0]->object ], [ $_[0]->package ]);
753 }
754  
755  
756 sub slotStruct_name () {0};
757 sub slotStruct_value () {1};
758 sub slotStruct_type () {2};
759 sub slotStruct_attribs () {3};
760 sub slotStruct_implementor () {4};
761 sub slotStruct_filters () {5};
762 sub slotStruct_advisories () {6};
763  
764  
765 #### The support for attribute rationalization is not very fancy
766 $Class::Prototyped::Mirror::attributes = {
767 FIELD => {
768 constant => {
769 type => 'implementor',
770 16 ▶ code => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
771 16 my($mirror, $slotName, $slotValue, $slotAttribs, $implementation, $slots) = @_;
772  
773 16 $slotAttribs->{constant} = 1;
774 10 ▶ return bless sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
775 10 $slots->{$slotName};
776 16 }, 'Class::Prototyped::FieldAccessor::Constant';
777 }
778 },
779  
780 autoload => {
781 type => 'filter',
782 rank => 50,
783 9 ▶ code => sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
784 9 my($mirror, $slotName, $slotValue, $slotAttribs, $implementation, $slots) = @_;
785  
786 9 ▶ if ($slotAttribs->{autoload} = $slotAttribs->{autoload} ? 1 : undef) {
Branch: 2 branches
BranchTrueFalseTotal
if ($slotAttribs->{"autoload"} = $slotAttribs->{"autoload"} ? 1 : undef) 9 0 9
$slotAttribs->{'autoload'} ? : 9 0 9
787 9 my $self = $mirror->object;
788 9 ▶ $implementation = bless sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
789 9 my $retval = &$slotValue;
790 9 my $attribs = $self->reflect->_attribs->{$slotName};
791 9 delete($attribs->{autoload});
792 9 $self->reflect->addSlot([$slotName, %$attribs] => $retval);
793 9 return $retval;
794 9 }, 'Class::Prototyped::FieldAccessor::Autoload';
795 }
796 9 return $implementation;
797 }
798 },
799  
800 profile => {
801 type => 'filter',
802 rank => 80,
803 0 ▶ code => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
804 0 my($mirror, $slotName, $slotValue, $slotAttribs, $implementation, $slots) = @_;
805  
806 0 my $profileLevel = $slotAttribs->{profile};
807 0 ▶ if ($profileLevel) {
Branch: 1 branch
BranchTrueFalseTotal
if ($profileLevel) 0 0 0
808 package Class::Prototyped::Mirror::PROFILE;
809 0 my $old_implementation = $implementation;
810 0 my $package = ref( ${ $mirror } );
811 0 ▶ $implementation = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
812 0 my $caller = '';
813 0 ▶ if ($profileLevel == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($profileLevel == 2) { } 0 0 0
814 0 my($pack, $file, $line) = caller;
815 0 $caller = "$file ($line)";
816 0 $Class::Prototyped::Mirror::PROFILE::counts->{$package}->{$slotName}->{$caller}++;
817 } else {
818 0 $Class::Prototyped::Mirror::PROFILE::counts->{$package}->{$slotName}++;
819 }
820 0 goto &$old_implementation;
821 0 };
822 }
823 0 return $implementation;
824 },
825 },
826  
827 'wantarray' => {
828 type => 'filter',
829 rank => 90,
830 2 ▶ code => sub {
Subroutine __ANON__: called CC=3 SCAR=12.0
831 2 my($mirror, $slotName, $slotValue, $slotAttribs, $implementation, $slots) = @_;
832  
833 2 ▶ if ($slotAttribs->{'wantarray'} = $slotAttribs->{'wantarray'} ? 1 : undef) {
Branch: 2 branches
BranchTrueFalseTotal
if ($slotAttribs->{"wantarray"} = $slotAttribs->{"wantarray"} ? 1 : undef) 2 0 2
$slotAttribs->{'wantarray'} ? : 2 0 2
834 2 my $old_implementation = $implementation;
835 4 ▶ $implementation = bless sub {
Subroutine __ANON__: called CC=3 SCAR=13.3
836 4 my $retval = &$old_implementation;
837 4 ▶ if (ref($retval) eq 'ARRAY' && wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $retval eq 'ARRAY' and wantarray) { } 0 4 4
Condition33%1/3
ref $retval eq "ARRAY" and wantarray
!ll&&!rl&&r
040
MC/DC0%0/2
ref $retval eq "ARRAY" and wantarray
ref $retval eq "ARRAY" wantarray
Truth table33%1/3
A and B
A= ref $retval eq "ARRAY"B= wantarray
ABresult
0X0
100
111
838 0 return (@$retval);
839 }
840 else {
841 4 return $retval;
842 }
843 2 }, 'Class::Prototyped::FieldAccessor::Wantarray';
844 }
845 2 return $implementation;
846 }
847 },
848  
849 description => {
850 type => 'advisory',
851 },
852 },
853  
854 METHOD => {
855 superable => {
856 type => 'filter',
857 rank => 10,
858 66 ▶ code => sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
859 66 my($mirror, $slotName, $slotValue, $slotAttribs, $implementation, $slots) = @_;
860  
861 66 ▶ if ($slotAttribs->{superable} = $slotAttribs->{superable} ? 1 : undef) {
Branch: 2 branches
BranchTrueFalseTotal
if ($slotAttribs->{"superable"} = $slotAttribs->{"superable"} ? 1 : undef) 66 0 66
$slotAttribs->{'superable'} ? : 66 0 66
862 package Class::Prototyped::Mirror::SUPER;
863 66 my $old_implementation = $implementation;
864 66 my $package = ref( ${ $mirror } );
865 99 ▶ $implementation = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
866 99 local $Class::Prototyped::Mirror::SUPER::package =
867 $package;
868 99 &$old_implementation;
869 66 };
870 package Class::Prototyped::Mirror;
871 }
872 66 return $implementation;
873 }
874 },
875  
876 profile => {
877 type => 'filter',
878 rank => 90,
879 0 ▶ code => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
880 0 my($mirror, $slotName, $slotValue, $slotAttribs, $implementation, $slots) = @_;
881  
882 0 my $profileLevel = $slotAttribs->{profile};
883 0 ▶ if ($profileLevel) {
Branch: 1 branch
BranchTrueFalseTotal
if ($profileLevel) 0 0 0
884 package Class::Prototyped::Mirror::PROFILE;
885 0 my $old_implementation = $implementation;
886 0 my $package = ref( ${ $mirror } );
887 0 ▶ $implementation = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
888 0 my $caller = '';
889 0 ▶ if ($profileLevel == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($profileLevel == 2) { } 0 0 0
890 0 my($pack, $file, $line) = caller;
891 0 $caller = "$file ($line)";
892 0 $Class::Prototyped::Mirror::PROFILE::counts->{$package}->{$slotName}->{$caller}++;
893 } else {
894 0 $Class::Prototyped::Mirror::PROFILE::counts->{$package}->{$slotName}++;
895 }
896 0 goto &$old_implementation;
897 0 };
898 }
899 0 return $implementation;
900 },
901 },
902  
903 13 overload => {
904 type => 'advisory',
905 },
906  
907 description => {
908 type => 'advisory',
909 },
910 },
911  
912 PARENT => {
913 description => {
914 type => 'advisory',
915 },
916  
917 promote => {
918 type => 'advisory',
919 },
920 },
921 };
922  
923 219 ▶sub addSlotsParser {
Subroutine addSlotsParser: called CC=60 SCAR=45.0
924 219 my $mirror = shift;
925  
926 219 ▶ Carp::croak("odd number of arguments to addSlotsParser\n")
Branch: 1 branch
BranchTrueFalseTotal
if scalar(@_) % 2 0 219 219
927 if scalar(@_) % 2;
928  
929 219 my($package, undef, undef, undef, $slots, undef, undef, $defaults) =
930 $mirror->_everything();
931  
932 219 my(@retvals);
933  
934 219 ▶ while (my($slotThing, $slotValue) = splice(@_, 0, 2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@_, 0, 2)) 411 209 620
935 411 my($slotName, $slotType, $slotAttribs, $slotImplementor, $slotFilters, $slotAdvisories);
936 411 my $isCode = UNIVERSAL::isa($slotValue, 'CODE');
937  
938 411 ▶ if (ref($slotThing) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $slotThing eq 'ARRAY') { } 163 248 411
939 163 $slotName = $slotThing->[0];
940  
941 163 ▶ my $temp = $slotThing->[1] || '';
Condition100%2/2
$slotThing->[1] || ''
l!l
15310
MC/DC100%1/1
$slotThing->[1] || ''
$slotThing->[1]
Truth table100%2/2
A
A= $slotThing->[1]
Aresult
11
00
942 163 ▶ if ($temp eq 'METHOD' || $temp eq 'FIELD' || $temp eq 'PARENT') {
Branch: 1 branch
BranchTrueFalseTotal
if ($temp eq 'METHOD' or $temp eq 'FIELD' or $temp eq 'PARENT') { } 123 40 163
Condition100%6/6
$temp eq "METHOD" or $temp eq "FIELD" or $temp eq "PARENT"
l!l&&r!l&&!r
1032040
$temp eq "METHOD" or $temp eq "FIELD"
l!l&&r!l&&!r
574660
MC/DC100%3/3
$temp eq "METHOD" or $temp eq "FIELD" or $temp eq "PARENT"
$temp eq "METHOD" $temp eq "FIELD" $temp eq "PARENT"
Truth table100%4/4
A or B or C
A= $temp eq "METHOD"B= $temp eq "FIELD"C= $temp eq "PARENT"
ABCresult
1XX1
01X1
0011
0000
943 123 $slotType = $temp;
944 123 $temp = 2;
945 }
946 else {
947 40 ▶ $slotType = $isCode ? 'METHOD' :
Branch: 2 branches
BranchTrueFalseTotal
$isCode ? : 10 30 40
substr($slotName, -1) eq '*' ? : 14 16 30
948 (substr($slotName, -1) eq '*' ? 'PARENT' : 'FIELD');
949 40 $temp = 1;
950 }
951  
952 163 ▶ if ($#{$slotThing} >= $temp) {
Branch: 2 branches
BranchTrueFalseTotal
if ($#{$slotThing;} >= $temp) { } 79 84 163
elsif (defined $defaults->{$slotType}) { } 0 84 84
953 79 ▶ if ($#{$slotThing} == $temp) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#{$slotThing;} == $temp) { } 31 48 79
954 $slotAttribs = defined $defaults->{$slotType}
955 0 ▶ ? { %{$defaults->{$slotType}}, $slotThing->[$temp] => 1 }
Branch: 1 branch
BranchTrueFalseTotal
defined $defaults->{$slotType} ? : 0 31 31
956 : { $slotThing->[$temp] => 1 };
957 }
958 else {
959 $slotAttribs = defined $defaults->{$slotType}
960 0 ? { %{$defaults->{$slotType}}, @{$slotThing}[$temp..$#{$slotThing}] }
961 48 ▶ : { @{$slotThing}[$temp..$#{$slotThing}] };
Branch: 1 branch
BranchTrueFalseTotal
defined $defaults->{$slotType} ? : 0 48 48
962 }
963 }
964 elsif (defined $defaults->{$slotType}) {
965 0 $slotAttribs = { %{$defaults->{$slotType}} };
966 }
967  
968 163 ▶ if ($slotType eq 'METHOD') {
Branch: 2 branches
BranchTrueFalseTotal
if ($slotType eq 'METHOD') { } 67 96 163
elsif ($slotType eq 'PARENT') { } 34 62 96
969 67 ▶ Carp::croak("it is not permitted to use '!' notation in conjunction with slot attributes")
Branch: 1 branch
BranchTrueFalseTotal
if substr($slotName, -1) eq "!" 0 67 67
970 if substr($slotName, -1) eq '!';
971  
972 67 ▶ Carp::croak("method slots have to have CODE refs as values")
Branch: 1 branch
BranchTrueFalseTotal
unless $isCode 1 66 67
973 if !$isCode;
974 }
975 elsif ($slotType eq 'PARENT') {
976 34 ▶ Carp::croak("it is not permitted to use '**' notation in conjunction with slot attributes")
Branch: 1 branch
BranchTrueFalseTotal
if substr($slotName, -2, 1) eq "*" 0 34 34
977 if substr($slotName, -2, 1) eq '*';
978 }
979 }
980 else {
981 248 $slotName = $slotThing;
982 248 ▶ $slotType = $isCode ? 'METHOD' :
Branch: 2 branches
BranchTrueFalseTotal
$isCode ? : 106 142 248
substr($slotName, -1) eq '*' ? : 65 77 142
983 (substr($slotName, -1) eq '*' ? 'PARENT' : 'FIELD');
984  
985 248 ▶ if (defined $defaults->{$slotType}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $defaults->{$slotType}) 1 247 248
986 1 $slotAttribs = { %{$defaults->{$slotType}} };
987 }
988  
989 # Slots that end in '!' mean that the method is superable
990 248 ▶ if ($slotType eq 'METHOD' && substr($slotName, -1) eq '!') {
Branch: 1 branch
BranchTrueFalseTotal
if ($slotType eq "METHOD" and substr($slotName, -1) eq "!") 35 213 248
Condition100%3/3
$slotType eq "METHOD" and substr($slotName, -1) eq "!"
!ll&&!rl&&r
1427135
MC/DC100%2/2
$slotType eq "METHOD" and substr($slotName, -1) eq "!"
$slotType eq "METHOD" substr($slotName, -1) eq "!"
Truth table100%3/3
A and B
A= $slotType eq "METHOD"B= substr($slotName, -1) eq "!"
ABresult
0X0
100
111
991 35 $slotName = substr($slotName, 0, -1);
992 35 $slotAttribs->{superable} = 1;
993 }
994  
995 # Temporary support for &
996 248 ▶ if ($slotType eq 'FIELD' && substr($slotName, -1) eq '&') {
Branch: 1 branch
BranchTrueFalseTotal
if ($slotType eq "FIELD" and substr($slotName, -1) eq "&") 0 248 248
Condition66%2/3
$slotType eq "FIELD" and substr($slotName, -1) eq "&"
!ll&&!rl&&r
171770
MC/DC0%0/2
$slotType eq "FIELD" and substr($slotName, -1) eq "&"
$slotType eq "FIELD" substr($slotName, -1) eq "&"
Truth table66%2/3
A and B
A= $slotType eq "FIELD"B= substr($slotName, -1) eq "&"
ABresult
0X0
100
111
997 0 $slotName = substr($slotName, 0, -1);
998 0 $slotAttribs->{constant} = 1;
999 }
1000  
1001 # Slots that end in '**' mean to push the slot
1002 # to the front of the parents list.
1003 248 ▶ if ($slotType eq 'PARENT' && substr($slotName, -2) eq '**') {
Branch: 1 branch
BranchTrueFalseTotal
if ($slotType eq "PARENT" and substr($slotName, -2) eq "**") 4 244 248
Condition100%3/3
$slotType eq "PARENT" and substr($slotName, -2) eq "**"
!ll&&!rl&&r
183614
MC/DC100%2/2
$slotType eq "PARENT" and substr($slotName, -2) eq "**"
$slotType eq "PARENT" substr($slotName, -2) eq "**"
Truth table100%3/3
A and B
A= $slotType eq "PARENT"B= substr($slotName, -2) eq "**"
ABresult
0X0
100
111
1004 4 $slotName = substr($slotName, 0, -1); # xyz** => xyz*
1005 4 $slotAttribs->{promote} = 1;
1006 }
1007 }
1008  
1009 410 ▶ if ($slotType eq 'METHOD' && exists($Class::Prototyped::overloadable_symbols{$slotName})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($slotType eq 'METHOD' and exists $Class::Prototyped::overloadable_symbols{$slotName}) { } 0 410 410
Condition66%2/3
$slotType eq "METHOD" and exists $Class::Prototyped::overloadable_symbols{$slotName}
!ll&&!rl&&r
2381720
MC/DC0%0/2
$slotType eq "METHOD" and exists $Class::Prototyped::overloadable_symbols{$slotName}
$slotType eq "METHOD" exists $Class::Prototyped::overloadable_symbols{$slotName}
Truth table66%2/3
A and B
A= $slotType eq "METHOD"B= exists $Class::Prototyped::overloadable_symbols{$slotName}
ABresult
0X0
100
111
1010 0 $slotAttribs->{overload} = 1;
1011 }
1012 else {
1013 Carp::croak("can't use slot attribute overload for slots that aren't overloadable")
1014 410 ▶ if ($slotAttribs->{overload} && !exists($Class::Prototyped::overloadable_symbols{$slotName}));
Branch: 1 branch
BranchTrueFalseTotal
if $slotAttribs->{"overload"} and not exists $Class::Prototyped::overloadable_symbols{$slotName} 0 410 410
Condition33%1/3
$slotAttribs->{"overload"} and not exists $Class::Prototyped::overloadable_symbols{$slotName}
!ll&&!rl&&r
41000
MC/DC0%0/2
$slotAttribs->{"overload"} and not exists $Class::Prototyped::overloadable_symbols{$slotName}
$slotAttribs->{"overload"} not exists $Class::Prototyped::overloadable_symbols{$slotName}
Truth table33%1/3
A and not(B)
A= $slotAttribs->{"overload"}B= not exists $Class::Prototyped::overloadable_symbols{$slotName}
ABresult
0X0
100
111
1015 }
1016  
1017 Carp::croak("slots should end in * if and only if the type is parent")
1018 410 ▶ if ( (substr($slotName, -1) eq '*') != ($slotType eq 'PARENT') && !$slotAttribs->{overload} );
Branch: 1 branch
BranchTrueFalseTotal
if (substr($slotName, -1) eq "*") != ($slotType eq "PARENT") and not $slotAttribs->{"overload"} 3 407 410
Condition66%2/3
(substr($slotName, -1) eq "*") != ($slotType eq "PARENT") and not $slotAttribs->{"overload"}
!ll&&!rl&&r
40703
MC/DC50%1/2
(substr($slotName, -1) eq "*") != ($slotType eq "PARENT") and not $slotAttribs->{"overload"}
(substr($slotName, -1) eq "*") != ($slotType eq "PARENT") not $slotAttribs->{"overload"}
Truth table66%2/3
A and not(B)
A= (substr($slotName, -1) eq "*") != ($slotType eq "PARENT")B= not $slotAttribs->{"overload"}
ABresult
0X0
100
111
1019  
1020 407 ▶ if ($slotName eq '*') {
Branch: 1 branch
BranchTrueFalseTotal
if ($slotName eq "*") 10 397 407
1021 10 ▶ $slotName = (ref($slotValue) || $slotValue) . $slotName;
Condition66%2/3
ref $slotValue || $slotValue
l!l&&r!l&&!r
910
MC/DC0%0/2
ref $slotValue || $slotValue
ref $slotValue $slotValue
Truth table66%2/3
A || B
A= ref $slotValueB= $slotValue
ABresult
1X1
011
000
1022 }
1023  
1024 407 ▶ if(scalar(keys(%{$slotAttribs}))) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %{$slotAttribs;}) 119 288 407
1025 119 my $attributes = $Class::Prototyped::Mirror::attributes->{$slotType};
1026  
1027 119 foreach my $attrib (keys %{$slotAttribs}) {
1028 Carp::croak("$slotType slots cannot have the '$attrib' attribute.")
1029 121 ▶ unless exists $attributes->{$attrib};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $attributes->{$attrib} 6 115 121
1030  
1031 115 my $atype = $attributes->{$attrib}->{type};
1032 115 ▶ if ($atype eq 'filter') {
Branch: 3 branches
BranchTrueFalseTotal
if ($atype eq 'filter') { } 77 38 115
elsif ($atype eq 'advisory') { } 22 16 38
elsif ($atype eq 'implementor') { } 16 0 16
1033 77 push(@{$slotFilters}, $attrib);
1034 }
1035 elsif ($atype eq 'advisory') {
1036 22 push(@{$slotAdvisories}, $attrib);
1037 }
1038 elsif ($atype eq 'implementor') {
1039 16 ▶ Carp::croak("slots cannot have more than one implementor.")
Branch: 1 branch
BranchTrueFalseTotal
if defined $slotImplementor 0 16 16
1040 if defined($slotImplementor);
1041 16 ▶ $slotImplementor = $attributes->{$attrib}->{code} if $slotAttribs->{$attrib};
Branch: 1 branch
BranchTrueFalseTotal
if $slotAttribs->{$attrib} 16 0 16
1042 }
1043 else {
1044 0 Carp::croak("unknown attribute type '$atype' for '$attrib'.");
1045 }
1046 }
1047  
1048 113 ▶ if (defined $slotFilters) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $slotFilters) 76 37 113
1049 77 @{$slotFilters} = map { $attributes->{$_}->{code} } sort {
1050 1 ▶ $attributes->{$a}->{rank} <=> $attributes->{$b}->{rank} || $a cmp $b
Condition33%1/3
$attributes->{$a}{'rank'} <=> $attributes->{$b}{'rank'} || $a cmp $b
l!l&&r!l&&!r
100
MC/DC0%0/2
$attributes->{$a}{'rank'} <=> $attributes->{$b}{'rank'} || $a cmp $b
$attributes->{$a}{'rank'} <=> $attributes->{$b}{'rank'} $a cmp $b
Truth table33%1/3
A || B
A= $attributes->{$a}{'rank'} <=> $attributes->{$b}{'rank'}B= $a cmp $b
ABresult
1X1
011
000
1051 76 } @{$slotFilters};
1052 }
1053  
1054 113 ▶ if (defined $slotAdvisories) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $slotAdvisories) 22 91 113
1055 22 @{$slotAdvisories} = grep {defined} map { $attributes->{$_}->{code} } sort @{ $slotAdvisories };
1056 }
1057 }
1058  
1059 401 ▶ Carp::croak("the slot self* is inviolable") if $slotName eq 'self*';
Branch: 1 branch
BranchTrueFalseTotal
if $slotName eq "self*" 0 401 401
1060  
1061 Carp::croak("Can only use operator names for method slots\nslot: $slotName")
1062 401 ▶ if ( exists($Class::Prototyped::overloadable_symbols{$slotName}) &&
Branch: 1 branch
BranchTrueFalseTotal
if exists $Class::Prototyped::overloadable_symbols{$slotName} and $slotType ne "METHOD" 0 401 401
Condition33%1/3
exists $Class::Prototyped::overloadable_symbols{$slotName} and $slotType ne "METHOD"
!ll&&!rl&&r
40100
MC/DC0%0/2
exists $Class::Prototyped::overloadable_symbols{$slotName} and $slotType ne "METHOD"
exists $Class::Prototyped::overloadable_symbols{$slotName} $slotType ne "METHOD"
Truth table33%1/3
A and B
A= exists $Class::Prototyped::overloadable_symbols{$slotName}B= $slotType ne "METHOD"
ABresult
0X0
100
111
1063 $slotType ne 'METHOD' );
1064  
1065 401 ▶ if ($slotType eq 'PARENT') {
Branch: 2 branches
BranchTrueFalseTotal
if ($slotType eq 'PARENT') { } 96 305 401
elsif ($slotType eq 'METHOD') { } 169 136 305
1066 96 ▶ Carp::croak("parent slots cannot be code blocks") if ($isCode);
Branch: 1 branch
BranchTrueFalseTotal
if $isCode 0 96 96
1067  
1068 96 ▶ unless (UNIVERSAL::isa($slotValue, 'Class::Prototyped')
Branch: 1 branch
BranchTrueFalseTotal
unless (UNIVERSAL::isa($slotValue, "Class::Prototyped") or ref \$slotValue eq "SCALAR" and defined $slotValue) 0 96 96
Condition50%3/6
UNIVERSAL::isa($slotValue, "Class::Prototyped") or ref \$slotValue eq "SCALAR" and defined $slotValue
l!l&&r!l&&!r
9420
ref \$slotValue eq "SCALAR" and defined $slotValue
!ll&&!rl&&r
002
MC/DC0%0/3
UNIVERSAL::isa($slotValue, "Class::Prototyped") or ref \$slotValue eq "SCALAR" and defined $slotValue
UNIVERSAL::isa($slotValue, "Class::Prototyped") ref \$slotValue eq "SCALAR" defined $slotValue
Truth table50%2/4
A or B and C
A= UNIVERSAL::isa($slotValue, "Class::Prototyped")B= ref \$slotValue eq "SCALAR"C= defined $slotValue
ABCresult
1XX1
0111
00X0
0100
1069 || (ref(\$slotValue) eq 'SCALAR' && defined $slotValue))
1070 {
1071 0 Carp::croak("attempt to add parent that isn't a "
1072 . "Class::Prototyped or package name\n"
1073 . "package: $package slot: $slotName parent: $slotValue");
1074 }
1075  
1076 96 ▶ if (UNIVERSAL::isa($slotValue, $package)) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($slotValue, $package)) 0 96 96
1077 0 Carp::croak("attempt at recursive inheritance\n"
1078 . "parent $slotValue is a package $package");
1079 }
1080 }
1081 elsif ($slotType eq 'METHOD') {
1082 169 ▶ Carp::croak("cannot replace DESTROY method for unnamed objects")
Branch: 1 branch
BranchTrueFalseTotal
if $slotName eq "DESTROY" and substr($package, 0, 5) eq "PKG0x" 0 169 169
Condition33%1/3
$slotName eq "DESTROY" and substr($package, 0, 5) eq "PKG0x"
!ll&&!rl&&r
16900
MC/DC0%0/2
$slotName eq "DESTROY" and substr($package, 0, 5) eq "PKG0x"
$slotName eq "DESTROY" substr($package, 0, 5) eq "PKG0x"
Truth table33%1/3
A and B
A= $slotName eq "DESTROY"B= substr($package, 0, 5) eq "PKG0x"
ABresult
0X0
100
111
1083 if ($slotName eq 'DESTROY' && substr($package, 0, PREFIX_LENGTH) eq PREFIX);
1084 }
1085  
1086 401 push(@retvals, [$slotName, $slotValue, $slotType, $slotAttribs, $slotImplementor, $slotFilters, $slotAdvisories]);
1087 }
1088 209 return \@retvals;
1089 }
1090  
1091 178 ▶sub addParsedSlots {
Subroutine addParsedSlots: called CC=18 SCAR=29.4
1092 178 my $mirror = shift;
1093  
1094 178 my($package, $isa, $parentOrder, $otherOrder, $slots, $types, $attribs, undef) =
1095 $mirror->_everything();
1096  
1097 178 ▶ while (@{$_[0]}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$_[0];}) 401 178 579
1098 401 my($slotName, $slotValue, $slotType, $slotAttribs, $slotImplementor, $slotFilters, $slotAdvisories) = @{ shift @{$_[0]} };
1099  
1100 401 ▶ &deleteSlots($mirror, $slotName) if exists($slots->{$slotName});
Branch: 1 branch
BranchTrueFalseTotal
if exists $slots->{$slotName} 16 385 401
1101  
1102 401 $slots->{$slotName} = $slotValue; #everything goes into the slots!!!!!
1103  
1104 401 ▶ if ($slotType eq 'PARENT') {
Branch: 1 branch
BranchTrueFalseTotal
if ($slotType eq 'PARENT') { } 96 305 401
1105 96 ▶ my $parentPackage = ref($slotValue) || $slotValue;
Condition66%2/3
ref $slotValue || $slotValue
l!l&&r!l&&!r
58380
MC/DC0%0/2
ref $slotValue || $slotValue
ref $slotValue $slotValue
Truth table66%2/3
A || B
A= ref $slotValueB= $slotValue
ABresult
1X1
011
000
1106  
1107 96 ▶ if (substr($parentPackage, 0, PREFIX_LENGTH) eq PREFIX) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($parentPackage, 0, 5) eq 'PKG0x') { } 58 38 96
1108 58 $Class::Prototyped::Mirror::parents{$package}->{$slotName} = $slotValue;
1109 }
1110 else {
1111 38 ▶ Carp::carp(
Branch: 1 branch
BranchTrueFalseTotal
if $parentPackage =~ /\.p[lm]$/i 0 38 38
1112 "it is recommended to use ->reflect->include for mixing in named files."
1113 )
1114 if $parentPackage =~ /\.p[lm]$/i;
1115  
1116 13 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
1117 38 ▶ if (!ref($slotValue)
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $slotValue or scalar keys %{"${parentPackage}::";}) 0 38 38
Condition33%1/3
ref $slotValue or scalar keys %{"${parentPackage}::";}
l!l&&r!l&&!r
0380
MC/DC0%0/2
ref $slotValue or scalar keys %{"${parentPackage}::";}
ref $slotValue scalar keys %{"${parentPackage}::";}
Truth table33%1/3
A or B
A= ref $slotValueB= scalar keys %{"${parentPackage}::";}
ABresult
1X1
011
000
1118 38 && !(scalar keys(%{"$parentPackage\::"})))
1119 {
1120 0 $mirror->include($parentPackage);
1121 }
1122 }
1123  
1124 96 ▶ my $splice_point = $slotAttribs->{promote} ? 0 : @$parentOrder;
Branch: 1 branch
BranchTrueFalseTotal
$slotAttribs->{'promote'} ? : 16 80 96
1125 96 delete $slotAttribs->{promote};
1126 96 splice(@$isa, $splice_point, 0, $parentPackage);
1127 {
1128 #Defends against ISA caching problems
1129 13 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
1130 96 delete ${"$package\::"}{'::ISA::CACHE::'};
1131 96 @$isa = @$isa;
1132 }
1133 96 splice(@$parentOrder, $splice_point, 0, $slotName);
1134 }
1135 else {
1136 my $implementation = defined $slotImplementor
1137 ? $slotImplementor->($mirror, $slotName, $slotValue, $slotAttribs, undef, $slots)
1138 : ( $slotType eq 'METHOD'
1139 ? $slotValue
1140 132 ▶ : bless sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
1141 132 ▶ @_ > 1 ? $slots->{$slotName} = $_[1] : $slots->{$slotName};
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 22 110 132
1142 305 ▶ }, 'Class::Prototyped::FieldAccessor'
Branch: 2 branches
BranchTrueFalseTotal
defined $slotImplementor ? : 16 289 305
$slotType eq 'METHOD' ? : 169 120 289
1143 );
1144  
1145 305 ▶ if (defined $slotFilters) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $slotFilters) 76 229 305
1146 76 foreach my $filter (@{ $slotFilters }) {
1147 77 $implementation = $filter->($mirror, $slotName, $slotValue, $slotAttribs, $implementation, $slots);
1148 }
1149 }
1150  
1151 305 ▶ if (defined $slotAdvisories) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $slotAdvisories) 4 301 305
1152 4 foreach my $advisory (@{ $slotAdvisories }) {
1153 0 $advisory->($mirror, $slotName, $slotValue, $slotAttribs, $implementation, $slots);
1154 }
1155 }
1156  
1157 305 ▶ if ($slotAttribs->{overload}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($slotAttribs->{'overload'}) { } 0 305 305
1158 0 eval "package $package;
1159 use overload '$slotName' => \$implementation, fallback => 1;
1160 bless \$object, \$package;";
1161 0 ▶ Carp::croak("Eval failed while defining overload\n"
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
1162 . "operation: \"$slotName\" error: $@")
1163 if $@;
1164 }
1165 else {
1166 13 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
1167 305 local $^W = 0; # suppress redefining messages.
1168 305 *{"$package\::$slotName"} = $implementation;
1169 }
1170 305 push (@$otherOrder, $slotName);
1171 }
1172 401 $attribs->{$slotName} = $slotAttribs;
1173 401 $types->{$slotName} = $slotType;
1174 }
1175  
1176 178 return $mirror;
1177 }
1178  
1179 92 ▶sub addSlots {
Subroutine addSlots: called CC=1 SCAR=0.0
1180 92 my $mirror = shift;
1181 92 $mirror->addParsedSlots( $mirror->addSlotsParser(@_) );
1182 }
1183  
1184 13 *addSlot = \&addSlots; # alias addSlot to addSlots
1185  
1186 # $obj->reflect->deleteSlots( name [, name [...]] );
1187 239 ▶sub deleteSlots {
Subroutine deleteSlots: called CC=18 SCAR=33.3
1188 239 my $mirror = shift;
1189 239 my (@deleteSlots) = @_;
1190  
1191 239 my($package, $isa, $parentOrder, $otherOrder, $slots, $types, $attribs, undef) =
1192 $mirror->_everything;
1193  
1194 239 foreach my $slot (@deleteSlots) {
1195 390 ▶ $slot = substr($slot, 0, -1) if substr($slot, -2) eq '**';
Branch: 1 branch
BranchTrueFalseTotal
if substr($slot, -2) eq "**" 0 390 390
1196 390 ▶ $slot = substr($slot, 0, -1) if substr($slot, -1) eq '!';
Branch: 1 branch
BranchTrueFalseTotal
if substr($slot, -1) eq "!" 0 390 390
1197  
1198 390 ▶ next if !exists($slots->{$slot});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $slots->{$slot} 0 390 390
1199  
1200 390 my $value = $slots->{$slot};
1201  
1202 390 ▶ if (substr($slot, -1) eq '*') { # parent slot
Branch: 1 branch
BranchTrueFalseTotal
if (substr($slot, -1) eq '*') { } 94 296 390
1203 94 my $index = 0;
1204 94 ▶ 1 while ($parentOrder->[$index] ne $slot
Branch: 1 branch
BranchTrueFalseTotal
$parentOrder->[$index] ne $slot and $index++ < @$parentOrder and do { '???' } 0 94 94
Condition33%1/3
$parentOrder->[$index] ne $slot and $index++ < @$parentOrder
!ll&&!rl&&r
9400
MC/DC0%0/2
$parentOrder->[$index] ne $slot and $index++ < @$parentOrder
$parentOrder->[$index] ne $slot $index++ < @$parentOrder
Truth table33%1/3
A and B
A= $parentOrder->[$index] ne $slotB= $index++ < @$parentOrder
ABresult
0X0
100
111
1205 and $index++ < @$parentOrder);
1206  
1207 94 ▶ if ($index < @$parentOrder) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index < @$parentOrder) { } 94 0 94
1208 94 splice(@$parentOrder, $index, 1);
1209 94 splice(@$isa, $index, 1);
1210 {
1211 #Defends against ISA caching problems
1212 13 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
1213 94 delete ${"$package\::"}{'::ISA::CACHE::'};
1214 94 @$isa=@$isa;
1215 }
1216 }
1217 else { # not found
1218  
1219 0 ▶ if (!$Class::Prototyped::Mirror::ending) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Class::Prototyped::Mirror::ending) 0 0 0
1220 0 Carp::cluck "couldn't find $slot in $package\n";
1221 0 $DB::single = 1;
1222 }
1223 }
1224  
1225 94 ▶ if (defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) { } 94 0 94
1226 94 my $parentPackage = ref($value);
1227 94 ▶ if (substr($parentPackage, 0, PREFIX_LENGTH) eq PREFIX) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($parentPackage, 0, 5) eq "PKG0x") 58 36 94
1228 delete
1229 58 ($Class::Prototyped::Mirror::parents{$package}->{$slot}
1230 );
1231 }
1232 }
1233 else {
1234  
1235 0 ▶ if (!$Class::Prototyped::Mirror::ending) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Class::Prototyped::Mirror::ending) 0 0 0
1236 0 Carp::cluck "slot undef for $slot in $package\n";
1237 0 $DB::single = 1;
1238 }
1239 }
1240 }
1241 else {
1242  
1243 296 ▶ if (exists($Class::Prototyped::overloadable_symbols{$slot})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Class::Prototyped::overloadable_symbols{$slot}) { } 0 296 296
1244 Carp::croak(
1245 "Perl segfaults when the last overload is removed. Boom!\n")
1246 if (1 == grep {
1247 0 ▶ exists($Class::Prototyped::overloadable_symbols{$_});
Branch: 1 branch
BranchTrueFalseTotal
if 1 == grep({exists $Class::Prototyped::overloadable_symbols{$_};} keys %$slots) 0 0 0
1248 } keys(%$slots));
1249  
1250 0 eval "package $package;
1251 no overload '$slot';
1252 bless {}, \$package;"
1253 ; # dummy bless so that overloading works.
1254 0 ▶ Carp::croak("Eval failed while removing overload\n"
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
1255 . "operation: \"$slot\" error: $@")
1256 if $@;
1257 }
1258 else { # we have a method by that name; delete it
1259 13 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
1260 296 my $name = "$package\::$slot";
1261  
1262 # save the glob...
1263 296 local *old = *{$name};
1264  
1265 # and restore everything else
1266 296 local *new;
1267 296 foreach my $type (qw(HASH IO FORMAT SCALAR ARRAY)) {
1268 1480 my $elem = *old{$type};
1269 1480 ▶ next if !defined($elem);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $elem 1184 296 1480
1270 296 *new = $elem;
1271 }
1272 296 *{$name} = *new;
1273 }
1274 296 @$otherOrder = grep { $_ ne $slot } @$otherOrder;
1275 }
1276 390 delete $slots->{$slot}; # and delete the data/sub ref
1277 390 delete $types->{$slot};
1278 390 delete $attribs->{$slot};
1279 }
1280  
1281 239 return $mirror;
1282 }
1283  
1284 13 *deleteSlot = \&deleteSlots; # alias deleteSlot to deleteSlots
1285  
1286 104 ▶sub super_slow {
Subroutine super_slow: called CC=3 SCAR=11.7
1287 104 ▶ return shift->super_fast(@_)
Branch: 1 branch
BranchTrueFalseTotal
if (caller 1)[0] eq "Class::Prototyped::Mirror::SUPER" 14 90 104
1288 if ((caller(1))[0] eq 'Class::Prototyped::Mirror::SUPER');
1289 90 ▶ return shift->super_fast(@_)
Branch: 1 branch
BranchTrueFalseTotal
if (caller 2)[0] eq "Class::Prototyped::Mirror::SUPER" 90 0 90
1290 if ((caller(2))[0] eq 'Class::Prototyped::Mirror::SUPER');
1291 0 Carp::croak(
1292 "attempt to call super on a method that was defined without !\n"
1293 . "method: " . $_[1]);
1294 }
1295  
1296 13 ▶*super = \&super_slow unless defined(*super{CODE});
Branch: 1 branch
BranchTrueFalseTotal
unless defined *super{"CODE"} 13 0 13
1297  
1298 104 ▶sub super_fast {
Subroutine super_fast: called CC=5 SCAR=16.2
1299 104 my $mirror = shift;
1300 104 my $message = shift;
1301  
1302 104 ▶ $message or Carp::croak("you have to pass the method name to super");
Branch: 1 branch
BranchTrueFalseTotal
$message or Carp::croak("you have to pass the method name to super") 104 0 104
1303  
1304 104 my $object = ${ $mirror };
1305  
1306 104 my (@isa);
1307 {
1308 13 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
1309 104 @isa = @{ $Class::Prototyped::Mirror::SUPER::package . '::ISA' };
1310 }
1311 104 my $method;
1312  
1313 104 foreach my $parentPackage (@isa) {
1314 104 $method = UNIVERSAL::can($parentPackage, $message);
1315 104 ▶ last if $method;
Branch: 1 branch
BranchTrueFalseTotal
if $method 104 0 104
1316 }
1317 $method
1318 104 ▶ or Carp::croak("could not find super in parents\nmessage: $message");
Branch: 1 branch
BranchTrueFalseTotal
$method or Carp::croak("could not find super in parents\nmessage: $message") 104 0 104
1319 104 $method->($object, @_);
1320 }
1321  
1322 210 ▶sub slotNames {
Subroutine slotNames: called CC=3 SCAR=11.0
1323 210 my $mirror = shift;
1324 210 my $type = shift;
1325  
1326 210 my($package, $isa, $parentOrder, $otherOrder, $slots, $types, $attribs, undef) =
1327 $mirror->_everything;
1328  
1329 210 my @slotNames = (@$parentOrder, @$otherOrder);
1330 210 ▶ if ($type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type) 152 58 210
1331 152 @slotNames = grep { $types->{$_} eq $type } @slotNames;
1332 }
1333 210 ▶ return wantarray ? @slotNames : \@slotNames;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 210 0 210
1334 }
1335  
1336 3 ▶sub slotType {
Subroutine slotType: called CC=2 SCAR=7.0
1337 3 my $mirror = shift;
1338 3 my $slotName = shift;
1339  
1340 3 my $types = $mirror->_types;
1341 Carp::croak(
1342 "attempt to determine slotType for unknown slot\nslot: $slotName")
1343 3 ▶ unless exists $types->{$slotName};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $types->{$slotName} 0 3 3
1344 3 return $types->{$slotName};
1345 }
1346  
1347 # may return dups
1348 4 ▶sub allSlotNames {
Subroutine allSlotNames: called CC=3 SCAR=11.0
1349 4 my $mirror = shift;
1350 4 my $type = shift;
1351  
1352 4 my @slotNames;
1353 4 foreach my $parent ($mirror->withAllParents()) {
1354 6 my $mirror = Class::Prototyped::Mirror->new($parent);
1355 6 push (@slotNames, $mirror->slotNames($type));
1356 }
1357 4 ▶ return wantarray ? @slotNames : \@slotNames;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 4 0 4
1358 }
1359  
1360 28 ▶sub parents {
Subroutine parents: called CC=1 SCAR=0.0
1361 28 my $mirror = shift;
1362  
1363 28 my $object = $mirror->object;
1364 28 my $slots = $mirror->_slots;
1365 28 return map { $slots->{$_} } $mirror->slotNames('PARENT');
1366 }
1367  
1368 24 ▶sub allParents {
Subroutine allParents: called CC=6 SCAR=17.9
1369 24 my $mirror = shift;
1370 24 ▶ my $retval = shift || [];
Condition100%2/2
shift() || []
l!l
204
MC/DC100%1/1
shift() || []
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
1371 24 ▶ my $seen = shift || {};
Condition100%2/2
shift() || {}
l!l
204
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
1372  
1373 24 foreach my $parent ($mirror->parents) {
1374 12 ▶ next if $seen->{$parent}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen->{$parent}++ 0 12 12
1375 12 push @$retval, $parent;
1376 12 my $mirror = Class::Prototyped::Mirror->new($parent);
1377 12 $mirror->allParents($retval, $seen);
1378 }
1379 24 ▶ return wantarray ? @$retval : $retval;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 12 12 24
1380 }
1381  
1382 8 ▶sub withAllParents {
Subroutine withAllParents: called CC=1 SCAR=0.0
1383 8 my $mirror = shift;
1384  
1385 8 my $object = $mirror->object;
1386 8 my $retval = [$object];
1387 8 my $seen = { $object => 1 };
1388 8 $mirror->allParents($retval, $seen);
1389 }
1390  
1391 # getSlot returns both the slotName and the slot in array context
1392 # so that it can append !'s to superable methods, so that getSlots does the
1393 # right thing, so that clone does the right thing.
1394 # However, in scalar context, it just returns the value.
1395  
1396 183 ▶sub getSlot {
Subroutine getSlot: called CC=8 SCAR=21.5
1397 183 my $mirror = shift;
1398 183 my $slot = shift;
1399 183 my $format = shift;
1400  
1401 183 my($package, $isa, $parentOrder, $otherOrder, $slots, $types, $attribs, undef) =
1402 $mirror->_everything;
1403  
1404 183 ▶ my $value = ($slot ne 'self*') ? $slots->{$slot} : $mirror->object;
Branch: 1 branch
BranchTrueFalseTotal
$slot ne 'self*' ? : 183 0 183
1405  
1406 183 ▶ return $value unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 33 150 183
1407  
1408 150 ▶ $slot = [$slot, $types->{$slot}, %{$attribs->{$slot} || {}}];
Condition100%2/2
$attribs->{$slot} || {}
l!l
1491
MC/DC100%1/1
$attribs->{$slot} || {}
$attribs->{$slot}
Truth table100%2/2
A
A= $attribs->{$slot}
Aresult
11
00
1409  
1410 150 ▶ if (!defined $format || $format eq 'default') {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $format or $format eq 'default') { } 0 150 150
elsif ($format eq 'simple') { } 2 148 150
elsif ($format eq 'rotated') { } 148 0 148
Condition33%1/3
not defined $format or $format eq "default"
l!l&&r!l&&!r
00150
MC/DC0%0/2
not defined $format or $format eq "default"
not defined $format $format eq "default"
Truth table33%1/3
not(A) or B
A= not defined $formatB= $format eq "default"
ABresult
1X1
011
000
1411 0 return ($slot, $value);
1412 }
1413 elsif ($format eq 'simple') {
1414 2 return ($slot->[0], $value);
1415 }
1416 elsif ($format eq 'rotated') {
1417 return ($slot->[0], {
1418 148 attribs => { @{$slot}[2..$#{$slot}] },
1419 type => $slot->[1],
1420 value => $value
1421 }
1422 );
1423 }
1424 }
1425  
1426 46 ▶sub getSlots {
Subroutine getSlots: called CC=5 SCAR=16.1
1427 46 my $mirror = shift;
1428 46 my $type = shift;
1429 46 my $format = shift;
1430  
1431 46 my @retval;
1432 46 ▶ if (defined $type || defined $format) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $type or defined $format) { } 24 22 46
Condition66%2/3
defined $type or defined $format
l!l&&r!l&&!r
02422
MC/DC50%1/2
defined $type or defined $format
defined $type defined $format
Truth table66%2/3
A or B
A= defined $typeB= defined $format
ABresult
1X1
011
000
1433 24 @retval = map { $mirror->getSlot($_, $format) } $mirror->slotNames($type);
1434 }
1435 else {
1436 22 my($package, $isa, $parentOrder, $otherOrder, $slots, $types, $attribs, undef) =
1437 $mirror->_everything;
1438 @retval = map {
1439 22 ▶ ([$_, $types->{$_}, %{$attribs->{$_} || {}}] => $slots->{$_})
Condition100%2/2
$attribs->{$_} || {}
l!l
1002
MC/DC100%1/1
$attribs->{$_} || {}
$attribs->{$_}
Truth table100%2/2
A
A= $attribs->{$_}
Aresult
11
00
1440 } (@$parentOrder, @$otherOrder);
1441 }
1442  
1443 46 ▶ return wantarray ? @retval : \@retval;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 46 0 46
1444 }
1445  
1446 0 ▶sub promoteParents {
Subroutine promoteParents: not called CC=5 SCAR=31.5
1447 0 my $mirror = shift;
1448 0 my (@newOrder) = @_;
1449  
1450 0 my($package, $isa, $parentOrder, $otherOrder, $slots, $types, $attribs, undef) =
1451 $mirror->_everything;
1452  
1453 0 my %seen;
1454 0 foreach my $slot (@newOrder) {
1455 0 $seen{$slot}++;
1456 0 ▶ if ($seen{$slot} > 1 || !exists($slots->{$slot})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seen{$slot} > 1 or not exists $slots->{$slot}) { } 0 0 0
Condition0%0/3
$seen{$slot} > 1 or not exists $slots->{$slot}
l!l&&r!l&&!r
000
MC/DC0%0/2
$seen{$slot} > 1 or not exists $slots->{$slot}
$seen{$slot} > 1 not exists $slots->{$slot}
Truth table0%0/3
A or not(B)
A= $seen{$slot} > 1B= not exists $slots->{$slot}
ABresult
1X1
011
000
1457 0 Carp::croak("promoteParents called with bad order list\nlist: @_");
1458 }
1459 else {
1460 0 @{$parentOrder} = grep { $_ ne $slot } @{$parentOrder};
1461 }
1462 }
1463  
1464 0 @{$parentOrder} = (@newOrder, @{$parentOrder});
1465  
1466 @$isa =
1467 0 ▶ ((map { ref($slots->{$_}) ? ref($slots->{$_}) : $slots->{$_} }
Branch: 1 branch
BranchTrueFalseTotal
ref $slots->{$_} ? : 0 0 0
1468 0 @{$parentOrder}), 'Class::Prototype');
1469  
1470 # this is required to re-cache @ISA
1471 13 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
1472 0 delete ${"$package\::"}{'::ISA::CACHE::'};
1473 0 @$isa=@$isa;
1474 }
1475  
1476 3 ▶sub wrap {
Subroutine wrap: called CC=2 SCAR=7.0
1477 3 my $mirror = shift;
1478 3 ▶ my $class = $mirror->class || 'Class::Prototyped';
Condition50%1/2
$mirror->class || 'Class::Prototyped'
l!l
03
MC/DC0%0/1
$mirror->class || 'Class::Prototyped'
$mirror->class
Truth table50%1/2
A
A= $mirror->class
Aresult
11
00
1479 3 my $wrapped = $class->new;
1480 3 my $wrappedMirror = $wrapped->reflect;
1481  
1482 # add all the slots from the original object
1483 3 $wrappedMirror->addSlots($mirror->getSlots);
1484  
1485 # delete all my original slots
1486 # so that the wrapped gets called
1487 3 $mirror->deleteSlots($mirror->slotNames);
1488 3 $mirror->addSlots(@_, [qw(wrapped* promote)] => $wrapped);
1489 3 $mirror;
1490 }
1491  
1492 2 ▶sub unwrap {
Subroutine unwrap: called CC=2 SCAR=7.0
1493 2 my $mirror = shift;
1494 2 ▶ my $wrapped = $mirror->getSlot('wrapped*')
Branch: 1 branch
BranchTrueFalseTotal
my $wrapped = $mirror->getSlot("wrapped*") or Carp::croak("unwrapping without a wrapped\n") 2 0 2
1495 or Carp::croak "unwrapping without a wrapped\n";
1496 2 my $wrappedMirror = $wrapped->reflect;
1497 2 $mirror->deleteSlots($mirror->slotNames);
1498 2 $mirror->addSlots($wrappedMirror->getSlots);
1499  
1500 # $wrappedMirror->deleteSlots( $wrappedMirror->slotNames );
1501 2 $mirror;
1502 }
1503  
1504 4 ▶sub delegate {
Subroutine delegate: called CC=10 SCAR=23.0
1505 4 my $mirror = shift;
1506  
1507 4 ▶ while (my ($name, $value) = splice(@_, 0, 2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@_, 0, 2)) 10 2 12
1508 10 ▶ my @names = (UNIVERSAL::isa($name, 'ARRAY') ? @$name : $name);
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($name, 'ARRAY') ? : 0 10 10
1509 10 my @conflicts;
1510  
1511 10 foreach my $slotName (@names) {
1512 10 push (@conflicts, grep { $_ eq $slotName } $mirror->slotNames);
1513 }
1514 Carp::croak(
1515 10 ▶ "delegate would cause conflict with existing slots\n" . "pattern: "
Branch: 1 branch
BranchTrueFalseTotal
if @conflicts 1 9 10
1516 . join ('|', @names) . " , conflicting slots: "
1517 . join (', ', @conflicts))
1518 if @conflicts;
1519  
1520 9 my $delegateMethod;
1521 9 ▶ if (UNIVERSAL::isa($value, 'ARRAY')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($value, "ARRAY")) 5 4 9
1522 5 $delegateMethod = $value->[1];
1523 5 $value = $value->[0];
1524 }
1525 9 ▶ my $delegate = $mirror->getSlot($value) || $value;
Condition66%2/3
$mirror->getSlot($value) || $value
l!l&&r!l&&!r
630
MC/DC0%0/2
$mirror->getSlot($value) || $value
$mirror->getSlot($value) $value
Truth table66%2/3
A || B
A= $mirror->getSlot($value)B= $value
ABresult
1X1
011
000
1526 9 ▶ Carp::croak("Can't delegate to a subroutine\nslot: $name")
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($delegate, "CODE") 1 8 9
1527 if (UNIVERSAL::isa($delegate, 'CODE'));
1528  
1529 8 foreach my $slotName (@names) {
1530 8 ▶ my $method = defined($delegateMethod) ? $delegateMethod : $slotName;
Branch: 1 branch
BranchTrueFalseTotal
defined $delegateMethod ? : 5 3 8
1531 $mirror->addSlot(
1532 7 ▶ $slotName => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1533 7 shift; # discard original recipient
1534 7 $delegate->$method(@_);
1535 }
1536 8 );
1537 }
1538 }
1539 }
1540  
1541 22 ▶sub findImplementation {
Subroutine findImplementation: called CC=8 SCAR=21.1
1542 22 my $mirror = shift;
1543 22 my $slotName = shift;
1544  
1545 22 my $object = $mirror->object;
1546 22 ▶ UNIVERSAL::can($object, $slotName) or return;
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::can($object, $slotName) or return 22 0 22
1547  
1548 22 my $slots = $mirror->_slots;
1549 22 ▶ exists $slots->{$slotName} and return wantarray ? 'self*' : $object;
Branch: 2 branches
BranchTrueFalseTotal
exists $slots->{$slotName} and return wantarray ? "self*" : $object 14 8 22
wantarray ? : 7 7 14
1550  
1551 8 foreach my $parentName ($mirror->slotNames('PARENT')) {
1552 8 my $mirror =
1553 Class::Prototyped::Mirror->new(
1554 scalar($mirror->getSlot($parentName)));
1555 8 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 4 4 8
1556 4 my (@retval) = $mirror->findImplementation($slotName);
1557 4 ▶ scalar(@retval) and return ($parentName, @retval);
Branch: 1 branch
BranchTrueFalseTotal
scalar @retval and return $parentName, @retval 4 0 4
1558 }
1559 else {
1560 4 my $retval = $mirror->findImplementation($slotName);
1561 4 ▶ $retval and return $retval;
Branch: 1 branch
BranchTrueFalseTotal
$retval and return $retval 4 0 4
1562 }
1563 }
1564 0 Carp::croak("fatal error in findImplementation");
1565 }
1566  
1567 # load the given file or package in the receiver's namespace
1568 # Note that no import is done.
1569 # Croaks on an eval error
1570 #
1571 # $mirror->include('Package');
1572 # $mirror->include('File.pl');
1573 #
1574 # $mirror->include('File.pl', 'thisObject');
1575 # makes thisObject() return the object into which the include
1576 # is happening (as long as you don't change packages in the
1577 # included code)
1578 1 ▶sub include {
Subroutine include: called CC=6 SCAR=18.4
1579 1 my $mirror = shift;
1580 1 my $name = shift;
1581 1 my $accessorName = shift;
1582  
1583 1 ▶ $name = "'$name'" if $name =~ /\.p[lm]$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /\.p[lm]$/i 1 0 1
1584  
1585 1 my $object = $mirror->object;
1586 1 my $package = $mirror->package;
1587 1 my $text = "package $package;\n";
1588 1 ▶ $text .= "*$package\::$accessorName = sub { \$object };\n"
Branch: 1 branch
BranchTrueFalseTotal
if defined $accessorName 1 0 1
1589 if defined($accessorName);
1590  
1591 # $text .= "sub $accessorName { \$object };\n" if defined($accessorName);
1592 1 $text .= "require $name;\n";
1593 1 my $retval = eval $text;
1594 1 ▶ Carp::croak("include failed\npackage: $package include: $name error: $@")
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
1595 if $@;
1596  
1597 1 ▶ if (substr($name, -1) eq "'") {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($name, -1) eq "'") 1 0 1
1598 1 $mirror->_vivified_methods(0);
1599 1 $mirror->_autovivify_methods;
1600 }
1601  
1602 1 ▶ $mirror->deleteSlots($accessorName) if defined($accessorName);
Branch: 1 branch
BranchTrueFalseTotal
if defined $accessorName 1 0 1
1603 }
1604  
1605 13 1;
1606 __END__
↑ Summary Prototyped/Graph.pm »