| 1 | package Catmandu::Fix::Bind::maybe; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use Catmandu::Sane; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | 1 | our $VERSION = '1.2025'; | |||||||||
| 6 | |||||||||||
| 7 | 1 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | ▶ | use Scalar::Util qw(reftype); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 1 | ▶ | use namespace::clean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 1 | with 'Catmandu::Fix::Bind'; | |||||||||
| 12 | |||||||||||
| 13 | # Copied from hiratara's Data::Monad::Maybe | ||||||||||
| 14 | 25 | ▶ | sub just { | ||||||||
|
Subroutine
just: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 15 | 25 | my ($self, @values) = @_; | |||||||||
| 16 | 25 | bless [@values], __PACKAGE__; | |||||||||
| 17 | } | ||||||||||
| 18 | |||||||||||
| 19 | 2 | ▶ | sub nothing { | ||||||||
|
Subroutine
nothing: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 20 | 2 | my ($self) = @_; | |||||||||
| 21 | 2 | bless \(my $d = undef), __PACKAGE__; | |||||||||
| 22 | } | ||||||||||
| 23 | |||||||||||
| 24 | 40 | ▶ | sub is_nothing { | ||||||||
|
Subroutine
is_nothing: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 25 | 40 | my ($self, $mvar) = @_; | |||||||||
| 26 | 40 | reftype $mvar ne 'ARRAY'; | |||||||||
| 27 | } | ||||||||||
| 28 | |||||||||||
| 29 | 26 | ▶ | sub value { | ||||||||
|
Subroutine
value: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 30 | 26 | my ($self, $mvar) = @_; | |||||||||
| 31 | |||||||||||
| 32 | 26 | ▶ | if ($self->is_nothing($mvar)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 33 | 1 | {}; | |||||||||
| 34 | } | ||||||||||
| 35 | else { | ||||||||||
| 36 | 25 | $mvar->[0]; | |||||||||
| 37 | } | ||||||||||
| 38 | } | ||||||||||
| 39 | |||||||||||
| 40 | 15 | ▶ | sub unit { | ||||||||
|
Subroutine
unit: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 41 | 15 | my ($self, $data) = @_; | |||||||||
| 42 | 15 | $self->just($data); | |||||||||
| 43 | } | ||||||||||
| 44 | |||||||||||
| 45 | 14 | ▶ | sub bind { | ||||||||
|
Subroutine
bind: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||
| 46 | 14 | my ($self, $mvar, $func) = @_; | |||||||||
| 47 | |||||||||||
| 48 | 14 | ▶ | if ($self->is_nothing($mvar)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 49 | 1 | return $self->nothing; | |||||||||
| 50 | } | ||||||||||
| 51 | |||||||||||
| 52 | 13 | my $res; | |||||||||
| 53 | |||||||||||
| 54 | 13 | eval {$res = $func->($self->value($mvar));}; | |||||||||
| 55 | |||||||||||
| 56 | 11 | ▶ | if ($@) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 57 | 0 | return $self->nothing; | |||||||||
| 58 | } | ||||||||||
| 59 | |||||||||||
| 60 | 11 | ▶ | if (defined $res) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | 10 | return $self->just($res); | |||||||||
| 62 | } | ||||||||||
| 63 | else { | ||||||||||
| 64 | 1 | return $self->nothing; | |||||||||
| 65 | } | ||||||||||
| 66 | } | ||||||||||
| 67 | |||||||||||
| 68 | 13 | ▶ | sub result { | ||||||||
|
Subroutine
result: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 69 | 13 | my ($self, $mvar) = @_; | |||||||||
| 70 | 13 | $self->value($mvar); | |||||||||
| 71 | } | ||||||||||
| 72 | |||||||||||
| 73 | 1 | 1; | |||||||||
| 74 | |||||||||||
| 75 | __END__ | ||||||||||