| 1 | package Data::Unixish::cond; | ||||||||||||
| 2 | |||||||||||||
| 3 | 2 | ▶ | use 5.010; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 2 | ▶ | use locale; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 2 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 2 | ▶ | use syntax 'each_on_array'; # to support perl < 5.12 | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 2 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | #use Log::Any '$log'; | ||||||||||||
| 9 | |||||||||||||
| 10 | 2 | require Data::Unixish; # for siduxs | |||||||||||
| 11 | 2 | ▶ | use Data::Unixish::Util qw(%common_args); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | |||||||||||||
| 13 | 2 | our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY | |||||||||||
| 14 | 2 | our $DATE = '2025-02-24'; # DATE | |||||||||||
| 15 | 2 | our $DIST = 'Data-Unixish'; # DIST | |||||||||||
| 16 | 2 | our $VERSION = '1.574'; # VERSION | |||||||||||
| 17 | |||||||||||||
| 18 | 2 | our %SPEC; | |||||||||||
| 19 | |||||||||||||
| 20 | $SPEC{cond} = { | ||||||||||||
| 21 | 2 | v => 1.1, | |||||||||||
| 22 | summary => 'Apply dux function conditionally', | ||||||||||||
| 23 | description => <<'MARKDOWN', | ||||||||||||
| 24 | |||||||||||||
| 25 | This dux function takes a condition (a Perl code/expression) and one or two | ||||||||||||
| 26 | other dux functions (A and B). Condition will be evaluated for each item (where | ||||||||||||
| 27 | `$_` will be set to the current item). If condition evaluates to true, then A is | ||||||||||||
| 28 | applied to the item, else B. All the dux functions must be itemfunc. | ||||||||||||
| 29 | |||||||||||||
| 30 | MARKDOWN | ||||||||||||
| 31 | args => { | ||||||||||||
| 32 | %common_args, | ||||||||||||
| 33 | if => { | ||||||||||||
| 34 | summary => 'Perl code that specifies the condition', | ||||||||||||
| 35 | schema => ['any*' => of => ['str*', 'code*']], | ||||||||||||
| 36 | req => 1, | ||||||||||||
| 37 | pos => 0, | ||||||||||||
| 38 | }, | ||||||||||||
| 39 | then => { | ||||||||||||
| 40 | summary => 'dux function to be applied if condition is true', | ||||||||||||
| 41 | schema => ['any*' => of => ['str*', 'array*']], # XXX dux | ||||||||||||
| 42 | req => 1, | ||||||||||||
| 43 | pos => 1, | ||||||||||||
| 44 | }, | ||||||||||||
| 45 | else => { | ||||||||||||
| 46 | summary => 'dux function to be applied if condition is false', | ||||||||||||
| 47 | schema => ['any*' => of => ['str*', 'array*']], # XXX dux | ||||||||||||
| 48 | pos => 2, | ||||||||||||
| 49 | }, | ||||||||||||
| 50 | }, | ||||||||||||
| 51 | tags => [qw/perl unsafe itemfunc/], | ||||||||||||
| 52 | "x.app.dux.is_stream_output" => 1, | ||||||||||||
| 53 | }; | ||||||||||||
| 54 | 2 | ▶ | sub cond { | ||||||||||
|
Subroutine
cond: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 55 | 2 | my %args = @_; | |||||||||||
| 56 | 2 | my ($in, $out) = ($args{in}, $args{out}); | |||||||||||
| 57 | |||||||||||||
| 58 | 2 | _cond_begin(\%args); | |||||||||||
| 59 | 1 | local $.; | |||||||||||
| 60 | 1 | my $item; | |||||||||||
| 61 | 1 | ▶ | while (($., $item) = each @$in) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 62 | 4 | push @$out, _cond_item->($item, \%args); | |||||||||||
| 63 | } | ||||||||||||
| 64 | |||||||||||||
| 65 | 1 | [200, "OK"]; | |||||||||||
| 66 | } | ||||||||||||
| 67 | |||||||||||||
| 68 | 2 | ▶ | sub _cond_begin { | ||||||||||
|
Subroutine
_cond_begin: called CC=6 SCAR=20.9
| |||||||||||||
| 69 | 2 | my $args = shift; | |||||||||||
| 70 | |||||||||||||
| 71 | 2 | ▶ | if (ref($args->{if}) ne 'CODE') { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 72 | 1 | ▶ | if ($args->{-cmdline}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 73 | 0 | $args->{if} = eval "no strict; no warnings; sub { $args->{if} }"; ## no critic: BuiltinFunctions::ProhibitStringyEval | |||||||||||
| 74 | 0 | ▶ | die "invalid Perl code for if: $@" if $@; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 75 | } else { | ||||||||||||
| 76 | 1 | die "Please supply coderef for 'if'"; | |||||||||||
| 77 | } | ||||||||||||
| 78 | } | ||||||||||||
| 79 | 1 | ▶ | $args->{then} //= 'cat'; | ||||||||||
|
Condition50%1/2
$args->{"then"} //= "cat"
MC/DC0%0/1
$args->{"then"} //= "cat"
$args->{"then"}
Truth table50%1/2
A
A= $args->{"then"}
| |||||||||||||
| 80 | 1 | ▶ | $args->{else} //= 'cat'; | ||||||||||
|
Condition50%1/2
$args->{"else"} //= "cat"
MC/DC0%0/1
$args->{"else"} //= "cat"
$args->{"else"}
Truth table50%1/2
A
A= $args->{"else"}
| |||||||||||||
| 81 | } | ||||||||||||
| 82 | |||||||||||||
| 83 | 4 | ▶ | sub _cond_item { | ||||||||||
|
Subroutine
_cond_item: called CC=2 SCAR=6.9
| |||||||||||||
| 84 | 4 | my ($item, $args) = @_; | |||||||||||
| 85 | |||||||||||||
| 86 | 4 | local $_ = $item; | |||||||||||
| 87 | |||||||||||||
| 88 | # XXX to be more efficient, skip siduxs and do it ourselves | ||||||||||||
| 89 | 4 | ▶ | if ($args->{if}->()) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 90 | 2 | return Data::Unixish::siduxs($args->{then}, $item); | |||||||||||
| 91 | } else { | ||||||||||||
| 92 | 2 | return Data::Unixish::siduxs($args->{else}, $item); | |||||||||||
| 93 | } | ||||||||||||
| 94 | } | ||||||||||||
| 95 | |||||||||||||
| 96 | 2 | 1; | |||||||||||
| 97 | # ABSTRACT: Apply dux function conditionally | ||||||||||||
| 98 | |||||||||||||
| 99 | __END__ | ||||||||||||