blib/lib/Params/PatternMatch.pm

filter: Statementstmt 97.3% 74 / 76 Branchbran 79.1% 19 / 24 Conditioncond 100% 9 / 9 MC/DCmcdc 60.0% 3 / 5 Subroutinesub 100% 23 / 23 Podpod 75.0% 6 / 8 totaltotal 92.4% 134 / 145 CC 16 SCAR 13.5
CC
16
Cov
94%
case
10.0
match
4.0
values
2.5
CRAP 3.9
↑ Summary
1 package Params::PatternMatch;
2  
3 # ABSTRACT: Pattern match-based argument binding for Perl.
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 1 ▶use B;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Data::Compare;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Exporter::Lite;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Scalar::Util qw/blessed/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use TryCatch;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 our $COMPARATOR = Data::Compare->new;
15  
16 1 our @EXPORT_OK = qw/as case match otherwise rest then/;
17  
18 1 our $VERSION = '0.01';
19  
20 1 our @args;
21  
22 43 ▶sub as(&) { @_ }
Subroutine as: called CC=1 SCAR=0.0
Pod documented
23  
24 74 ▶sub case {
Subroutine case: called CC=10 SCAR=23.0
Pod documented
25 74 my $action = pop;
26 74 ▶ Carp::croak('Not a CodeRef.') if ref $action ne 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $action ne "CODE" 0 74 74
27  
28 74 my ($i, $j) = (0, 0);
29 74 ▶ for (; $i < @args and $j < @_; ++$i, ++$j) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @Params::PatternMatch::args and $j < @_) 69 53 122
Condition100%3/3
$i < @Params::PatternMatch::args and $j < @_
!ll&&!rl&&r
411269
MC/DC100%2/2
$i < @Params::PatternMatch::args and $j < @_
$i < @Params::PatternMatch::args $j < @_
Truth table100%3/3
A and B
A= $i < @Params::PatternMatch::argsB= $j < @_
ABresult
0X0
100
111
30 69 ▶ if (is_slurp_arg($_[$j])) {
Branch: 1 branch
BranchTrueFalseTotal
if (&is_slurp_arg($_[$j])) 9 60 69
31 9 $_[$j]->set(@args[$i .. $#args]);
32 9 $i = $#args;
33 9 next;
34 }
35 60 ▶ if (is_lvalue($_[$j])) {
Branch: 1 branch
BranchTrueFalseTotal
if (&is_lvalue($_[$j])) 32 28 60
36 32 $_[$j] = $args[$i];
37 32 next;
38 }
39 28 ▶ next if $COMPARATOR->Cmp($args[$i], $_[$j]) != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $Params::PatternMatch::COMPARATOR->Cmp($Params::PatternMatch::args[$i], $_[$j]) != 0 7 21 28
40  
41 21 return; # Pattern didn't match.
42 }
43 53 ▶ return unless $i == @args and $j == @_ or is_slurp_arg($_[$j]);
Branch: 1 branch
BranchTrueFalseTotal
unless $i == @Params::PatternMatch::args and $j == @_ or &is_slurp_arg($_[$j]) 12 41 53
Condition100%6/6
$i == @Params::PatternMatch::args and $j == @_ or &is_slurp_arg($_[$j])
l!l&&r!l&&!r
39212
$i == @Params::PatternMatch::args and $j == @_
!ll&&!rl&&r
12239
MC/DC33%1/3
$i == @Params::PatternMatch::args and $j == @_ or &is_slurp_arg($_[$j])
$i == @Params::PatternMatch::args $j == @_ &is_slurp_arg($_[$j])
Truth table60%3/5
A and B or C
A= $i == @Params::PatternMatch::argsB= $j == @_C= &is_slurp_arg($_[$j])
ABCresult
11X1
0X11
1011
0X00
1000
44  
45 41 die Params::PatternMatch::Values->new($action->(@args));
46 }
47  
48 60 ▶sub is_lvalue($) { +(B::svref_2object(\$_[0])->FLAGS & B::SVf_READONLY) == 0 }
Subroutine is_lvalue: called CC=1 SCAR=0.0
Pod undocumented
49  
50 83 ▶sub is_slurp_arg($) {
Subroutine is_slurp_arg: called CC=2 SCAR=6.9
Pod undocumented
51 83 ▶ blessed $_[0] and $_[0]->isa('Params::PatternMatch::SlurpArg');
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($_[0]) and $_[0]->isa("Params::PatternMatch::SlurpArg") 11 72 83
52 }
53  
54 43 ▶sub match {
Subroutine match: called CC=4 SCAR=13.9
Pod documented
55 43 my $patterns = pop;
56 43 ▶ Carp::croak('Not a CodeRef.') if ref $patterns ne 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $patterns ne "CODE" 0 43 43
57  
58 43 local *args = \@_;
59 1 ▶ try {
Subroutine BEGIN: called CC=1 SCAR=0.0
60 43 $patterns->();
61 1 ▶ } catch (Params::PatternMatch::Values $retval) {
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 2 branches
BranchTrueFalseTotal
if ($TryCatch::Error) 43 0 43
if ('TryCatch'->check_tc('Params::PatternMatch::Values')) { } 42 1 43
62 42 return $retval->values;
63 1 ▶ } catch ($error) {
Subroutine BEGIN: called CC=1 SCAR=0.0
64 1 die $error;
65 0 };
66 }
67  
68 2 ▶sub otherwise(&) {
Subroutine otherwise: called CC=2 SCAR=7.2
Pod documented
69 2 ▶ Carp::croak('Not a CodeRef.') if ref $_[0] ne 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] ne "CODE" 0 2 2
70 2 die Params::PatternMatch::Values->new($_[0]->(@args));
71 }
72  
73 11 ▶sub rest(\@) { Params::PatternMatch::SlurpArg->new($_[0]) }
Subroutine rest: called CC=1 SCAR=0.0
Pod documented
74  
75 74 ▶sub then(&) { @_ }
Subroutine then: called CC=1 SCAR=0.0
Pod documented
76  
77 package Params::PatternMatch::SlurpArg;
78  
79 11 ▶sub new { bless $_[1] => $_[0] }
Subroutine new: called CC=1 SCAR=0.0
80  
81 9 ▶sub set { @{ $_[0] } = @_[1 .. $#_ ] }
Subroutine set: called CC=1 SCAR=0.0
82  
83 package Params::PatternMatch::Values;
84  
85 42 ▶sub new { bless \@_ => shift }
Subroutine new: called CC=1 SCAR=0.0
86  
87 0 ▶sub values { wantarray ? @{ $_[0] } : $_[0][0] }
Subroutine values: called CC=2 SCAR=9.2
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 42 42
88  
89 1 1;
90  
91 __END__
↑ Summary