lib/Class/Spiffy.pm

filter: Statementstmt 93.4% 326 / 349 Branchbran 75.7% 103 / 136 Conditioncond 68.8% 31 / 45 MC/DCmcdc 42.8% 12 / 28 Subroutinesub 89.8% 53 / 59 Podpod 30.7% 8 / 26 totaltotal 82.8% 533 / 643 CC 103 SCAR 18.8
CC
103
Cov
87%
field
16.1
import
14.1
super
11.0
CRAP 6.5
↑ Summary blib/lib/Class/Spiffy/mixin.pm »
1 package Class::Spiffy;
2 41 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 26 ▶use 5.006001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 28 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 26 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 26 require Exporter;
7 26 our $VERSION = '0.15';
8 26 our @EXPORT = ();
9 26 our @EXPORT_BASE = qw(field const stub super);
10 26 our @EXPORT_OK = (@EXPORT_BASE, qw(id WWW XXX YYY ZZZ));
11 26 our %EXPORT_TAGS = (XXX => [qw(WWW XXX YYY ZZZ)]);
12  
13 26 my $stack_frame = 0;
14 26 my $dump = 'yaml';
15 26 my $bases_map = {};
16  
17 sub WWW; sub XXX; sub YYY; sub ZZZ;
18  
19 # This line is here to convince "autouse" into believing we are autousable.
20 7 ▶sub can {
Subroutine can: called CC=3 SCAR=11.1
Pod undocumented
21 7 ▶ ($_[1] eq 'import' and caller()->isa('autouse'))
Branch: 1 branch
BranchTrueFalseTotal
$_[1] eq 'import' && (caller())->isa('autouse') ? : 6 6 12
Condition66%2/3
$_[1] eq "import" and (caller())->isa("autouse")
!ll&&!rl&&r
601
MC/DC50%1/2
$_[1] eq "import" and (caller())->isa("autouse")
$_[1] eq "import" (caller())->isa("autouse")
Truth table66%2/3
A and B
A= $_[1] eq "import"B= (caller())->isa("autouse")
ABresult
0X0
100
111
22 ? \&Exporter::import # pacify autouse's equality test
23 : $_[0]->SUPER::can($_[1]) # normal case
24 }
25  
26 # TODO
27 #
28 # Exported functions like field and super should be hidden so as not to
29 # be confused with methods that can be inherited.
30 #
31  
32 19 ▶sub new {
Subroutine new: called CC=3 SCAR=11.1
Pod undocumented
33 19 my $class = shift;
34 19 ▶ $class = ref($class) || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
0190
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
35 19 my $self = bless {}, $class;
36 19 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 5 19 24
37 3 my $method = shift;
38 3 $self->$method(shift);
39 }
40 19 return $self;
41 }
42  
43 57 ▶sub import {
Subroutine import: called CC=14 SCAR=26.5
44 26 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
45 26 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 57 my $self_package = shift;
47  
48 # XXX Using parse_arguments here might cause confusion, because the
49 # subclass's boolean_arguments and paired_arguments can conflict, causing
50 # difficult debugging. Consider using something truly local.
51 57 my ($args, @export_list) = do {
52 57 ▶ local *boolean_arguments = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
53 57 qw(
54 -base -Base -mixin -selfless
55 -XXX -dumper -yaml
56 )
57 57 };
58 57 ▶ local *paired_arguments = sub { qw(-package) };
Subroutine __ANON__: called CC=1 SCAR=0.0
59 57 $self_package->parse_arguments(@_);
60 };
61 return spiffy_mixin_import(scalar(caller(0)), $self_package, @export_list)
62 57 ▶ if $args->{-mixin};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"-mixin"} 2 57 59
63  
64 57 ▶ croak <<'...' if $args->{-Base};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"-Base"} 1 56 57
65 Use of '-Base' with Class::Spiffy is illegal.
66 Please use '-base' or 'use Spiffy -Base'
67 ...
68  
69 56 ▶ $dump = 'yaml' if $args->{-yaml};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"-yaml"} 0 56 56
70 56 ▶ $dump = 'dumper' if $args->{-dumper};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"-dumper"} 0 56 56
71  
72 56 local @EXPORT_BASE = @EXPORT_BASE;
73  
74 56 ▶ if ($args->{-XXX}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"-XXX"}) 2 54 56
75 2 ▶ push @EXPORT_BASE, @{$EXPORT_TAGS{XXX}}
Branch: 1 branch
BranchTrueFalseTotal
unless grep /^XXX$/, @Class::Spiffy::EXPORT_BASE 2 0 2
76 unless grep /^XXX$/, @EXPORT_BASE;
77 }
78  
79 56 ▶ my $caller_package = $args->{-package} || caller($stack_frame);
Condition66%2/3
$args->{'-package'} || caller $stack_frame
l!l&&r!l&&!r
1550
MC/DC0%0/2
$args->{'-package'} || caller $stack_frame
$args->{'-package'} caller $stack_frame
Truth table66%2/3
A || B
A= $args->{'-package'}B= caller $stack_frame
ABresult
1X1
011
000
80 51 push @{"$caller_package\::ISA"}, $self_package
81 56 ▶ if $args->{-base};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"-base"} 51 5 56
82  
83 56 for my $class (@{all_my_bases($self_package)}) {
84 84 ▶ next unless $class->isa('Class::Spiffy');
Branch: 1 branch
BranchTrueFalseTotal
unless $class->isa("Class::Spiffy") 1 83 84
85 my @export = grep {
86 225 not defined &{"$caller_package\::$_"};
87 83 ▶ } ( @{"$class\::EXPORT"},
Branch: 1 branch
BranchTrueFalseTotal
$args->{'-base'} ? : 75 8 83
88 83 $args->{-base} ? @{"$class\::EXPORT_BASE"} : (),
89 );
90 my @export_ok = grep {
91 965 not defined &{"$caller_package\::$_"};
92 83 } @{"$class\::EXPORT_OK"};
93  
94 # Avoid calling the expensive Exporter::export
95 # if there is nothing to do (optimization)
96 83 my %exportable = map { ($_, 1) } @export, @export_ok;
97 83 ▶ next unless keys %exportable;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %exportable 14 69 83
98  
99 69 my @export_save = @{"$class\::EXPORT"};
100 69 my @export_ok_save = @{"$class\::EXPORT_OK"};
101 69 @{"$class\::EXPORT"} = @export;
102 69 @{"$class\::EXPORT_OK"} = @export_ok;
103 my @list = grep {
104 69 (my $v = $_) =~ s/^[\!\:]//;
105 0 ▶ $exportable{$v} or ${"$class\::EXPORT_TAGS"}{$v};
Condition33%1/3
$exportable{$v} || do { "${class}::EXPORT_TAGS" }->{$v}
l!l&&r!l&&!r
800
MC/DC0%0/2
$exportable{$v} || do { "${class}::EXPORT_TAGS" }->{$v}
$exportable{$v} do { "${class}::EXPORT_TAGS" }->{$v}
Truth table33%1/3
A || B
A= $exportable{$v}B= do { "${class}::EXPORT_TAGS" }->{$v}
ABresult
1X1
011
000
106 } @export_list;
107 69 Exporter::export($class, $caller_package, @list);
108 69 @{"$class\::EXPORT"} = @export_save;
109 69 @{"$class\::EXPORT_OK"} = @export_ok_save;
110 }
111 }
112  
113 3 ▶sub base {
Subroutine base: called CC=1 SCAR=0.0
Pod undocumented
114 3 push @_, -base;
115 3 goto &import;
116 }
117  
118 93 ▶sub all_my_bases {
Subroutine all_my_bases: called CC=3 SCAR=11.0
Pod undocumented
119 93 my $class = shift;
120  
121 return $bases_map->{$class}
122 93 ▶ if defined $bases_map->{$class};
Branch: 1 branch
BranchTrueFalseTotal
if defined $bases_map->{$class} 44 49 93
123  
124 49 my @bases = ($class);
125 26 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
126 49 for my $base_class (@{"${class}::ISA"}) {
127 25 push @bases, @{all_my_bases($base_class)};
128 }
129 49 my $used = {};
130 49 $bases_map->{$class} = [grep {not $used->{$_}++} @bases];
131 }
132  
133 26 my %code = (
134 sub_start =>
135 "sub {\n",
136 set_default =>
137 " \$_[0]->{%s} = %s\n unless exists \$_[0]->{%s};\n",
138 init =>
139 " return \$_[0]->{%s} = do { my \$self = \$_[0]; %s }\n" .
140 " unless \$#_ > 0 or defined \$_[0]->{%s};\n",
141 weak_init =>
142 " return do {\n" .
143 " \$_[0]->{%s} = do { my \$self = \$_[0]; %s };\n" .
144 " Scalar::Util::weaken(\$_[0]->{%s}) if ref \$_[0]->{%s};\n" .
145 " \$_[0]->{%s};\n" .
146 " } unless \$#_ > 0 or defined \$_[0]->{%s};\n",
147 return_if_get =>
148 " return \$_[0]->{%s} unless \$#_ > 0;\n",
149 set =>
150 " \$_[0]->{%s} = \$_[1];\n",
151 weaken =>
152 " Scalar::Util::weaken(\$_[0]->{%s}) if ref \$_[0]->{%s};\n",
153 sub_end =>
154 " return \$_[0]->{%s};\n}\n",
155 );
156  
157 23 ▶sub field {
Subroutine field: called CC=16 SCAR=27.8
Pod documented
158 23 my $package = caller;
159 23 my ($args, @values) = do {
160 26 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
161 23 ▶ local *boolean_arguments = sub { (qw(-weak)) };
Subroutine __ANON__: called CC=1 SCAR=0.0
162 23 ▶ local *paired_arguments = sub { (qw(-package -init)) };
Subroutine __ANON__: called CC=1 SCAR=0.0
163 23 Class::Spiffy->parse_arguments(@_);
164 };
165 23 my ($field, $default) = @values;
166 23 ▶ $package = $args->{-package} if defined $args->{-package};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args->{"-package"} 1 22 23
167 die "Cannot have a default for a weakened field ($field)"
168 23 ▶ if defined $default && $args->{-weak};
Branch: 1 branch
BranchTrueFalseTotal
if defined $default and $args->{"-weak"} 0 23 23
Condition66%2/3
defined $default and $args->{"-weak"}
!ll&&!rl&&r
8150
MC/DC0%0/2
defined $default and $args->{"-weak"}
defined $default $args->{"-weak"}
Truth table66%2/3
A and B
A= defined $defaultB= $args->{"-weak"}
ABresult
0X0
100
111
169 23 ▶ return if defined &{"${package}::$field"};
Branch: 1 branch
BranchTrueFalseTotal
if defined &{"${package}::$field";} 0 23 23
170 23 ▶ require Scalar::Util if $args->{-weak};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"-weak"} 1 22 23
171 23 ▶ my $default_string =
Branch: 2 branches
BranchTrueFalseTotal
ref $default eq 'ARRAY' && !@$default ? : 2 21 23
ref $default eq 'HASH' && !keys(%$default) ? : 2 19 21
Condition100%6/6
ref $default eq "ARRAY" and not @$default
!ll&&!rl&&r
1922
ref $default eq "HASH" and not keys %$default
!ll&&!rl&&r
1722
MC/DC100%2/2
ref $default eq "ARRAY" and not @$default
ref $default eq "ARRAY" not @$default
MC/DC100%2/2
ref $default eq "HASH" and not keys %$default
ref $default eq "HASH" not keys %$default
Truth table100%3/3
A and not(B)
A= ref $default eq "ARRAY"B= not @$default
ABresult
0X0
100
111
Truth table100%3/3
A and not(B)
A= ref $default eq "HASH"B= not keys %$default
ABresult
0X0
100
111
172 ( ref($default) eq 'ARRAY' and not @$default )
173 ? '[]'
174 : (ref($default) eq 'HASH' and not keys %$default )
175 ? '{}'
176 : default_as_code($default);
177  
178 23 my $code = $code{sub_start};
179 23 ▶ if ($args->{-init}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"-init"}) 2 21 23
180 2 ▶ my $fragment = $args->{-weak} ? $code{weak_init} : $code{init};
Branch: 1 branch
BranchTrueFalseTotal
$args->{'-weak'} ? : 1 1 2
181 2 $code .= sprintf $fragment, $field, $args->{-init}, ($field) x 4;
182 }
183 23 ▶ $code .= sprintf $code{set_default}, $field, $default_string, $field
Branch: 1 branch
BranchTrueFalseTotal
if defined $default 15 8 23
184 if defined $default;
185 23 $code .= sprintf $code{return_if_get}, $field;
186 23 $code .= sprintf $code{set}, $field;
187 $code .= sprintf $code{weaken}, $field, $field
188 23 ▶ if $args->{-weak};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"-weak"} 1 22 23
189 23 $code .= sprintf $code{sub_end}, $field;
190  
191 0 ▶ my $sub = eval $code;
Subroutine __ANON__: called CC=2 SCAR=7.1
Branch: 2 branches
BranchTrueFalseTotal
unless exists $_[0]{"volume"} 7 3 10
unless $#_ > 0 4 0 4
192 23 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 23 23
193 26 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
194 23 *{"${package}::$field"} = $sub;
195 23 ▶ return $code if defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if defined wantarray 7 16 23
196 }
197  
198 19 ▶sub default_as_code {
Subroutine default_as_code: called CC=1 SCAR=0.0
Pod undocumented
199 19 require Data::Dumper;
200 19 local $Data::Dumper::Sortkeys = 1;
201 19 my $code = Data::Dumper::Dumper(shift);
202 19 $code =~ s/^\$VAR1 = //;
203 19 $code =~ s/;$//;
204 19 return $code;
205 }
206  
207 3 ▶sub const {
Subroutine const: called CC=3 SCAR=11.0
Pod documented
208 3 my $package = caller;
209 3 my ($args, @values) = do {
210 26 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
211 3 ▶ local *paired_arguments = sub { (qw(-package)) };
Subroutine __ANON__: called CC=1 SCAR=0.0
212 3 Class::Spiffy->parse_arguments(@_);
213 };
214 3 my ($field, $default) = @values;
215 3 ▶ $package = $args->{-package} if defined $args->{-package};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args->{"-package"} 0 3 3
216 26 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
217 3 ▶ return if defined &{"${package}::$field"};
Branch: 1 branch
BranchTrueFalseTotal
if defined &{"${package}::$field";} 0 3 3
218 3 ▶ *{"${package}::$field"} = sub { $default }
Subroutine __ANON__: called CC=1 SCAR=0.0
219 3 }
220  
221 1 ▶sub stub {
Subroutine stub: called CC=3 SCAR=11.0
Pod documented
222 1 my $package = caller;
223 1 my ($args, @values) = do {
224 26 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
225 1 ▶ local *paired_arguments = sub { (qw(-package)) };
Subroutine __ANON__: called CC=1 SCAR=0.0
226 1 Class::Spiffy->parse_arguments(@_);
227 };
228 1 my ($field, $default) = @values;
229 1 ▶ $package = $args->{-package} if defined $args->{-package};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args->{"-package"} 0 1 1
230 26 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
231 1 ▶ return if defined &{"${package}::$field"};
Branch: 1 branch
BranchTrueFalseTotal
if defined &{"${package}::$field";} 0 1 1
232 1 *{"${package}::$field"} =
233 1 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
234 1 require Carp;
235 1 Carp::confess
236 "Method $field in package $package must be subclassed";
237 }
238 1 }
239  
240 85 ▶sub parse_arguments {
Subroutine parse_arguments: called CC=10 SCAR=23.3
Pod documented
241 85 my $class = shift;
242 85 my ($args, @values) = ({}, ());
243 85 my %booleans = map { ($_, 1) } $class->boolean_arguments;
244 85 my %pairs = map { ($_, 1) } $class->paired_arguments;
245 85 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 112 85 197
246 112 my $elem = shift;
247 112 ▶ if (defined $elem and defined $booleans{$elem}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $elem and defined $booleans{$elem}) { } 55 57 112
elsif (defined $elem and defined $pairs{$elem} and @_) { } 4 53 57
Condition66%2/3
defined $elem and defined $booleans{$elem}
!ll&&!rl&&r
05755
MC/DC50%1/2
defined $elem and defined $booleans{$elem}
defined $elem defined $booleans{$elem}
Truth table66%2/3
A and B
A= defined $elemB= defined $booleans{$elem}
ABresult
0X0
100
111
248 55 ▶ $args->{$elem} = (@_ and $_[0] =~ /^[01]$/)
Branch: 1 branch
BranchTrueFalseTotal
@_ && $_[0] =~ /^[01]$/ ? : 0 55 55
Condition66%2/3
@_ and $_[0] =~ /^[01]$/
!ll&&!rl&&r
4780
MC/DC0%0/2
@_ and $_[0] =~ /^[01]$/
@_ $_[0] =~ /^[01]$/
Truth table66%2/3
A and B
A= @_B= $_[0] =~ /^[01]$/
ABresult
0X0
100
111
249 ? shift
250 : 1;
251 }
252 ▶ elsif (defined $elem and defined $pairs{$elem} and @_) {
Condition66%4/6
defined $elem and defined $pairs{$elem} and @_
!ll&&!rl&&r
5304
defined $elem and defined $pairs{$elem}
!ll&&!rl&&r
0534
MC/DC33%1/3
defined $elem and defined $pairs{$elem} and @_
defined $elem defined $pairs{$elem} @_
Truth table50%2/4
A and B and C
A= defined $elemB= defined $pairs{$elem}C= @_
ABCresult
0XX0
10X0
1100
1111
253 4 $args->{$elem} = shift;
254 }
255 else {
256 53 push @values, $elem;
257 }
258 }
259 85 ▶ return wantarray ? ($args, @values) : $args;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 84 1 85
260 }
261  
262 5 ▶sub boolean_arguments { () }
Subroutine boolean_arguments: called CC=1 SCAR=0.0
Pod documented
263 1 ▶sub paired_arguments { () }
Subroutine paired_arguments: called CC=1 SCAR=0.0
Pod documented
264  
265 # get a unique id for any node
266 4 ▶sub id {
Subroutine id: called CC=5 SCAR=23.4
Pod undocumented
267 4 ▶ if (not ref $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $_[0]) 0 4 4
268 0 ▶ return 'undef' if not defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 0 0 0
269 0 ▶ \$_[0] =~ /\((\w+)\)$/o or die;
Branch: 1 branch
BranchTrueFalseTotal
(\$_[0]) =~ /\((\w+)\)$/o or die 0 0 0
270 0 return "$1-S";
271 }
272 4 require overload;
273 4 ▶ overload::StrVal($_[0]) =~ /\((\w+)\)$/o or die;
Branch: 1 branch
BranchTrueFalseTotal
overload::StrVal($_[0]) =~ /\((\w+)\)$/o or die 4 0 4
274 4 return $1;
275 }
276  
277 #===============================================================================
278 # It's super, man.
279 #===============================================================================
280 package DB;
281 {
282 26 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
283 12 ▶ sub super_args {
Subroutine super_args: called CC=2 SCAR=7.2
Pod undocumented
284 12 ▶ my @dummy = caller(@_ ? $_[0] : 2);
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 12 0 12
285 12 return @DB::args;
286 }
287 }
288  
289 26 package Class::Spiffy;
290 12 ▶sub super {
Subroutine super: called CC=11 SCAR=24.0
Pod documented
291 12 my $method;
292 12 my $frame = 1;
293 12 ▶ while ($method = (caller($frame++))[3]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method = (caller $frame++)[3]) 14 0 14
294 14 ▶ $method =~ s/.*::// and last;
Branch: 1 branch
BranchTrueFalseTotal
$method =~ s/.*::// and last 12 2 14
295 }
296 12 my @args = DB::super_args($frame);
297 12 ▶ @_ = @_ ? ($args[0], @_) : @args;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 12 12
298 12 ▶ my $class = ref $_[0] ? ref $_[0] : $_[0];
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 7 5 12
299 12 my $caller_class = caller;
300 12 my $seen = 0;
301 my @super_classes = reverse grep {
302 50 ▶ ($seen or $seen = ($_ eq $caller_class)) ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$seen || ($seen = $_ eq $caller_class) ? : 20 30 50
Condition100%3/3
$seen or $seen = $_ eq $caller_class
l!l&&r!l&&!r
81230
MC/DC100%2/2
$seen or $seen = $_ eq $caller_class
$seen $seen = $_ eq $caller_class
Truth table100%3/3
A or B
A= $seenB= $seen = $_ eq $caller_class
ABresult
1X1
011
000
303 12 } reverse @{all_my_bases($class)};
304 12 for my $super_class (@super_classes) {
305 26 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
306 16 ▶ next if $super_class eq $class;
Branch: 1 branch
BranchTrueFalseTotal
if $super_class eq $class 0 16 16
307 16 ▶ if (defined &{"${super_class}::$method"}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined &{"${super_class}::$method";}) 10 6 16
308 10 ▶ ${"$super_class\::AUTOLOAD"} = ${"$class\::AUTOLOAD"}
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "AUTOLOAD" 2 8 10
309 if $method eq 'AUTOLOAD';
310 10 return &{"${super_class}::$method"};
311 }
312 }
313 2 return;
314 }
315  
316 #===============================================================================
317 # This code deserves a spanking, because it is being very naughty.
318 # It is exchanging base.pm's import() for its own, so that people
319 # can use base.pm with Class::Spiffy modules, without being the wiser.
320 #===============================================================================
321 26 my $real_base_import;
322 my $real_mixin_import;
323  
324 26 ▶BEGIN {
Subroutine BEGIN: called CC=3 SCAR=11.1
325 26 ▶ require base unless defined $INC{'base.pm'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $INC{"base.pm"} 9 32 41
326 26 ▶ $INC{'mixin.pm'} ||= 'Class/Spiffy/mixin.pm';
Condition33%1/3
$INC{"mixin.pm"} ||= "Class/Spiffy/mixin.pm"
l!l
0260
MC/DC0%0/1
$INC{"mixin.pm"} ||= "Class/Spiffy/mixin.pm"
$INC{"mixin.pm"}
Truth table50%1/2
A
A= $INC{"mixin.pm"}
Aresult
11
00
327 26 $real_base_import = \&base::import;
328 26 $real_mixin_import = \&mixin::import;
329 26 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
330 26 *base::import = \&spiffy_base_import;
331 26 *mixin::import = \&spiffy_mixin_import;
332 }
333  
334 # my $i = 0;
335 # while (my $caller = caller($i++)) {
336 # next unless $caller eq 'base' or $caller eq 'mixin';
337 # croak <<END;
338 # Class::Spiffy must be loaded before calling 'use base' or 'use mixin' with a
339 # Class::Spiffy module. See the documentation of Class::Spiffy for details.
340 # END
341 # }
342  
343 23 ▶sub spiffy_base_import {
Subroutine spiffy_base_import: called CC=6 SCAR=18.0
Pod undocumented
344 23 my @base_classes = @_;
345 23 shift @base_classes;
346 26 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
347 goto &$real_base_import
348 unless grep {
349 23 ▶ eval "require $_" unless %{"$_\::"};
Branch: 2 branches
BranchTrueFalseTotal
unless grep {eval "require $_" unless %{"${_}::";}; $_->isa("Class::Spiffy");} @base_classes 3 20 23
unless %{"${_}::";} 0 24 24
350 24 $_->isa('Class::Spiffy');
351 } @base_classes;
352 20 my $inheritor = caller(0);
353 20 for my $base_class (@base_classes) {
354 21 ▶ next if $inheritor->isa($base_class);
Branch: 1 branch
BranchTrueFalseTotal
if $inheritor->isa($base_class) 3 18 21
355 18 ▶ croak "Can't mix Class::Spiffy and non-Class::Spiffy classes in 'use base'.\n",
Branch: 1 branch
BranchTrueFalseTotal
unless $base_class->isa("Class::Spiffy") 0 18 18
356 "See the documentation of Class::Spiffy for details\n "
357 unless $base_class->isa('Class::Spiffy');
358 18 $stack_frame = 1; # tell import to use different caller
359 18 import($base_class, '-base');
360 18 $stack_frame = 0;
361 }
362 }
363  
364 1 ▶sub mixin {
Subroutine mixin: called CC=1 SCAR=0.0
Pod documented
365 1 my $self = shift;
366 1 my $target_class = ref($self);
367 1 spiffy_mixin_import($target_class, @_)
368 }
369  
370 7 ▶sub spiffy_mixin_import {
Subroutine spiffy_mixin_import: called CC=4 SCAR=13.9
Pod undocumented
371 7 my $target_class = shift;
372 7 ▶ $target_class = caller(0)
Branch: 1 branch
BranchTrueFalseTotal
if $target_class eq "mixin" 6 1 7
373 if $target_class eq 'mixin';
374 7 ▶ my $mixin_class = shift
Branch: 1 branch
BranchTrueFalseTotal
my $mixin_class = shift() or die "Nothing to mixin" 7 0 7
375 or die "Nothing to mixin";
376 7 eval "require $mixin_class";
377 7 my @roles = @_;
378 7 my $pseudo_class = join '-', $target_class, $mixin_class, @roles;
379 7 my %methods = spiffy_mixin_methods($mixin_class, @roles);
380 26 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
381 26 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
382 7 @{"$pseudo_class\::ISA"} = @{"$target_class\::ISA"};
383 7 @{"$target_class\::ISA"} = ($pseudo_class);
384 7 for (keys %methods) {
385 497 *{"$pseudo_class\::$_"} = $methods{$_};
386 }
387 }
388  
389 7 ▶sub spiffy_mixin_methods {
Subroutine spiffy_mixin_methods: called CC=3 SCAR=11.1
Pod undocumented
390 7 my $mixin_class = shift;
391 26 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
392 7 my %methods = spiffy_all_methods($mixin_class);
393 map {
394 7 $methods{$_}
395 497 ? ($_, \ &{"$methods{$_}\::$_"})
396 0 ▶ : ($_, \ &{"$mixin_class\::$_"})
Branch: 2 branches
BranchTrueFalseTotal
$methods{$_} ? : 497 0 497
@_ ? : 5 2 7
397 } @_
398 ? (get_roles($mixin_class, @_))
399 : (keys %methods);
400 }
401  
402 5 ▶sub get_roles {
Subroutine get_roles: called CC=8 SCAR=20.8
Pod undocumented
403 5 my $mixin_class = shift;
404 5 my @roles = @_;
405 5 ▶ while (grep /^!*:/, @roles) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep /^!*:/, @roles) 6 5 11
406 @roles = map {
407 6 s/!!//g;
408 /^!:(.*)/ ? do {
409 3 my $m = "_role_$1";
410 3 ▶ map("!$_", $mixin_class->$m);
Branch: 1 branch
BranchTrueFalseTotal
/^:(.*)/ ? : 5 10 15
411 } :
412 18 ▶ /^:(.*)/ ? do {
Branch: 1 branch
BranchTrueFalseTotal
/^!:(.*)/ ? : 3 15 18
413 5 my $m = "_role_$1";
414 5 ($mixin_class->$m);
415 } :
416 ($_)
417 } @roles;
418 }
419 5 ▶ if (@roles and $roles[0] =~ /^!/) {
Branch: 1 branch
BranchTrueFalseTotal
if (@roles and $roles[0] =~ /^!/) 2 3 5
Condition66%2/3
@roles and $roles[0] =~ /^!/
!ll&&!rl&&r
032
MC/DC50%1/2
@roles and $roles[0] =~ /^!/
@roles $roles[0] =~ /^!/
Truth table66%2/3
A and B
A= @rolesB= $roles[0] =~ /^!/
ABresult
0X0
100
111
420 2 my %methods = spiffy_all_methods($mixin_class);
421 2 unshift @roles, keys(%methods);
422 }
423 5 my %roles;
424 5 for (@roles) {
425 42 s/!!//g;
426 42 ▶ delete $roles{$1}, next
Branch: 1 branch
BranchTrueFalseTotal
if /^!(.*)/ 14 28 42
427 if /^!(.*)/;
428 28 $roles{$_} = 1;
429 }
430 5 keys %roles;
431 }
432  
433 12 ▶sub spiffy_all_methods {
Subroutine spiffy_all_methods: called CC=4 SCAR=13.9
Pod undocumented
434 26 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
435 12 my $class = shift;
436 12 ▶ return if $class eq 'Class::Spiffy';
Branch: 1 branch
BranchTrueFalseTotal
if $class eq "Class::Spiffy" 0 12 12
437 my %methods = map {
438 532 ($_, $class)
439 } grep {
440 638 ▶ defined &{"$class\::$_"} and not /^_/
Condition100%3/3
defined &{"${class}::$_";} && !/^_/
!ll&&!rl&&r
8422532
MC/DC100%2/2
defined &{"${class}::$_";} && !/^_/
defined &{"${class}::$_";} !/^_/
Truth table100%3/3
A && not(B)
A= defined &{"${class}::$_";}B= !/^_/
ABresult
0X0
100
111
441 12 } keys %{"$class\::"};
442 12 my %super_methods;
443 3 %super_methods = spiffy_all_methods(${"$class\::ISA"}[0])
444 12 ▶ if @{"$class\::ISA"};
Branch: 1 branch
BranchTrueFalseTotal
if @{"${class}::ISA";} 3 9 12
445 12 %{{%super_methods, %methods}};
446 }
447  
448  
449 # END of naughty code.
450 #===============================================================================
451 # Debugging support
452 #===============================================================================
453 0 ▶sub spiffy_dump {
Subroutine spiffy_dump: not called CC=2 SCAR=13.4
Pod undocumented
454 26 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
455 0 ▶ if ($dump eq 'dumper') {
Branch: 1 branch
BranchTrueFalseTotal
if ($dump eq "dumper") 0 0 0
456 0 require Data::Dumper;
457 0 $Data::Dumper::Sortkeys = 1;
458 0 $Data::Dumper::Indent = 1;
459 0 return Data::Dumper::Dumper(@_);
460 }
461 0 require YAML;
462 0 $YAML::UseVersion = 0;
463 0 return YAML::Dump(@_) . "...\n";
464 }
465  
466 0 ▶sub at_line_number {
Subroutine at_line_number: not called CC=1 SCAR=6.9
Pod undocumented
467 0 my ($file_path, $line_number) = (caller(1))[1,2];
468 0 " at $file_path line $line_number\n";
469 }
470  
471 0 ▶sub WWW {
Subroutine WWW: not called CC=2 SCAR=17.9
Pod undocumented
472 0 warn spiffy_dump(@_) . at_line_number;
473 0 ▶ return wantarray ? @_ : $_[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
474 }
475  
476 0 ▶sub XXX {
Subroutine XXX: not called CC=1 SCAR=6.9
Pod undocumented
477 0 die spiffy_dump(@_) . at_line_number;
478 }
479  
480 0 ▶sub YYY {
Subroutine YYY: not called CC=2 SCAR=17.9
Pod undocumented
481 0 print spiffy_dump(@_) . at_line_number;
482 0 ▶ return wantarray ? @_ : $_[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
483 }
484  
485 0 ▶sub ZZZ {
Subroutine ZZZ: not called CC=1 SCAR=6.9
Pod undocumented
486 0 require Carp;
487 0 Carp::confess spiffy_dump(@_);
488 }
489  
490 26 1;
491  
492 __END__
↑ Summary blib/lib/Class/Spiffy/mixin.pm »