| 1 | package Data::Unixish::chain; | ||||||||||
| 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 | ▶ | use Data::Unixish::Util qw(%common_args); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 2 | our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY | |||||||||
| 13 | 2 | our $DATE = '2025-02-24'; # DATE | |||||||||
| 14 | 2 | our $DIST = 'Data-Unixish'; # DIST | |||||||||
| 15 | 2 | our $VERSION = '1.574'; # VERSION | |||||||||
| 16 | |||||||||||
| 17 | 2 | our %SPEC; | |||||||||
| 18 | |||||||||||
| 19 | $SPEC{chain} = { | ||||||||||
| 20 | 2 | v => 1.1, | |||||||||
| 21 | summary => 'Chain several dux functions together', | ||||||||||
| 22 | description => <<'MARKDOWN', | ||||||||||
| 23 | |||||||||||
| 24 | Currently works for itemfunc only. | ||||||||||
| 25 | |||||||||||
| 26 | See also the <pm:Data::Unixish::Apply> function, which is related. | ||||||||||
| 27 | |||||||||||
| 28 | MARKDOWN | ||||||||||
| 29 | args => { | ||||||||||
| 30 | %common_args, | ||||||||||
| 31 | functions => { | ||||||||||
| 32 | summary => 'The functions to chain', | ||||||||||
| 33 | schema => ['array*' => of => ['any*', of => [ | ||||||||||
| 34 | 'str*', | ||||||||||
| 35 | ['array*', min_len=>1, elems=>['str*','hash*']], | ||||||||||
| 36 | ]]], | ||||||||||
| 37 | description => <<'MARKDOWN', | ||||||||||
| 38 | |||||||||||
| 39 | Each element must either be function name (like `date`) or a 2-element array | ||||||||||
| 40 | containing the function name and its arguments (like `[bool, {style: dot}]`). | ||||||||||
| 41 | |||||||||||
| 42 | MARKDOWN | ||||||||||
| 43 | req => 1, | ||||||||||
| 44 | pos => 0, | ||||||||||
| 45 | greedy => 1, | ||||||||||
| 46 | cmdline_aliases => {f => {}}, | ||||||||||
| 47 | }, | ||||||||||
| 48 | }, | ||||||||||
| 49 | tags => [qw/itemfunc func/], | ||||||||||
| 50 | }; | ||||||||||
| 51 | 2 | ▶ | sub chain { | ||||||||
|
Subroutine
chain: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 52 | 2 | my %args = @_; | |||||||||
| 53 | 2 | my ($in, $out) = ($args{in}, $args{out}); | |||||||||
| 54 | |||||||||||
| 55 | 2 | _chain_begin(\%args); | |||||||||
| 56 | 2 | local ($., $_); | |||||||||
| 57 | 2 | ▶ | while (($., $_) = each @$in) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 58 | 4 | push @$out, _chain_item($_, \%args); | |||||||||
| 59 | } | ||||||||||
| 60 | |||||||||||
| 61 | 2 | [200, "OK"]; | |||||||||
| 62 | } | ||||||||||
| 63 | |||||||||||
| 64 | 4 | ▶ | sub _chain_begin { | ||||||||
|
Subroutine
_chain_begin: called CC=6 SCAR=19.5
| |||||||||||
| 65 | 2 | ▶ | no strict 'refs'; ## no critic: TestingAndDebugging::ProhibitNoStrict | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 66 | |||||||||||
| 67 | 4 | my $args = shift; | |||||||||
| 68 | 4 | my $ff = []; | |||||||||
| 69 | 4 | for my $f (@{ $args->{functions} }) { | |||||||||
| 70 | 2 | my ($fn, $args); | |||||||||
| 71 | 2 | ▶ | if (ref($f) eq 'ARRAY') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 72 | 0 | $fn = $f->[0];; | |||||||||
| 73 | 0 | $args = $f->[1]; | |||||||||
| 74 | } else { | ||||||||||
| 75 | 2 | $fn = $f; | |||||||||
| 76 | 2 | $args = {}; | |||||||||
| 77 | } | ||||||||||
| 78 | 2 | ▶ | unless ($fn =~ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 79 | /\A[A-Za-z_][A-Za-z0-9_]*(::[A-Za-z_][A-Za-z0-9_]*)*\z/) { | ||||||||||
| 80 | 0 | die "Invalid function name $fn, please use letter+alphanums only"; | |||||||||
| 81 | } | ||||||||||
| 82 | 2 | my $mod = "Data::Unixish::$fn"; | |||||||||
| 83 | 2 | ▶ | unless (eval "require $mod") { ## no critic: BuiltinFunctions::ProhibitStringyEval | ||||||||
|
Branch: 1 branch
| |||||||||||
| 84 | 0 | die "Can't load dux function $fn: $@"; | |||||||||
| 85 | } | ||||||||||
| 86 | 2 | my $fnleaf = $fn; $fnleaf =~ s/.+:://; | |||||||||
| 87 | 2 | ▶ | if (defined &{"$mod\::_${fnleaf}_begin"}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 88 | 0 | my $begin = \&{"$mod\::_${fnleaf}_begin"}; | |||||||||
| 89 | 0 | $begin->($args); | |||||||||
| 90 | } | ||||||||||
| 91 | 2 | push @$ff, [$mod, $fn, $fnleaf, \&{"$mod\::_${fnleaf}_item"}, $args]; | |||||||||
| 92 | } | ||||||||||
| 93 | # abuse to store state | ||||||||||
| 94 | 4 | $args->{-functions} = $ff; | |||||||||
| 95 | } | ||||||||||
| 96 | |||||||||||
| 97 | 8 | ▶ | sub _chain_item { | ||||||||
|
Subroutine
_chain_item: called CC=2 SCAR=6.9
| |||||||||||
| 98 | 8 | my ($item, $args) = @_; | |||||||||
| 99 | 8 | local $_ = $item; | |||||||||
| 100 | 8 | for my $f (@{ $args->{-functions} }) { | |||||||||
| 101 | 4 | $item = $f->[3]->($item, $f->[4]); | |||||||||
| 102 | } | ||||||||||
| 103 | 8 | $item; | |||||||||
| 104 | } | ||||||||||
| 105 | |||||||||||
| 106 | 2 | ▶ | sub _chain_end { | ||||||||
|
Subroutine
_chain_end: called CC=3 SCAR=11.4
| |||||||||||
| 107 | 2 | ▶ | no strict 'refs'; ## no critic: TestingAndDebugging::ProhibitNoStrict | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 108 | |||||||||||
| 109 | 2 | my $args = shift; | |||||||||
| 110 | 2 | for my $f (@{ $args->{-functions} }) { | |||||||||
| 111 | 1 | my $mod = $f->[0]; | |||||||||
| 112 | 1 | my $fnleaf = $f->[2]; | |||||||||
| 113 | 1 | my $args = $f->[4]; | |||||||||
| 114 | 1 | ▶ | if (defined &{"$mod\::_${fnleaf}_end"}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 115 | 0 | my $end = \&{"$mod\::_${fnleaf}_end"}; | |||||||||
| 116 | 0 | $end->($args); | |||||||||
| 117 | } | ||||||||||
| 118 | } | ||||||||||
| 119 | } | ||||||||||
| 120 | |||||||||||
| 121 | 2 | 1; | |||||||||
| 122 | # ABSTRACT: Chain several dux functions together | ||||||||||
| 123 | |||||||||||
| 124 | __END__ | ||||||||||