blib/lib/Class/Multimethods.pm

filter: Statementstmt 35.6% 234 / 656 Branchbran 23.4% 96 / 410 Conditioncond 27.5% 54 / 196 MC/DCmcdc 14.6% 18 / 123 Subroutinesub 62.8% 22 / 35 Podpod 5.8% 1 / 17 totaltotal 29.5% 425 / 1437 CC 310 SCAR 59.5
CC
310
Cov
30%
analyse
1892.0
mm
231.0
mm
231.0
CRAP 382.5
↑ Summary
1 package Class::Multimethods;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use vars qw($VERSION @ISA @EXPORT);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 our $VERSION = '1.701';
8  
9 1 require Exporter;
10 1 @ISA = qw(Exporter);
11 1 @EXPORT = qw( multimethod resolve_ambiguous resolve_no_match superclass multimethod_wrapper );
12  
13 1 ▶use vars qw(%dispatch %cached %hasgeneric %ambiguous_handler %no_match_handler %max_args %min_args %dispatch_installed);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 %dispatch = (); # THE DISPATCH TABLE
16 1 %cached = (); # THE CACHE OF PREVIOUS RESOLUTIONS OF EMPTY SLOTS
17 1 %hasgeneric = (); # WHETHER A GIVEN MULTIMETHOD HAS ANY GENERIC VARIANTS
18 1 %ambiguous_handler = (); # HANDLERS FOR AMBIGUOUS CALLS
19 1 %no_match_handler = (); # HANDLERS FOR AMBIGUOUS CALLS
20 1 %max_args = (); # RECORDS MAX NUM OF ARGS IN ANY VARIANT
21 1 %min_args = (); # RECORDS MIN NUM OF ARGS IN ANY VARIANT
22  
23 1 %dispatch_installed = (); # RECORDS DISPATCHES ALREADY INSTALLED __BY__ __US__
24  
25 # THIS IS INTERPOSED BETWEEN THE CALLING PACKAGE AND Exporter TO SUPPORT THE
26 # use Class:Multimethods @methodnames SYNTAX
27  
28 sub import
29 5 ▶{
Subroutine import: called CC=2 SCAR=6.9
30 5 my $package = (caller)[0];
31 5 ▶ install_dispatch($package,pop @_) while $#_;
Branch: 1 branch
BranchTrueFalseTotal
$#_ and do { install_dispatch($package, pop @_) } 1 5 6
32 5 Class::Multimethods->export_to_level(1);
33 }
34  
35  
36 # INSTALL A DISPATCHING SUB FOR THE NAMED MULTIMETHOD IN THE CALLING PACKAGE
37  
38 sub install_dispatch
39 28 ▶{
Subroutine install_dispatch: called CC=3 SCAR=17.4
Pod undocumented
40 28 my ($pkg, $name) = @_;
41 # eval "sub ${pkg}::$name { Class::Multimethods::dispatch('$name',\@_) }"
42 28 ▶ if ( ! $dispatch_installed{$pkg}{$name} )
Branch: 1 branch
BranchTrueFalseTotal
if (not $Class::Multimethods::dispatch_installed{$pkg}{$name}) 8 20 28
43 {
44 0 ▶ eval(make_dispatch($pkg,$name)) || croak "internal error: $@";
Subroutine mm: called CC=26 SCAR=54.4
Subroutine mm: called CC=26 SCAR=54.4
Subroutine mm: called CC=26 SCAR=54.4
Subroutine new: called CC=26 SCAR=54.4
Subroutine set: called CC=26 SCAR=54.4
Subroutine set: called CC=26 SCAR=54.4
Subroutine val: called CC=26 SCAR=54.4
Subroutine wild: called CC=26 SCAR=54.4
Branch: 145 branches
BranchTrueFalseTotal
eval make_dispatch($pkg, $name) or croak("internal error: $@") 8 0 8
(~$_ & $_) eq 0 ? : 1 0 1
if $code 1 0 1
if ($i < @types) 0 0 0
if index($types[$i], "Class::Multimethods::SUPERCLASS") == 0 0 0 0
(resolve("mm", \@candidates, \@code, \%tried) or not @candidates) or do { '???' } 0 0 0
if (@code == 0 and $Class::Multimethods::hasgeneric{"mm"}) 0 0 0
if ($gci < @gencandidates) 0 0 0
if ($i < @types) 0 0 0
(resolve("mm", \@candidates, \@code, \%tried) or not @candidates) or do { '???' } 0 0 0
if @code 0 0 0
if (@code == 1) { } 0 0 0
elsif (@code > 1) { } 0 0 0
if (defined $handler) 0 0 0
if ref $handler 0 0 0
if defined $Class::Multimethods::dispatch{"mm"}{$handler} 0 0 0
if (defined $handler) 0 0 0
if ref $handler 0 0 0
if defined $Class::Multimethods::dispatch{"mm"}{$handler} 0 0 0
(~$_ & $_) eq 0 ? : 1 2 3
if $code 32 220 252
if ($i < @types) 222 220 442
if index($types[$i], "Class::Multimethods::SUPERCLASS") == 0 0 222 222
(resolve("mm", \@candidates, \@code, \%tried) or not @candidates) or do { '???' } 220 510 730
if (@code == 0 and $Class::Multimethods::hasgeneric{"mm"}) 0 220 220
if ($gci < @gencandidates) 0 0 0
if ($i < @types) 0 0 0
(resolve("mm", \@candidates, \@code, \%tried) or not @candidates) or do { '???' } 0 0 0
if @code 0 0 0
if (@code == 1) { } 25 195 220
elsif (@code > 1) { } 12 183 195
if (defined $handler) 0 12 12
if ref $handler 0 0 0
if defined $Class::Multimethods::dispatch{"mm"}{$handler} 0 0 0
if (defined $handler) 0 183 183
if ref $handler 0 0 0
if defined $Class::Multimethods::dispatch{"mm"}{$handler} 0 0 0
(~$_ & $_) eq 0 ? : 1 0 1
if $code 2 0 2
if ($i < @types) 0 0 0
if index($types[$i], "Class::Multimethods::SUPERCLASS") == 0 0 0 0
(resolve("mm", \@candidates, \@code, \%tried) or not @candidates) or do { '???' } 0 0 0
if (@code == 0 and $Class::Multimethods::hasgeneric{"mm"}) 0 0 0
if ($gci < @gencandidates) 0 0 0
if ($i < @types) 0 0 0
(resolve("mm", \@candidates, \@code, \%tried) or not @candidates) or do { '???' } 0 0 0
if @code 0 0 0
if (@code == 1) { } 0 0 0
elsif (@code > 1) { } 0 0 0
if (defined $handler) 0 0 0
if ref $handler 0 0 0
if defined $Class::Multimethods::dispatch{"mm"}{$handler} 0 0 0
if (defined $handler) 0 0 0
if ref $handler 0 0 0
if defined $Class::Multimethods::dispatch{"mm"}{$handler} 0 0 0
(~$_ & $_) eq 0 ? : 2 6 8
if $code 4 0 4
if ($i < @types) 0 0 0
if index($types[$i], "Class::Multimethods::SUPERCLASS") == 0 0 0 0
(resolve("new", \@candidates, \@code, \%tried) or not @candidates) or do { '???' } 0 0 0
if (@code == 0 and $Class::Multimethods::hasgeneric{"new"}) 0 0 0
if ($gci < @gencandidates) 0 0 0
if ($i < @types) 0 0 0
(resolve("new", \@candidates, \@code, \%tried) or not @candidates) or do { '???' } 0 0 0
if @code 0 0 0
if (@code == 1) { } 0 0 0
elsif (@code > 1) { } 0 0 0
if (defined $handler) 0 0 0
if ref $handler 0 0 0
if defined $Class::Multimethods::dispatch{"new"}{$handler} 0 0 0
if (defined $handler) 0 0 0
if ref $handler 0 0 0
if defined $Class::Multimethods::dispatch{"new"}{$handler} 0 0 0
(~$_ & $_) eq 0 ? : 1 1 2
if $code 2 0 2
if ($i < @types) 0 0 0
if index($types[$i], "Class::Multimethods::SUPERCLASS") == 0 0 0 0
(resolve("set", \@candidates, \@code, \%tried) or not @candidates) or do { '???' } 0 0 0
if (@code == 0 and $Class::Multimethods::hasgeneric{"set"}) 0 0 0
if ($gci < @gencandidates) 0 0 0
if ($i < @types) 0 0 0
(resolve("set", \@candidates, \@code, \%tried) or not @candidates) or do { '???' } 0 0 0
if @code 0 0 0
if (@code == 1) { } 0 0 0
elsif (@code > 1) { } 0 0 0
if (defined $handler) 0 0 0
if ref $handler 0 0 0
if defined $Class::Multimethods::dispatch{"set"}{$handler} 0 0 0
if (defined $handler) 0 0 0
if ref $handler 0 0 0
if defined $Class::Multimethods::dispatch{"set"}{$handler} 0 0 0
(~$_ & $_) eq 0 ? : 1 1 2
if $code 0 3 3
if ($i < @types) 3 3 6
if index($types[$i], "Class::Multimethods::SUPERCLASS") == 0 0 3 3
(resolve("set", \@candidates, \@code, \%tried) or not @candidates) or do { '???' } 3 3 6
if (@code == 0 and $Class::Multimethods::hasgeneric{"set"}) 0 3 3
if ($gci < @gencandidates) 0 0 0
if ($i < @types) 0 0 0
(resolve("set", \@candidates, \@code, \%tried) or not @candidates) or do { '???' } 0 0 0
if @code 0 0 0
if (@code == 1) { } 3 0 3
elsif (@code > 1) { } 0 0 0
if (defined $handler) 0 0 0
if ref $handler 0 0 0
if defined $Class::Multimethods::dispatch{"set"}{$handler} 0 0 0
if (defined $handler) 0 0 0
if ref $handler 0 0 0
if defined $Class::Multimethods::dispatch{"set"}{$handler} 0 0 0
(~$_ & $_) eq 0 ? : 4 4 8
if $code 3 1 4
if ($i < @types) 1 1 2
if index($types[$i], "Class::Multimethods::SUPERCLASS") == 0 0 1 1
(resolve("val", \@candidates, \@code, \%tried) or not @candidates) or do { '???' } 1 1 2
if (@code == 0 and $Class::Multimethods::hasgeneric{"val"}) 0 1 1
if ($gci < @gencandidates) 0 0 0
if ($i < @types) 0 0 0
(resolve("val", \@candidates, \@code, \%tried) or not @candidates) or do { '???' } 0 0 0
if @code 0 0 0
if (@code == 1) { } 1 0 1
elsif (@code > 1) { } 0 0 0
if (defined $handler) 0 0 0
if ref $handler 0 0 0
if defined $Class::Multimethods::dispatch{"val"}{$handler} 0 0 0
if (defined $handler) 0 0 0
if ref $handler 0 0 0
if defined $Class::Multimethods::dispatch{"val"}{$handler} 0 0 0
(~$_ & $_) eq 0 ? : 0 0 0
if $code 2 79 81
if ($i < @types) 79 79 158
if index($types[$i], "Class::Multimethods::SUPERCLASS") == 0 0 79 79
(resolve("wild", \@candidates, \@code, \%tried) or not @candidates) or do { '???' } 79 186 265
if (@code == 0 and $Class::Multimethods::hasgeneric{"wild"}) 61 18 79
if ($gci < @gencandidates) 61 61 122
if ($i < @types) 122 61 183
(resolve("wild", \@candidates, \@code, \%tried) or not @candidates) or do { '???' } 61 66 127
if @code 61 0 61
if (@code == 1) { } 76 3 79
elsif (@code > 1) { } 3 0 3
if (defined $handler) 0 3 3
if ref $handler 0 0 0
if defined $Class::Multimethods::dispatch{"wild"}{$handler} 0 0 0
if (defined $handler) 0 0 0
if ref $handler 0 0 0
if defined $Class::Multimethods::dispatch{"wild"}{$handler} 0 0 0
Condition35%43/120
ref $_ || ((~$_ & $_) eq 0 ? '#' : '$')
l!l&&r!l&&!r
110
$Class::Multimethods::dispatch{'mm'}{$sig} || $Class::Multimethods::cached{'mm'}{$sig}
l!l&&r!l&&!r
100
resolve("mm", \@candidates, \@code, \%tried) or not @candidates
l!l&&r!l&&!r
000
@code == 0 and $Class::Multimethods::hasgeneric{"mm"}
!ll&&!rl&&r
000
resolve("mm", \@candidates, \@code, \%tried) or not @candidates
l!l&&r!l&&!r
000
ref $_ || ((~$_ & $_) eq 0 ? '#' : '$')
l!l&&r!l&&!r
50530
$Class::Multimethods::dispatch{'mm'}{$sig} || $Class::Multimethods::cached{'mm'}{$sig}
l!l&&r!l&&!r
2012220
resolve("mm", \@candidates, \@code, \%tried) or not @candidates
l!l&&r!l&&!r
37183510
@code == 0 and $Class::Multimethods::hasgeneric{"mm"}
!ll&&!rl&&r
371830
resolve("mm", \@candidates, \@code, \%tried) or not @candidates
l!l&&r!l&&!r
000
ref $_ || ((~$_ & $_) eq 0 ? '#' : '$')
l!l&&r!l&&!r
310
$Class::Multimethods::dispatch{'mm'}{$sig} || $Class::Multimethods::cached{'mm'}{$sig}
l!l&&r!l&&!r
200
resolve("mm", \@candidates, \@code, \%tried) or not @candidates
l!l&&r!l&&!r
000
@code == 0 and $Class::Multimethods::hasgeneric{"mm"}
!ll&&!rl&&r
000
resolve("mm", \@candidates, \@code, \%tried) or not @candidates
l!l&&r!l&&!r
000
ref $_ || ((~$_ & $_) eq 0 ? '#' : '$')
l!l&&r!l&&!r
080
$Class::Multimethods::dispatch{'new'}{$sig} || $Class::Multimethods::cached{'new'}{$sig}
l!l&&r!l&&!r
400
resolve("new", \@candidates, \@code, \%tried) or not @candidates
l!l&&r!l&&!r
000
@code == 0 and $Class::Multimethods::hasgeneric{"new"}
!ll&&!rl&&r
000
resolve("new", \@candidates, \@code, \%tried) or not @candidates
l!l&&r!l&&!r
000
ref $_ || ((~$_ & $_) eq 0 ? '#' : '$')
l!l&&r!l&&!r
220
$Class::Multimethods::dispatch{'set'}{$sig} || $Class::Multimethods::cached{'set'}{$sig}
l!l&&r!l&&!r
200
resolve("set", \@candidates, \@code, \%tried) or not @candidates
l!l&&r!l&&!r
000
@code == 0 and $Class::Multimethods::hasgeneric{"set"}
!ll&&!rl&&r
000
resolve("set", \@candidates, \@code, \%tried) or not @candidates
l!l&&r!l&&!r
000
ref $_ || ((~$_ & $_) eq 0 ? '#' : '$')
l!l&&r!l&&!r
420
$Class::Multimethods::dispatch{'set'}{$sig} || $Class::Multimethods::cached{'set'}{$sig}
l!l&&r!l&&!r
003
resolve("set", \@candidates, \@code, \%tried) or not @candidates
l!l&&r!l&&!r
303
@code == 0 and $Class::Multimethods::hasgeneric{"set"}
!ll&&!rl&&r
300
resolve("set", \@candidates, \@code, \%tried) or not @candidates
l!l&&r!l&&!r
000
ref $_ || ((~$_ & $_) eq 0 ? '#' : '$')
l!l&&r!l&&!r
080
$Class::Multimethods::dispatch{'val'}{$sig} || $Class::Multimethods::cached{'val'}{$sig}
l!l&&r!l&&!r
301
resolve("val", \@candidates, \@code, \%tried) or not @candidates
l!l&&r!l&&!r
101
@code == 0 and $Class::Multimethods::hasgeneric{"val"}
!ll&&!rl&&r
100
resolve("val", \@candidates, \@code, \%tried) or not @candidates
l!l&&r!l&&!r
000
ref $_ || ((~$_ & $_) eq 0 ? '#' : '$')
l!l&&r!l&&!r
16200
$Class::Multimethods::dispatch{'wild'}{$sig} || $Class::Multimethods::cached{'wild'}{$sig}
l!l&&r!l&&!r
2079
resolve("wild", \@candidates, \@code, \%tried) or not @candidates
l!l&&r!l&&!r
1861186
@code == 0 and $Class::Multimethods::hasgeneric{"wild"}
!ll&&!rl&&r
18061
resolve("wild", \@candidates, \@code, \%tried) or not @candidates
l!l&&r!l&&!r
61066
MC/DC0%0/2
ref $_ || ((~$_ & $_) eq 0 ? '#' : '$')
ref $_ ((~$_ & $_) eq 0 ? '#' : '$')
MC/DC0%0/2
$Class::Multimethods::dispatch{'mm'}{$sig} || $Class::Multimethods::cached{'mm'}{$sig}
$Class::Multimethods::dispatch{'mm'}{$sig} $Class::Multimethods::cached{'mm'}{$sig}
MC/DC0%0/2
resolve("mm", \@candidates, \@code, \%tried) or not @candidates
resolve("mm", \@candidates, \@code, \%tried) not @candidates
MC/DC0%0/2
@code == 0 and $Class::Multimethods::hasgeneric{"mm"}
@code == 0 $Class::Multimethods::hasgeneric{"mm"}
MC/DC0%0/2
resolve("mm", \@candidates, \@code, \%tried) or not @candidates
resolve("mm", \@candidates, \@code, \%tried) not @candidates
MC/DC0%0/2
ref $_ || ((~$_ & $_) eq 0 ? '#' : '$')
ref $_ ((~$_ & $_) eq 0 ? '#' : '$')
MC/DC100%2/2
$Class::Multimethods::dispatch{'mm'}{$sig} || $Class::Multimethods::cached{'mm'}{$sig}
$Class::Multimethods::dispatch{'mm'}{$sig} $Class::Multimethods::cached{'mm'}{$sig}
MC/DC100%2/2
resolve("mm", \@candidates, \@code, \%tried) or not @candidates
resolve("mm", \@candidates, \@code, \%tried) not @candidates
MC/DC0%0/2
@code == 0 and $Class::Multimethods::hasgeneric{"mm"}
@code == 0 $Class::Multimethods::hasgeneric{"mm"}
MC/DC0%0/2
resolve("mm", \@candidates, \@code, \%tried) or not @candidates
resolve("mm", \@candidates, \@code, \%tried) not @candidates
MC/DC0%0/2
ref $_ || ((~$_ & $_) eq 0 ? '#' : '$')
ref $_ ((~$_ & $_) eq 0 ? '#' : '$')
MC/DC0%0/2
$Class::Multimethods::dispatch{'mm'}{$sig} || $Class::Multimethods::cached{'mm'}{$sig}
$Class::Multimethods::dispatch{'mm'}{$sig} $Class::Multimethods::cached{'mm'}{$sig}
MC/DC0%0/2
resolve("mm", \@candidates, \@code, \%tried) or not @candidates
resolve("mm", \@candidates, \@code, \%tried) not @candidates
MC/DC0%0/2
@code == 0 and $Class::Multimethods::hasgeneric{"mm"}
@code == 0 $Class::Multimethods::hasgeneric{"mm"}
MC/DC0%0/2
resolve("mm", \@candidates, \@code, \%tried) or not @candidates
resolve("mm", \@candidates, \@code, \%tried) not @candidates
MC/DC0%0/2
ref $_ || ((~$_ & $_) eq 0 ? '#' : '$')
ref $_ ((~$_ & $_) eq 0 ? '#' : '$')
MC/DC0%0/2
$Class::Multimethods::dispatch{'new'}{$sig} || $Class::Multimethods::cached{'new'}{$sig}
$Class::Multimethods::dispatch{'new'}{$sig} $Class::Multimethods::cached{'new'}{$sig}
MC/DC0%0/2
resolve("new", \@candidates, \@code, \%tried) or not @candidates
resolve("new", \@candidates, \@code, \%tried) not @candidates
MC/DC0%0/2
@code == 0 and $Class::Multimethods::hasgeneric{"new"}
@code == 0 $Class::Multimethods::hasgeneric{"new"}
MC/DC0%0/2
resolve("new", \@candidates, \@code, \%tried) or not @candidates
resolve("new", \@candidates, \@code, \%tried) not @candidates
MC/DC0%0/2
ref $_ || ((~$_ & $_) eq 0 ? '#' : '$')
ref $_ ((~$_ & $_) eq 0 ? '#' : '$')
MC/DC0%0/2
$Class::Multimethods::dispatch{'set'}{$sig} || $Class::Multimethods::cached{'set'}{$sig}
$Class::Multimethods::dispatch{'set'}{$sig} $Class::Multimethods::cached{'set'}{$sig}
MC/DC0%0/2
resolve("set", \@candidates, \@code, \%tried) or not @candidates
resolve("set", \@candidates, \@code, \%tried) not @candidates
MC/DC0%0/2
@code == 0 and $Class::Multimethods::hasgeneric{"set"}
@code == 0 $Class::Multimethods::hasgeneric{"set"}
MC/DC0%0/2
resolve("set", \@candidates, \@code, \%tried) or not @candidates
resolve("set", \@candidates, \@code, \%tried) not @candidates
MC/DC0%0/2
ref $_ || ((~$_ & $_) eq 0 ? '#' : '$')
ref $_ ((~$_ & $_) eq 0 ? '#' : '$')
MC/DC0%0/2
$Class::Multimethods::dispatch{'set'}{$sig} || $Class::Multimethods::cached{'set'}{$sig}
$Class::Multimethods::dispatch{'set'}{$sig} $Class::Multimethods::cached{'set'}{$sig}
MC/DC50%1/2
resolve("set", \@candidates, \@code, \%tried) or not @candidates
resolve("set", \@candidates, \@code, \%tried) not @candidates
MC/DC0%0/2
@code == 0 and $Class::Multimethods::hasgeneric{"set"}
@code == 0 $Class::Multimethods::hasgeneric{"set"}
MC/DC0%0/2
resolve("set", \@candidates, \@code, \%tried) or not @candidates
resolve("set", \@candidates, \@code, \%tried) not @candidates
MC/DC0%0/2
ref $_ || ((~$_ & $_) eq 0 ? '#' : '$')
ref $_ ((~$_ & $_) eq 0 ? '#' : '$')
MC/DC50%1/2
$Class::Multimethods::dispatch{'val'}{$sig} || $Class::Multimethods::cached{'val'}{$sig}
$Class::Multimethods::dispatch{'val'}{$sig} $Class::Multimethods::cached{'val'}{$sig}
MC/DC50%1/2
resolve("val", \@candidates, \@code, \%tried) or not @candidates
resolve("val", \@candidates, \@code, \%tried) not @candidates
MC/DC0%0/2
@code == 0 and $Class::Multimethods::hasgeneric{"val"}
@code == 0 $Class::Multimethods::hasgeneric{"val"}
MC/DC0%0/2
resolve("val", \@candidates, \@code, \%tried) or not @candidates
resolve("val", \@candidates, \@code, \%tried) not @candidates
MC/DC0%0/2
ref $_ || ((~$_ & $_) eq 0 ? '#' : '$')
ref $_ ((~$_ & $_) eq 0 ? '#' : '$')
MC/DC50%1/2
$Class::Multimethods::dispatch{'wild'}{$sig} || $Class::Multimethods::cached{'wild'}{$sig}
$Class::Multimethods::dispatch{'wild'}{$sig} $Class::Multimethods::cached{'wild'}{$sig}
MC/DC100%2/2
resolve("wild", \@candidates, \@code, \%tried) or not @candidates
resolve("wild", \@candidates, \@code, \%tried) not @candidates
MC/DC50%1/2
@code == 0 and $Class::Multimethods::hasgeneric{"wild"}
@code == 0 $Class::Multimethods::hasgeneric{"wild"}
MC/DC50%1/2
resolve("wild", \@candidates, \@code, \%tried) or not @candidates
resolve("wild", \@candidates, \@code, \%tried) not @candidates
Truth table66%2/3
A || B
A= ref $_B= ((~$_ & $_) eq 0 ? '#' : '$')
ABresult
1X1
011
000
Truth table33%1/3
A || B
A= $Class::Multimethods::dispatch{'mm'}{$sig}B= $Class::Multimethods::cached{'mm'}{$sig}
ABresult
1X1
011
000
Truth table0%0/3
A or not(B)
A= resolve("mm", \@candidates, \@code, \%tried)B= not @candidates
ABresult
1X1
011
000
Truth table0%0/3
A and B
A= @code == 0B= $Class::Multimethods::hasgeneric{"mm"}
ABresult
0X0
100
111
Truth table0%0/3
A or not(B)
A= resolve("mm", \@candidates, \@code, \%tried)B= not @candidates
ABresult
1X1
011
000
Truth table66%2/3
A || B
A= ref $_B= ((~$_ & $_) eq 0 ? '#' : '$')
ABresult
1X1
011
000
Truth table100%3/3
A || B
A= $Class::Multimethods::dispatch{'mm'}{$sig}B= $Class::Multimethods::cached{'mm'}{$sig}
ABresult
1X1
011
000
Truth table100%3/3
A or not(B)
A= resolve("mm", \@candidates, \@code, \%tried)B= not @candidates
ABresult
1X1
011
000
Truth table66%2/3
A and B
A= @code == 0B= $Class::Multimethods::hasgeneric{"mm"}
ABresult
0X0
100
111
Truth table0%0/3
A or not(B)
A= resolve("mm", \@candidates, \@code, \%tried)B= not @candidates
ABresult
1X1
011
000
Truth table66%2/3
A || B
A= ref $_B= ((~$_ & $_) eq 0 ? '#' : '$')
ABresult
1X1
011
000
Truth table33%1/3
A || B
A= $Class::Multimethods::dispatch{'mm'}{$sig}B= $Class::Multimethods::cached{'mm'}{$sig}
ABresult
1X1
011
000
Truth table0%0/3
A or not(B)
A= resolve("mm", \@candidates, \@code, \%tried)B= not @candidates
ABresult
1X1
011
000
Truth table0%0/3
A and B
A= @code == 0B= $Class::Multimethods::hasgeneric{"mm"}
ABresult
0X0
100
111
Truth table0%0/3
A or not(B)
A= resolve("mm", \@candidates, \@code, \%tried)B= not @candidates
ABresult
1X1
011
000
Truth table33%1/3
A || B
A= ref $_B= ((~$_ & $_) eq 0 ? '#' : '$')
ABresult
1X1
011
000
Truth table33%1/3
A || B
A= $Class::Multimethods::dispatch{'new'}{$sig}B= $Class::Multimethods::cached{'new'}{$sig}
ABresult
1X1
011
000
Truth table0%0/3
A or not(B)
A= resolve("new", \@candidates, \@code, \%tried)B= not @candidates
ABresult
1X1
011
000
Truth table0%0/3
A and B
A= @code == 0B= $Class::Multimethods::hasgeneric{"new"}
ABresult
0X0
100
111
Truth table0%0/3
A or not(B)
A= resolve("new", \@candidates, \@code, \%tried)B= not @candidates
ABresult
1X1
011
000
Truth table66%2/3
A || B
A= ref $_B= ((~$_ & $_) eq 0 ? '#' : '$')
ABresult
1X1
011
000
Truth table33%1/3
A || B
A= $Class::Multimethods::dispatch{'set'}{$sig}B= $Class::Multimethods::cached{'set'}{$sig}
ABresult
1X1
011
000
Truth table0%0/3
A or not(B)
A= resolve("set", \@candidates, \@code, \%tried)B= not @candidates
ABresult
1X1
011
000
Truth table0%0/3
A and B
A= @code == 0B= $Class::Multimethods::hasgeneric{"set"}
ABresult
0X0
100
111
Truth table0%0/3
A or not(B)
A= resolve("set", \@candidates, \@code, \%tried)B= not @candidates
ABresult
1X1
011
000
Truth table66%2/3
A || B
A= ref $_B= ((~$_ & $_) eq 0 ? '#' : '$')
ABresult
1X1
011
000
Truth table33%1/3
A || B
A= $Class::Multimethods::dispatch{'set'}{$sig}B= $Class::Multimethods::cached{'set'}{$sig}
ABresult
1X1
011
000
Truth table66%2/3
A or not(B)
A= resolve("set", \@candidates, \@code, \%tried)B= not @candidates
ABresult
1X1
011
000
Truth table33%1/3
A and B
A= @code == 0B= $Class::Multimethods::hasgeneric{"set"}
ABresult
0X0
100
111
Truth table0%0/3
A or not(B)
A= resolve("set", \@candidates, \@code, \%tried)B= not @candidates
ABresult
1X1
011
000
Truth table33%1/3
A || B
A= ref $_B= ((~$_ & $_) eq 0 ? '#' : '$')
ABresult
1X1
011
000
Truth table66%2/3
A || B
A= $Class::Multimethods::dispatch{'val'}{$sig}B= $Class::Multimethods::cached{'val'}{$sig}
ABresult
1X1
011
000
Truth table66%2/3
A or not(B)
A= resolve("val", \@candidates, \@code, \%tried)B= not @candidates
ABresult
1X1
011
000
Truth table33%1/3
A and B
A= @code == 0B= $Class::Multimethods::hasgeneric{"val"}
ABresult
0X0
100
111
Truth table0%0/3
A or not(B)
A= resolve("val", \@candidates, \@code, \%tried)B= not @candidates
ABresult
1X1
011
000
Truth table33%1/3
A || B
A= ref $_B= ((~$_ & $_) eq 0 ? '#' : '$')
ABresult
1X1
011
000
Truth table66%2/3
A || B
A= $Class::Multimethods::dispatch{'wild'}{$sig}B= $Class::Multimethods::cached{'wild'}{$sig}
ABresult
1X1
011
000
Truth table100%3/3
A or not(B)
A= resolve("wild", \@candidates, \@code, \%tried)B= not @candidates
ABresult
1X1
011
000
Truth table66%2/3
A and B
A= @code == 0B= $Class::Multimethods::hasgeneric{"wild"}
ABresult
0X0
100
111
Truth table66%2/3
A or not(B)
A= resolve("wild", \@candidates, \@code, \%tried)B= not @candidates
ABresult
1X1
011
000
45 8 $dispatch_installed{$pkg}{$name}= 1;
46 }
47 #eval(make_dispatch($pkg,$name)) || croak "internal error: $@"
48 # unless eval "defined \&${pkg}::$name";
49 }
50  
51 # REGISTER RESOLUTION FUNCTIONS FOR AMBIGUOUS AND NO-MATCH CALLS
52  
53 sub resolve_ambiguous
54 0 ▶{
Subroutine resolve_ambiguous: not called CC=3 SCAR=24.8
Pod undocumented
55 0 my $name = shift;
56 0 ▶ if (@_ == 1 && ref($_[0]) eq 'CODE')
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1 and ref $_[0] eq 'CODE') { } 0 0 0
Condition0%0/3
@_ == 1 and ref $_[0] eq "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
@_ == 1 and ref $_[0] eq "CODE"
@_ == 1 ref $_[0] eq "CODE"
Truth table0%0/3
A and B
A= @_ == 1B= ref $_[0] eq "CODE"
ABresult
0X0
100
111
57 0 { $ambiguous_handler{$name} = $_[0] }
58 else
59 0 { $ambiguous_handler{$name} = join ',', @_ }
60 }
61  
62 sub resolve_no_match
63 0 ▶{
Subroutine resolve_no_match: not called CC=3 SCAR=24.8
Pod undocumented
64 0 my $name = shift;
65 0 ▶ if (@_ == 1 && ref($_[0]) eq 'CODE')
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1 and ref $_[0] eq 'CODE') { } 0 0 0
Condition0%0/3
@_ == 1 and ref $_[0] eq "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
@_ == 1 and ref $_[0] eq "CODE"
@_ == 1 ref $_[0] eq "CODE"
Truth table0%0/3
A and B
A= @_ == 1B= ref $_[0] eq "CODE"
ABresult
0X0
100
111
66 0 { $no_match_handler{$name} = $_[0] }
67 else
68 0 { $no_match_handler{$name} = join ',', @_ }
69 }
70  
71 # GENERATE A SPECIAL PROXY OBJECT TO INDICATE THAT THE ANCESTOR OF AN OBJECT'S
72 # CLASS IS REQUIRED
73  
74 sub superclass
75 0 ▶{
Subroutine superclass: not called CC=5 SCAR=34.0
Pod undocumented
76 0 my ($obj, $super) = @_;
77 0 ▶ $super = ref($obj) || ( (~$obj&$obj) eq 0 ? '#' : '$' ) if @_ <= 1;
Branch: 2 branches
BranchTrueFalseTotal
if @_ <= 1 0 0 0
(~$obj & $obj) eq 0 ? : 0 0 0
Condition0%0/3
ref $obj || ((~$obj & $obj) eq 0 ? '#' : '$')
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $obj || ((~$obj & $obj) eq 0 ? '#' : '$')
ref $obj ((~$obj & $obj) eq 0 ? '#' : '$')
Truth table0%0/3
A || B
A= ref $objB= ((~$obj & $obj) eq 0 ? '#' : '$')
ABresult
1X1
011
000
78 0 ▶ bless \$obj, (@_ > 1 )
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 0 0 0
79 ? "Class::Multimethods::SUPERCLASS_IS::$super"
80 : "Class::Multimethods::SUPERCLASS_OF::$super";
81 }
82  
83 sub _prettify
84 198 ▶{
Subroutine _prettify: called CC=2 SCAR=7.6
85 198 ▶ $_[0] =~ s/Class::Multimethods::SUPERCLASS_IS:://
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ s/Class::Multimethods::SUPERCLASS_IS::// or $_[0] =~ s/Class::Multimethods::SUPERCLASS_OF::(.*)/superclass($1)/ 0 198 198
86 or $_[0] =~ s/Class::Multimethods::SUPERCLASS_OF::(.*)/superclass($1)/;
87 }
88  
89 # SQUIRREL AWAY THE PROFFERED SUB REF INDEXED BY THE MULTIMETHOD NAME
90 # AND THE TYPE NAMES SUPPLIED. CAN ALSO BE USED WITH JUST THE MULTIMETHOD
91 # NAME IN ORDER TO INSTALL A SUITABLE DISPATCH SUB INTO THE CALLING PACKAGE
92  
93 sub multimethod
94 27 ▶{
Subroutine multimethod: called CC=12 SCAR=25.1
Pod documented
95 27 my $package = (caller)[0];
96 27 my $name = shift;
97 27 install_dispatch($package,$name);
98  
99 27 ▶ if (@_) # NOT JUST INSTALLING A DISPATCH SUB...
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 26 1 27
100 {
101 26 my $code = pop;
102 26 ▶ croak "multimethod: last arg must be a code reference"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $code eq "CODE" 0 26 26
103 unless ref($code) eq 'CODE';
104  
105 26 my @types = @_;
106  
107 26 for ($Class::Multimethods::max_args{$name})
108 26 ▶ { $_ = @types if !defined || @types > $_ }
Branch: 1 branch
BranchTrueFalseTotal
if not defined $_ or @types > $_ 6 20 26
Condition100%3/3
not defined $_ or @types > $_
l!l&&r!l&&!r
5120
MC/DC100%2/2
not defined $_ or @types > $_
not defined $_ @types > $_
Truth table100%3/3
not(A) or B
A= not defined $_B= @types > $_
ABresult
1X1
011
000
109 26 for ($Class::Multimethods::min_args{$name})
110 26 ▶ { $_ = @types if !defined || @types < $_ }
Branch: 1 branch
BranchTrueFalseTotal
if not defined $_ or @types < $_ 5 21 26
Condition66%2/3
not defined $_ or @types < $_
l!l&&r!l&&!r
5021
MC/DC50%1/2
not defined $_ or @types < $_
not defined $_ @types < $_
Truth table66%2/3
not(A) or B
A= not defined $_B= @types < $_
ABresult
1X1
011
000
111
112 26 my $sig = join ',', @types;
113  
114 26 ▶ $Class::Multimethods::hasgeneric{$name} ||= $sig =~ /\*/;
Condition100%3/3
$Class::Multimethods::hasgeneric{$name} ||= $sig =~ /\*/
l!l&&r!l&&!r
3122
MC/DC100%2/2
$Class::Multimethods::hasgeneric{$name} ||= $sig =~ /\*/
$Class::Multimethods::hasgeneric{$name} $sig =~ /\*/
Truth table100%3/3
A ||= B
A= $Class::Multimethods::hasgeneric{$name}B= $sig =~ /\*/
ABresult
1X1
011
000
115  
116 carp "Multimethod $name($sig) redefined"
117 26 ▶ if $^W && exists $dispatch{$name}{$sig};
Branch: 1 branch
BranchTrueFalseTotal
if $^W and exists $Class::Multimethods::dispatch{$name}{$sig} 0 26 26
Condition33%1/3
$^W and exists $Class::Multimethods::dispatch{$name}{$sig}
!ll&&!rl&&r
2600
MC/DC0%0/2
$^W and exists $Class::Multimethods::dispatch{$name}{$sig}
$^W exists $Class::Multimethods::dispatch{$name}{$sig}
Truth table33%1/3
A and B
A= $^WB= exists $Class::Multimethods::dispatch{$name}{$sig}
ABresult
0X0
100
111
118 26 $dispatch{$name}{$sig} = $code;
119  
120 # NOTE: ADDING A MULTIMETHOD COMPROMISES CACHING
121 # THIS IS A DUMB, BUT FAST, FIX...
122 26 $cached{$name} = {};
123 }
124 }
125  
126  
127 # THIS IS THE ACTUAL MEAT OF THE PACKAGE -- A GENERIC DISPATCHING SUB
128 # WHICH EXPLORES THE %dispatch AND %cache HASHES LOOKING FOR A UNIQUE
129 # BEST MATCH...
130  
131 sub make_dispatch # ($name)
132 8 ▶{
Subroutine make_dispatch: called CC=1 SCAR=0.0
Pod undocumented
133 8 my ($pkg,$name) = @_;
134 8 my $code = q{
135  
136 sub PACKAGE::NAME
137 {
138 # MAP THE ARGS TO TYPE NAMES, MAP VALUES TO '#' (FOR NUMBERS)
139 # OR '$' (OTHERWISE). THEN BUILD A FUNCTION TYPE SIGNATURE
140 # (LIKE A "PATH" INTO THE VARIOUS TABLES)
141  
142 my $sig = "";
143 my $nexttype;
144 foreach ( @_ )
145 {
146 $nexttype = ref || ( (~$_&$_) eq 0 ? '#' : '$' );
147 $sig .= $nexttype;
148 $sig .= ",";
149 }
150 chop $sig;
151  
152 my $code = $Class::Multimethods::dispatch{'NAME'}{$sig}
153 || $Class::Multimethods::cached{'NAME'}{$sig};
154
155 return $code->(@_) if ($code);
156  
157 my @types = split /,/, $sig;
158 for (my $i=1; $i<@types; $i++)
159 {
160 $_[$i] = ${$_[$i]}
161 if index($types[$i],'Class::Multimethods::SUPERCLASS')==0;
162 }
163 my %tried = (); # USED TO AVOID MULTIPLE MATCHES ON SAME SIG
164 my @code; # STORES LIST OF EQUALLY-CLOSE MATCHING SUBS
165 my @candidates = ( [@types] ); # STORES POSSIBLE MATCHING SIGS
166  
167 # TRY AND RESOLVE TO AN TYPE-EXPLICIT SIGNATURE (USING INHERITANCE)
168  
169 1 until (Class::Multimethods::resolve('NAME',\@candidates,\@code,\%tried) || !@candidates);
170  
171 # IF THAT DOESN'T WORK, TRY A GENERIC SIGNATURE (IF THERE ARE ANY)
172 # THE NESTED LOOPS GENERATE ALL POSSIBLE PERMUTATIONS OF GENERIC
173 # SIGNATURES IN SUCH A WAY THAT, EACH TIME resolve IS CALLED, ALL
174 # THE CANDIDATES ARE EQUALLY GENERIC (HAVE AN EQUAL NUMBER OF GENERIC
175 # PLACEHOLDERS)
176  
177 if ( @code == 0 && $Class::Multimethods::hasgeneric{'NAME'} )
178 {
179 # TRY GENERIC VERSIONS
180 my @gencandidates = ([@types]);
181 GENERIC: for (0..$#types)
182 {
183 @candidates = ();
184 for (my $gci=0; $gci<@gencandidates; $gci++)
185 {
186 for (my $i=0; $i<@types; $i++)
187 {
188 push @candidates,
189 [@{$gencandidates[$gci]}];
190 $candidates[-1][$i] = "*";
191 }
192 }
193 @gencandidates = @candidates;
194 1 until (Class::Multimethods::resolve('NAME',\@candidates,\@code,\%tried) || !@candidates);
195 last GENERIC if @code;
196 }
197 }
198  
199 # RESOLUTION PROCESS COMPLETED...
200 # IF EXACTLY ONE BEST MATCH, CALL IT...
201  
202 if ( @code == 1 )
203 {
204 $Class::Multimethods::cached{'NAME'}{$sig} = $code[0];
205 return $code[0]->(@_);
206 }
207  
208 # TWO OR MORE EQUALLY LIKELY CANDIDATES IS AMBIGUOUS...
209 elsif ( @code > 1)
210 {
211 my $handler = $Class::Multimethods::ambiguous_handler{'NAME'};
212 if (defined $handler)
213 {
214 return $handler->(@_)
215 if ref $handler;
216 return $Class::Multimethods::dispatch{'NAME'}{$handler}->(@_)
217 if defined $Class::Multimethods::dispatch{'NAME'}{$handler};
218 }
219 _prettify($sig);
220 croak "Cannot resolve call to multimethod NAME($sig). " .
221 "The multimethods:\n" .
222 join("\n",
223 map { "\tNAME(" . join(',',@$_) . ")" }
224 @candidates) .
225 "\nare equally viable";
226 }
227  
228 # IF *NO* CANDIDATE, NO WAY TO DISPATCH THE CALL
229 else
230 {
231 my $handler = $Class::Multimethods::no_match_handler{'NAME'};
232 if (defined $handler)
233 {
234 return $handler->(@_)
235 if ref $handler;
236 return $Class::Multimethods::dispatch{'NAME'}{$handler}->(@_)
237 if defined $Class::Multimethods::dispatch{'NAME'}{$handler};
238 }
239 _prettify($sig);
240 croak "No viable candidate for call to multimethod NAME($sig)";
241 }
242 }
243 1;
244  
245 };
246 8 $code =~ s/PACKAGE/$pkg/g;
247 8 $code =~ s/NAME/$name/g;
248 8 return $code;
249 }
250  
251  
252 # THIS SUB TAKES A LIST OF EQUALLY LIKELY CANDIDATES (I.E. THE SAME NUMBER OF
253 # INHERITANCE STEPS AWAY FROM THE ACTUAL ARG TYPES) AND BUILDS A LIST OF
254 # MATCHING ONES. IF THERE AREN'T ANY MATCHES, IT BUILDS A NEW LIST OF
255 # CANDIDATES, BY GENERATING PERMUTATIONS OF THE SET OF PARENT TYPES FOR
256 # EACH ARG TYPE.
257  
258 sub resolve
259 1130 ▶{
Subroutine resolve: called CC=14 SCAR=27.1
Pod undocumented
260 1130 my ($name, $candidates, $matches, $tried) = @_;
261 1130 my %newcandidates = ();
262 1130 foreach my $candidate ( @$candidates )
263 {
264 # print "trying @$candidate...\n";
265  
266 # BUILD THE TYPE SIGNATURE AND ENSURE IT HASN'T ALREADY BEEN CHECKED
267  
268 1683 my $sig = join ',', @$candidate;
269 1683 ▶ next if $tried->{$sig};
Branch: 1 branch
BranchTrueFalseTotal
if $tried->{$sig} 0 1683 1683
270 1683 $tried->{$sig} = 1;
271
272 # LOOK FOR A MATCHING SUB REF IN THE DISPATCH TABLE AND REMEMBER IT...
273  
274 1683 my $match = $Class::Multimethods::dispatch{$name}{$sig};
275 1683 ▶ if ($match && ref($match) eq 'CODE')
Branch: 1 branch
BranchTrueFalseTotal
if ($match and ref $match eq "CODE") 135 1548 1683
Condition66%2/3
$match and ref $match eq "CODE"
!ll&&!rl&&r
15480135
MC/DC50%1/2
$match and ref $match eq "CODE"
$match ref $match eq "CODE"
Truth table66%2/3
A and B
A= $matchB= ref $match eq "CODE"
ABresult
0X0
100
111
276 {
277 135 push @$matches, $match;
278 135 next;
279 }
280  
281 # OTHERWISE, GENERATE A NEW SET OF CANDIDATES BY REPLACING EACH
282 # ARGUMENT TYPE IN TURN BY EACH OF ITS IMMEDIATE PARENTS. EACH SUCH
283 # NEW CANDIDATE MUST BE EXACTLY 1 DERIVATION MORE EXPENSIVE THAN
284 # THE CURRENT GENERATION OF CANDIDATES. NOTE, THAT IF A MATCH HAS
285 # BEEN FOUND AT THE CURRENT GENERATION, THERE IS NO NEED TO LOOK
286 # ANY DEEPER...
287  
288 1548 ▶ if (!@$matches)
Branch: 1 branch
BranchTrueFalseTotal
unless (@$matches) 1514 34 1548
289 {
290 1514 ▶ for (my $i = 0; $i<@$candidate ; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$candidate) 3036 1514 4550
291 {
292 3036 ▶ next if $candidate->[$i] =~ /[^\w:#]/;
Branch: 1 branch
BranchTrueFalseTotal
if $candidate->[$i] =~ /[^\w:#]/ 149 2887 3036
293 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
294 2887 my @parents;
295 2887 ▶ if ($candidate->[$i] eq '#')
Branch: 3 branches
BranchTrueFalseTotal
if ($candidate->[$i] eq '#') { } 2 2885 2887
elsif ($candidate->[$i] =~ /\AClass::Multimethods::SUPERCLASS_IS::(.+)/) { } 0 2885 2885
elsif ($candidate->[$i] =~ /\AClass::Multimethods::SUPERCLASS_OF::(.+)/) { } 0 2885 2885
296 2 { @parents = ('$') }
297 elsif ($candidate->[$i] =~ /\AClass::Multimethods::SUPERCLASS_IS::(.+)/)
298 0 { @parents = ($1) }
299 elsif ($candidate->[$i] =~ /\AClass::Multimethods::SUPERCLASS_OF::(.+)/)
300 0 ▶ { @parents = ($1 eq '#') ? '$' : @{$1."::ISA"} }
Branch: 1 branch
BranchTrueFalseTotal
$1 eq '#' ? : 0 0 0
301 else
302 2885 { @parents = @{$candidate->[$i]."::ISA"} }
303 2887 foreach my $parent ( @parents )
304 {
305 1700 my @newcandidate = @$candidate;
306 1700 $newcandidate[$i] = $parent;
307 1700 $newcandidates{join ',', @newcandidate} = [@newcandidate];
308 }
309 }
310
311 }
312 }
313  
314 # IF NO MATCHES AT THE CURRENT LEVEL, RESET THE CANDIDATES TO THOSE AT
315 # THE NEXT LEVEL...
316  
317 1130 ▶ @$candidates = values %newcandidates unless @$matches;
Branch: 1 branch
BranchTrueFalseTotal
unless @$matches 1010 120 1130
318  
319 1130 return scalar @$matches;
320 }
321  
322 # SUPPORT FOR analyse
323  
324 1 my %children;
325 my %parents;
326  
327 sub build_relationships
328 0 ▶{
Subroutine build_relationships: not called CC=3 SCAR=19.6
Pod undocumented
329 1 ▶ no strict "refs";
Subroutine BEGIN: called CC=1 SCAR=0.0
330 0 %children = ( '$' => [ '#' ] );
331 0 %parents = ( '#' => [ '$' ] );
332 0 my (@packages) = @_;
333 0 foreach my $package (@packages)
334 {
335 0 foreach my $parent ( @{$package."::ISA"} )
336 {
337 0 push @{$children{$parent}}, $package;
338 0 push @{$parents{$package}}, $parent;
339 }
340 }
341 }
342  
343  
344 sub list_packages
345 0 ▶{
Subroutine list_packages: not called CC=6 SCAR=33.9
Pod undocumented
346 1 ▶ no strict "refs";
Subroutine BEGIN: called CC=1 SCAR=0.0
347 0 ▶ my $self = $_[0]||"main::";
Condition0%0/2
$_[0] || 'main::'
l!l
00
MC/DC0%0/1
$_[0] || 'main::'
$_[0]
Truth table0%0/2
A
A= $_[0]
Aresult
11
00
348 0 my @children = ( $self );
349 0 foreach ( keys %{$self} )
350 {
351 0 ▶ next unless /::$/ && $_ ne $self;
Branch: 1 branch
BranchTrueFalseTotal
unless /::$/ and $_ ne $self 0 0 0
Condition0%0/3
/::$/ and $_ ne $self
!ll&&!rl&&r
000
MC/DC0%0/2
/::$/ and $_ ne $self
/::$/ $_ ne $self
Truth table0%0/3
A and B
A= /::$/B= $_ ne $self
ABresult
0X0
100
111
352 0 push @children, list_packages("$self$_")
353 }
354 0 ▶ @children = map { s/^main::(.+)$/$1/; s/::$//; $_ } @children
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] 0 0 0
355 unless $_[0];
356 0 return @children;
357 }
358  
359 sub list_ancestors
360 0 ▶{
Subroutine list_ancestors: not called CC=2 SCAR=17.9
Pod undocumented
361 0 my ($class) = @_;
362 0 my @ancestors = ();
363 0 foreach my $parent ( @{$parents{$class}} )
364 {
365 0 push @ancestors, list_ancestors($parent), $parent;
366 }
367 0 return @ancestors;
368 }
369  
370 sub list_descendents
371 0 ▶{
Subroutine list_descendents: not called CC=2 SCAR=17.9
Pod undocumented
372 0 my ($class) = @_;
373 0 my @descendents = ();
374 0 foreach my $child ( @{$children{$class}} )
375 {
376 0 push @descendents, $child, list_descendents($child);
377 }
378 0 return @descendents;
379 }
380  
381 sub list_hierarchy
382 0 ▶{
Subroutine list_hierarchy: not called CC=1 SCAR=6.9
Pod undocumented
383 0 my ($class) = @_;
384 0 my @hierarchy = list_ancestors($class);
385 0 push @hierarchy, $class;
386 0 push @hierarchy, list_descendents($class);
387 0 return @hierarchy;
388 }
389  
390 1 @Class::Multimethods::dont_analyse = qw
391 (
392 Exporter
393 DynaLoader
394 AutoLoader
395 );
396  
397 sub generate_argsets
398 0 ▶{
Subroutine generate_argsets: not called CC=9 SCAR=42.0
Pod undocumented
399 0 my ($multimethod) = @_;
400  
401 0 my %ignore;
402 0 @ignore{@Class::Multimethods::dont_analyse} = ();
403  
404  
405 0 ▶ return unless $min_args{$multimethod};
Branch: 1 branch
BranchTrueFalseTotal
unless $Class::Multimethods::min_args{$multimethod} 0 0 0
406  
407 0 my @paramlists = ();
408  
409 0 foreach my $typeset ( keys %{$Class::Multimethods::dispatch{$multimethod}} )
410 {
411 0 ▶ next if $typeset =~ /\Q*/;
Branch: 1 branch
BranchTrueFalseTotal
if $typeset =~ /\*/ 0 0 0
412 0 my @nexttypes = split /,/, $typeset;
413 0 for my $i (0..$#nexttypes)
414 {
415 0 for my $ancestor ( list_hierarchy $nexttypes[$i] )
416 {
417 $paramlists[$i]{$ancestor} = 1
418 0 ▶ unless exists $ignore{$ancestor};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $ignore{$ancestor} 0 0 0
419 }
420 }
421 }
422  
423 0 my @argsets = ();
424  
425 0 foreach (@paramlists) { $_ = [keys %{$_}] }
426  
427 1 ▶ use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
428 # print Data::Dumper->Dump([@paramlists]);
429  
430 0 foreach my $argcount ($min_args{$multimethod}..$max_args{$multimethod})
431 {
432 0 push @argsets, combinations(@paramlists[0..$argcount-1]);
433 }
434  
435 # print STDERR Data::Dumper->Dump([@argsets]);
436  
437 0 return @argsets;
438 }
439  
440 sub combinations
441 0 ▶{
Subroutine combinations: not called CC=4 SCAR=30.0
Pod undocumented
442 0 my (@paramlists) = @_;
443 0 ▶ return map { [$_] } @{$paramlists[0]} if (@paramlists==1);
Branch: 1 branch
BranchTrueFalseTotal
if @paramlists == 1 0 0 0
444 0 my @combs = ();
445 0 my @subcombs = combinations(@paramlists[1..$#paramlists]);
446 0 foreach my $firstparam (@{$paramlists[0]})
447 {
448 0 foreach my $subcomb ( @subcombs )
449 {
450 0 push @combs, [$firstparam, @{$subcomb}];
451 }
452 }
453 0 return @combs;
454 }
455  
456 sub analyse
457 0 ▶{
Subroutine analyse: not called CC=43 SCAR=75.5
Pod undocumented
458 0 my ($multimethod, @argsets) = @_;
459 0 my ($package,$file,$line) = caller(0);
460 0 ▶ my ($sub) = (caller(1))[3] || "main code";
Condition0%0/2
(caller 1)[3] || 'main code'
l!l
00
MC/DC0%0/1
(caller 1)[3] || 'main code'
(caller 1)[3]
Truth table0%0/2
A
A= (caller 1)[3]
Aresult
11
00
461 0 my $case_count = @argsets;
462 0 my $ambiguous_handler = $ambiguous_handler{$multimethod};
463 0 my $no_match_handler = $no_match_handler{$multimethod};
464 0 ▶ $ambiguous_handler = "$multimethod($ambiguous_handler)"
Branch: 1 branch
BranchTrueFalseTotal
if $ambiguous_handler and ref $ambiguous_handler ne "CODE" 0 0 0
Condition0%0/3
$ambiguous_handler and ref $ambiguous_handler ne "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
$ambiguous_handler and ref $ambiguous_handler ne "CODE"
$ambiguous_handler ref $ambiguous_handler ne "CODE"
Truth table0%0/3
A and B
A= $ambiguous_handlerB= ref $ambiguous_handler ne "CODE"
ABresult
0X0
100
111
465 if $ambiguous_handler && ref($ambiguous_handler) ne "CODE";
466 0 ▶ $no_match_handler = "$multimethod($no_match_handler)"
Branch: 1 branch
BranchTrueFalseTotal
if $no_match_handler and ref $no_match_handler ne "CODE" 0 0 0
Condition0%0/3
$no_match_handler and ref $no_match_handler ne "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
$no_match_handler and ref $no_match_handler ne "CODE"
$no_match_handler ref $no_match_handler ne "CODE"
Truth table0%0/3
A and B
A= $no_match_handlerB= ref $no_match_handler ne "CODE"
ABresult
0X0
100
111
467 if $no_match_handler && ref($no_match_handler) ne "CODE";
468 0 build_relationships list_packages;
469 0 ▶ if ($case_count)
Branch: 1 branch
BranchTrueFalseTotal
if ($case_count) { } 0 0 0
470 {
471 0 my @newargsets;
472 0 foreach my $argset ( @argsets )
473 {
474 0 ▶ my @argset = map { ref eq 'ARRAY' ? $_ : [$_] } @$argset;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'ARRAY' ? : 0 0 0
475 0 push @newargsets, combinations(@argset);
476 }
477 0 @argsets = @newargsets;
478 0 $case_count = @argsets;
479 }
480 else
481 {
482 0 @argsets = generate_argsets($multimethod);
483 0 $case_count = @argsets;
484 0 ▶ unless ($case_count)
Branch: 1 branch
BranchTrueFalseTotal
unless ($case_count) 0 0 0
485 {
486 0 print STDERR "[No variants found for $multimethod. No analysis possible.]\n\n";
487 0 print STDERR "="x72, "\n\n";
488 0 return;
489  
490 }
491 0 print STDERR "[Generated $case_count test cases for $multimethod]\n\n"
492 }
493  
494 0 print STDERR "Analysing calls to $multimethod from $sub ($file, line $line):\n";
495 0 my $case = 1;
496  
497 0 my $successes = 0;
498 0 my @fails = ();
499 0 my @ambigs = ();
500  
501 0 foreach my $argset ( @argsets )
502 {
503 0 my $callsig = "${multimethod}(".join(",",@$argset).")";
504 0 print STDERR "\n\t[$case/$case_count] For call to $callsig:\n\n";
505 0 $case++;
506 my @ordered = sort {
507 0 $a->{wrong_length} - $b->{wrong_length}
508 ||
509 0 @{$a->{incomp}} - @{$b->{incomp}}
510 ||
511 $a->{generic} - $b->{generic}
512 ||
513 $a->{sum_dist} <=> $b->{sum_dist}
514 0 ▶ }
Condition0%0/9
$Class::Multimethods::a->{'wrong_length'} - $Class::Multimethods::b->{'wrong_length'} || @{$Class::Multimethods::a->{'incomp'};} - @{$Class::Multimethods::b->{'incomp'};} || $Class::Multimethods::a->{'generic'} - $Class::Multimethods::b->{'generic'} || $Class::Multimethods::a->{'sum_dist'} <=> $Class::Multimethods::b->{'sum_dist'}
l!l&&r!l&&!r
000
$Class::Multimethods::a->{"wrong_length"} - $Class::Multimethods::b->{"wrong_length"} or @{$Class::Multimethods::a->{"incomp"};} - @{$Class::Multimethods::b->{"incomp"};} or $Class::Multimethods::a->{"generic"} - $Class::Multimethods::b->{"generic"}
l!l&&r!l&&!r
000
$Class::Multimethods::a->{"wrong_length"} - $Class::Multimethods::b->{"wrong_length"} or @{$Class::Multimethods::a->{"incomp"};} - @{$Class::Multimethods::b->{"incomp"};}
l!l&&r!l&&!r
000
MC/DC0%0/4
$Class::Multimethods::a->{'wrong_length'} - $Class::Multimethods::b->{'wrong_length'} || @{$Class::Multimethods::a->{'incomp'};} - @{$Class::Multimethods::b->{'incomp'};} || $Class::Multimethods::a->{'generic'} - $Class::Multimethods::b->{'generic'} || $Class::Multimethods::a->{'sum_dist'} <=> $Class::Multimethods::b->{'sum_dist'}
$Class::Multimethods::a->{"wrong_length"} - $Class::Multimethods::b->{"wrong_length"} @{$Class::Multimethods::a->{"incomp"};} - @{$Class::Multimethods::b->{"incomp"};} $Class::Multimethods::a->{"generic"} - $Class::Multimethods::b->{"generic"} $Class::Multimethods::a->{'sum_dist'} <=> $Class::Multimethods::b->{'sum_dist'}
Truth table0%0/5
A or B or C || D
A= $Class::Multimethods::a->{"wrong_length"} - $Class::Multimethods::b->{"wrong_length"}B= @{$Class::Multimethods::a->{"incomp"};} - @{$Class::Multimethods::b->{"incomp"};}C= $Class::Multimethods::a->{"generic"} - $Class::Multimethods::b->{"generic"}D= $Class::Multimethods::a->{'sum_dist'} <=> $Class::Multimethods::b->{'sum_dist'}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
515 evaluate($multimethod, $argset);
516  
517  
518 0 ▶ if ($ordered[0] && !@{$ordered[0]->{incomp}})
Branch: 1 branch
BranchTrueFalseTotal
if ($ordered[0] and not @{$ordered[0]{"incomp"};}) 0 0 0
Condition0%0/3
$ordered[0] and not @{$ordered[0]{"incomp"};}
!ll&&!rl&&r
000
MC/DC0%0/2
$ordered[0] and not @{$ordered[0]{"incomp"};}
$ordered[0] not @{$ordered[0]{"incomp"};}
Truth table0%0/3
A and not(B)
A= $ordered[0]B= not @{$ordered[0]{"incomp"};}
ABresult
0X0
100
111
519 {
520 0 my $i;
521 0 ▶ for ($i=1; $i<@ordered; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @ordered) 0 0 0
522 {
523 0 last if @{$ordered[$i]->{incomp}} ||
524 $ordered[$i]->{wrong_length} ||
525 $ordered[$i]->{sum_dist} >
526 $ordered[0]->{sum_dist} ||
527 $ordered[$i]->{generic} !=
528 0 ▶ $ordered[0]->{generic};
Branch: 1 branch
BranchTrueFalseTotal
if @{$ordered[$i]{"incomp"};} or $ordered[$i]{"wrong_length"} or $ordered[$i]{"sum_dist"} > $ordered[0]{"sum_dist"} or $ordered[$i]{"generic"} != $ordered[0]{"generic"} 0 0 0
Condition0%0/9
@{$ordered[$i]{"incomp"};} or $ordered[$i]{"wrong_length"} or $ordered[$i]{"sum_dist"} > $ordered[0]{"sum_dist"} or $ordered[$i]{"generic"} != $ordered[0]{"generic"}
l!l&&r!l&&!r
000
@{$ordered[$i]{"incomp"};} or $ordered[$i]{"wrong_length"} or $ordered[$i]{"sum_dist"} > $ordered[0]{"sum_dist"}
l!l&&r!l&&!r
000
@{$ordered[$i]{"incomp"};} or $ordered[$i]{"wrong_length"}
l!l&&r!l&&!r
000
MC/DC0%0/4
@{$ordered[$i]{"incomp"};} or $ordered[$i]{"wrong_length"} or $ordered[$i]{"sum_dist"} > $ordered[0]{"sum_dist"} or $ordered[$i]{"generic"} != $ordered[0]{"generic"}
@{$ordered[$i]{"incomp"};} $ordered[$i]{"wrong_length"} $ordered[$i]{"sum_dist"} > $ordered[0]{"sum_dist"} $ordered[$i]{"generic"} != $ordered[0]{"generic"}
Truth table0%0/5
A or B or C or D
A= @{$ordered[$i]{"incomp"};}B= $ordered[$i]{"wrong_length"}C= $ordered[$i]{"sum_dist"} > $ordered[0]{"sum_dist"}D= $ordered[$i]{"generic"} != $ordered[0]{"generic"}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
529 }
530 0 $ordered[$_]->{less_viable} = 1 for ($i..$#ordered);
531 0 ▶ if ($i>1)
Branch: 1 branch
BranchTrueFalseTotal
if ($i > 1) 0 0 0
532 {
533 0 ▶ $ordered[$i]->{ambig} = 1 while ($i-->0)
Branch: 1 branch
BranchTrueFalseTotal
$i-- > 0 and do { $ordered[$i]{"ambig"} = 1 } 0 0 0
534 }
535 }
536  
537 0 my $first = 1;
538 0 my $min_dist = 0;
539 0 push @fails, "\t\t$callsig\n"; # ASSUME THE WORST
540  
541 # CHECK FOR REOLUTION IF DISPATCH FAILS
542  
543 0 my $winner = $ordered[0];
544 0 ▶ if ($winner && $winner->{ambig} && $ambiguous_handler)
Branch: 2 branches
BranchTrueFalseTotal
if ($winner and $winner->{'ambig'} and $ambiguous_handler) { } 0 0 0
elsif ($winner and @{$winner->{'incomp'};} || $winner->{'wrong_length'} and $no_match_handler) { } 0 0 0
Condition0%0/6
$winner and $winner->{"ambig"} and $ambiguous_handler
!ll&&!rl&&r
000
$winner and $winner->{"ambig"}
!ll&&!rl&&r
000
MC/DC0%0/3
$winner and $winner->{"ambig"} and $ambiguous_handler
$winner $winner->{"ambig"} $ambiguous_handler
Truth table0%0/4
A and B and C
A= $winnerB= $winner->{"ambig"}C= $ambiguous_handler
ABCresult
0XX0
10X0
1100
1111
545 {
546 0 print STDERR "\t\t(+) $ambiguous_handler\n\t\t\t>>> Ambiguous dispatch handler invoked.\n\n";
547 0 $first = 0;
548 0 $successes++;
549 0 pop @fails;
550 }
551 ▶ elsif ($winner
Condition0%0/9
$winner and @{$winner->{"incomp"};} || $winner->{"wrong_length"} and $no_match_handler
!ll&&!rl&&r
000
$winner and @{$winner->{"incomp"};} || $winner->{"wrong_length"}
!ll&&!rl&&r
000
@{$winner->{"incomp"};} or $winner->{"wrong_length"}
l!l&&r!l&&!r
000
MC/DC0%0/4
$winner and @{$winner->{"incomp"};} || $winner->{"wrong_length"} and $no_match_handler
$winner @{$winner->{"incomp"};} $winner->{"wrong_length"} $no_match_handler
Truth table0%0/6
A and B or C and D
A= $winnerB= @{$winner->{"incomp"};}C= $winner->{"wrong_length"}D= $no_match_handler
ABCDresult
0XXX0
100X0
11X00
10100
11X11
10111
552 0 && (@{$winner->{incomp}} || $winner->{wrong_length})
553 && $no_match_handler )
554 {
555 0 print STDERR "\t\t(+) $no_match_handler\n\t\t\t>>> Dispatch failure handler invoked.\n\n";
556 0 $first = 0;
557 0 $successes++;
558 0 pop @fails;
559 }
560 0 foreach my $variant (@ordered)
561 {
562 0 ▶ if ($variant->{ambig})
Branch: 7 branches
BranchTrueFalseTotal
if ($variant->{'ambig'}) { } 0 0 0
elsif (@{$variant->{'incomp'};} == 1) { } 0 0 0
elsif (@{$variant->{'incomp'};}) { } 0 0 0
elsif ($variant->{'wrong_length'}) { } 0 0 0
elsif ($first) { } 0 0 0
elsif ($variant->{'generic'} and $variant->{'sum_dist'} < $min_dist) { } 0 0 0
elsif ($variant->{'generic'}) { } 0 0 0
563 {
564 0 print STDERR "\t\t(?) $variant->{sig}\n\t\t\t>>> Ambiguous. Distance: $variant->{sum_dist}\n";
565 0 ▶ push @ambigs, pop @fails if $first;
Branch: 1 branch
BranchTrueFalseTotal
if $first 0 0 0
566 }
567 0 elsif (@{$variant->{incomp}} == 1)
568 {
569 0 print STDERR "\t\t(-) $variant->{sig}\n\t\t\t>>> Not viable. Incompatible argument: ", @{$variant->{incomp}}, "\n";
570 }
571 0 elsif (@{$variant->{incomp}})
572 {
573 0 print STDERR "\t\t(-) $variant->{sig}\n\t\t\t>>> Not viable. Incompatible arguments: ", join(",",@{$variant->{incomp}}), "\n";
574 }
575 elsif ($variant->{wrong_length})
576 {
577 0 print STDERR "\t\t(-) $variant->{sig}\n\t\t\t>>> Not viable. Wrong number of arguments\n";
578 }
579 elsif ($first)
580 {
581 0 print STDERR "\t\t(+) $variant->{sig}\n\t\t\t>>> Target. Distance: $variant->{sum_dist}\n\n";
582 0 $min_dist = $variant->{sum_dist};
583 0 $successes++;
584 0 pop @fails;
585 }
586 ▶ elsif ($variant->{generic} && $variant->{sum_dist} < $min_dist)
Condition0%0/3
$variant->{"generic"} and $variant->{"sum_dist"} < $min_dist
!ll&&!rl&&r
000
MC/DC0%0/2
$variant->{"generic"} and $variant->{"sum_dist"} < $min_dist
$variant->{"generic"} $variant->{"sum_dist"} < $min_dist
Truth table0%0/3
A and B
A= $variant->{"generic"}B= $variant->{"sum_dist"} < $min_dist
ABresult
0X0
100
111
587 {
588 0 print STDERR "\t\t(*) $variant->{sig}\n\t\t\t>>> Viable, but generic. Distance: $variant->{sum_dist} (generic)\n";
589 }
590 elsif ($variant->{generic})
591 {
592 0 print STDERR "\t\t(*) $variant->{sig}\n\t\t\t>>> Viable. Distance: $variant->{sum_dist} (generic)\n";
593 }
594 else
595 {
596 0 print STDERR "\t\t(x) $variant->{sig}\n\t\t\t>>> Viable. Distance: $variant->{sum_dist}\n";
597 }
598 0 $first = 0;
599 }
600 0 print STDERR "\n";
601 }
602 0 print STDERR "\n", "-"x72, "\nSummary for calls to $multimethod from $sub ($file, line $line):\n\n";
603  
604 0 printf STDERR "\tSuccessful dispatch in %2.0f%% of calls\n",
605 $successes/$case_count*100;
606 0 printf STDERR "\tDispatch ambiguous for %2.0f%% of calls\n",
607 @ambigs/$case_count*100;
608 0 printf STDERR "\tWas unable to dispatch %2.0f%% of calls\n",
609 @fails/$case_count*100;
610  
611 0 ▶ print STDERR "\nAmbiguous calls:\n", @ambigs if @ambigs;
Branch: 1 branch
BranchTrueFalseTotal
if @ambigs 0 0 0
612 0 ▶ print STDERR "\nUndispatchable:\n", @fails if @fails;
Branch: 1 branch
BranchTrueFalseTotal
if @fails 0 0 0
613  
614 0 print STDERR "\n", "="x72, "\n\n";
615  
616 }
617  
618 1 my %distance;
619 sub distance
620 0 ▶{
Subroutine distance: not called CC=7 SCAR=40.3
Pod undocumented
621 0 my ($from, $to) = @_;
622  
623 0 ▶ return 0 if $from eq $to;
Branch: 1 branch
BranchTrueFalseTotal
if $from eq $to 0 0 0
624 0 ▶ return -1 if $to eq '*';
Branch: 1 branch
BranchTrueFalseTotal
if $to eq "*" 0 0 0
625 0 ▶ return $distance{$from}{$to} if defined $distance{$from}{$to};
Branch: 1 branch
BranchTrueFalseTotal
if defined $distance{$from}{$to} 0 0 0
626  
627 0 ▶ if ($parents{$from})
Branch: 1 branch
BranchTrueFalseTotal
if ($parents{$from}) 0 0 0
628 {
629 0 foreach my $parent ( @{$parents{$from}} )
630 {
631 0 my $distance = distance($parent,$to);
632 0 ▶ if (defined $distance)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $distance) 0 0 0
633 {
634 0 $distance{$from}{$to} = $distance+1;
635 0 return $distance+1;
636 }
637 }
638 }
639 0 return undef;
640 }
641  
642 sub evaluate
643 0 ▶{
Subroutine evaluate: not called CC=6 SCAR=37.4
Pod undocumented
644 0 my ($name, $types) = @_;
645 0 my @results = ();
646 0 my $sig = join ',', @$types;
647  
648 0 SET: foreach my $typeset ( keys %{$Class::Multimethods::dispatch{$name}} )
649 {
650
651 0 push @results, { sig => "$name($typeset)",
652 incomp => [],
653 sum_dist => 0,
654 wrong_length => 0,
655 generic => 0,
656 };
657 0 my @nexttypes = split /,/, $typeset;
658 0 ▶ if (@nexttypes != @$types)
Branch: 1 branch
BranchTrueFalseTotal
if (@nexttypes != @$types) 0 0 0
659 {
660 0 $results[-1]->{wrong_length} = 1;
661 0 next SET;
662 }
663  
664 0 my @dist;
665 0 ▶ PARAM: for (my $i=0; $i<@$types; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$types) 0 0 0
666 {
667 0 my $nextdist = distance($types->[$i], $nexttypes[$i]);
668 0 push @{$results[-1]->{dist}}, $nextdist;
669 0 ▶ if (!defined $nextdist)
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $nextdist) { } 0 0 0
elsif ($nextdist < 0) { } 0 0 0
670 {
671 0 push @{$results[-1]->{incomp}}, $i;
672 }
673 elsif ($nextdist < 0)
674 {
675 0 $results[-1]->{generic} = 1;
676 }
677 else
678 {
679 0 $results[-1]->{sum_dist} += $nextdist
680 }
681 }
682 }
683 0 return @results;
684 }
685  
686  
687 1 1;
688 __END__
↑ Summary