classes.pm

filter: Statementstmt 78.0% 600 / 769 Branchbran 63.2% 243 / 384 Conditioncond 50.9% 83 / 163 MC/DCmcdc 29.1% 30 / 103 Subroutinesub 77.7% 56 / 72 Podpod 100% 14 / 14 totaltotal 68.1% 1026 / 1505 CC 271 SCAR 52.9
CC
271
Cov
70%
classes
480.3
dump
420.0
clone
210.0
CRAP 198.7
↑ Summary classes/Test.pm »
1 package classes;
2  
3 # $Id: classes.pm 147 2008-03-08 16:04:33Z rmuhle $
4  
5 27 our $VERSION = '0.944';
6 45 ▶use 5.006_001;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 27 ▶use Scalar::Util 'reftype', 'blessed'; # standard from 5.8.1
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # keep false alarms quiet
10 27 ▶use strict; no strict 'refs'; no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 27 sub classes (@); *define = \&classes::classes;
13 sub load;
14  
15 # fastest PERL_VERSION constant (from Schwern's CLASS module)
16 27 ▶BEGIN{ *PERL_VERSION = eval "sub () { $] } " }
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 # do not change
19 27 $classes::ok_class_name
20 = qr/^(?!(?:
21 B$| # binary module
22 _$
23 )
24 )
25 (?i:(?:[a-z_]\w*\:\:)*[a-z_]\w*)$/xo;
26 27 $classes::ok_attr_name = qr/^(?i:[a-z_]\w*)$/xo;
27  
28 # this one changable via tag
29 27 $classes::def_base_exception = 'X::classes::traceable';
30  
31 ######################################################################
32  
33 173 ▶sub import {
Subroutine import: called CC=3 SCAR=11.0
34 173 my $package = shift;
35 173 my (@class_declarations, $dynamic);
36 173 my $caller = caller;
37  
38 # use classes DECLARATION;
39 173 ▶ if ( !ref $_[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $_[0]) { } 171 2 173
40 171 ▶ push @_, 1 if @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 0 171 171
41 171 push @class_declarations, {@_};
42 }
43  
44 # use classes {DECLARATION}, {DECLARATION};
45 else {
46 2 @class_declarations = @_;
47 }
48  
49 # implied name
50 173 map { $_->{'caller'} = $caller } @class_declarations;
51  
52 173 classes::classes(@class_declarations);
53  
54 173 return $package;
55 }
56  
57 ######################################################################
58 # Called by import() itself to define classes at compile-time.
59 # Exported into anything that calls 'use classes'.
60 501 ▶sub classes (@) {
Subroutine classes: called CC=117 SCAR=61.7
Pod documented
61 501 my @args = @_;
62 501 my $tags;
63  
64 # classes { DECLARATION };
65 501 ▶ if ( ref $args[0] eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $args[0] eq 'HASH') { } 471 30 501
elsif (@args > 1) { } 29 1 30
66 471 $tags = $args[0];
67 471 ▶ map { classes $_ } @args if @args > 1; # recurse
Branch: 1 branch
BranchTrueFalseTotal
if @args > 1 27 444 471
68 }
69  
70 # classes DECLARATION;
71 elsif ( @args > 1 ) {
72 29 $tags = {@args};
73 }
74  
75 #-----------------------------------------------------------------
76  
77 501 my ($class, $type, $inherits, $extends,
78 $throws, $exceptions, $attrs, $methods,
79 $class_attrs, $class_attrs_ro, $class_methods, $attrs_ro,
80 $new_m, $init_m, $clone_m, $caller,
81 $mixes, $mixes_def, $class_mixes, $dump_m,
82 $base_exception, $unqualified, $pkg_mixes, $needs,
83 $pkg_methods, $needs, $attrs_pr, $justahash,
84 $class_attrs_pr, $noaccessors, $def_base_exception,
85 );
86  
87 501 my $lookup = {
88 caller => \$caller,
89 name => \$class,
90 attrs => \$attrs,
91 attrs_ro => \$attrs_ro,
92 attrs_pr => \$attrs_pr,
93 class_attrs => \$class_attrs,
94 class_attrs_ro => \$class_attrs_ro,
95 class_attrs_pr => \$class_attrs_pr,
96 mixes => \$mixes,
97 class_mixes => \$class_mixes,
98 pkg_mixes => \$pkg_mixes,
99 mixes_def => \$mixes_def,
100 methods => \$methods,
101 class_methods => \$class_methods,
102 pkg_methods => \$pkg_methods,
103 throws => \$throws,
104 needs => \$needs,
105 extends => \$extends,
106 inherits => \$inherits,
107 type => \$type,
108 exceptions => \$exceptions,
109 base_exception => \$base_exception,
110 new => \$new_m,
111 init => \$init_m,
112 clone => \$clone_m,
113 dump => \$dump_m,
114 def_base_exception => \$def_base_exception,
115 unqualified => \$unqualified,
116 noaccessors => \$noaccessors,
117 justahash => \$justahash,
118 };
119  
120 501 ▶ while ( my ( $tag, $val ) = each %$tags ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$tags) 1521 501 2022
121 1521 ▶ my $ref = $lookup->{$tag}
Branch: 1 branch
BranchTrueFalseTotal
my $ref = $lookup->{$tag} or "X::Usage"->throw("$tag ??") 1521 0 1521
122 or X::Usage->throw( "$tag ??" );
123 1521 $$ref = $val;
124 }
125  
126 501 ▶ $class ||= $caller || caller;
Condition66%4/6
$class ||= $caller || caller
l!l&&r!l&&!r
3451560
$caller || caller
l!l&&r!l&&!r
143130
MC/DC0%0/3
$class ||= $caller || caller
$class $caller caller
Truth table0%0/4
A ||= B || C
A= $classB= $callerC= caller
ABCresult
1XX1
01X1
0011
0000
127 501 ▶ X::InvalidName->throw("name=>'$class'")
Branch: 1 branch
BranchTrueFalseTotal
unless $class =~ /$classes::ok_class_name/x 0 501 501
128 if $class !~ $classes::ok_class_name; # main ok
129  
130 #-----------------------------------------------------------------
131 # add CLASS and $CLASS constants (from Michael Schwern's CLASS)
132  
133 501 my $const_CLASS = $class . '::CLASS';
134 501 ▶ if (!$$const_CLASS) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($$const_CLASS) 464 37 501
135 464 *$const_CLASS = \$class;
136 464 if ( PERL_VERSION >= 5.008 ) {
137 0 ▶ *$const_CLASS = sub () {$class};
Subroutine __ANON__: not called CC=1 SCAR=1.2
138 }
139 else {
140 *$const_CLASS = eval " sub () { q[$class] } ";
141 }
142 }
143  
144 #-----------------------------------------------------------------
145  
146 501 my $class_decl = $class . '::DECL';
147 501 ▶ if (!*$class_decl{CODE}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (*$class_decl{"CODE"}) 464 37 501
148 464 $$class_decl->{'name'} = $class;
149 464 ▶ $$class_decl->{'type'} ||= 'static';
Condition50%1/2
$$class_decl->{"type"} ||= "static"
l!l
0464
MC/DC0%0/1
$$class_decl->{"type"} ||= "static"
$$class_decl->{"type"}
Truth table50%1/2
A
A= $$class_decl->{"type"}
Aresult
11
00
150 464 if ( PERL_VERSION >= 5.008 ) {
151 464 ▶ *$class_decl = sub () { $$class_decl };
Subroutine __ANON__: called CC=1 SCAR=0.0
152 }
153 else {
154 *$class_decl = eval 'sub () { $' . $class_decl . '}';
155 }
156 }
157  
158 #-----------------------------------------------------------------
159  
160 # setup the special MIXIN method and var
161 501 my $class_MIXIN = $class . '::MIXIN';
162 501 ▶ if (!*$class_MIXIN{CODE}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (*$class_MIXIN{"CODE"}) 464 37 501
163 464 $$class_MIXIN = undef;
164 464 if ( PERL_VERSION >= 5.008 ) {
165 0 ▶ *$class_MIXIN = sub () { $$class_MIXIN };
Subroutine __ANON__: not called CC=1 SCAR=1.2
166 }
167 else {
168 *$class_MIXIN = eval 'sub () { $' . $class_MIXIN . '}';
169 }
170 }
171  
172 #-----------------------------------------------------------------
173  
174 501 ▶ $classes::def_base_exception = $def_base_exception
Branch: 1 branch
BranchTrueFalseTotal
if $def_base_exception 2 499 501
175 if $def_base_exception;
176  
177 #-----------------------------------------------------------------
178  
179 # keep using a type that has already been declared
180 501 ▶ if ($type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type) { } 40 461 501
181 40 ▶ X::Usage->throw( "type=>'$type' ??\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $type eq "static" or $type eq "dynamic" or $type eq "mixable" 0 40 40
Condition83%5/6
$type eq "static" or $type eq "dynamic" or $type eq "mixable"
l!l&&r!l&&!r
5350
$type eq "static" or $type eq "dynamic"
l!l&&r!l&&!r
3235
MC/DC0%0/3
$type eq "static" or $type eq "dynamic" or $type eq "mixable"
$type eq "static" $type eq "dynamic" $type eq "mixable"
Truth table75%3/4
A or B or C
A= $type eq "static"B= $type eq "dynamic"C= $type eq "mixable"
ABCresult
1XX1
01X1
0011
0000
182 . " type => 'static'|'dynamic'|'mixable'\n" )
183 if ! ( $type eq 'static'
184 || $type eq 'dynamic'
185 || $type eq 'mixable' );
186 40 $$class_decl->{'type'} = $type;
187 }
188 else {
189 461 $type = $$class_decl->{'type'};
190 }
191  
192 #-----------------------------------------------------------------
193 # export a 'classes' function/statement into caller
194  
195 501 ▶ *{ $class . '::classes' } = \&classes::classes
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "dynamic" 4 497 501
196 if $type eq 'dynamic';
197  
198 #-----------------------------------------------------------------
199  
200 501 my $class_super = $class . '::SUPER';
201 501 ▶ if (!*$class_super{CODE}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (*$class_super{"CODE"}) 464 37 501
202 464 if ( PERL_VERSION >= 5.008 ) {
203 464 ▶ *$class_super = sub () { ${ $class.'::ISA' }[0] };
Subroutine __ANON__: called CC=1 SCAR=0.0
204 }
205 else {
206 *$class_super = eval 'sub () { $'.$class.'::ISA[0] }';
207 }
208 }
209  
210 #-----------------------------------------------------------------
211  
212 501 ▶ _define_mixins( $class, $mixes, $mixes_def, '')
Branch: 1 branch
BranchTrueFalseTotal
if defined $mixes 51 450 501
213 if defined $mixes;
214 501 ▶ _define_mixins( $class, $class_mixes, $mixes_def, 'class_' )
Branch: 1 branch
BranchTrueFalseTotal
if defined $class_mixes 0 501 501
215 if defined $class_mixes;
216 501 ▶ _define_mixins( $class, $pkg_mixes, $mixes_def, 'pkg_' )
Branch: 1 branch
BranchTrueFalseTotal
if defined $pkg_mixes 0 501 501
217 if defined $pkg_mixes;
218  
219 #-----------------------------------------------------------------
220 # same as 'throws' for now
221  
222 # 'needs' IS DEPRECATED, use perl 'use' instead, too many
223 # compilers/builders depend on 'use' to identify needed modules
224 501 ▶ if ($needs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($needs) 0 501 501
225 0 my $is_ref = ref $needs;
226  
227 # needs => 'SomeOtherMod',
228 0 ▶ if ( !$is_ref ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_ref) 0 0 0
229 0 $needs = [$needs];
230 0 $is_ref = 'ARRAY';
231 }
232  
233 0 ▶ X::Usage->throw( <<'EOM' ) if $is_ref ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if $is_ref ne "ARRAY" 0 0 0
234  
235 DEPRECATED, use perl 'use' instead, too many compilers/builders
236 depend on 'use' to identify which modules are needed
237 EOM
238  
239 0 for my $pkg (@$needs) {
240 0 classes::load($pkg);
241 0 push @{ $$class_decl->{'needs'} }, $pkg;
242 }
243 } # needs
244  
245  
246 #-----------------------------------------------------------------
247  
248 501 ▶ if ($throws) {
Branch: 1 branch
BranchTrueFalseTotal
if ($throws) 1 500 501
249 1 my $is_ref = ref $throws;
250  
251 # throws => 'X::Usage',
252 1 ▶ if ( !$is_ref ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_ref) 1 0 1
253 1 $throws = [$throws];
254 1 $is_ref = 'ARRAY';
255 }
256  
257 1 ▶ X::Usage->throw( <<'EOM' ) if $is_ref ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if $is_ref ne "ARRAY" 0 1 1
258  
259 throws => 'X::Usage',
260 throws => [ 'X::Usage', 'X::Mine::NotFound' ],
261 EOM
262  
263 1 for my $exc (@$throws) {
264 1 classes::load($exc);
265 1 push @{ $$class_decl->{'throws'} }, $exc;
266 }
267 } # throws
268  
269 #-----------------------------------------------------------------
270
271 501 ▶ if ($base_exception) {
Branch: 1 branch
BranchTrueFalseTotal
if ($base_exception) 1 500 501
272  
273 # base_exception => {name=>'X::MyBase',},
274 1 ▶ if (ref $base_exception) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $base_exception) { } 0 1 1
275 0 classes::classes($base_exception);
276 0 $base_exception = $base_exception->{'name'};
277 }
278  
279 # base_exception => 'X::class',
280 # base_exception => 'Exception::Class',
281 else {
282 1 classes::load($base_exception);
283 }
284  
285 1 $$class_decl->{'base_exception'} = $base_exception;
286 }
287  
288 #-----------------------------------------------------------------
289  
290 501 ▶ if ($exceptions) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exceptions) 7 494 501
291 7 my $is_ref = ref $exceptions;
292 7 my $usage = <<'EOM';
293  
294 exceptions => 'X::MyClass::Own1',
295 exceptions => [
296 'X::MyClass::Own1',
297 'X::MyClass::Own2',
298 { name=>'X::MyClass::EOF', attrs=>['file'], ... },
299 ],
300 EOM
301  
302 # go with base exception class if one is declared
303 7 my $base_x_class = $base_exception;
304
305 # otherwise define X::MyClass default base
306 7 ▶ if (!$base_x_class) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($base_x_class) 6 1 7
307 6 $base_x_class = "X::$class";
308  
309 # but create only if hasn't been created already
310 6 ▶ if (!%{$base_x_class.'::'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (%{$base_x_class . '::';}) 5 1 6
311 5 classes::classes(
312 name => $base_x_class,
313 extends => $classes::def_base_exception,
314 );
315 }
316 }
317  
318 7 ▶ if ($is_ref ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_ref ne "ARRAY") 6 1 7
319 6 $exceptions = [$exceptions];
320 6 $is_ref = 'ARRAY';
321 }
322  
323 7 for my $exc (@$exceptions) {
324 8 my $exc_is_ref = ref $exc;
325  
326 # exceptions => ['X::Name']
327 8 ▶ if (!$exc_is_ref) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $exc_is_ref) { } 8 0 8
elsif ($exc_is_ref eq 'HASH') { } 0 0 0
328 8 ▶ X::NameUnavailable->throw([$exc]) if %{$exc.'::'};
Branch: 1 branch
BranchTrueFalseTotal
if %{$exc . '::';} 0 8 8
329 8 classes::classes(name=>$exc, extends=>$base_x_class);
330  
331 }
332  
333 # exceptions => [{name=>'X::Name',attrs=>['file']}]
334 elsif ($exc_is_ref eq 'HASH') {
335 0 my $name = $exc->{'name'};
336 0 ▶ $exc->{'extends'} ||= $base_x_class;
Condition0%0/3
$exc->{"extends"} ||= $base_x_class
l!l&&r!l&&!r
000
MC/DC0%0/2
$exc->{"extends"} ||= $base_x_class
$exc->{"extends"} $base_x_class
Truth table0%0/3
A ||= B
A= $exc->{"extends"}B= $base_x_class
ABresult
1X1
011
000
337 X::NameUnavailable->throw([$name])
338 0 ▶ if %{$name.'::'};
Branch: 1 branch
BranchTrueFalseTotal
if %{$name . '::';} 0 0 0
339 0 classes::classes($exc);
340 }
341
342 # bad
343 else {
344 0 X::Usage->throw($exceptions . $usage);
345 }
346  
347 8 push @{ $$class_decl->{'exceptions'} }, $exc;
348 }
349  
350 } # exceptions
351  
352 #-----------------------------------------------------------------
353  
354 # extends trumps inherits
355 501 ▶ if ($extends) {
Branch: 1 branch
BranchTrueFalseTotal
if ($extends) 352 149 501
356 352 ▶ X::Usage->throw(
Branch: 1 branch
BranchTrueFalseTotal
if $inherits or ref $extends 2 350 352
Condition100%3/3
$inherits or ref $extends
l!l&&r!l&&!r
11350
MC/DC100%2/2
$inherits or ref $extends
$inherits ref $extends
Truth table100%3/3
A or B
A= $inheritsB= ref $extends
ABresult
1X1
011
000
357 "extends=>'parent' OR inherits=>[qw( Parent1 Parent2 )]"
358 )
359 if $inherits || ref $extends;
360 350 classes::load($extends);
361 350 @{$class . '::ISA'} = ($extends);
362 350 $$class_decl->{'extends'} = $extends;
363 350 delete $$class_decl->{'inherits'};
364 }
365  
366 #-----------------------------------------------------------------
367  
368 499 ▶ if ($inherits) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inherits) 11 488 499
369 11 my $is_ref = ref $inherits;
370 11 my $usage = <<'EOM';
371  
372 extends => 'SuperClass',
373 inherits => 'SuperClass',
374 inherits => [qw( Class1 Class2 )],
375 EOM
376  
377 11 ▶ if (!$is_ref) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_ref) 8 3 11
378 8 $inherits = [$inherits];
379 8 $is_ref = 'ARRAY';
380 }
381  
382 11 ▶ X::Usage->throw($usage)
Branch: 1 branch
BranchTrueFalseTotal
if not $is_ref or $is_ref ne "ARRAY" 1 10 11
Condition66%2/3
not $is_ref or $is_ref ne "ARRAY"
l!l&&r!l&&!r
0110
MC/DC50%1/2
not $is_ref or $is_ref ne "ARRAY"
not $is_ref $is_ref ne "ARRAY"
Truth table66%2/3
not(A) or B
A= not $is_refB= $is_ref ne "ARRAY"
ABresult
1X1
011
000
383 if !$is_ref || $is_ref ne 'ARRAY';
384  
385 10 for my $parent (@$inherits) {
386 12 ▶ next if $class->isa($parent);
Branch: 1 branch
BranchTrueFalseTotal
if $class->isa($parent) 1 11 12
387 11 classes::load($parent);
388 8 push @{ $class . '::ISA' }, $parent;
389 8 push @{ $$class_decl->{'inherits'} }, $parent;
390 }
391  
392 } # inherits
393  
394 #-----------------------------------------------------------------
395  
396 495 ▶ if ($class_attrs_ro) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class_attrs_ro) 5 490 495
397 5 my $is_ref = ref $class_attrs_ro;
398 5 my $usage = <<'EOM';
399  
400 class_attrs_ro => [qw( attr1 attr2 )],
401 class_attrs_ro => {
402 attr0 => undef,
403 attr1 => 1,
404 attr2 => 'scalar',
405 attr3 => \$scalar_ref,
406 attr4 => [ 'array', 'ref' ],
407 attr5 => { 'hash' => 'ref' },
408 attr6 => sub { ... },
409 attr7 => qr/^Get the idea(?:\?|\!)$/o,
410 attr8 => 0,
411 attr9 => '',
412 },
413 EOM
414 5 ▶ if ( $is_ref eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($is_ref eq 'ARRAY') { } 4 1 5
elsif ($is_ref eq 'HASH') { } 1 0 1
415 4 for my $name (@$class_attrs_ro) {
416 4 ▶ X::InvalidName->throw(
Branch: 1 branch
BranchTrueFalseTotal
unless $name and $name =~ /$classes::ok_attr_name/ 0 4 4
Condition33%1/3
$name and $name =~ /$classes::ok_attr_name/
!ll&&!rl&&r
004
MC/DC0%0/2
$name and $name =~ /$classes::ok_attr_name/
$name $name =~ /$classes::ok_attr_name/
Truth table33%1/3
A and B
A= $nameB= $name =~ /$classes::ok_attr_name/
ABresult
0X0
100
111
417 "class_attrs_ro=>['$name']"
418 ) if !$name || $name !~ $classes::ok_attr_name;
419 4 _define_class_attr( $class, $name => undef, 'ro' );
420 }
421 }
422 elsif ( $is_ref eq 'HASH' ) {
423 1 ▶ while ( my ( $name, $i_value ) = each %$class_attrs_ro ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$class_attrs_ro) 1 1 2
424 1 ▶ X::InvalidName->throw(
Branch: 1 branch
BranchTrueFalseTotal
unless $name and $name =~ /$classes::ok_attr_name/ 0 1 1
Condition33%1/3
$name and $name =~ /$classes::ok_attr_name/
!ll&&!rl&&r
001
MC/DC0%0/2
$name and $name =~ /$classes::ok_attr_name/
$name $name =~ /$classes::ok_attr_name/
Truth table33%1/3
A and B
A= $nameB= $name =~ /$classes::ok_attr_name/
ABresult
0X0
100
111
425 "class_attrs_ro=>['$name']"
426 ) if !$name || $name !~ $classes::ok_attr_name;
427 1 _define_class_attr( $class, $name => $i_value, 'ro' );
428 }
429 }
430 else {
431 0 X::Usage->throw($usage);
432 }
433 } # class_attrs_ro
434  
435 #-----------------------------------------------------------------
436  
437 495 ▶ if ($class_attrs_pr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class_attrs_pr) 5 490 495
438 5 my $is_ref = ref $class_attrs_pr;
439 5 my $usage = <<'EOM';
440  
441 class_attrs_pr => [qw( attr1 attr2 )],
442 class_attrs_pr => {
443 attr0 => undef,
444 attr1 => 1,
445 attr2 => 'scalar',
446 attr3 => \$scalar_ref,
447 attr4 => [ 'array', 'ref' ],
448 attr5 => { 'hash' => 'ref' },
449 attr6 => sub { ... },
450 attr7 => qr/^Get the idea(?:\?|\!)$/o,
451 attr8 => 0,
452 attr9 => '',
453 },
454 EOM
455 5 ▶ if ( $is_ref eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($is_ref eq 'ARRAY') { } 5 0 5
elsif ($is_ref eq 'HASH') { } 0 0 0
456 5 for my $name (@$class_attrs_pr) {
457 5 ▶ X::InvalidName->throw(
Branch: 1 branch
BranchTrueFalseTotal
unless $name and $name =~ /$classes::ok_attr_name/ 0 5 5
Condition33%1/3
$name and $name =~ /$classes::ok_attr_name/
!ll&&!rl&&r
005
MC/DC0%0/2
$name and $name =~ /$classes::ok_attr_name/
$name $name =~ /$classes::ok_attr_name/
Truth table33%1/3
A and B
A= $nameB= $name =~ /$classes::ok_attr_name/
ABresult
0X0
100
111
458 "class_attrs_pr=>['$name']"
459 ) if !$name || $name !~ $classes::ok_attr_name;
460 5 _define_class_attr( $class, $name => undef, 'pr' );
461 }
462 }
463 elsif ( $is_ref eq 'HASH' ) {
464 0 ▶ while ( my ( $name, $i_value ) = each %$class_attrs_pr ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$class_attrs_pr) 0 0 0
465 0 ▶ X::InvalidName->throw(
Branch: 1 branch
BranchTrueFalseTotal
unless $name and $name =~ /$classes::ok_attr_name/ 0 0 0
Condition0%0/3
$name and $name =~ /$classes::ok_attr_name/
!ll&&!rl&&r
000
MC/DC0%0/2
$name and $name =~ /$classes::ok_attr_name/
$name $name =~ /$classes::ok_attr_name/
Truth table0%0/3
A and B
A= $nameB= $name =~ /$classes::ok_attr_name/
ABresult
0X0
100
111
466 "class_attrs_pr=>['$name']"
467 ) if !$name || $name !~ $classes::ok_attr_name;
468 0 _define_class_attr( $class, $name => $i_value, 'pr' );
469 }
470 }
471 else {
472 0 X::Usage->throw($usage);
473 }
474 } # class_attrs_pr
475  
476 #-----------------------------------------------------------------
477  
478 495 ▶ if ($class_attrs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class_attrs) 45 450 495
479 45 my $is_ref = ref $class_attrs;
480 45 my $usage = <<'EOM';
481  
482 class_attrs => [qw( attr1 attr2 )],
483 class_attrs => {
484 attr0 => undef,
485 attr1 => 1,
486 attr2 => 'scalar',
487 attr3 => \$scalar_ref,
488 attr4 => [ 'array', 'ref' ],
489 attr5 => { 'hash' => 'ref' },
490 attr6 => sub { ... },
491 attr7 => qr/^Get the idea(?:\?|\!)$/o,
492 attr8 => 0,
493 attr9 => '',
494 },
495 EOM
496  
497 45 ▶ if ( $is_ref eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($is_ref eq 'ARRAY') { } 2 43 45
elsif ($is_ref eq 'HASH') { } 43 0 43
498 2 for my $name (@$class_attrs) {
499 2 ▶ X::InvalidName->throw(
Branch: 1 branch
BranchTrueFalseTotal
unless $name and $name =~ /$classes::ok_attr_name/ 0 2 2
Condition33%1/3
$name and $name =~ /$classes::ok_attr_name/
!ll&&!rl&&r
002
MC/DC0%0/2
$name and $name =~ /$classes::ok_attr_name/
$name $name =~ /$classes::ok_attr_name/
Truth table33%1/3
A and B
A= $nameB= $name =~ /$classes::ok_attr_name/
ABresult
0X0
100
111
500 "class_attrs=>['$name']"
501 ) if !$name || $name !~ $classes::ok_attr_name;
502 2 _define_class_attr( $class, $name => undef );
503 }
504 }
505 elsif ( $is_ref eq 'HASH' ) {
506 43 ▶ while ( my ( $name, $i_value ) = each %$class_attrs ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$class_attrs) 151 43 194
507 151 ▶ X::InvalidName->throw(
Branch: 1 branch
BranchTrueFalseTotal
unless $name and $name =~ /$classes::ok_attr_name/ 0 151 151
Condition33%1/3
$name and $name =~ /$classes::ok_attr_name/
!ll&&!rl&&r
00151
MC/DC0%0/2
$name and $name =~ /$classes::ok_attr_name/
$name $name =~ /$classes::ok_attr_name/
Truth table33%1/3
A and B
A= $nameB= $name =~ /$classes::ok_attr_name/
ABresult
0X0
100
111
508 "class_attrs=>['$name']"
509 ) if !$name || $name !~ $classes::ok_attr_name;
510 151 _define_class_attr( $class, $name => $i_value );
511 }
512 }
513 else {
514 0 X::Usage->throw($usage);
515 }
516  
517 } # class_attrs
518  
519 #-----------------------------------------------------------------
520  
521 495 ▶ if ($justahash) {
Branch: 1 branch
BranchTrueFalseTotal
if ($justahash) { } 1 494 495
522 1 $$class_decl->{'justahash'} = $justahash;
523 1 $noaccessors = 1;
524 1 $unqualified = 1;
525 1 ▶ $new_m ||= 'classes::new_fast';
Condition50%1/2
$new_m ||= "classes::new_fast"
l!l
01
MC/DC0%0/1
$new_m ||= "classes::new_fast"
$new_m
Truth table50%1/2
A
A= $new_m
Aresult
11
00
526 }
527 else {
528 494 ▶ $$class_decl->{'unqualified'} = $unqualified if $unqualified;
Branch: 1 branch
BranchTrueFalseTotal
if $unqualified 1 493 494
529 494 ▶ $$class_decl->{'noaccessors'} = $noaccessors if $noaccessors;
Branch: 1 branch
BranchTrueFalseTotal
if $noaccessors 1 493 494
530 }
531  
532 #-----------------------------------------------------------------
533  
534 495 ▶ if ($attrs_ro) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attrs_ro) 32 463 495
535 32 my $is_ref = ref $attrs_ro;
536 32 my $usage = <<'EOM';
537  
538 attrs_ro => [qw( attr1 attr2 )],
539 EOM
540 32 ▶ if ( $is_ref eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_ref eq 'ARRAY') { } 32 0 32
541 32 for my $name (@$attrs_ro) {
542 302 ▶ X::InvalidName->throw(
Branch: 1 branch
BranchTrueFalseTotal
unless $name and $name =~ /$classes::ok_attr_name/ 0 302 302
Condition33%1/3
$name and $name =~ /$classes::ok_attr_name/
!ll&&!rl&&r
00302
MC/DC0%0/2
$name and $name =~ /$classes::ok_attr_name/
$name $name =~ /$classes::ok_attr_name/
Truth table33%1/3
A and B
A= $nameB= $name =~ /$classes::ok_attr_name/
ABresult
0X0
100
111
543 "attrs_ro=>['$name']"
544 ) if !$name || $name !~ $classes::ok_attr_name;
545 302 _define_attr( $class, $name, 'ro',
546 $unqualified, $noaccessors );
547 }
548 }
549 else {
550 0 X::Usage->throw($usage);
551 }
552  
553 } # attrs_ro
554  
555 #-----------------------------------------------------------------
556  
557 495 ▶ if ($attrs_pr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attrs_pr) 4 491 495
558 4 my $is_ref = ref $attrs_pr;
559 4 my $usage = <<'EOM';
560  
561 attrs_pr => [qw( attr1 attr2 )],
562 EOM
563 4 ▶ if ( $is_ref eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_ref eq 'ARRAY') { } 4 0 4
564 4 for my $name (@$attrs_pr) {
565 4 ▶ X::InvalidName->throw(
Branch: 1 branch
BranchTrueFalseTotal
unless $name and $name =~ /$classes::ok_attr_name/ 0 4 4
Condition33%1/3
$name and $name =~ /$classes::ok_attr_name/
!ll&&!rl&&r
004
MC/DC0%0/2
$name and $name =~ /$classes::ok_attr_name/
$name $name =~ /$classes::ok_attr_name/
Truth table33%1/3
A and B
A= $nameB= $name =~ /$classes::ok_attr_name/
ABresult
0X0
100
111
566 "attrs_pr=>['$name']"
567 ) if !$name || $name !~ $classes::ok_attr_name;
568 4 _define_attr( $class, $name, 'pr',
569 $unqualified, $noaccessors );
570 }
571 }
572 else {
573 0 X::Usage->throw($usage);
574 }
575  
576 } # attrs_pr
577 #-----------------------------------------------------------------
578  
579 495 ▶ if ($attrs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attrs) 46 449 495
580 46 my $is_ref = ref $attrs;
581 46 my $usage = <<'EOM';
582  
583 attrs => [qw( attr1 attr2 )],
584 EOM
585 46 ▶ if ( $is_ref eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_ref eq 'ARRAY') { } 46 0 46
586 46 for my $name (@$attrs) {
587 128 ▶ X::InvalidName->throw( "attrs=>['$name']" )
Branch: 1 branch
BranchTrueFalseTotal
unless $name and $name =~ /$classes::ok_attr_name/ 0 128 128
Condition33%1/3
$name and $name =~ /$classes::ok_attr_name/
!ll&&!rl&&r
00128
MC/DC0%0/2
$name and $name =~ /$classes::ok_attr_name/
$name $name =~ /$classes::ok_attr_name/
Truth table33%1/3
A and B
A= $nameB= $name =~ /$classes::ok_attr_name/
ABresult
0X0
100
111
588 if !$name || $name !~ $classes::ok_attr_name;
589 128 _define_attr( $class, $name, '',
590 $unqualified, $noaccessors );
591 }
592 }
593 else {
594 0 X::Usage->throw($usage);
595 }
596  
597 } # attrs
598  
599 #-----------------------------------------------------------------
600  
601 495 ▶ if ($class_methods) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class_methods) 37 458 495
602 37 my $is_ref = ref $class_methods;
603 37 my $usage = <<'EOM';
604  
605 class_methods => [ 'method1', 'method2' ],
606 class_methods => {
607 method1 => 'method1',
608 method2 => 'local_method',
609 method3 => 'Extern::library::method',
610 method4 => 'ABSTRACT',
611 method5 => <false> | 'EMPTY',
612 method6 => sub { ... } | $code_ref | \&some::method,
613 },
614 EOM
615 37 ▶ if ( $is_ref eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($is_ref eq 'ARRAY') { } 2 35 37
elsif ($is_ref eq 'HASH') { } 35 0 35
616 2 for my $name (@$class_methods) {
617 2 _define_method( $class, $name => $name, 'class_' );
618 }
619 }
620 elsif ( $is_ref eq 'HASH' ) {
621 35 ▶ while ( my ( $name, $method ) = each %$class_methods ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$class_methods) 90 35 125
622 90 my $is_ref = ref $method;
623 90 _define_method( $class, $name => $method, 'class_');
624 }
625 }
626 else {
627 0 X::Usage->throw($usage);
628 }
629 } # class_methods
630  
631 #---------------------------------------------------------------
632  
633 495 ▶ if ($pkg_methods) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pkg_methods) 0 495 495
634 0 my $is_ref = ref $pkg_methods;
635 0 my $usage = <<'EOM';
636  
637 pkg_methods => [ 'method1', 'method2' ],
638 pkg_methods => {
639 method1 => 'method1',
640 method2 => 'local_method',
641 method3 => 'Extern::library::method',
642 method4 => 'ABSTRACT',
643 method5 => <false> | 'EMPTY',
644 method6 => sub { ... } | $code_ref | \&some::method,
645 },
646 EOM
647 0 ▶ if ( $is_ref eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($is_ref eq 'ARRAY') { } 0 0 0
elsif ($is_ref eq 'HASH') { } 0 0 0
648 0 for my $name (@$pkg_methods) {
649 0 _define_method( $class, $name => $name, 'pkg_' );
650 }
651 }
652 elsif ( $is_ref eq 'HASH' ) {
653 0 ▶ while ( my ( $name, $method ) = each %$pkg_methods ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$pkg_methods) 0 0 0
654 0 my $is_ref = ref $method;
655 0 _define_method( $class, $name => $method, 'pkg_');
656 }
657 }
658 else {
659 0 X::Usage->throw($usage);
660 }
661  
662 # allows pkg_methods to be imported on request
663 0 my $class_decl = $class . '::DECL';
664 0 ▶ *{$class.'::import'} = sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
665 0 my $package = shift;
666 0 my $caller = caller;
667  
668 # use MyPackage qw(my_pkg_method another_function);
669 0 ▶ if ($_[0] and $_[0] ne ':all') {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] and $_[0] ne ':all') { } 0 0 0
Condition0%0/3
$_[0] and $_[0] ne ":all"
!ll&&!rl&&r
000
MC/DC0%0/2
$_[0] and $_[0] ne ":all"
$_[0] $_[0] ne ":all"
Truth table0%0/3
A and B
A= $_[0]B= $_[0] ne ":all"
ABresult
0X0
100
111
670 0 for my $method (@_) {
671 X::NotPkgMethod->throw([$method])
672 0 ▶ if !$$class_decl->{'pkg_methods'}->{$method};
Branch: 1 branch
BranchTrueFalseTotal
unless $$class_decl->{"pkg_methods"}{$method} 0 0 0
673 0 *{$caller.'::'.$method} = \&{$class.'::'.$method};
674 }
675 }
676
677 # use MyPackage ':all';
678 else {
679 0 for my $method (keys %{$$class_decl->{'pkg_methods'}}){
680 0 *{$caller.'::'.$method} = \&{$class.'::'.$method};
681 }
682 }
683 0 return $package;
684 0 };
685  
686 } # pkg_methods
687  
688 #---------------------------------------------------------------
689  
690 495 ▶ if ($methods) {
Branch: 1 branch
BranchTrueFalseTotal
if ($methods) 91 404 495
691 91 my $is_ref = ref $methods;
692 91 my $usage = <<'EOM';
693  
694 methods => [ 'method1', 'method2' ],
695 methods => {
696 method1 => 'method1',
697 method2 => 'local_method',
698 method3 => 'Extern::library::method',
699 method4 => 'ABSTRACT',
700 method5 => <false> || 'EMPTY',
701 method6 => sub { ... } || $code_ref || \&some::method,
702 },
703 EOM
704 91 ▶ if ( $is_ref eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($is_ref eq 'ARRAY') { } 57 34 91
elsif ($is_ref eq 'HASH') { } 34 0 34
705 57 for my $name (@$methods) {
706 58 _define_method( $class, $name => $name, '' );
707 }
708 }
709 elsif ( $is_ref eq 'HASH' ) {
710 34 ▶ while ( my ( $name, $method ) = each %$methods ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$methods) 88 34 122
711 88 _define_method( $class, $name => $method, '' );
712 }
713 }
714 else {
715 0 X::Usage->throw($usage);
716 }
717  
718 } # methods
719  
720 #---------------------------------------------------------------
721 # new, clone, initialize, and dump shortcuts
722  
723 495 ▶ _define_method( $class, 'initialize' => $init_m, '' ) if $init_m;
Branch: 1 branch
BranchTrueFalseTotal
if $init_m 55 440 495
724 495 ▶ _define_method( $class, 'clone' => $clone_m, '' ) if $clone_m;
Branch: 1 branch
BranchTrueFalseTotal
if $clone_m 54 441 495
725 495 ▶ _define_method( $class, 'dump' => $dump_m, '' ) if $dump_m;
Branch: 1 branch
BranchTrueFalseTotal
if $dump_m 54 441 495
726 495 ▶ _define_method( $class, 'new' => $new_m, 'class_' ) if $new_m;
Branch: 1 branch
BranchTrueFalseTotal
if $new_m 100 395 495
727  
728 495 return $class;
729 }
730  
731 ####################################################################
732  
733 417 ▶sub load {
Subroutine load: called CC=8 SCAR=22.2
Pod documented
734 417 my $pkg = shift;
735  
736 417 ▶ X::InvalidName->throw( [$pkg] )
Branch: 1 branch
BranchTrueFalseTotal
unless $pkg =~ /$classes::ok_class_name/x 2 415 417
737 if $pkg !~ $classes::ok_class_name;
738  
739 # from 'base', don't bother loading if have VERSION,
740 415 my $vglob = ${$pkg.'::'}{VERSION};
741 415 ▶ return $pkg if $vglob && *$vglob{SCALAR};
Branch: 1 branch
BranchTrueFalseTotal
if $vglob and *$vglob{"SCALAR"} 292 123 415
Condition100%2/2
$vglob and *$vglob{"SCALAR"}
!ll
123292
MC/DC100%1/1
$vglob and *$vglob{"SCALAR"}
$vglob
Truth table100%2/2
A
A= $vglob
Aresult
00
11
742  
743 # unlike 'base' that does a 'require', we 'use' instead to
744 123 local $SIG{__DIE__} = 'IGNORE';
745 0 ▶ eval "use $pkg";
Subroutine BEGIN: called CC=1 SCAR=2.0
Subroutine BEGIN: called CC=1 SCAR=2.0
Subroutine BEGIN: called CC=1 SCAR=2.0
746  
747 # unlike 'base' that does a 'require', we 'use' instead to
748 # propogate exception as is unless "can't locate", which is
749 # expected when the base pkg is defined in same file, etc.
750 # had to remove the '^' initial match because of braindead
751 # idiot programs like Indigo's perl2exe that stick a bunch
752 # of irrelevant text before the actual expected error string
753 123 ▶ die if $@ && $@ !~ /Can't locate .*? at \(eval /o;
Branch: 1 branch
BranchTrueFalseTotal
if $@ and not $@ =~ /Can't locate .*? at \(eval /o 0 123 123
Condition33%1/3
$@ and not $@ =~ /Can't locate .*? at \(eval /o
!ll&&!rl&&r
01230
MC/DC0%0/2
$@ and not $@ =~ /Can't locate .*? at \(eval /o
$@ not $@ =~ /Can't locate .*? at \(eval /o
Truth table33%1/3
A and not(B)
A= $@B= not $@ =~ /Can't locate .*? at \(eval /o
ABresult
0X0
100
111
754  
755 # problem if the pkg doesn't have any var or sub symbols in it
756 # milage may vary when loading pkgs that use DynaLoader (ugh)
757 123 ▶ X::Empty->throw([$pkg]) if !%{$pkg.'::'};
Branch: 1 branch
BranchTrueFalseTotal
unless %{$pkg . '::';} 2 121 123
758  
759 # for 'use base' compatibility, if the loaded pkg didn't have
760 # a VERSION of its own give it one so we don't load it again
761 121 ${$pkg.'::VERSION'} = "-1, set by classes.pm"
762 121 ▶ if !defined ${$pkg.'::VERSION'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined ${$pkg . '::VERSION';} 121 0 121
763  
764 121 return $pkg;
765 }
766  
767  
768 ####################################################################
769  
770 51 ▶sub _define_mixins {
Subroutine _define_mixins: called CC=37 SCAR=36.7
771 51 my ($class, $mixes, $mixes_def, $type) = @_;
772 51 my $is_ref = ref $mixes;
773 51 my $class_DECL = $class.'::DECL';
774 51 my $class_MIXIN = $class.'::MIXIN';
775  
776 51 my $usage = <<'EOM';
777  
778 [class_|pkg_]mixes => 'Module',
779 [class_|pkg_]mixes => { Module => ... },
780 [class_|pkg_]mixes => [
781 'Module1',
782 { Module2 => ['method1'] },
783 { Module3 => 'ALL'|'PUB'|'SAFE' },
784 { Module4 => qr/.../ },
785 { Module5 => ... , scope=>'CLASS' },
786 ],
787  
788 mixes_def => 'ALL'|'PUB'|'SAFE',
789 EOM
790 # mixes => 'Module1',
791 # mixes => { Module1 => ... },
792 51 ▶ if ( !$is_ref || $is_ref eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $is_ref or $is_ref eq "HASH") 50 1 51
Condition100%3/3
not $is_ref or $is_ref eq "HASH"
l!l&&r!l&&!r
4821
MC/DC100%2/2
not $is_ref or $is_ref eq "HASH"
not $is_ref $is_ref eq "HASH"
Truth table100%3/3
not(A) or B
A= not $is_refB= $is_ref eq "HASH"
ABresult
1X1
011
000
793 50 $mixes = [$mixes];
794 50 $is_ref = 'ARRAY';
795 }
796  
797 51 ▶ X::Usage->throw($usage) if $is_ref ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if $is_ref ne "ARRAY" 0 51 51
798  
799 MIXIN:
800 51 for my $module ( reverse @$mixes ) { # first wins
801  
802 52 my $is_ref = ref $module;
803 52 ▶ my $filter = $mixes_def || 'SAFE';
Condition100%2/2
$mixes_def || 'SAFE'
l!l
745
MC/DC100%1/1
$mixes_def || 'SAFE'
$mixes_def
Truth table100%2/2
A
A= $mixes_def
Aresult
11
00
804  
805 # mixes => [ 'Module1' ],
806 52 ▶ if ( !$is_ref ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_ref) 50 2 52
807 50 $module = { $module => $filter };
808 50 $is_ref = 'HASH';
809 }
810  
811 52 ▶ X::Usage->throw($usage) if $is_ref ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if $is_ref ne "HASH" 0 52 52
812  
813 # mixes => [ { Module1 => ... } ],
814 52 ($module, $filter) = each %$module; # only one left
815  
816 52 ▶ X::InvalidName->throw( "mixes=>'$module' ??" )
Branch: 1 branch
BranchTrueFalseTotal
unless $module and $module =~ /$classes::ok_class_name/ 0 52 52
Condition33%1/3
$module and $module =~ /$classes::ok_class_name/
!ll&&!rl&&r
0052
MC/DC0%0/2
$module and $module =~ /$classes::ok_class_name/
$module $module =~ /$classes::ok_class_name/
Truth table33%1/3
A and B
A= $moduleB= $module =~ /$classes::ok_class_name/
ABresult
0X0
100
111
817 if !$module || $module !~ $classes::ok_class_name;
818  
819 # only SAFE, PUB, and ALL acceptable non-refs
820 52 my $filter_is_ref = ref $filter;
821 52 ▶ if ( !$filter_is_ref ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($filter_is_ref) 48 4 52
822 {
823 48 ▶ $filter = qr/^(?!(?:_|\d|[A-Z0-9_]+$))/o,
Branch: 1 branch
BranchTrueFalseTotal
if $filter eq "SAFE" 44 4 48
824 last if $filter eq 'SAFE';
825 4 ▶ $filter = qr/^
Branch: 1 branch
BranchTrueFalseTotal
if $filter eq "PUB" 1 3 4
826 (?!_|BEGIN|CHECK|END|INIT|CLONE
827 |CLASS|SUPER|DECL|MIXIN)
828 [a-z_]\w*$/xoi,
829 last if $filter eq 'PUB';
830 3 ▶ $filter = qr/^
Branch: 1 branch
BranchTrueFalseTotal
if $filter eq "ALL" 3 0 3
831 (?!BEGIN|CHECK|END|INIT|CLONE
832 |CLASS|SUPER|DECL|MIXIN)
833 [a-z_]\w*$/xoi,
834 last if $filter eq 'ALL';
835 0 X::Usage->throw($filter." ??\n".$usage);
836 }
837 48 $filter_is_ref = 'Regexp';
838 }
839
840 52 classes::load($module);
841 52 my $mod_sym = $module.'::';
842  
843 # unfortunately the following causes an empty DECL
844 # to autovivify if not one there already, perl's problem
845 52 my $mod_DECL = ${"$module\::DECL"};
846  
847 52 my $mod_type = ${"$module\::DECL"}->{'type'};
848  
849 # if declared AND type is set by author to 'mixin'
850 # go ahead and only use the declaration.
851  
852 52 ▶ if ( $mod_type and $mod_type eq 'mixable' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mod_type and $mod_type eq "mixable") 38 14 52
Condition100%3/3
$mod_type and $mod_type eq "mixable"
!ll&&!rl&&r
10438
MC/DC100%2/2
$mod_type and $mod_type eq "mixable"
$mod_type $mod_type eq "mixable"
Truth table100%3/3
A and B
A= $mod_typeB= $mod_type eq "mixable"
ABresult
0X0
100
111
853 38 my $methods = $mod_DECL->{'methods'};
854 38 my $class_methods = $mod_DECL->{'class_methods'};
855 38 my $pkg_methods = $mod_DECL->{'pkg_methods'};
856 38 my $attrs = $mod_DECL->{'attrs'};
857 38 my $attrs_ro = $mod_DECL->{'attrs_ro'};
858 38 my $attrs_pr = $mod_DECL->{'attrs_pr'};
859 38 my $class_attrs = $mod_DECL->{'class_attrs'};
860 38 my $class_attrs_ro = $mod_DECL->{'class_attrs_ro'};
861 38 my $class_attrs_pr = $mod_DECL->{'class_attrs_pr'};
862  
863 38 ▶ while ( my ($name, $ivalue) = each %$class_methods ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$class_methods) 95 38 133
864 95 _clear_method_name($class, $name);
865 95 _mixin( $module => $class, $name );
866 95 $$class_DECL->{'class_methods'}->{$name} = $ivalue;
867 }
868  
869 38 ▶ while ( my ($name, $ivalue) = each %$methods ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$methods) 96 38 134
870 96 _clear_method_name($class, $name);
871 96 _mixin( $module => $class, $name );
872 96 $$class_DECL->{'methods'}->{$name} = $ivalue;
873 }
874  
875 38 ▶ while ( my ($name, $ivalue) = each %$pkg_methods ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$pkg_methods) 0 38 38
876 0 _clear_method_name($class, $name);
877 0 _mixin( $module => $class, $name );
878 0 $$class_DECL->{'pkg_methods'}->{$name} = $ivalue;
879 }
880  
881 38 for my $name ( @$attrs ) {
882 8 _clear_method_name($class, "set_$name");
883 8 _clear_method_name($class, "get_$name");
884 8 _mixin( $module => $class, "set_$name" );
885 8 _mixin( $module => $class, "get_$name" );
886 8 _mixin_key( $module => $class, $name );
887 8 push @{$$class_DECL->{'attrs'}}, $name;
888 }
889  
890 38 for my $name ( @$attrs_ro ) {
891 4 _clear_method_name($class, "set_$name");
892 4 _clear_method_name($class, "get_$name");
893 4 _mixin( $module => $class, "get_$name" );
894 4 _mixin_key( $module => $class, $name );
895 4 push @{$$class_DECL->{'attrs_ro'}}, $name;
896 }
897  
898 38 for my $name ( @$attrs_pr ) {
899 4 _clear_method_name($class, "set_$name");
900 4 _clear_method_name($class, "get_$name");
901 4 _mixin_key( $module => $class, $name );
902 4 push @{$$class_DECL->{'attrs_pr'}}, $name;
903 }
904  
905 38 ▶ while ( my ($name, $ivalue) = each %$class_attrs ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$class_attrs) 8 38 46
906 8 _clear_method_name($class, "set_$name");
907 8 _clear_method_name($class, "get_$name");
908 8 _mixin( $module => $class, "set_$name" );
909 8 _mixin( $module => $class, "get_$name" );
910 8 _mixin_key( $module => $class, $name, 'class' );
911 8 $$class_DECL->{'class_attrs'}->{$name} = $ivalue;
912 }
913  
914 38 ▶ while ( my ($name, $ivalue) = each %$class_attrs_ro ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$class_attrs_ro) 4 38 42
915 4 _clear_method_name($class, "set_$name");
916 4 _clear_method_name($class, "get_$name");
917 4 _mixin( $module => $class, "get_$name" );
918 4 _mixin_key( $module => $class, $name, 'class' );
919 4 $$class_DECL->{'class_attrs_ro'}->{$name} = $ivalue;
920 }
921  
922 38 ▶ while ( my ($name, $ivalue) = each %$class_attrs_pr ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$class_attrs_pr) 5 38 43
923 5 _clear_method_name($class, "set_$name");
924 5 _clear_method_name($class, "get_$name");
925 5 _mixin_key( $module => $class, $name, 'class' );
926 5 $$class_DECL->{'class_attrs_pr'}->{$name} = $ivalue;
927 }
928  
929 38 next MIXIN;
930 }
931  
932 # not declared as a mixable ...
933  
934 # we only look at the methods in the symbol table here
935 # and don't care how they got there. these may very well have
936 # classes declarations but probably not. more likely these
937 # are old fashioned function libraries
938  
939 # mixes => [ { Module1 => ['method'] } ],
940 14 ▶ if ( $filter_is_ref eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($filter_is_ref eq 'ARRAY') { } 2 12 14
elsif ($filter_is_ref eq 'Regexp' or $filter_is_ref eq 'Regex') { } 12 0 12
941 2 for my $name ( @$filter ){
942  
943 2 ▶ X::InvalidName->throw(
Branch: 1 branch
BranchTrueFalseTotal
unless $name and $name =~ /^[a-z_]\w*$/o 0 2 2
Condition33%1/3
$name and $name =~ /^[a-z_]\w*$/o
!ll&&!rl&&r
002
MC/DC0%0/2
$name and $name =~ /^[a-z_]\w*$/o
$name $name =~ /^[a-z_]\w*$/o
Truth table33%1/3
A and B
A= $nameB= $name =~ /^[a-z_]\w*$/o
ABresult
0X0
100
111
944 "mixes=>{$module=>['$name']} ??"
945 ) if !$name || $name !~ /^[a-z_]\w*$/o;
946  
947 2 my $glob = $$mod_sym{$name};
948 2 my $code;
949  
950 # careful: only CODE slot, not others
951 2 ▶ next if !$glob || !( $code = *$glob{CODE} );
Branch: 1 branch
BranchTrueFalseTotal
unless $glob and $code = *$glob{"CODE"} 0 2 2
Condition33%1/3
$glob and $code = *$glob{"CODE"}
!ll&&!rl&&r
002
MC/DC0%0/2
$glob and $code = *$glob{"CODE"}
$glob $code = *$glob{"CODE"}
Truth table33%1/3
A and B
A= $globB= $code = *$glob{"CODE"}
ABresult
0X0
100
111
952  
953 # import
954 2 *{ $class . '::' . $name } = $code;
955 2 _clear_method_name($class, $name);
956  
957 2 $$class_MIXIN->{$name} = $module.'::'.$name;
958 2 $$class_DECL->{$type.'methods'}->{$name}
959 = $module.'::'.$name;
960 }
961 }
962  
963 # mixes => [ { Module1 => qr/.../o } ],
964 ▶ elsif ( $filter_is_ref eq 'Regexp'
Condition33%1/3
$filter_is_ref eq "Regexp" or $filter_is_ref eq "Regex"
l!l&&r!l&&!r
1200
MC/DC0%0/2
$filter_is_ref eq "Regexp" or $filter_is_ref eq "Regex"
$filter_is_ref eq "Regexp" $filter_is_ref eq "Regex"
Truth table33%1/3
A or B
A= $filter_is_ref eq "Regexp"B= $filter_is_ref eq "Regex"
ABresult
1X1
011
000
965 or $filter_is_ref eq 'Regex' ) { # older
966 12 ▶ while ( my ($name) = each %$mod_sym ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($name) = each %$mod_sym) 91 12 103
967  
968 91 my $glob = $$mod_sym{$name};
969 91 my $code;
970  
971 # careful: only CODE slot, not others
972 next if $name !~ $filter || !$glob
973 91 ▶ || !( $code = *$glob{CODE} );
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /$filter/ and $glob and $code = *$glob{"CODE"} 57 34 91
Condition83%5/6
$name =~ /$filter/ and $glob and $code = *$glob{"CODE"}
!ll&&!rl&&r
471034
$name =~ /$filter/ and $glob
!ll&&!rl&&r
47044
MC/DC66%2/3
$name =~ /$filter/ and $glob and $code = *$glob{"CODE"}
$name =~ /$filter/ $glob $code = *$glob{"CODE"}
Truth table75%3/4
A and B and C
A= $name =~ /$filter/B= $globC= $code = *$glob{"CODE"}
ABCresult
0XX0
10X0
1100
1111
974  
975 # import
976 34 *{ $class . '::' . $name } = $code;
977 34 _clear_method_name($class,$name);
978 34 $$class_MIXIN->{$name} = $module;
979  
980 34 $$class_DECL->{$type.'methods' }->{$name}
981 = $module.'::'.$name;
982 }
983 }
984  
985 # ref to something else
986 else {
987 0 X::Usage->throw($filter_is_ref . $usage);
988 } # filter_is_ref
989 } # module
990 }
991  
992 468 ▶sub _mixin {
Subroutine _mixin: called CC=3 SCAR=11.1
993 468 my ($from_pkg, $to_pkg, $name, $dest_name) = @_;
994 468 ▶ $dest_name ||= $name;
Condition66%2/3
$dest_name ||= $name
l!l&&r!l&&!r
2372310
MC/DC0%0/2
$dest_name ||= $name
$dest_name $name
Truth table66%2/3
A ||= B
A= $dest_nameB= $name
ABresult
1X1
011
000
995 468 *{ $to_pkg . '::' . $dest_name } = \&{ $from_pkg . '::' . $name };
996 468 my $really_from = ${ $from_pkg.'::MIXIN' }->{$name};
997 468 ▶ ${ $to_pkg.'::MIXIN' }->{$dest_name} = $really_from || $from_pkg;
Condition66%2/3
$really_from || $from_pkg
l!l&&r!l&&!r
214470
MC/DC0%0/2
$really_from || $from_pkg
$really_from $from_pkg
Truth table66%2/3
A || B
A= $really_fromB= $from_pkg
ABresult
1X1
011
000
998 }
999  
1000 33 ▶sub _mixin_key {
Subroutine _mixin_key: called CC=3 SCAR=11.0
1001 33 my ($from_pkg, $to_pkg, $name, $is_class) = @_;
1002 33 ▶ my $var_name = ($is_class && 'CLASS_') . 'ATTR_' . $name;
Condition100%2/2
$is_class && 'CLASS_'
!ll
1617
MC/DC100%1/1
$is_class && 'CLASS_'
$is_class
Truth table100%2/2
A
A= $is_class
Aresult
00
11
1003 33 *{$to_pkg.'::'.$var_name} = \${$from_pkg.'::'.$var_name};
1004 33 my $really_from = ${$from_pkg.'::MIXIN'}->{"\$$var_name"};
1005 33 ▶ ${ $to_pkg.'::MIXIN' }->{"\$$var_name"}
Condition66%2/3
$really_from || $from_pkg
l!l&&r!l&&!r
10230
MC/DC0%0/2
$really_from || $from_pkg
$really_from $from_pkg
Truth table66%2/3
A || B
A= $really_fromB= $from_pkg
ABresult
1X1
011
000
1006 = $really_from || $from_pkg;
1007 }
1008  
1009 794 ▶sub _clear_method_name {
Subroutine _clear_method_name: called CC=4 SCAR=13.9
1010 794 my $decl = ${ $_[0].'::DECL' };
1011 794 my ($cm, $m, $pkm);
1012 794 ▶ $pkm = $decl->{'pkg_methods'} and delete $pkm->{$_[1]};
Branch: 1 branch
BranchTrueFalseTotal
$pkm = $decl->{"pkg_methods"} and delete $$pkm{$_[1]} 0 794 794
1013 794 ▶ $cm = $decl->{'class_methods'} and delete $cm->{$_[1]};
Branch: 1 branch
BranchTrueFalseTotal
$cm = $decl->{"class_methods"} and delete $$cm{$_[1]} 510 284 794
1014 794 ▶ $m = $decl->{'methods'} and delete $m->{$_[1]};
Branch: 1 branch
BranchTrueFalseTotal
$m = $decl->{"methods"} and delete $$m{$_[1]} 454 340 794
1015 794 delete ${ $_[0].'::MIXIN' }->{$_[1]};
1016 794 return;
1017 }
1018  
1019 ####################################################################
1020  
1021 163 ▶sub _define_class_attr {
Subroutine _define_class_attr: called CC=11 SCAR=24.0
1022 163 my ( $class, $name => $i_value, $scope ) = @_;
1023 163 my $qual_name = $class.'::'.$name;
1024 163 ▶ my $scope_x = sub {X::AttrScope->throw([$name])};
Subroutine __ANON__: called CC=1 SCAR=0.0
1025 163 my $has_getter = $class->can("get_$name");
1026 163 my $has_setter = $class->can("set_$name");
1027  
1028 # set the initial value
1029 163 $$qual_name = $i_value;
1030  
1031 # create a string containing the name of the class attribute
1032 # for use privately within the class
1033 163 *{"$class\::CLASS_ATTR_$name"} = \$qual_name;
1034  
1035 # define 'getter' for all but private
1036 158 ▶ *{"$class\::get_$name"} = sub { $$qual_name }
Subroutine __ANON__: called CC=1 SCAR=0.0
1037 163 ▶ if $scope ne 'pr';
Branch: 1 branch
BranchTrueFalseTotal
if $scope ne "pr" 158 5 163
1038  
1039 # define 'setter' for read-write
1040 163 ▶ if ($scope ne 'pr' and $scope ne 'ro') {
Branch: 1 branch
BranchTrueFalseTotal
if ($scope ne "pr" and $scope ne "ro") 153 10 163
Condition100%3/3
$scope ne "pr" and $scope ne "ro"
!ll&&!rl&&r
55153
MC/DC100%2/2
$scope ne "pr" and $scope ne "ro"
$scope ne "pr" $scope ne "ro"
Truth table100%3/3
A and B
A= $scope ne "pr"B= $scope ne "ro"
ABresult
0X0
100
111
1041 153 *{"$class\::set_$name"}
1042 153 ▶ = sub { $$qual_name = $_[1]; return };
Subroutine __ANON__: called CC=1 SCAR=0.0
1043 }
1044  
1045 # private shouldn't have getter or setter, if so, cause exception
1046 163 ▶ *{"$class\::get_$name"} = $scope_x
Branch: 1 branch
BranchTrueFalseTotal
if $scope eq "pr" and $has_getter 1 162 163
Condition100%3/3
$scope eq "pr" and $has_getter
!ll&&!rl&&r
15841
MC/DC100%2/2
$scope eq "pr" and $has_getter
$scope eq "pr" $has_getter
Truth table100%3/3
A and B
A= $scope eq "pr"B= $has_getter
ABresult
0X0
100
111
1047 if $scope eq 'pr' && $has_getter;
1048 163 ▶ *{"$class\::set_$name"} = $scope_x
Branch: 1 branch
BranchTrueFalseTotal
if $scope eq "pr" and $has_setter 1 162 163
Condition100%3/3
$scope eq "pr" and $has_setter
!ll&&!rl&&r
15841
MC/DC100%2/2
$scope eq "pr" and $has_setter
$scope eq "pr" $has_setter
Truth table100%3/3
A and B
A= $scope eq "pr"B= $has_setter
ABresult
0X0
100
111
1049 if $scope eq 'pr' && $has_setter;
1050  
1051 # read-only shouldn't have setter if so, cause exception
1052 163 ▶ *{"$class\::set_$name"} = $scope_x
Branch: 1 branch
BranchTrueFalseTotal
if $scope eq "ro" and $has_setter 1 162 163
Condition100%3/3
$scope eq "ro" and $has_setter
!ll&&!rl&&r
15841
MC/DC100%2/2
$scope eq "ro" and $has_setter
$scope eq "ro" $has_setter
Truth table100%3/3
A and B
A= $scope eq "ro"B= $has_setter
ABresult
0X0
100
111
1053 if $scope eq 'ro' && $has_setter;
1054  
1055 # update declaration
1056 163 my $decl = ${ $class.'::DECL' };
1057 $decl->{'class_attrs'
1058 163 ▶ . ($scope ? "_$scope" : '')}->{$name} = $i_value;
Branch: 1 branch
BranchTrueFalseTotal
$scope ? : 10 153 163
1059  
1060 163 return $class;
1061 }
1062  
1063 ####################################################################
1064  
1065 434 ▶sub _define_attr {
Subroutine _define_attr: called CC=14 SCAR=26.4
1066 434 my ( $class, $name, $scope, $unqual, $noaccess ) = @_;
1067 434 my $qual_name = $class.'::'.$name;
1068 434 ▶ my $attr_name = $unqual ? $name : $qual_name;
Branch: 1 branch
BranchTrueFalseTotal
$unqual ? : 2 432 434
1069  
1070 # create a string containing the name of the attribute
1071 # for use privately within the class as a object hash key
1072 434 *{"$class\::ATTR_$name"} = \$attr_name;
1073  
1074 # noaccess means don't create accessors, plain old perl objects
1075 434 ▶ if (!$noaccess) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($noaccess) 432 2 434
1076 432 ▶ my $scope_x = sub {X::AttrScope->throw([$name])};
Subroutine __ANON__: called CC=1 SCAR=0.0
1077 432 my $has_getter = $class->can("get_$name");
1078 432 my $has_setter = $class->can("set_$name");
1079  
1080 # define 'getter' for all but private
1081 428 ▶ *{"$class\::get_$name"} = sub { $_[0]->{$attr_name} }
Subroutine __ANON__: called CC=1 SCAR=0.0
1082 432 ▶ if $scope ne 'pr';
Branch: 1 branch
BranchTrueFalseTotal
if $scope ne "pr" 428 4 432
1083  
1084 # define 'setter' for read-write
1085 432 ▶ if ($scope ne 'pr' and $scope ne 'ro') {
Branch: 1 branch
BranchTrueFalseTotal
if ($scope ne "pr" and $scope ne "ro") 126 306 432
Condition100%3/3
$scope ne "pr" and $scope ne "ro"
!ll&&!rl&&r
4302126
MC/DC100%2/2
$scope ne "pr" and $scope ne "ro"
$scope ne "pr" $scope ne "ro"
Truth table100%3/3
A and B
A= $scope ne "pr"B= $scope ne "ro"
ABresult
0X0
100
111
1086 126 *{"$class\::set_$name"}
1087 126 ▶ = sub { $_[0]->{$attr_name} = $_[1]; return };
Subroutine __ANON__: called CC=1 SCAR=0.0
1088 }
1089  
1090 # private shouldn't have getter or setter
1091 # if so override with one that throws an exception at run time
1092 432 ▶ *{"$class\::get_$name"} = $scope_x
Branch: 1 branch
BranchTrueFalseTotal
if $scope eq "pr" and $has_getter 1 431 432
Condition100%3/3
$scope eq "pr" and $has_getter
!ll&&!rl&&r
42831
MC/DC100%2/2
$scope eq "pr" and $has_getter
$scope eq "pr" $has_getter
Truth table100%3/3
A and B
A= $scope eq "pr"B= $has_getter
ABresult
0X0
100
111
1093 if $scope eq 'pr' && $has_getter;
1094 432 ▶ *{"$class\::set_$name"} = $scope_x
Branch: 1 branch
BranchTrueFalseTotal
if $scope eq "pr" and $has_setter 1 431 432
Condition100%3/3
$scope eq "pr" and $has_setter
!ll&&!rl&&r
42831
MC/DC100%2/2
$scope eq "pr" and $has_setter
$scope eq "pr" $has_setter
Truth table100%3/3
A and B
A= $scope eq "pr"B= $has_setter
ABresult
0X0
100
111
1095 if $scope eq 'pr' && $has_setter;
1096  
1097 # read-only shouldn't have setter if so, cause exception
1098 432 ▶ *{"$class\::set_$name"} = $scope_x
Branch: 1 branch
BranchTrueFalseTotal
if $scope eq "ro" and $has_setter 1 431 432
Condition100%3/3
$scope eq "ro" and $has_setter
!ll&&!rl&&r
1303011
MC/DC100%2/2
$scope eq "ro" and $has_setter
$scope eq "ro" $has_setter
Truth table100%3/3
A and B
A= $scope eq "ro"B= $has_setter
ABresult
0X0
100
111
1099 if $scope eq 'ro' && $has_setter;
1100 }
1101  
1102 # update declaration
1103 434 my $decl = ${ $class.'::DECL' };
1104 434 ▶ my $tag = 'attrs' . ($scope ? "_$scope" : '');
Branch: 1 branch
BranchTrueFalseTotal
$scope ? : 306 128 434
1105 434 push @{$decl->{$tag}}, $name
1106 434 ▶ if !grep {$_ eq $name} @{$decl->{$tag}};
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $name;} @{$$decl{$tag};} 434 0 434
1107  
1108 434 return $class;
1109 }
1110  
1111 ####################################################################
1112  
1113 501 ▶sub _define_method {
Subroutine _define_method: called CC=10 SCAR=23.2
1114 501 my ( $class, $name, $method, $type ) = @_; # trust
1115 501 my $is_ref = ref $method;
1116 501 my ($code, $m_pkg, $m_name);
1117  
1118 501 ▶ $method = 'EMPTY' if !$method;
Branch: 1 branch
BranchTrueFalseTotal
unless $method 1 500 501
1119  
1120 # classes::abstract, classes::empty, Module::method, method
1121 501 ▶ if (!$is_ref ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $is_ref) { } 489 12 501
1122  
1123 # inline anon to get better X::Unimplemented message
1124 489 ▶ if ( $method eq 'ABSTRACT' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($method eq 'ABSTRACT') { } 1 488 489
elsif ($method eq 'EMPTY') { } 1 487 488
1125 0 ▶ $code = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1126 0 X::Unimplemented->throw( "$class->$name()" );
1127 1 };
1128 }
1129  
1130 elsif ( $method eq 'EMPTY' ) {
1131 1 ▶ $code = sub { };
Subroutine __ANON__: not called CC=1 SCAR=0.0
1132 }
1133  
1134 # Module::Mine::method_name or method_name
1135 else {
1136 487 ($m_pkg, $m_name) = $method
1137 =~ /^
1138 (?:
1139 (
1140 (?:
1141 [a-z_]\w*\:\: # opt: Module::
1142 )*
1143 [a-z_]\w* # opt: Mine
1144 )
1145 :: # opt: ::
1146 )?
1147 (
1148 [a-z_]\w* # req: method_name
1149 )
1150 $/iogx;
1151  
1152 487 ▶ X::InvalidName->throw( [$method] ) if !$m_name;
Branch: 1 branch
BranchTrueFalseTotal
unless $m_name 0 487 487
1153  
1154 # no package name, method_name only - qualify
1155 487 ▶ if (!$m_pkg) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $m_pkg) { } 250 237 487
1156 250 $method = $class . '::' . $m_name;
1157 250 ▶ $m_pkg ||= $class;
Condition33%1/3
$m_pkg ||= $class
l!l&&r!l&&!r
02500
MC/DC0%0/2
$m_pkg ||= $class
$m_pkg $class
Truth table33%1/3
A ||= B
A= $m_pkgB= $class
ABresult
1X1
011
000
1158 }
1159  
1160 # treat as any other mixin
1161 else {
1162 237 _clear_method_name($class, $name);
1163 237 _mixin( $m_pkg => $class, $m_name => $name );
1164 237 my $decl = ${ $class.'::DECL' };
1165 237 $decl->{$type.'methods'}->{$name} = $method;
1166 237 return $class;
1167 }
1168  
1169 250 ▶ load $m_pkg if !%{$m_pkg.'::'};
Branch: 1 branch
BranchTrueFalseTotal
unless %{$m_pkg . '::';} 0 250 250
1170  
1171 # unfortunately since we allow compile-time
1172 # class definition it is too early to test for defined
1173 # method, we have to trust that one will be defined
1174 # or caught by perl run-time, the following springs
1175 # a symbolic code ref into life no matter what,
1176 # rather annoying, but best we can do for now
1177  
1178 250 $code = \&$method;
1179 }
1180  
1181 }
1182  
1183 # sub { ... } or \&method
1184 else {
1185 12 ▶ X::Usage->throw( "$name $is_ref ??" )
Branch: 1 branch
BranchTrueFalseTotal
if $is_ref ne "CODE" 0 12 12
1186 if $is_ref ne 'CODE';
1187 12 $code = $method;
1188 12 $method = 'CODE';
1189 }
1190  
1191 264 *{ $class . '::' . $name } = $code;
1192  
1193 # update declaration - same named removed since all are methods
1194 264 _clear_method_name($class, $name);
1195 264 my $decl = ${ $class.'::DECL' };
1196 264 $decl->{$type.'methods'}->{$name} = $method;
1197  
1198 264 return $class;
1199 }
1200  
1201 ######################################################################
1202  
1203 15 ▶sub new_args {
Subroutine new_args: called CC=1 SCAR=0.0
Pod documented
1204 15 my $class = shift;
1205 15 my $self = {};
1206 15 bless $self, $class;
1207 15 return $self->classes::init_args(@_);
1208 }
1209  
1210 34 ▶sub new_only {
Subroutine new_only: called CC=1 SCAR=0.0
Pod documented
1211 34 return bless {}, $_[0];
1212 }
1213  
1214 39 ▶sub new_init {
Subroutine new_init: called CC=1 SCAR=0.0
Pod documented
1215 39 my $class = shift;
1216 39 my $self = {};
1217 39 bless $self, $class;
1218 39 return $self->initialize(@_);
1219 }
1220  
1221 2 ▶sub new_fast {
Subroutine new_fast: called CC=3 SCAR=11.4
Pod documented
1222 2 my $class = shift;
1223 2 ▶ return bless($_[0], $class) if ref $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] 1 1 2
1224 1 ▶ return bless({@_}, $class) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 1 0 1
1225 0 return bless({}, $class);
1226 }
1227  
1228 ######################################################################
1229  
1230 41 ▶sub init_args {
Subroutine init_args: called CC=4 SCAR=13.9
Pod documented
1231 41 my $self = shift;
1232 41 my $attrs = $_[0];
1233 41 ▶ $attrs = {@_} if ref $attrs ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $attrs ne "HASH" 40 1 41
1234  
1235 41 ▶ while ( my ( $attr, $value ) = each %$attrs ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$attrs) 10 41 51
1236 10 ▶ my $setter = $self->can("set_$attr")
Branch: 1 branch
BranchTrueFalseTotal
my $setter = $self->can("set_$attr") or "X::UnknownAttr"->throw([$attr]) 10 0 10
1237 or X::UnknownAttr->throw([$attr]);
1238 10 $self->$setter($value);
1239 }
1240  
1241 41 return $self;
1242 }
1243  
1244 ######################################################################
1245 # Modeled after Clone::PP and Clone, Matthew Simon Cavalletto,
1246 # David Muir Sharnoff, Ray Finch, chocolateboy. See pod for
1247 # differences.
1248 #
1249 # Could use some work to get object 'clone' methods observed--
1250 # especially with the prevalence of unclonable perl classes
1251 # out there, but alas, this makes cloning self refs difficult
1252 # because their cached copies no longer match.
1253  
1254 0 ▶sub clone {
Subroutine clone: not called CC=14 SCAR=53.5
Pod documented
1255 0 ▶ X::Usage->throw('$obj->clone') if @_ != 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 0 0
1256 0 my ($this) = @_;
1257  
1258 # 'undef' is a valid leaf clone value
1259 0 ▶ return $this if !defined $this;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $this 0 0 0
1260  
1261 0 my $is_ref = reftype $this;
1262 0 my $blessed_as = blessed $this;
1263 0 my $clone = undef;
1264  
1265 # cache of back references to prevent recursion, mark top,
1266 # localized global makes the clone_cache visible down through
1267 # the recursive subroutine calls
1268  
1269 local %classes::_clone_cache = ( __top => 1, __self => $this )
1270 0 ▶ unless ( exists $classes::_clone_cache{'__self'} );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $classes::_clone_cache{"__self"} 0 0 0
1271  
1272 0 ▶ if ( $classes::_clone_cache{__top} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($classes::_clone_cache{"__top"}) 0 0 0
1273  
1274 # put stuff for first run, if needed, here
1275  
1276 0 delete $classes::_clone_cache{__top};
1277 }
1278  
1279 # block recursion, seen already
1280 return $classes::_clone_cache{$this}
1281 0 ▶ if exists $classes::_clone_cache{$this};
Branch: 1 branch
BranchTrueFalseTotal
if exists $classes::_clone_cache{$this} 0 0 0
1282  
1283 # scalar = leaf
1284 0 ▶ return $this if !$is_ref;
Branch: 1 branch
BranchTrueFalseTotal
unless $is_ref 0 0 0
1285  
1286 # clone each key or value that is a ref recursively
1287 0 ▶ if ( $is_ref eq 'HASH' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($is_ref eq 'HASH') { } 0 0 0
elsif ($is_ref eq 'ARRAY') { } 0 0 0
elsif ($is_ref =~ /^(REF|SCALAR)$/o and not $blessed_as) { } 0 0 0
1288 0 $classes::_clone_cache{$this} = $clone = {};
1289 0 ▶ %$clone = map { !ref $_ ? $_ : classes::clone($_) } %$this;
Branch: 1 branch
BranchTrueFalseTotal
!ref($_) ? : 0 0 0
1290 }
1291  
1292 # clone each element in array that is a ref recursively
1293 elsif ( $is_ref eq 'ARRAY' ) {
1294 0 $classes::_clone_cache{$this} = $clone = [];
1295 0 ▶ @$clone = map { !ref $_ ? $_ : classes::clone($_) } @$this;
Branch: 1 branch
BranchTrueFalseTotal
!ref($_) ? : 0 0 0
1296 }
1297  
1298 # clone refs to refs and simple refs to scalars
1299 ▶ elsif ( $is_ref =~ /^(REF|SCALAR)$/o && !$blessed_as ) {
Condition0%0/3
$is_ref =~ /^(REF|SCALAR)$/o and not $blessed_as
!ll&&!rl&&r
000
MC/DC0%0/2
$is_ref =~ /^(REF|SCALAR)$/o and not $blessed_as
$is_ref =~ /^(REF|SCALAR)$/o not $blessed_as
Truth table0%0/3
A and not(B)
A= $is_ref =~ /^(REF|SCALAR)$/oB= not $blessed_as
ABresult
0X0
100
111
1300 0 $classes::_clone_cache{$this} = $clone = \( my $var = q[] );
1301 0 $$clone = classes::clone($$this);
1302 }
1303  
1304 # plain copies of anything else (globs, regx, etc.)
1305 else {
1306 0 $classes::_clone_cache{$this} = $clone = $this;
1307 }
1308  
1309 0 ▶ bless $clone, $blessed_as if $blessed_as;
Branch: 1 branch
BranchTrueFalseTotal
if $blessed_as 0 0 0
1310 0 return $clone;
1311 }
1312  
1313 ######################################################################
1314  
1315 2 ▶sub sprintf {
Subroutine sprintf: called CC=2 SCAR=7.0
Pod documented
1316 2 my ($self, $format, @attrs) = @_;
1317 my @values = map {
1318 2 my $getter = $self->can("get_$_");
1319 4 ▶ defined $getter ? $self->$getter : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $getter ? : 4 0 4
1320 } @attrs;
1321 2 CORE::sprintf $format, @values;
1322 }
1323  
1324 0 ▶sub printf {
Subroutine printf: not called CC=2 SCAR=17.9
Pod documented
1325 0 my ($self, $format, @attrs) = @_;
1326 my @values = map {
1327 0 my $getter = $self->can("get_$_");
1328 0 ▶ defined $getter ? $self->$getter : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $getter ? : 0 0 0
1329 } @attrs;
1330 0 CORE::printf $format, @values;
1331 }
1332  
1333 ######################################################################
1334  
1335 12 ▶sub set {
Subroutine set: called CC=2 SCAR=7.6
Pod documented
1336 12 my ( $self, $name, $value ) = @_;
1337 12 ▶ my $accessor = $self->can("set_$name")
Condition33%1/3
$self->can("set_$name") || 'X::MethodNotFound'->throw("set_$name")
l!l&&r!l&&!r
1200
MC/DC0%0/2
$self->can("set_$name") || 'X::MethodNotFound'->throw("set_$name")
$self->can("set_$name") 'X::MethodNotFound'->throw("set_$name")
Truth table33%1/3
A || B
A= $self->can("set_$name")B= 'X::MethodNotFound'->throw("set_$name")
ABresult
1X1
011
000
1338 || X::MethodNotFound->throw("set_$name");
1339 12 return $self->$accessor($value);
1340 }
1341  
1342 0 ▶sub get {
Subroutine get: not called CC=2 SCAR=17.9
Pod documented
1343 0 my ( $self, $name ) = @_;
1344 0 ▶ my $accessor = $self->can("get_$name")
Condition0%0/3
$self->can("get_$name") || 'X::MethodNotFound'->throw("get_$name")
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->can("get_$name") || 'X::MethodNotFound'->throw("get_$name")
$self->can("get_$name") 'X::MethodNotFound'->throw("get_$name")
Truth table0%0/3
A || B
A= $self->can("get_$name")B= 'X::MethodNotFound'->throw("get_$name")
ABresult
1X1
011
000
1345 || X::MethodNotFound->throw("get_$name");
1346 0 return $self->$accessor;
1347 }
1348  
1349 ####################################################################
1350  
1351 0 ▶sub dump {
Subroutine dump: not called CC=20 SCAR=60.4
Pod documented
1352 0 my ( $this, $out ) = @_;
1353 0 my $is_ref = ref $out;
1354 0 my $blessed_as = blessed $this;
1355 0 ▶ my $class = $blessed_as || $this || caller;
Condition0%0/6
$blessed_as || $this || caller
l!l&&r!l&&!r
000
$blessed_as or $this
l!l&&r!l&&!r
000
MC/DC0%0/3
$blessed_as || $this || caller
$blessed_as $this caller
Truth table0%0/4
A or B || C
A= $blessed_asB= $thisC= caller
ABCresult
1XX1
01X1
0011
0000
1356 0 my $decl = ${ $class . '::DECL' };
1357 0 my $mixins = ${ $class . '::MIXIN' };
1358  
1359 0 my $usage = <<'EOM';
1360  
1361 MyClass|$object->classes::dump;
1362 ... ->classes::dump( $handle );
1363 ... ->classes::dump( \$buffer );
1364 classes::dump(['just','anything']);
1365 EOM
1366  
1367 0 ▶ X::Usage->throw($usage)
Branch: 1 branch
BranchTrueFalseTotal
if $out and not $is_ref 0 0 0
Condition0%0/3
$out and not $is_ref
!ll&&!rl&&r
000
MC/DC0%0/2
$out and not $is_ref
$out not $is_ref
Truth table0%0/3
A and not(B)
A= $outB= not $is_ref
ABresult
0X0
100
111
1368 if $out && !$is_ref; # handle or scalar ref
1369  
1370 0 ▶ $out ||= *STDERR;
Condition0%0/3
$out ||= *STDERR
l!l&&r!l&&!r
000
MC/DC0%0/2
$out ||= *STDERR
$out *STDERR
Truth table0%0/3
A ||= B
A= $outB= *STDERR
ABresult
1X1
011
000
1371  
1372 0 require Data::Dumper;
1373  
1374 0 local $Data::Dumper::Varname;
1375 0 local $Data::Dumper::Indent = 1;
1376 0 my $buf;
1377  
1378 # header line
1379 0 my $len = length $class;
1380 0 my $pad = int( ( 70 - $len ) / 2 );
1381 0 my $rest = 70 - ( $pad*2 + $len );
1382 0 $pad -= 2;
1383 0 $buf .= '#' x $pad . " $class "
1384 . '#' x ( $pad + $rest ) . "\n\n";
1385  
1386 0 ▶ if ( $decl ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($decl) { } 0 0 0
1387 0 $Data::Dumper::Varname = 'DECL';
1388 0 $buf .= Data::Dumper::Dumper($decl) . "\n";
1389  
1390 0 ▶ if ( $mixins ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mixins) 0 0 0
1391 0 $Data::Dumper::Varname = 'MIXIN';
1392 0 $buf .= Data::Dumper::Dumper($mixins) . "\n";
1393 }
1394  
1395 0 my $d_class_attrs = $decl->{'class_attrs'};
1396 0 my $d_class_attrs_ro = $decl->{'class_attrs_ro'};
1397  
1398 # pull out internal class attrs (if any) by accessor
1399 0 ▶ if ( $d_class_attrs || $d_class_attrs_ro ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d_class_attrs or $d_class_attrs_ro) 0 0 0
Condition0%0/3
$d_class_attrs or $d_class_attrs_ro
l!l&&r!l&&!r
000
MC/DC0%0/2
$d_class_attrs or $d_class_attrs_ro
$d_class_attrs $d_class_attrs_ro
Truth table0%0/3
A or B
A= $d_class_attrsB= $d_class_attrs_ro
ABresult
1X1
011
000
1400 0 my %class_attr;
1401 0 $Data::Dumper::Varname = 'CLASS_STATE';
1402 0 ▶ if ( $d_class_attrs ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d_class_attrs) 0 0 0
1403 0 ▶ while ( my ( $tag, $val ) = each %$d_class_attrs ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$d_class_attrs) 0 0 0
1404 0 my $getter = "get_$tag";
1405 0 my $accessor = $class->can($getter);
1406 0 ▶ $class_attr{$tag} =
Branch: 1 branch
BranchTrueFalseTotal
$accessor ? : 0 0 0
1407 $accessor
1408 ? $class->$accessor
1409 : 'ERROR_NO_ACCESSOR';
1410 }
1411 }
1412 0 ▶ if ( $d_class_attrs_ro ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d_class_attrs_ro) 0 0 0
1413 0 ▶ while ( my ($tag, $val) = each %$d_class_attrs_ro ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$d_class_attrs_ro) 0 0 0
1414 0 my $getter = "get_$tag";
1415 0 my $accessor = $class->can($getter);
1416 0 ▶ $class_attr{$tag} =
Branch: 1 branch
BranchTrueFalseTotal
$accessor ? : 0 0 0
1417 $accessor
1418 ? $class->$accessor
1419 : 'ERROR_NO_ACCESSOR';
1420 }
1421 }
1422 0 $buf .= Data::Dumper::Dumper( \%class_attr ) . "\n";
1423 }
1424  
1425 # object only
1426 0 ▶ if ($blessed_as) {
Branch: 1 branch
BranchTrueFalseTotal
if ($blessed_as) 0 0 0
1427 0 $Data::Dumper::Varname = 'OBJECT_STATE';
1428 0 $buf .= Data::Dumper::Dumper($this) . "\n";
1429 }
1430  
1431 }
1432  
1433 else {
1434 0 $Data::Dumper::Varname = 'THIS';
1435 0 $buf .= Data::Dumper::Dumper( $this );
1436  
1437 0 ▶ if ($blessed_as) {
Branch: 1 branch
BranchTrueFalseTotal
if ($blessed_as) 0 0 0
1438 0 $Data::Dumper::Varname = 'PACKAGE_SYMBOLS';
1439 0 $buf .= Data::Dumper::Dumper( \%{$blessed_as.'::'} );
1440 }
1441
1442 }
1443  
1444 # string buffer
1445 0 ▶ if ( $is_ref and $is_ref eq 'SCALAR' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_ref and $is_ref eq 'SCALAR') { } 0 0 0
Condition0%0/3
$is_ref and $is_ref eq "SCALAR"
!ll&&!rl&&r
000
MC/DC0%0/2
$is_ref and $is_ref eq "SCALAR"
$is_ref $is_ref eq "SCALAR"
Truth table0%0/3
A and B
A= $is_refB= $is_ref eq "SCALAR"
ABresult
0X0
100
111
1446 0 $$out = $buf;
1447 }
1448  
1449 # io handle
1450 else {
1451 0 print $out $buf;
1452 }
1453  
1454 0 return $this;
1455 }
1456  
1457 ######################################################################
1458  
1459 0 ▶sub id { Scalar::Util::refaddr($_[0]) }
Subroutine id: not called CC=1 SCAR=6.9
Pod documented
1460  
1461 ######################################################################
1462 # Throwable mixin - the guts of perl exceptions
1463  
1464 package classes::Throwable;
1465 28 ▶use strict 'subs'; no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
1466 27 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
1467  
1468 use classes
1469 27 ▶ type => 'mixable',
Subroutine BEGIN: called CC=1 SCAR=0.0
1470 class_methods => {
1471 throw => 'throw',
1472 caught => 'caught',
1473 catch => 'caught', # synonym
1474 },
1475 methods => {
1476 capture => 'capture',
1477 rethrow => 'rethrow',
1478 send => 'rethrow',
1479 },
1480 27 ; # END DECLARATION
1481  
1482 0 ▶sub capture { $_[0] };
Subroutine capture: not called CC=1 SCAR=6.9
1483 22 ▶sub throw { my $x = shift->new(@_); $x->capture; die $x }
Subroutine throw: called CC=1 SCAR=0.0
1484 0 ▶sub rethrow { die $_[0] if $_[0] }
Subroutine rethrow: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] 0 0 0
1485  
1486 0 ▶sub caught {
Subroutine caught: not called CC=3 SCAR=24.8
1487 0 my $class = shift;
1488 0 my $last_err = $@;
1489  
1490 0 ▶ return $last_err
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($last_err) and $last_err->isa($class) 0 0 0
Condition0%0/3
builtin::blessed($last_err) and $last_err->isa($class)
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($last_err) and $last_err->isa($class)
builtin::blessed($last_err) $last_err->isa($class)
Truth table0%0/3
A and B
A= builtin::blessed($last_err)B= $last_err->isa($class)
ABresult
0X0
100
111
1491 if Scalar::Util::blessed $last_err && $last_err->isa($class);
1492  
1493 0 return undef;
1494 }
1495  
1496 ######################################################################
1497 # Bare minimum exception class
1498  
1499 package X::classes;
1500 27 ▶use strict 'subs'; no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
1501 27 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
1502  
1503 use classes
1504 27 ▶ mixes => 'classes::Throwable',
Subroutine BEGIN: called CC=1 SCAR=0.0
1505 new => 'classes::new_init',
1506 init => 'classes::init_args',
1507 clone => 'classes::clone',
1508 dump => 'classes::dump',
1509 methods => ['as_string'],
1510 27 ;
1511  
1512 0 ▶sub as_string {
Subroutine as_string: not called CC=1 SCAR=6.9
1513 0 my $c = Scalar::Util::blessed shift;
1514 0 return "exception $c\n"
1515 }
1516  
1517 0 ▶use overload bool => sub {1}, '""' => 'as_string', fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine __ANON__: not called CC=1 SCAR=0.2
1518
1519 ######################################################################
1520 # base exception class
1521  
1522 package X::classes::traceable;
1523 27 ▶use strict 'subs'; no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
1524 27 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
1525  
1526 use classes
1527 27 ▶ extends => 'X::classes',
Subroutine BEGIN: called CC=1 SCAR=0.0
1528 new => 'classes::new_init',
1529 init => 'initialize',
1530 clone => 'classes::clone',
1531 dump => 'classes::dump',
1532 class_attrs => {
1533 'Verbose' => 3,
1534 'Order' => ['item','message'],
1535 'Format' => '',
1536 'Caller_Format' => q[ at %4$s %1$s '%2$s' %3$s],
1537 'Whole_Stack' => undef,
1538 },
1539 attrs => [qw(
1540 item message full_message attr_list
1541 )],
1542 attrs_ro => [qw(
1543 pid uid euid gid egid time
1544 call_stack call_package call_method call_file call_line
1545 )],
1546 methods => [ 'as_string' ],
1547 27 ;
1548  
1549 3 ▶sub get_full_message { get_message(@_) }
Subroutine get_full_message: called CC=1 SCAR=0.0
1550 0 ▶sub set_full_message { set_message(@_) }
Subroutine set_full_message: not called CC=1 SCAR=6.9
1551  
1552 22 ▶sub capture {
Subroutine capture: called CC=6 SCAR=18.1
1553 22 my ($self, $offset) = @_;
1554  
1555 22 my @call_stack;
1556 22 ▶ $offset = @_ >= 2 ? $offset : 0;
Branch: 1 branch
BranchTrueFalseTotal
@_ >= 2 ? : 0 22 22
1557  
1558 # remove this method (at least)
1559 22 my $whole_stack = $self->get_Whole_Stack;
1560 22 ▶ my $frame = $whole_stack ? 1 : 1 + $offset;
Branch: 1 branch
BranchTrueFalseTotal
$whole_stack ? : 0 22 22
1561  
1562 # fixes problems with some versions of perl time
1563 22 $self->{$ATTR_time} = CORE::time();
1564  
1565 # process ownership
1566 22 $self->{$ATTR_pid} = $$;
1567 22 $self->{$ATTR_uid} = $<;
1568 22 $self->{$ATTR_euid} = $>;
1569 22 $self->{$ATTR_gid} = $(;
1570 22 $self->{$ATTR_egid} = $);
1571  
1572 CALL_TRACE:
1573 22 while (1) {
1574 164 my @call_frame = caller($frame);
1575 164 ▶ last CALL_TRACE if !@call_frame;
Branch: 1 branch
BranchTrueFalseTotal
unless @call_frame 22 142 164
1576 142 push @call_stack, \@call_frame;
1577 142 ++$frame;
1578 }
1579  
1580 # if at least one frame, give context of the caller's caller
1581 22 ▶ shift @call_stack if @call_stack > 1 && !$whole_stack;
Branch: 1 branch
BranchTrueFalseTotal
if @call_stack > 1 and not $whole_stack 22 0 22
Condition33%1/3
@call_stack > 1 and not $whole_stack
!ll&&!rl&&r
0022
MC/DC0%0/2
@call_stack > 1 and not $whole_stack
@call_stack > 1 not $whole_stack
Truth table33%1/3
A and not(B)
A= @call_stack > 1B= not $whole_stack
ABresult
0X0
100
111
1582  
1583 22 my $call_stack_ref = \@call_stack;
1584 22 $self->{$ATTR_call_stack} = $call_stack_ref;
1585 22 $self->{$ATTR_call_package} = $call_stack[0][0];
1586 22 $self->{$ATTR_call_file} = $call_stack[0][1];
1587 22 $self->{$ATTR_call_line} = $call_stack[0][2];
1588 22 $self->{$ATTR_call_method} = $call_stack[0][3];
1589  
1590 22 return $self;
1591 }
1592  
1593 32 ▶sub initialize {
Subroutine initialize: called CC=4 SCAR=13.9
1594 32 my $self = shift;
1595 32 my $is_ref = ref $_[0];
1596  
1597 32 $self->{$ATTR_message} = '';
1598 32 $self->{$ATTR_full_message} = '';
1599  
1600 # X::classes::traceable->new('some message')
1601 32 ▶ if ( !$is_ref && @_ == 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $is_ref and @_ == 1) { } 3 29 32
elsif ($is_ref eq 'ARRAY') { } 12 17 29
Condition100%3/3
not $is_ref and @_ == 1
!ll&&!rl&&r
12173
MC/DC100%2/2
not $is_ref and @_ == 1
not $is_ref @_ == 1
Truth table100%3/3
not(A) and B
A= not $is_refB= @_ == 1
ABresult
0X0
100
111
1602 3 $self->{$ATTR_message} = $_[0];
1603 3 return $self;
1604 }
1605  
1606 # X::classes::traceable->new( ['item'] )
1607 elsif ( $is_ref eq 'ARRAY' ) {
1608 12 $self->set_attr_list($_[0]);
1609 12 return $self;
1610 }
1611 # normal
1612 # X::classes::traceable->new( message=>'blah', item=>2 )
1613 # X::classes::traceable->new( -message=>'blah', -item=>2 )
1614 # X::classes::traceable->new( { message=>'blah', item=>2 } )
1615 # X::classes::traceable->new( { -message=>'blah', -item=>2 } )
1616 17 return $self->classes::init_args(@_);
1617 }
1618  
1619 12 ▶sub set_attr_list {
Subroutine set_attr_list: called CC=5 SCAR=16.4
1620 12 my $self = shift;
1621 12 ▶ X::Usage->throw("\$exc->set_attr_list(ARRAYREF)\n")
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] ne "ARRAY" 0 12 12
1622 if ref $_[0] ne 'ARRAY';
1623 12 my $attr_list = $self->{$ATTR_attr_list} = $_[0];
1624  
1625 # Rest is dispatch side-effect ...
1626  
1627 12 my $order = $self->get_Order; # commonly overriden
1628 12 my @order = @$order;
1629  
1630 # cannot dispatch if no 'Order' of attr_list
1631 12 ▶ return $attr_list unless @order >= 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @order >= 1 0 12 12
1632  
1633 12 for my $attr_value (@$attr_list) {
1634 12 my $attr_name = shift @order;
1635  
1636 # order = 'name', undef, 'rank'
1637 # list = 'Bob', 'ignored', 'private', 'ignored2'
1638 12 ▶ next if !$attr_name;
Branch: 1 branch
BranchTrueFalseTotal
unless $attr_name 0 12 12
1639  
1640 # dispatch to accessor
1641 12 $self->classes::set( $attr_name => $attr_value );
1642 }
1643  
1644 12 return $attr_list;
1645 }
1646  
1647 3 ▶sub as_string {
Subroutine as_string: called CC=10 SCAR=25.6
1648 3 my $self = shift;
1649 3 my $verbose = $self->get_Verbose;
1650  
1651 3 ▶ return '' if not $verbose;
Branch: 1 branch
BranchTrueFalseTotal
unless $verbose 0 3 3
1652  
1653 3 my $exception_name = Scalar::Util::blessed $self;
1654 3 my $full_message = $self->get_full_message;
1655  
1656 3 my $attrs = $self->get_attr_list;
1657 3 ▶ my @attrs = @$attrs if $attrs;
Branch: 1 branch
BranchTrueFalseTotal
if $attrs 0 3 3
1658 3 my $format = $self->get_Format;
1659  
1660 3 ▶ return $exception_name . "\n" if $verbose == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose == 1 0 3 3
1661  
1662 # exception
1663 3 my $string = $exception_name;
1664  
1665 # exception + message or attr_list
1666 3 ▶ if ( $verbose >= 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose >= 2) 3 0 3
1667 3 $string .= ' ';
1668 3 ▶ $string .= $full_message
Branch: 1 branch
BranchTrueFalseTotal
$format ? : 0 3 3
Condition33%1/3
$full_message || ($format ? sprintf($format, @attrs) : join($", @attrs))
l!l&&r!l&&!r
003
MC/DC0%0/2
$full_message || ($format ? sprintf($format, @attrs) : join($", @attrs))
$full_message ($format ? sprintf($format, @attrs) : join($", @attrs))
Truth table33%1/3
A || B
A= $full_messageB= ($format ? sprintf($format, @attrs) : join($", @attrs))
ABresult
1X1
011
000
1669 || (
1670 ($format)
1671 ? sprintf( $format, @attrs )
1672 : "@attrs"
1673 );
1674 3 $string .= "\n";
1675 }
1676  
1677 # exception + message or attr_list + trace (3=file, 4=path)
1678 3 ▶ if ( $verbose >= 3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose >= 3) 3 0 3
1679 3 my $caller_format = $self->get_Caller_Format;
1680 3 my $call_stack = $self->get_call_stack;
1681 3 for my $caller (@$call_stack) {
1682 0 ▶ if ( $verbose == 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose == 3) 0 0 0
1683 27 ▶ use File::Basename 'basename';
Subroutine BEGIN: called CC=1 SCAR=0.0
1684 0 $$caller[1] = basename $$caller[1];
1685 }
1686 0 $string .= sprintf $caller_format . "\n", @$caller;
1687 }
1688 }
1689  
1690 3 return $string;
1691 }
1692  
1693 27 ▶use overload bool => sub {1}, '""' => 'as_string', fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
1694  
1695 ################################################################
1696 ## exceptions used by 'classes' itself
1697 ################################################################
1698  
1699 package main;
1700 27 classes::classes
1701 { name=>'X::NameUnavailable', extends=>'X::classes::traceable' },
1702 { name=>'X::InvalidName', extends=>'X::classes::traceable' },
1703 { name=>'X::NotPkgMethod', extends=>'X::classes::traceable' },
1704 { name=>'X::Unimplemented', extends=>'X::classes::traceable' },
1705 { name=>'X::Usage', extends=>'X::classes::traceable' },
1706 { name=>'X::Undefined', extends=>'X::classes::traceable' },
1707 { name=>'X::Empty', extends=>'X::classes::traceable' },
1708 { name=>'X::MethodNotFound', extends=>'X::classes::traceable' },
1709 { name=>'X::AttrScope', extends=>'X::classes::traceable' },
1710 { name=>'X::UnknownAttr', extends=>'X::classes::traceable' },
1711 ;
1712  
1713 27 1;
1714 __END__
↑ Summary classes/Test.pm »