blib/lib/Sub/SmartMatch.pm

filter: Statementstmt 94.5% 87 / 92 Branchbran 67.6% 23 / 34 Conditioncond 59.2% 16 / 27 MC/DCmcdc 31.2% 5 / 16 Subroutinesub 100% 17 / 17 Podpod 66.6% 4 / 6 totaltotal 79.1% 152 / 192 CC 29 SCAR 17.3
CC
29
Cov
82%
multi
13.1
full_name
6.2
def_variants
5.0
CRAP 5.6
↑ Summary
1 #!/usr/bin/perl
2  
3 package Sub::SmartMatch;
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Scalar::Util qw(reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 our $VERSION = "0.02";
14  
15 1 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 our @EXPORT = our @EXPORT_OK = qw(multi multi_default def_multi exactly);
18  
19  
20  
21 1 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.1
22 # If we have Sub::Name, great. If not, nevermoose
23  
24 1 local $@;
25  
26 1 eval {
27 1 require Sub::Name;
28 1 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
29 0 *subname = \&Sub::Name::subname;
30 };
31  
32 1 ▶ *subname = sub { $_[1] } unless defined &subname;
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
unless defined &Sub::SmartMatch::subname 1 0 1
33 }
34  
35 1 ▶sub exactly ($) {
Subroutine exactly: called CC=3 SCAR=13.3
Pod documented
36 1 my $value = shift;
37  
38 1 ▶ if ( ref($value) and ref($value) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value and ref $value eq 'ARRAY') { } 1 0 1
Condition33%1/3
ref $value and ref $value eq "ARRAY"
!ll&&!rl&&r
001
MC/DC0%0/2
ref $value and ref $value eq "ARRAY"
ref $value ref $value eq "ARRAY"
Truth table33%1/3
A and B
A= ref $valueB= ref $value eq "ARRAY"
ABresult
0X0
100
111
39 1 bless \$value, __PACKAGE__ . "::Exact";
40 } else {
41 0 return $value;
42 }
43 }
44  
45 # guess the fully qualified name for a sub using caller()
46 32 ▶sub full_name ($) {
Subroutine full_name: called CC=6 SCAR=18.3
Pod undocumented
47 32 my $name = shift;
48  
49 32 ▶ croak "A subroutine name is required"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and length $name 0 32 32
Condition33%1/3
defined $name and length $name
!ll&&!rl&&r
0032
MC/DC0%0/2
defined $name and length $name
defined $name length $name
Truth table33%1/3
A and B
A= defined $nameB= length $name
ABresult
0X0
100
111
50 unless defined($name) and length($name);
51  
52 32 ▶ return $name if $name =~ /::/;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /::/ 22 10 32
53  
54 10 foreach my $level ( 0 .. 2 ) {
55 20 my $pkg = caller($level);
56 20 ▶ next if $pkg eq __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
if $pkg eq __PACKAGE__ 10 10 20
57 10 return join "::", $pkg, $name;
58 }
59 }
60  
61  
62 1 our ( %variants, %default );
63  
64 11 ▶sub multi ($$$) {
Subroutine multi: called CC=10 SCAR=25.7
Pod documented
65 11 my ( $name, $case, $body ) = @_;
66  
67 11 $name = full_name($name);
68  
69  
70 11 ▶ unless ( ref($body) and reftype($body) eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $body and builtin::reftype($body) eq "CODE") 0 11 11
Condition33%1/3
ref $body and builtin::reftype($body) eq "CODE"
!ll&&!rl&&r
0011
MC/DC0%0/2
ref $body and builtin::reftype($body) eq "CODE"
ref $body builtin::reftype($body) eq "CODE"
Truth table33%1/3
A and B
A= ref $bodyB= builtin::reftype($body) eq "CODE"
ABresult
0X0
100
111
71 0 ▶ my $body_str = defined($body)
Branch: 2 branches
BranchTrueFalseTotal
defined $body ? : 0 0 0
ref $body ? : 0 0 0
72 ? ( ref($body) ? $body : "'$body'" )
73 : "undef";
74  
75 0 croak "$body_str is not a code reference";
76 }
77  
78 11 def_multi($name);
79  
80 11 ▶ my $exact = ref($case) && ref($case) eq __PACKAGE__ . "::Exact";
Condition66%2/3
ref $case && ref $case eq 'Sub::SmartMatch::Exact'
!ll&&!rl&&r
0101
MC/DC50%1/2
ref $case && ref $case eq 'Sub::SmartMatch::Exact'
ref $case ref $case eq 'Sub::SmartMatch::Exact'
Truth table66%2/3
A && B
A= ref $caseB= ref $case eq 'Sub::SmartMatch::Exact'
ABresult
0X0
100
111
81 11 ▶ $case = $$case if $exact;
Branch: 1 branch
BranchTrueFalseTotal
if $exact 1 10 11
82  
83 11 ▶ my $partial_match = not($exact) && ref($case) && ref($case) eq 'ARRAY' && @$case;
Condition88%8/9
!$exact && ref $case && ref $case eq 'ARRAY' && @$case
!ll&&!rl&&r
317
not $exact and ref $case and ref $case eq "ARRAY"
!ll&&!rl&&r
128
not $exact and ref $case
!ll&&!rl&&r
1010
MC/DC75%3/4
!$exact && ref $case && ref $case eq 'ARRAY' && @$case
not $exact ref $case ref $case eq "ARRAY" @$case
Truth table80%4/5
not(A) and B and C && D
A= not $exactB= ref $caseC= ref $case eq "ARRAY"D= @$case
ABCDresult
0XXX0
10XX0
110X0
11100
11111
84  
85 11 push @{ $variants{$name} }, [ $partial_match, $case, $body ];
86  
87 11 return $body;
88 }
89  
90 2 ▶sub multi_default ($$) {
Subroutine multi_default: called CC=3 SCAR=11.8
Pod documented
91 2 my ( $name, $body ) = @_;
92  
93 2 ▶ croak "$body is not a code reference"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $body and builtin::reftype($body) eq "CODE" 0 2 2
Condition33%1/3
ref $body and builtin::reftype($body) eq "CODE"
!ll&&!rl&&r
002
MC/DC0%0/2
ref $body and builtin::reftype($body) eq "CODE"
ref $body builtin::reftype($body) eq "CODE"
Truth table33%1/3
A and B
A= ref $bodyB= builtin::reftype($body) eq "CODE"
ABresult
0X0
100
111
94 unless ref($body) and reftype($body) eq 'CODE';
95  
96 2 $name = full_name($name);
97  
98 2 def_multi($name);
99  
100 2 $default{$name} = $body;
101 }
102  
103 17 ▶sub def_multi ($;@) {
Subroutine def_multi: called CC=3 SCAR=11.0
Pod documented
104 17 my ( $name, @args ) = @_;
105 17 $name = full_name($name);
106  
107 17 ▶ unless ( exists $variants{$name} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $Sub::SmartMatch::variants{$name}) 6 11 17
108 6 my @variants;
109  
110 37 ▶ my $sub = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
111 37 given ( \@_ ) {
112 37 foreach my $variant ( @variants ) {
113 55 my ( $partial, $case, $body ) = @$variant;
114  
115 55 ▶ if ( $partial ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($partial) { } 37 18 55
116 37 given ( [ @_[0 .. $#$case] ] ) {
117 37 when ( $case ) { goto $body }
118 }
119 } else {
120 18 when ( $case ) { goto $body }
121 }
122 }
123  
124 6 default {
125 6 ▶ if ( my $default = $default{$name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $default = $Sub::SmartMatch::default{$name}) { } 6 0 6
126 6 goto $default;
127 } else {
128 0 croak "No variant found for arguments";
129 }
130 }
131 }
132 6 };
133  
134 {
135 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
136 6 *$name = subname $name, $sub;
137 }
138  
139 6 $variants{$name} = \@variants;
140 }
141  
142 17 ▶ def_variants($name, @args) if @args;
Branch: 1 branch
BranchTrueFalseTotal
if @args 2 15 17
143 }
144  
145 2 ▶sub def_variants ($;) {
Subroutine def_variants: called CC=5 SCAR=16.2
Pod undocumented
146 2 my ( $name, @variants ) = @_;
147  
148 2 $name = full_name($name);
149  
150 2 def_multi($name);
151  
152 2 ▶ croak "The variant list is not even sized"
Branch: 1 branch
BranchTrueFalseTotal
unless @variants % 2 == 0 0 2 2
153 unless @variants % 2 == 0;
154  
155 2 ▶ while ( @variants ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@variants) 5 2 7
156 5 my ( $case, $body ) = splice(@variants, 0, 2);
157  
158 5 ▶ if ( not ref($case) and $case ~~ 'default' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $case and $case ~~ 'default') { } 2 3 5
Condition66%2/3
not ref $case and $case ~~ "default"
!ll&&!rl&&r
302
MC/DC50%1/2
not ref $case and $case ~~ "default"
not ref $case $case ~~ "default"
Truth table66%2/3
not(A) and B
A= not ref $caseB= $case ~~ "default"
ABresult
0X0
100
111
159 2 multi_default $name, $body;
160 } else{
161 3 multi $name, $case, $body;
162 }
163 }
164 }
165  
166 1 __PACKAGE__
167  
168 __END__
↑ Summary