blib/lib/Class/Classless/C3.pm

filter: Statementstmt 89.5% 145 / 162 Branchbran 67.5% 50 / 74 Conditioncond 52.3% 11 / 21 MC/DCmcdc 15.3% 2 / 13 Subroutinesub 85.7% 24 / 28 Podpod 100% 5 / 5 totaltotal 78.2% 237 / 303 CC 49 SCAR 19.6
CC
49
Cov
80%
can
19.8
parents
8.7
declassify
6.3
CRAP 7.1
↑ Summary
1 package Class::Classless::C3;
2 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 our $VERSION = '1.01';
6  
7 1 ▶use Algorithm::C3;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # set this to a scalar ref for tracing
10 1 our $trace;
11  
12 # the root object
13 1 our $ROOT;
14  
15 # for caching results from Algorithm::C3::merge
16 1 our %c3cache;
17  
18 # Class::Classless::C3->new( ['name', method=>sub, ... ] )
19 # $classless->new( [ 'name', method => sub, ... ] )
20 # name recommended
21 sub new
22 9 ▶{
Subroutine new: called CC=4 SCAR=13.9
Pod documented
23 9 my $parent = shift;
24 9 ▶ my $class = ref $parent || $parent;
Condition66%2/3
ref $parent || $parent
l!l&&r!l&&!r
630
MC/DC0%0/2
ref $parent || $parent
ref $parent $parent
Truth table66%2/3
A || B
A= ref $parentB= $parent
ABresult
1X1
011
000
25 9 my $self = bless {}, $class;
26 # Meta class is not subclassable this way....
27 9 $self->{_meta} = Class::Classless::C3::Meta->new();
28 9 ▶ $self->meta->parent(ref $parent ? $parent : $ROOT);
Branch: 1 branch
BranchTrueFalseTotal
ref $parent ? : 6 3 9
29 9 my $name = shift;
30 9 $self->meta->name($name);
31 9 ▶ $self->meta->addmethod( splice(@_,0,2) ) while @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ and do { $self->meta->addmethod(splice(@_, 0, 2)) } 1 9 10
32 9 $self->init($name,@_);
33 9 return $self;
34 }
35  
36 1 $ROOT = bless {}, 'Class::Classless::C3';
37 1 $ROOT->{_meta} = Class::Classless::C3::Meta->new();
38 1 $ROOT->meta->name('ROOT');
39 1 ▶$ROOT->meta->addmethod( init => sub {} );
Subroutine __ANON__: called CC=1 SCAR=0.0
40  
41  
42 sub meta
43 55 ▶{
Subroutine meta: called CC=1 SCAR=0.0
Pod documented
44 55 return $_[0]->{_meta};
45 }
46  
47 1 our $AUTOLOAD;
48 # top level call
49 sub AUTOLOAD
50 14 ▶{
Subroutine AUTOLOAD: called CC=4 SCAR=14.9
51 14 my $self = $_[0];
52 my $sub = $self->can($AUTOLOAD) or
53 14 ▶ die("cannot call method ".($AUTOLOAD =~ m/([^:]*)$/g)[0]." on ".(ref($self)?$self->{_meta}->{name}:"'$self'"));
Branch: 2 branches
BranchTrueFalseTotal
my $sub = $self->can($Class::Classless::C3::AUTOLOAD) or die 'cannot call method ' . ($Class::Classless::C3::AUTOLOAD =~ /([^:]*)$/g)[0] . ' on ' . (ref $self ? $self->{"_meta"}{"name"} : "'${self}'") 14 0 14
ref $self ? : 0 0 0
54 14 ▶ $$trace .= "called ".$self->{_meta}->name."->".($AUTOLOAD =~ m/([^:]*)$/g)[0]." (@_[1..$#_])\n" if ref $trace eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
if ref $Class::Classless::C3::trace eq "SCALAR" 1 13 14
55 14 goto $sub;
56 }
57  
58 # inherited call
59 sub NEXT
60 13 ▶{
Subroutine NEXT: called CC=5 SCAR=16.2
Pod documented
61 13 my $self = $_[0];
62 13 my $class;
63 my $method;
64 13 my $level = 1;
65 13 my $caller;
66 # caller is subname-ed to instance-name::method-name
67 13 ▶ while ($caller = (caller($level++))[3]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($caller = (caller $level++)[3]) 13 0 13
68 13 ($class,$method) = ($caller =~ m/^(.*)::([^:]+)$/s);
69 13 ▶ last unless $method =~ m/^(\(eval\)|__ANON__|DB::.*)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $method =~ /^(\(eval\)|__ANON__|DB::.*)$/ 13 0 13
70 }
71 # need to start from parent of owner of current method
72 13 my $sub = $self->can($method,from=>$class);
73 13 ▶ return unless $sub; # do not die on NEXT
Branch: 1 branch
BranchTrueFalseTotal
unless $sub 5 8 13
74 8 ▶ $$trace .= "NEXT $method from $class\n" if ref $trace eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
if ref $Class::Classless::C3::trace eq "SCALAR" 3 5 8
75 8 goto $sub;
76 }
77  
78 sub isa
79 4 ▶{
Subroutine isa: called CC=5 SCAR=16.4
Pod documented
80 4 my $self = shift;
81 4 my $what = shift;
82  
83 my $c3 = $c3cache{$self->{_meta}->{name}} ||= [
84 Algorithm::C3::merge( $self,
85 0 ▶ sub { @{ $_[0]->{_meta}->{parents} } },
Subroutine __ANON__: not called CC=1 SCAR=6.9
86 4 ▶ )];
Condition50%1/2
$Class::Classless::C3::c3cache{$self->{"_meta"}{"name"}} ||= [Algorithm::C3::merge($self, sub { @{$_[0]{"_meta"}{"parents"};}; } )]
l!l
40
MC/DC0%0/1
$Class::Classless::C3::c3cache{$self->{"_meta"}{"name"}} ||= [Algorithm::C3::merge($self, sub { @{$_[0]{"_meta"}{"parents"};}; } )]
$Class::Classless::C3::c3cache{$self->{"_meta"}{"name"}}
Truth table50%1/2
A
A= $Class::Classless::C3::c3cache{$self->{"_meta"}{"name"}}
Aresult
11
00
87 4 ▶ if (ref $what) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $what) { } 2 2 4
88 2 ▶ return grep($_ eq $what, @$c3) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
grep(($_ eq $what), @$c3) ? : 1 1 2
89 } else {
90 2 ▶ return grep($_->{_meta}->{name} eq $what, @$c3) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
grep(($_->{'_meta'}{'name'} eq $what), @$c3) ? : 1 1 2
91 }
92 }
93  
94 # this is here to avoid calling can('DESTROY') after meta is gone
95 sub DESTROY
96 0 ▶{
Subroutine DESTROY: not called CC=1 SCAR=0.0
97 }
98  
99 sub can
100 30 ▶{
Subroutine can: called CC=15 SCAR=29.9
Pod documented
101 30 my $self = shift;
102 30 my $method = shift;
103 30 $method =~ s/^.*:://;
104 30 ▶ my $from = $_[0] && $_[0] eq 'from' ? $_[1] : undef;
Branch: 1 branch
BranchTrueFalseTotal
$_[0] && $_[0] eq 'from' ? : 13 17 30
Condition66%2/3
$_[0] and $_[0] eq "from"
!ll&&!rl&&r
17013
MC/DC50%1/2
$_[0] and $_[0] eq "from"
$_[0] $_[0] eq "from"
Truth table66%2/3
A and B
A= $_[0]B= $_[0] eq "from"
ABresult
0X0
100
111
105  
106 0 ▶ if (!$self->{_meta}) { warn("cannot can '$method' without meta"); }
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_meta"}) 0 30 30
107 my $c3 = $c3cache{$self->{_meta}->{name}} ||= [
108 Algorithm::C3::merge( $self,
109 38 ▶ sub { @{ $_[0]->{_meta}->{parents} } },
Subroutine __ANON__: called CC=1 SCAR=0.0
110 30 ▶ )];
Condition100%2/2
$Class::Classless::C3::c3cache{$self->{"_meta"}{"name"}} ||= [Algorithm::C3::merge($self, sub { @{$_[0]{"_meta"}{"parents"};}; } )]
l!l
1911
MC/DC100%1/1
$Class::Classless::C3::c3cache{$self->{"_meta"}{"name"}} ||= [Algorithm::C3::merge($self, sub { @{$_[0]{"_meta"}{"parents"};}; } )]
$Class::Classless::C3::c3cache{$self->{"_meta"}{"name"}}
Truth table100%2/2
A
A= $Class::Classless::C3::c3cache{$self->{"_meta"}{"name"}}
Aresult
11
00
111 30 my $sub;
112 30 for my $o ( @$c3 ) {
113 86 ▶ if ($from) {
Branch: 1 branch
BranchTrueFalseTotal
if ($from) 27 59 86
114 27 ▶ next if $o->{_meta}->{name} ne $from;
Branch: 1 branch
BranchTrueFalseTotal
if $o->{"_meta"}{"name"} ne $from 14 13 27
115 13 undef $from;
116 13 next;
117 }
118 59 ▶ if (ref $o && $o->{_meta}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $o and $o->{'_meta'}) { } 59 0 59
Condition33%1/3
ref $o and $o->{"_meta"}
!ll&&!rl&&r
0059
MC/DC0%0/2
ref $o and $o->{"_meta"}
ref $o $o->{"_meta"}
Truth table33%1/3
A and B
A= ref $oB= $o->{"_meta"}
ABresult
0X0
100
111
119 59 $sub = $o->{_meta}->{methods}->{$method};
120 59 ▶ return $sub if $sub;
Branch: 1 branch
BranchTrueFalseTotal
if $sub 22 37 59
121 # for optional autoload-like behavior
122 37 ▶ if (ref $Class::Classless::C3::autoload eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $Class::Classless::C3::autoload eq "CODE") 0 37 37
123 0 $sub = $Class::Classless::C3::autoload->($o,$method);
124 0 ▶ return $sub if $sub;
Branch: 1 branch
BranchTrueFalseTotal
if $sub 0 0 0
125 }
126 } else {
127 0 $sub = UNIVERSAL::can($o,$method);
128 0 ▶ return $sub if $sub;
Branch: 1 branch
BranchTrueFalseTotal
if $sub 0 0 0
129 }
130 }
131 # catch methods defined in Class::Classless::C3
132 8 $sub = UNIVERSAL::can($self,$method);
133 8 ▶ return $sub if $sub;
Branch: 1 branch
BranchTrueFalseTotal
if $sub 1 7 8
134  
135 7 return undef;
136 }
137  
138 1 ▶$Class::Classless::C3::autoload ||= '';
Condition50%1/2
$Class::Classless::C3::autoload ||= ""
l!l
01
MC/DC0%0/1
$Class::Classless::C3::autoload ||= ""
$Class::Classless::C3::autoload
Truth table50%1/2
A
A= $Class::Classless::C3::autoload
Aresult
11
00
139  
140  
141 package # hide from pause
142 Class::Classless::C3::Meta;
143 1 ▶use Sub::Name;
Subroutine BEGIN: called CC=1 SCAR=0.0
144  
145 1 $Class::Classless::C3::Meta::uid = 0;
146  
147 sub new
148 10 ▶{
Subroutine new: called CC=2 SCAR=7.2
149 10 my $object = shift;
150 10 ▶ my $class = ref $object || $object;
Condition33%1/3
ref $object || $object
l!l&&r!l&&!r
0100
MC/DC0%0/2
ref $object || $object
ref $object $object
Truth table33%1/3
A || B
A= ref $objectB= $object
ABresult
1X1
011
000
151 10 my $self = bless {}, $class;
152 10 $self->init(@_);
153 10 return $self;
154 }
155  
156 sub init
157 10 ▶{
Subroutine init: called CC=1 SCAR=0.0
158 10 my $self = shift;
159 10 $self->{parents} = [];
160 }
161  
162 sub name
163 32 ▶{
Subroutine name: called CC=5 SCAR=16.1
164 32 my $self = shift;
165 32 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 10 22 32
166 10 ▶ $self->purge_c3cache if $self->{name};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"name"} 0 10 10
167 10 my $name = shift;
168 10 $self->{name} = $name;
169 10 ▶ unless ($self->{name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"name"}) 1 9 10
170 1 $self->{name} = 'x_'.++$Class::Classless::C3::Meta::uid;
171 }
172 10 subname $name.'::'.$_ => $self->{methods}->{$_} for keys %{$self->{methods}};
173 10 $self->purge_c3cache;
174 }
175 32 return $self->{name};
176 }
177  
178 sub parent
179 10 ▶{
Subroutine parent: called CC=4 SCAR=14.0
180 10 my $self = shift;
181 10 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 9 1 10
182 # clear any isa caching
183 9 ▶ $self->purge_c3cache if @{$self->{parents}};
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->{"parents"};} 0 9 9
184 9 my $par = shift;
185 9 ▶ die("called parent with nonref '$par'") unless ref $par;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $par 0 9 9
186 9 $self->{parents} = [$par];
187 }
188 10 return $self->{parents}->[0];
189 }
190  
191 sub parents
192 1 ▶{
Subroutine parents: called CC=4 SCAR=21.7
193 1 my $self = shift;
194 1 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 1 1
195 0 ▶ $self->purge_c3cache if $self->{parents};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"parents"} 0 0 0
196 0 ▶ if (ref $_[0] eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'ARRAY') { } 0 0 0
197 0 $self->{parents} = [@{$_[0]}];
198 } else {
199 0 $self->{parents} = [@_];
200 }
201 }
202 # return a copy of the array, so they cannot change our copy
203 # we need to clear the c3cache if our copy changes
204 1 return @{$self->{parents}};
205 }
206  
207 sub addparent
208 1 ▶{
Subroutine addparent: called CC=2 SCAR=7.0
209 1 my $self = shift;
210 1 my $newp = shift;
211 1 ▶ return unless $newp;
Branch: 1 branch
BranchTrueFalseTotal
unless $newp 0 1 1
212 1 $self->purge_c3cache;
213 # maybe this should unshift???
214 1 push @{ $self->{parents} }, $newp;
215 }
216  
217 sub addmethod
218 10 ▶{
Subroutine addmethod: called CC=1 SCAR=0.0
219 10 my $self = shift;
220 10 my($name,$sub) = @_;
221 10 my $fullname = $self->{name}.'::'.$name;
222 10 $self->{methods}->{$name} = subname $fullname => $sub;
223 }
224  
225 sub delmethod
226 0 ▶{
Subroutine delmethod: not called CC=1 SCAR=6.9
227 0 my $self = shift;
228 0 my($name) = @_;
229 0 delete $self->{methods}->{$name};
230 }
231  
232 sub clone
233 0 ▶{
Subroutine clone: not called CC=1 SCAR=0.0
234 }
235  
236 # creates a Classless object from an existing package
237 sub declassify
238 1 ▶{
Subroutine declassify: called CC=6 SCAR=18.3
239 1 my $class = shift;
240 1 my $self = Class::Classless::C3->new($class);
241  
242 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
243 1 my $symtable = \%{$class.'::'};
244 1 for my $sym ( keys %$symtable ) {
245 5 ▶ next if $sym =~ m/^(AUTOLOAD|NEXT|can|isa|VERSION|meta|new)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $sym =~ /^(AUTOLOAD|NEXT|can|isa|VERSION|meta|new)$/ 1 4 5
246 my $sub = ref \$symtable->{$sym} eq 'GLOB'
247 4 ▶ ? *{$symtable->{$sym}}{CODE}
Branch: 1 branch
BranchTrueFalseTotal
exists &{$class . '::' . $sym;} ? : 0 0 0
248 0 : exists &{$class.'::'.$sym}
249 0 ▶ ? \&{$class.'::'.$sym}
Branch: 1 branch
BranchTrueFalseTotal
ref \$symtable->{$sym} eq 'GLOB' ? : 4 0 4
250 : undef;
251 4 ▶ if (defined $sub) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sub) 2 2 4
252 2 $self->meta->addmethod($sym => $sub);
253 2 delete ${$class.'::'}{$sym}; #deletes all glob-parts
254 }
255 }
256 1 return $self;
257 }
258  
259 # clear any c3cache entries which contain this object
260 # (called when an object's parents change or object's name changes)
261 sub purge_c3cache
262 11 ▶{
Subroutine purge_c3cache: called CC=4 SCAR=14.1
263 11 my $self = shift;
264 11 ▶ my $who = shift || $self->{name};
Condition33%1/3
shift() || $self->{'name'}
l!l&&r!l&&!r
0110
MC/DC0%0/2
shift() || $self->{'name'}
shift() $self->{'name'}
Truth table33%1/3
A || B
A= shift()B= $self->{'name'}
ABresult
1X1
011
000
265 11 for my $k (keys %Class::Classless::C3::c3cache) {
266 45 ▶ if (grep $who eq $_->{_meta}->{name}, @{ $Class::Classless::C3::c3cache{$k} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep(($who eq $_->{"_meta"}{"name"}), @{$Class::Classless::C3::c3cache{$k};})) 2 43 45
267 2 delete $Class::Classless::C3::c3cache{$k};
268 }
269 }
270 }
271  
272 sub show_c3cache # for debugging
273 2 ▶{
Subroutine show_c3cache: called CC=1 SCAR=0.0
274 2 my $self = shift;
275 return join ',',
276 11 map { $_->meta->name }
277 2 @{ $Class::Classless::C3::c3cache{$self->{name}} };
278 }
279  
280  
281 1 1;
282 __END__
↑ Summary