| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 74 | my ($i, $j) = (0, 0); | |||||||||||||||||||||||||||||||||||||||||||||
| 29 | 74 | ▶ | for (; $i < @args and $j < @_; ++$i, ++$j) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$i < @Params::PatternMatch::args and $j < @_
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 < @_
| |||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 69 | ▶ | if (is_slurp_arg($_[$j])) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 9 | $_[$j]->set(@args[$i .. $#args]); | |||||||||||||||||||||||||||||||||||||||||||||
| 32 | 9 | $i = $#args; | |||||||||||||||||||||||||||||||||||||||||||||
| 33 | 9 | next; | |||||||||||||||||||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 60 | ▶ | if (is_lvalue($_[$j])) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 32 | $_[$j] = $args[$i]; | |||||||||||||||||||||||||||||||||||||||||||||
| 37 | 32 | next; | |||||||||||||||||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 28 | ▶ | next if $COMPARATOR->Cmp($args[$i], $_[$j]) != 0; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%6/6
$i == @Params::PatternMatch::args and $j == @_ or &is_slurp_arg($_[$j])
$i == @Params::PatternMatch::args and $j == @_
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])
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||||||
| 91 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||