| 1 | package Result::Simple; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 3 | our $VERSION = "0.07"; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use Exporter::Shiny qw( | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | ok | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | err | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | result_for | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | chain | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | pipeline | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | combine | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | combine_with_all_errors | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | flatten | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | match | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | unsafe_unwrap | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | unsafe_unwrap_err | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 3 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 3 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 3 | ▶ | use Scope::Upper (); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 3 | ▶ | use Sub::Util (); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 3 | ▶ | use Scalar::Util (); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | # If this option is true, then check `ok` and `err` functions usage and check a return value type. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | # However, it should be falsy for production code, because of performance, and it is an assertion, not a validation. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 3 | ▶ | use constant CHECK_ENABLED => $ENV{RESULT_SIMPLE_CHECK_ENABLED} // 1; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=6.9
Condition100%2/2
$ENV{"RESULT_SIMPLE_CHECK_ENABLED"} // 1
MC/DC100%1/1
$ENV{"RESULT_SIMPLE_CHECK_ENABLED"} // 1
$ENV{"RESULT_SIMPLE_CHECK_ENABLED"}
Truth table100%2/2
A
A= $ENV{"RESULT_SIMPLE_CHECK_ENABLED"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | # err does not allow these values. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 3 | ▶ | use constant FALSY_VALUES => [0, '0', '', undef]; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | # When the function is successful, it should return this. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 55 | ▶ | sub ok { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
ok: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 55 | if (CHECK_ENABLED) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 55 | ▶ | croak "`ok` must be called in list context" unless wantarray; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 49 | ▶ | croak "`ok` does not allow multiple arguments" if @_ > 1; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 48 | ▶ | croak "`ok` does not allow no arguments" if @_ == 0; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 47 | ($_[0], undef) | |||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | # When the function fails, it should return this. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 35 | ▶ | sub err { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
err: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 35 | if (CHECK_ENABLED) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 35 | ▶ | croak "`err` must be called in list context" unless wantarray; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 27 | ▶ | croak "`err` does not allow multiple arguments." if @_ > 1; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 26 | ▶ | croak "`err` does not allow no arguments" if @_ == 0; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 25 | ▶ | croak "`err` does not allow a falsy value: @{[ _ddf($_[0]) ]}" unless $_[0]; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 22 | (undef, $_[0]) | |||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | # result_for foo => (T, E); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | # This is used to define a function that returns a success or failure. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | # Example | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | # result_for div => Int, ErrorMessage; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | # sub div { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | # my ($a, $b) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | # if ($b == 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | # return err('Division by zero'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | # } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | # return ok($a / $b); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | # } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 21 | ▶ | sub result_for { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
result_for: called CC=10 SCAR=23.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 21 | unless (CHECK_ENABLED) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | # This is a no-op if CHECK_ENABLED is false. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | return; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 21 | my ($function_name, $T, $E, %opts) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 13 | ▶ | my @caller = caller($opts{caller_level} || 0); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$opts{'caller_level'} || 0
MC/DC0%0/1
$opts{'caller_level'} || 0
$opts{'caller_level'}
Truth table50%1/2
A
A= $opts{'caller_level'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 13 | ▶ | my $package = $opts{package} || $caller[0]; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$opts{'package'} || $caller[0]
MC/DC0%0/2
$opts{'package'} || $caller[0]
$opts{'package'}
$caller[0]
Truth table33%1/3
A || B
A= $opts{'package'}B= $caller[0]
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 13 | my $filename = $caller[1]; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 13 | my $line = $caller[2]; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 13 | my $code = $package->can($function_name); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 13 | ▶ | unless ($code) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 1 | croak "result_for: function `$function_name` not found in package `$package` at $filename line $line\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 12 | ▶ | unless (Scalar::Util::blessed($T) && $T->can('check')) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($T) and $T->can("check")
MC/DC50%1/2
builtin::blessed($T) and $T->can("check")
builtin::blessed($T)
$T->can("check")
Truth table66%2/3
A and B
A= builtin::blessed($T)B= $T->can("check")
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 1 | croak "result_for T requires `check` method, got: @{[ _ddf($T) ]} at $filename line $line\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 11 | ▶ | if (defined $E) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 9 | ▶ | unless (Scalar::Util::blessed($E) && $E->can('check')) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($E) and $E->can("check")
MC/DC50%1/2
builtin::blessed($E) and $E->can("check")
builtin::blessed($E)
$E->can("check")
Truth table66%2/3
A and B
A= builtin::blessed($E)B= $E->can("check")
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 1 | croak "result_for E requires `check` method, got: @{[ _ddf($E) ]} at $filename line $line\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 8 | ▶ | if (my @f = grep { $E->check($_) } @{ FALSY_VALUES() }) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 1 | croak "result_for E should not allow falsy values: @{[ _ddf(\@f) ]} at $filename line $line\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 9 | wrap_code($code, $package, $function_name, $T, $E); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | # Wrap the original coderef with type check. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 9 | ▶ | sub wrap_code { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
wrap_code: called CC=2 SCAR=8.6
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 9 | my ($code, $package, $name, $T, $E) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 9 | ▶ | my $wrapped = sub { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
a_few_result: called CC=9 SCAR=31.4
Subroutine
__ANON__: not called CC=9 SCAR=31.4
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 0 | ▶ | croak "Must handle error in `$name`" unless wantarray; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | my @result = &Scope::Upper::uplevel($code, @_, &Scope::Upper::CALLER(0)); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | ▶ | unless (@result == 2) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | Carp::confess "Invalid result tuple (T, E) in `$name`. Do you forget to call `ok` or `err` function? Got: @{[ _ddf(\@result) ]}"; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 0 | my ($data, $err) = @result; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | ▶ | if ($err) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 0 | ▶ | if (defined $E) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 0 | ▶ | if (!$E->check($err) || defined $data) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition16%1/6
not $E->check($err) or defined $data
not $E->check($err) or defined $data
MC/DC0%0/2
not $E->check($err) or defined $data
not $E->check($err)
defined $data
MC/DC0%0/2
not $E->check($err) or defined $data
not $E->check($err)
defined $data
Truth table33%1/3
not(A) or B
A= not $E->check($err)B= defined $data
Truth table0%0/3
not(A) or B
A= not $E->check($err)B= defined $data
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | 0 | Carp::confess "Invalid failure result in `$name`: @{[ _ddf([$data, $err]) ]}"; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | # Result(T, undef) should not return an error. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 0 | Carp::confess "Never return error in `$name`: @{[ _ddf([$data, $err]) ]}"; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 0 | ▶ | if (!$T->check($data) || defined $err) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition33%2/6
not $T->check($data) or defined $err
not $T->check($data) or defined $err
MC/DC50%1/2
not $T->check($data) or defined $err
not $T->check($data)
defined $err
MC/DC0%0/2
not $T->check($data) or defined $err
not $T->check($data)
defined $err
Truth table66%2/3
not(A) or B
A= not $T->check($data)B= defined $err
Truth table0%0/3
not(A) or B
A= not $T->check($data)B= defined $err
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | 0 | Carp::confess "Invalid success result in `$name`: @{[ _ddf([$data, $err]) ]}"; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | 0 | ($data, $err); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 9 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 9 | my $fullname = "$package\::$name"; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | 9 | Sub::Util::set_subname($fullname, $wrapped); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | 9 | my $prototype = Sub::Util::prototype($code); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | 9 | ▶ | if (defined $prototype) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | 1 | Sub::Util::set_prototype($prototype, $wrapped); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | 3 | ▶ | no strict qw(refs); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | 3 | ▶ | no warnings qw(redefine); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | 9 | *{$fullname} = $wrapped; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | # `chain` takes a function name and a result tuple (T, E) and returns a new result tuple (T, E). | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | 7 | ▶ | sub chain { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
chain: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 7 | my ($function, $value, $error) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | 7 | if (CHECK_ENABLED) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | 7 | ▶ | croak "`chain` must be called in list context" unless wantarray; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 6 | ▶ | croak "`chain` arguments must be func and result like (func, T, E)" unless @_ == 3; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | 5 | ▶ | my $code = ref $function ? $function : do { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | 5 | my $caller = caller(0); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | 5 | ▶ | $caller->can($function) or croak "Function `$function` not found in $caller"; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$caller->can($function) || croak("Function `$function` not found in $caller")
MC/DC100%1/1
$caller->can($function) || croak("Function `$function` not found in $caller")
$caller->can($function)
Truth table100%2/2
A
A= $caller->can($function)
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | 4 | ▶ | return err($error) if $error; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | 3 | return $code->($value); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | # `pipeline` takes a list of function names and returns a new function. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | 3 | ▶ | sub pipeline { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
pipeline: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | 3 | my (@functions) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | my @codes = map { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | 3 | my $f = $_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | 5 | ▶ | ref $f ? $f : do { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | 5 | my $caller = caller(0); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | 5 | ▶ | $caller->can($f) or croak "Function `$f` not found in $caller"; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$caller->can($f) || croak("Function `$f` not found in $caller")
MC/DC100%1/1
$caller->can($f) || croak("Function `$f` not found in $caller")
$caller->can($f)
Truth table100%2/2
A
A= $caller->can($f)
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | } @functions; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | 6 | ▶ | my $pipelined = sub { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | 6 | my ($value, $error) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | 6 | if (CHECK_ENABLED) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | 6 | ▶ | croak "pipelined function must be called in list context" unless wantarray; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | 5 | ▶ | croak "pipelined function arguments must be result such as (T, E) " unless @_ == 2; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | 4 | ▶ | return err($error) if $error; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | 4 | for my $code (@codes) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | 7 | ($value, $error) = $code->($value); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | 6 | ▶ | return err($error) if $error; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | 1 | return ok($value); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | 2 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | 2 | my $package = caller(0); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | 2 | my $fullname = "$package\::__PIPELINED_FUNCTION__"; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | 2 | Sub::Util::set_subname($fullname, $pipelined); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | 2 | return $pipelined; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | # `combine` takes a list of Result like `((T1,E1), (T2,E2), (T3,E3))` and returns a new Result like `([T1,T2,T3], E)`. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | 4 | ▶ | sub combine { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
combine: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | 4 | my @results = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | 4 | if (CHECK_ENABLED) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | 4 | ▶ | croak "`combine` must be called in list context" unless wantarray; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | 3 | ▶ | croak "`combine` arguments must be Result list" unless @_ % 2 == 0; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | 2 | my @values; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | 2 | ▶ | for (my $i = 0; $i < @results; $i += 2) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | 5 | my ($value, $error) = @results[$i, $i + 1]; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | 5 | ▶ | if ($error) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | 1 | return err($error); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | 4 | push @values, $value; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | 1 | return ok(\@values); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | # `combine_with_all_errors` takes a list of Result like `((T1,E1), (T2,E2), (T3,E3))` and returns a new Result like `([T1,T2,T3], [E1,E2,E3])`. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | 2 | ▶ | sub combine_with_all_errors { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
combine_with_all_errors: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | 2 | my @results = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | 2 | if (CHECK_ENABLED) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | 2 | ▶ | croak "`combine_with_all_errors` must be called in list context" unless wantarray; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | 2 | ▶ | croak "`combine_with_all_errors` arguments must be Result list" unless @_ % 2 == 0; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | 2 | my @values; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | my @errors; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | 2 | ▶ | for (my $i = 0; $i < @results; $i += 2) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | 7 | my ($value, $err) = @results[$i, $i + 1]; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | 7 | ▶ | if ($err) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | 2 | push @errors, $err; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | 5 | push @values, $value; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | 2 | ▶ | return err(\@errors) if @errors; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | 1 | return ok(\@values); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | # `flatten` takes a list of Result like `([T1,E1], [T2,E2], [T3,E3])` and returns a new Result like ((T1,E1), (T2,E2), (T3,E3)). | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | 2 | ▶ | sub flatten { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
flatten: called CC=3 SCAR=13.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | 2 | ▶ | map { ref $_ && ref $_ eq 'ARRAY' ? @$_ : $_ } @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $_ and ref $_ eq "ARRAY"
MC/DC0%0/2
ref $_ and ref $_ eq "ARRAY"
ref $_
ref $_ eq "ARRAY"
Truth table33%1/3
A and B
A= ref $_B= ref $_ eq "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | # `match` takes two coderefs for on success and on failure, and returns a new function. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | 2 | ▶ | sub match { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
match: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | 2 | my ($on_success, $on_failure) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | 2 | if (CHECK_ENABLED) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | 2 | ▶ | croak "`match` arguments must be two coderefs for on success and on error" unless _is_callable($on_success) && _is_callable($on_failure); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
_is_callable($on_success) and _is_callable($on_failure)
MC/DC0%0/2
_is_callable($on_success) and _is_callable($on_failure)
_is_callable($on_success)
_is_callable($on_failure)
Truth table33%1/3
A and B
A= _is_callable($on_success)B= _is_callable($on_failure)
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | 4 | ▶ | my $match = sub { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | 4 | my ($value, $err) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | 4 | if (CHECK_ENABLED) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | 4 | ▶ | croak "`match` function arguments must be result like (T, E)" unless @_ == 2; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | 3 | ▶ | if ($err) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | 1 | return $on_failure->($err); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | 2 | return $on_success->($value); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | 2 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | 2 | my $package = caller(0); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | 2 | my $fullname = "$package\::__MATCHER_FUNCTION__"; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | 2 | Sub::Util::set_subname($fullname, $match); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | 2 | return $match; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | # `unsafe_nwrap` takes a Result<T, E> and returns a T when the result is an Ok, otherwise it throws exception. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | # It should be used in tests or debugging code. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | 2 | ▶ | sub unsafe_unwrap { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
unsafe_unwrap: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | 2 | my ($value, $err) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | 2 | ▶ | if ($err) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | 1 | croak "Error called in `unsafe_unwrap`: @{[ _ddf($err) ]}" | |||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | 1 | return $value; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | # `unsafe_unwrap_err` takes a Result<T, E> and returns an E when the result is an Err, otherwise it throws exception. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | # It should be used in tests or debugging code. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | 2 | ▶ | sub unsafe_unwrap_err { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
unsafe_unwrap_err: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | 2 | my ($value, $err) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | 2 | ▶ | if (!$err) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | 1 | croak "No error called in `unsafe_unwrap_err`: @{[ _ddf($value) ]}" | |||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | 1 | return $err; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | # Dump data for debugging. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | 13 | ▶ | sub _ddf { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_ddf: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | 13 | my $v = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | 3 | ▶ | no warnings 'once'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | 13 | require Data::Dumper; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | 13 | local $Data::Dumper::Indent = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | 13 | local $Data::Dumper::Useqq = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | 13 | local $Data::Dumper::Terse = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | 13 | local $Data::Dumper::Sortkeys = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | 13 | local $Data::Dumper::Maxdepth = 2; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | 13 | Data::Dumper::Dumper($v); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | # Check if the argument is a callable. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | 4 | ▶ | sub _is_callable { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_is_callable: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | 4 | my $code = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | 4 | ▶ | (Scalar::Util::reftype($code)||'') eq 'CODE' | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
builtin::reftype($code) || ''
MC/DC0%0/1
builtin::reftype($code) || ''
builtin::reftype($code)
Truth table50%1/2
A
A= builtin::reftype($code)
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | 3 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||