C3.pm

filter: Statementstmt 50.2% 88 / 175 Branchbran 17.1% 12 / 70 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 78.1% 25 / 32 Podpod 100% 3 / 3 totaltotal 41.9% 128 / 305 CC 85 SCAR 29.5
CC
85
Cov
38%
_calculate_method_dispatch_table
45.0
_calculate_method_dispatch_table
45.0
BEGIN
38.4
CRAP 19.1
↑ Summary C3/next.pm »
1 package Class::C3;
2  
3 21 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
4 21 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
5  
6 20 our $VERSION = '0.35';
7  
8 20 our $C3_IN_CORE;
9 21 our $C3_XS;
10  
11 21 ▶BEGIN {
Subroutine BEGIN: called CC=7 SCAR=36.5
Subroutine __ANON__: called CC=7 SCAR=36.5
12 21 ▶ if($] > 5.009_004) {
Branch: 4 branches
BranchTrueFalseTotal
if ($] > 5.009004) { } 21 0 21
elsif ($Class::C3::C3_XS or not defined $Class::C3::C3_XS) { } 0 0 0
if ($] > 5.009004) { } 1 0 1
elsif ($Class::C3::C3_XS or not defined $Class::C3::C3_XS) { } 0 0 0
13 21 $C3_IN_CORE = 1;
14 21 require mro;
15 }
16 ▶ elsif($C3_XS or not defined $C3_XS) {
Condition0%0/6
$Class::C3::C3_XS or not defined $Class::C3::C3_XS
l!l&&r!l&&!r
000
$Class::C3::C3_XS or not defined $Class::C3::C3_XS
l!l&&r!l&&!r
000
MC/DC0%0/2
$Class::C3::C3_XS or not defined $Class::C3::C3_XS
$Class::C3::C3_XS not defined $Class::C3::C3_XS
MC/DC0%0/2
$Class::C3::C3_XS or not defined $Class::C3::C3_XS
$Class::C3::C3_XS not defined $Class::C3::C3_XS
Truth table0%0/3
A or not(B)
A= $Class::C3::C3_XSB= not defined $Class::C3::C3_XS
ABresult
1X1
011
000
Truth table0%0/3
A or not(B)
A= $Class::C3::C3_XSB= not defined $Class::C3::C3_XS
ABresult
1X1
011
000
17 0 my $error = do {
18 0 local $@;
19 0 eval { require Class::C3::XS };
20 0 $@;
21 };
22  
23 0 ▶ if ($error) {
Branch: 2 branches
BranchTrueFalseTotal
if ($error) { } 0 0 0
if ($error) { } 0 0 0
24 0 ▶ die $error if $error !~ /\blocate\b/;
Branch: 2 branches
BranchTrueFalseTotal
unless $error =~ /\blocate\b/ 0 0 0
unless $error =~ /\blocate\b/ 0 0 0
25  
26 0 ▶ if ($C3_XS) {
Branch: 2 branches
BranchTrueFalseTotal
if ($Class::C3::C3_XS) 0 0 0
if ($Class::C3::C3_XS) 0 0 0
27 0 require Carp;
28 0 Carp::croak( "XS explicitly requested but Class::C3::XS is not available" );
29 }
30  
31 0 require Algorithm::C3;
32 0 require Class::C3::next;
33 }
34 else {
35 0 $C3_XS = 1;
36 }
37 }
38 }
39  
40 # this is our global stash of both
41 # MRO's and method dispatch tables
42 # the structure basically looks like
43 # this:
44 #
45 # $MRO{$class} = {
46 # MRO => [ <class precedence list> ],
47 # methods => {
48 # orig => <original location of method>,
49 # code => \&<ref to original method>
50 # },
51 # has_overload_fallback => (1 | 0)
52 # }
53 #
54 21 our %MRO;
55  
56 # use these for debugging ...
57 0 ▶sub _dump_MRO_table { %MRO }
Subroutine _dump_MRO_table: not called CC=1 SCAR=6.9
58 21 our $TURN_OFF_C3 = 0;
59  
60 # state tracking for initialize()/uninitialize()
61 21 our $_initialized = 0;
62  
63 85 ▶sub import {
Subroutine import: called CC=5 SCAR=16.7
64 85 my $class = caller();
65 # skip if the caller is main::
66 # since that is clearly not relevant
67 85 ▶ return if $class eq 'main';
Branch: 1 branch
BranchTrueFalseTotal
if $class eq "main" 2 83 85
68  
69 83 ▶ return if $TURN_OFF_C3;
Branch: 1 branch
BranchTrueFalseTotal
if $Class::C3::TURN_OFF_C3 0 83 83
70 83 ▶ mro::set_mro($class, 'c3') if $C3_IN_CORE;
Branch: 1 branch
BranchTrueFalseTotal
if $Class::C3::C3_IN_CORE 83 0 83
71  
72 # make a note to calculate $class
73 # during INIT phase
74 83 ▶ $MRO{$class} = undef unless exists $MRO{$class};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $Class::C3::MRO{$class} 83 0 83
75 }
76  
77 ## initializers
78  
79 # This prevents silly warnings when Class::C3 is
80 # used explicitly along with MRO::Compat under 5.9.5+
81  
82 21 ▶{ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
83  
84 21 ▶sub initialize {
Subroutine initialize: called CC=6 SCAR=26.9
Pod documented
85 21 %next::METHOD_CACHE = ();
86 # why bother if we don't have anything ...
87 21 ▶ return unless keys %MRO;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %Class::C3::MRO 0 21 21
88 21 ▶ if($C3_IN_CORE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Class::C3::C3_IN_CORE) { } 21 0 21
89 21 mro::set_mro($_, 'c3') for keys %MRO;
90 }
91 else {
92 0 ▶ if($_initialized) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Class::C3::_initialized) 0 0 0
93 0 uninitialize();
94 0 $MRO{$_} = undef foreach keys %MRO;
95 }
96 0 _calculate_method_dispatch_tables();
97 0 _apply_method_dispatch_tables();
98 0 $_initialized = 1;
99 }
100 }
101  
102 4 ▶sub uninitialize {
Subroutine uninitialize: called CC=4 SCAR=16.1
Pod documented
103 # why bother if we don't have anything ...
104 4 %next::METHOD_CACHE = ();
105 4 ▶ return unless keys %MRO;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %Class::C3::MRO 0 4 4
106 4 ▶ if($C3_IN_CORE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Class::C3::C3_IN_CORE) { } 4 0 4
107 4 mro::set_mro($_, 'dfs') for keys %MRO;
108 }
109 else {
110 0 _remove_method_dispatch_tables();
111 0 $_initialized = 0;
112 }
113 }
114  
115 1 ▶sub reinitialize { goto &initialize }
Subroutine reinitialize: called CC=1 SCAR=0.0
Pod documented
116  
117 } # end of "no warnings 'redefine'"
118  
119 ## functions for applying C3 to classes
120  
121 21 ▶sub _calculate_method_dispatch_tables {
Subroutine _calculate_method_dispatch_tables: not called CC=3 SCAR=21.6
122 0 ▶ return if $C3_IN_CORE;
Branch: 1 branch
BranchTrueFalseTotal
if $Class::C3::C3_IN_CORE 0 0 0
123 0 my %merge_cache;
124 0 foreach my $class (keys %MRO) {
125 0 _calculate_method_dispatch_table($class, \%merge_cache);
126 }
127 }
128  
129 0 ▶sub _calculate_method_dispatch_table {
Subroutine _calculate_method_dispatch_table: not called CC=8 SCAR=38.1
130 0 ▶ return if $C3_IN_CORE;
Branch: 1 branch
BranchTrueFalseTotal
if $Class::C3::C3_IN_CORE 0 0 0
131 0 my ($class, $merge_cache) = @_;
132 21 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
133 0 my @MRO = calculateMRO($class, $merge_cache);
134 0 $MRO{$class} = { MRO => \@MRO };
135 0 my $has_overload_fallback;
136 my %methods;
137 # NOTE:
138 # we do @MRO[1 .. $#MRO] here because it
139 # makes no sense to interrogate the class
140 # which you are calculating for.
141 0 foreach my $local (@MRO[1 .. $#MRO]) {
142 # if overload has tagged this module to
143 # have use "fallback", then we want to
144 # grab that value
145 0 $has_overload_fallback = ${"${local}::()"}
146 0 ▶ if !defined $has_overload_fallback && defined ${"${local}::()"};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $has_overload_fallback and defined ${"${local}::()";} 0 0 0
Condition0%0/3
not defined $has_overload_fallback and defined ${"${local}::()";}
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $has_overload_fallback and defined ${"${local}::()";}
not defined $has_overload_fallback defined ${"${local}::()";}
Truth table0%0/3
not(A) and B
A= not defined $has_overload_fallbackB= defined ${"${local}::()";}
ABresult
0X0
100
111
147 0 foreach my $method (grep { defined &{"${local}::$_"} } keys %{"${local}::"}) {
148 # skip if already overridden in local class
149 0 ▶ next unless !defined *{"${class}::$method"}{CODE};
Branch: 1 branch
BranchTrueFalseTotal
if defined *{"${class}::$method";}{"CODE"} 0 0 0
150 $methods{$method} = {
151 orig => "${local}::$method",
152 0 code => \&{"${local}::$method"}
153 0 ▶ } unless exists $methods{$method};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $methods{$method} 0 0 0
154 }
155 }
156 # now stash them in our %MRO table
157 0 $MRO{$class}->{methods} = \%methods;
158 0 $MRO{$class}->{has_overload_fallback} = $has_overload_fallback;
159 }
160  
161 0 ▶sub _apply_method_dispatch_tables {
Subroutine _apply_method_dispatch_tables: not called CC=3 SCAR=24.8
162 0 ▶ return if $C3_IN_CORE;
Branch: 1 branch
BranchTrueFalseTotal
if $Class::C3::C3_IN_CORE 0 0 0
163 0 foreach my $class (keys %MRO) {
164 0 _apply_method_dispatch_table($class);
165 }
166 }
167  
168 0 ▶sub _apply_method_dispatch_table {
Subroutine _apply_method_dispatch_table: not called CC=7 SCAR=34.7
169 0 ▶ return if $C3_IN_CORE;
Branch: 1 branch
BranchTrueFalseTotal
if $Class::C3::C3_IN_CORE 0 0 0
170 0 my $class = shift;
171 21 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
172 0 ${"${class}::()"} = $MRO{$class}->{has_overload_fallback}
173 0 if !defined &{"${class}::()"}
174 0 ▶ && defined $MRO{$class}->{has_overload_fallback};
Branch: 1 branch
BranchTrueFalseTotal
if not defined &{"${class}::()";} and defined $Class::C3::MRO{$class}{"has_overload_fallback"} 0 0 0
Condition0%0/3
not defined &{"${class}::()";} and defined $Class::C3::MRO{$class}{"has_overload_fallback"}
!ll&&!rl&&r
000
MC/DC0%0/2
not defined &{"${class}::()";} and defined $Class::C3::MRO{$class}{"has_overload_fallback"}
not defined &{"${class}::()";} defined $Class::C3::MRO{$class}{"has_overload_fallback"}
Truth table0%0/3
not(A) and B
A= not defined &{"${class}::()";}B= defined $Class::C3::MRO{$class}{"has_overload_fallback"}
ABresult
0X0
100
111
175 0 foreach my $method (keys %{$MRO{$class}->{methods}}) {
176 0 ▶ if ( $method =~ /^\(/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method =~ /^\(/) 0 0 0
177 0 my $orig = $MRO{$class}->{methods}->{$method}->{orig};
178 0 ▶ ${"${class}::$method"} = $$orig if defined $$orig;
Branch: 1 branch
BranchTrueFalseTotal
if defined $$orig 0 0 0
179 }
180 0 *{"${class}::$method"} = $MRO{$class}->{methods}->{$method}->{code};
181 }
182 }
183  
184 0 ▶sub _remove_method_dispatch_tables {
Subroutine _remove_method_dispatch_tables: not called CC=3 SCAR=24.8
185 0 ▶ return if $C3_IN_CORE;
Branch: 1 branch
BranchTrueFalseTotal
if $Class::C3::C3_IN_CORE 0 0 0
186 0 foreach my $class (keys %MRO) {
187 0 _remove_method_dispatch_table($class);
188 }
189 }
190  
191 0 ▶sub _remove_method_dispatch_table {
Subroutine _remove_method_dispatch_table: not called CC=6 SCAR=30.8
192 0 ▶ return if $C3_IN_CORE;
Branch: 1 branch
BranchTrueFalseTotal
if $Class::C3::C3_IN_CORE 0 0 0
193 0 my $class = shift;
194 21 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
195 0 ▶ delete ${"${class}::"}{"()"} if $MRO{$class}->{has_overload_fallback};
Branch: 1 branch
BranchTrueFalseTotal
if $Class::C3::MRO{$class}{"has_overload_fallback"} 0 0 0
196 0 foreach my $method (keys %{$MRO{$class}->{methods}}) {
197 0 delete ${"${class}::"}{$method}
198 0 if defined *{"${class}::${method}"}{CODE} &&
199 0 ▶ (*{"${class}::${method}"}{CODE} eq $MRO{$class}->{methods}->{$method}->{code});
Branch: 1 branch
BranchTrueFalseTotal
if defined *{"${class}::$method";}{"CODE"} and *{"${class}::$method";}{"CODE"} eq $Class::C3::MRO{$class}{"methods"}{$method}{"code"} 0 0 0
Condition0%0/3
defined *{"${class}::$method";}{"CODE"} and *{"${class}::$method";}{"CODE"} eq $Class::C3::MRO{$class}{"methods"}{$method}{"code"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined *{"${class}::$method";}{"CODE"} and *{"${class}::$method";}{"CODE"} eq $Class::C3::MRO{$class}{"methods"}{$method}{"code"}
defined *{"${class}::$method";}{"CODE"} *{"${class}::$method";}{"CODE"} eq $Class::C3::MRO{$class}{"methods"}{$method}{"code"}
Truth table0%0/3
A and B
A= defined *{"${class}::$method";}{"CODE"}B= *{"${class}::$method";}{"CODE"} eq $Class::C3::MRO{$class}{"methods"}{$method}{"code"}
ABresult
0X0
100
111
200 }
201 }
202  
203 sub calculateMRO {
204 my ($class, $merge_cache) = @_;
205  
206 return Algorithm::C3::merge($class, sub {
207 21 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
208 @{$_[0] . '::ISA'};
209 }, $merge_cache);
210 }
211  
212 # Method overrides to support 5.9.5+ or Class::C3::XS
213  
214 16 ▶sub _core_calculateMRO { @{mro::get_linear_isa($_[0], 'c3')} }
Subroutine _core_calculateMRO: called CC=1 SCAR=0.0
215  
216 21 ▶if($C3_IN_CORE) {
Branch: 2 branches
BranchTrueFalseTotal
if ($Class::C3::C3_IN_CORE) { } 21 0 21
elsif ($Class::C3::C3_XS) { } 0 0 0
217 21 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
218 21 *Class::C3::calculateMRO = \&_core_calculateMRO;
219 }
220 elsif($C3_XS) {
221 21 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
222 1 *Class::C3::calculateMRO = \&Class::C3::XS::calculateMRO;
223 1 *Class::C3::_calculate_method_dispatch_table
224 = \&Class::C3::XS::_calculate_method_dispatch_table;
225 }
226  
227 20 1;
228  
229 __END__
↑ Summary C3/next.pm »