Data/Unixish/Apply.pm

filter: Statementstmt 96.6% 58 / 60 Branchbran 60.0% 12 / 20 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 100% 8 / 8 Podpod 100% 1 / 1 totaltotal 86.9% 80 / 92 CC 13 SCAR 22.6
CC
13
Cov
87%
apply
14.2
BEGIN
1.0
BEGIN
1.0
CRAP 9.5
« Data/Unixish.pm ↑ Summary Data/Unixish/sort.pm »
1 package Data::Unixish::Apply;
2  
3 2 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 #use Log::Any '$log';
7  
8 2 ▶use Data::Unixish::Util qw(%common_args filter_args);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Module::Load;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Package::Util::Lite qw(package_exists);
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{apply} = {
20 2 v => 1.1,
21 summary => 'Apply one or more dux functions',
22 args => {
23 in => {
24 schema => ['any'], # XXX stream
25 req => 1,
26 },
27 functions => {
28 summary => 'Function(s) to apply',
29 schema => ['any*', of => [
30 'str*',
31 ['array*', of => ['any' => of => [['str*'], ['array*']]]],
32 ]],
33 req => 1,
34 description => <<'MARKDOWN',
35  
36 A list of functions to apply. Each element is either a string (function name),
37 or a 2-element array (function names + arguments hashref). If you do not want to
38 specify arguments to a function, you can use a string.
39  
40 Example:
41  
42 [
43 'sort', # no arguments (all default)
44 'date', # no arguments (all default)
45 ['head', {items=>5}], # specify arguments
46 ]
47  
48 MARKDOWN
49 },
50  
51 },
52 };
53 2 ▶sub apply {
Subroutine apply: called CC=13 SCAR=26.5
Pod documented
54 2 my %args = @_;
55 2 ▶ my $in0 = $args{in} or return [400, "Please specify in"];
Branch: 1 branch
BranchTrueFalseTotal
my $in0 = $args{"in"} or return [400, "Please specify in"] 2 0 2
56 2 ▶ my $ff0 = $args{functions} or return [400, "Please specify functions"];
Branch: 1 branch
BranchTrueFalseTotal
my $ff0 = $args{"functions"} or return [400, "Please specify functions"] 2 0 2
57 2 ▶ $ff0 = [$ff0] unless ref($ff0) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $ff0 eq "ARRAY" 0 2 2
58  
59 # special case
60 2 ▶ unless (@$ff0) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$ff0) 0 2 2
61 0 return [200, "No processing done", $in0];
62 }
63  
64 2 my @ff;
65 2 my ($in, $out);
66 2 for my $i (0..@$ff0-1) {
67 3 my $f = $ff0->[$i];
68 #$log->tracef("Applying dux function %s ...", $f);
69 3 my ($fn0, $fargs);
70 3 ▶ if (ref($f) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $f eq 'ARRAY') { } 1 2 3
71 1 $fn0 = $f->[0];
72 1 ▶ $fargs = filter_args($f->[1]) // {};
Condition50%1/2
filter_args($f->[1]) // {}
l!l
10
MC/DC0%0/1
filter_args($f->[1]) // {}
filter_args($f->[1])
Truth table50%1/2
A
A= filter_args($f->[1])
Aresult
11
00
73 } else {
74 2 $fn0 = $f;
75 2 $fargs = {};
76 }
77  
78 3 ▶ if ($i == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == 0) { } 2 1 3
79 2 $in = $in0;
80 } else {
81 1 $in = $out;
82 }
83 3 $out = [];
84  
85 # XXX load all functions before applying, like in Unix pipes
86 3 my $pkg = "Data::Unixish::$fn0";
87 3 ▶ unless (package_exists($pkg)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (package_exists($pkg)) 3 0 3
88 3 ▶ eval { load $pkg; 1 } or
Branch: 1 branch
BranchTrueFalseTotal
eval { do { &load($pkg); 1 } } or return [500, "Can't load package for dux function ${fn0}: $@"] 3 0 3
89 return [500,
90 "Can't load package for dux function $fn0: $@"];
91 }
92  
93 3 my $fnl = $fn0; $fnl =~ s/.+:://;
94 3 my $fn = "Data::Unixish::$fn0\::$fnl";
95 3 ▶ return [500, "Subroutine &$fn not defined"] unless defined &$fn;
Branch: 1 branch
BranchTrueFalseTotal
unless defined &$fn 0 3 3
96  
97 2 ▶ no strict 'refs'; ## no critic: TestingAndDebugging::ProhibitNoStrict
Subroutine BEGIN: called CC=1 SCAR=0.0
98 3 my $res = $fn->(%$fargs, in=>$in, out=>$out);
99 3 ▶ unless ($res->[0] == 200) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->[0] == 200) 0 3 3
100 0 return [500, "Function $fn0 did not return success: ".
101 "$res->[0] - $res->[1]"];
102 }
103 }
104  
105 2 [200, "OK", $out];
106 }
107  
108 2 1;
109 # ABSTRACT: Apply one or more dux functions
110  
111 __END__
« Data/Unixish.pm ↑ Summary Data/Unixish/sort.pm »