Data/Unixish.pm

filter: Statementstmt 93.0% 147 / 158 Branchbran 78.7% 52 / 66 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 91.4% 32 / 35 Podpod 100% 21 / 21 totaltotal 90.0% 252 / 280 CC 39 SCAR 23.9
CC
39
Cov
89%
_dux
22.2
_idux
17.5
__ANON__
3.0
CRAP 10.9
« Data/Unixish/wc.pm ↑ Summary Data/Unixish/Apply.pm »
1 package Data::Unixish;
2  
3 49 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 49 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 49 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 49 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 49 ▶use Module::Load;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 49 ▶use Package::Util::Lite qw(package_exists);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 49 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
12 49 our $DATE = '2025-02-24'; # DATE
13 49 our $DIST = 'Data-Unixish'; # DIST
14 49 our $VERSION = '1.574'; # VERSION
15  
16 49 our @EXPORT_OK =
17 qw(
18 aduxa cduxa fduxa lduxa
19 aduxc cduxc fduxc lduxc
20 aduxf cduxf fduxf lduxf
21 aduxl cduxl fduxl lduxl
22  
23 siduxs
24 aiduxa aiduxl
25 liduxa liduxl
26 );
27 49 our %EXPORT_TAGS = (
28 all => [
29 qw/
30 aduxa cduxa fduxa lduxa
31 aduxc cduxc fduxc lduxc
32 aduxf cduxf fduxf lduxf
33 aduxl cduxl fduxl lduxl
34  
35 siduxs
36 aiduxa aiduxl
37 liduxa liduxl
38 /],
39 );
40  
41 51 ▶sub _dux {
Subroutine _dux: called CC=22 SCAR=31.0
42 51 my $accepts = shift;
43 51 my $returns = shift;
44  
45 51 my $func = shift;
46  
47 51 my %args;
48  
49 51 my ($icallback, $ocallback);
50 51 ▶ if ($accepts eq 'c') {
Branch: 1 branch
BranchTrueFalseTotal
if ($accepts eq "c") 6 45 51
51 6 $icallback = shift;
52 }
53 51 ▶ if ($returns eq 'c') {
Branch: 1 branch
BranchTrueFalseTotal
if ($returns eq "c") 4 47 51
54 4 $ocallback = shift;
55 }
56  
57 51 ▶ if ($accepts eq 'f') {
Branch: 4 branches
BranchTrueFalseTotal
if ($accepts eq 'f') { } 14 37 51
elsif ($accepts eq 'c') { } 6 31 37
elsif ($accepts eq 'l') { } 15 16 31
elsif ($accepts eq 'a') { } 16 0 16
58 14 require Tie::File;
59 14 my @in;
60 14 tie @in, "Tie::File", @_;
61 14 $args{in} = \@in;
62 } elsif ($accepts eq 'c') {
63 6 require Tie::Simple;
64 6 my @in;
65 my @els;
66 6 my $elcount = 0;
67 tie(@in, "Tie::Simple", undef,
68 20 ▶ FETCHSIZE => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
69 20 my $data = shift; # from Tie::Simple
70 20 my @res = $icallback->();
71 20 $elcount += @res;
72 20 push @els, @res;
73 #say "D: res=[".join(",", @res), "], elcount=$elcount";
74 20 $elcount;
75 },
76 15 ▶ FETCH => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
77 15 my $data = shift; # from Tie::Simple
78 15 shift @els;
79 }
80 6 );
81 6 $args{in} = \@in;
82 } elsif ($accepts eq 'l') {
83 15 $args{in} = \@_;
84 } elsif ($accepts eq 'a') {
85 16 $args{in} = $_[0];
86 } else {
87 0 die "Invalid accepts, must be a|c|f|l";
88 }
89  
90 51 ▶ if (ref($func) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $func eq "ARRAY") 10 41 51
91 10 $args{$_} = $func->[1]{$_} for grep {/\A\w+\z/} keys %{$func->[1]};
92 10 $func = $func->[0];
93 }
94  
95 51 my $pkg = "Data::Unixish::$func";
96 51 ▶ load $pkg unless package_exists($pkg);
Branch: 1 branch
BranchTrueFalseTotal
unless package_exists($pkg) 15 36 51
97 51 my $funcleaf = $func; $funcleaf =~ s/.+:://;
98 51 my $funcname = "Data::Unixish::$func\::$funcleaf";
99 51 ▶ die "Subroutine &$funcname not defined" unless defined &$funcname;
Branch: 1 branch
BranchTrueFalseTotal
unless defined &$funcname 0 51 51
100  
101 51 my @out;
102 my $kidfh;
103 51 my $pid;
104 51 ▶ if ($returns eq 'c') {
Branch: 2 branches
BranchTrueFalseTotal
if ($returns eq 'c') { } 4 47 51
elsif ($returns eq 'f') { } 14 33 47
105 4 require Tie::Simple;
106 tie @out, "Tie::Simple", undef,
107 13 ▶ PUSH => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
108 13 my $data = shift; # from Tie::Simple
109 13 $ocallback->($_) for @_;
110 4 };
111 4 $args{out} = \@out;
112 } elsif ($returns eq 'f') {
113 14 require Tie::Simple;
114 tie @out, "Tie::Simple", undef,
115 13 ▶ PUSH => sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
116 13 my $data = shift; # from Tie::Simple
117 13 for my $item (@_) {
118 13 ▶ $item .= "\n" unless $item =~ /\n\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $item =~ /\n\z/ 13 0 13
119 13 print STDOUT $item;
120 }
121 14 };
122 14 $args{out} = \@out;
123 14 $pid = open $kidfh, "-|";
124 14 ▶ defined $pid or die "Can't fork: $!";
Branch: 1 branch
BranchTrueFalseTotal
defined $pid or die "Can't fork: $!" 14 0 14
125 } else {
126 33 $args{out} = \@out;
127 }
128  
129 51 ▶ unless ($pid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pid) 41 10 51
130 49 ▶ no strict 'refs'; ## no critic: TestingAndDebugging::ProhibitNoStrict
Subroutine BEGIN: called CC=1 SCAR=0.0
131 41 my $res = $funcname->(%args);
132 41 ▶ die "Dux function $funcname failed: $res->[0] - $res->[1]"
Branch: 1 branch
BranchTrueFalseTotal
unless $res->[0] == 200 0 41 41
133 unless $res->[0] == 200;
134 }
135  
136 51 ▶ if ($returns eq 'l') {
Branch: 4 branches
BranchTrueFalseTotal
if ($returns eq 'l') { } 16 35 51
elsif ($returns eq 'a') { } 17 18 35
elsif ($returns eq 'c') { } 4 14 18
elsif ($returns eq 'f') { } 14 0 14
137 16 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 11 5 16
138 11 return @out;
139 } else {
140 5 return $out[0];
141 }
142 } elsif ($returns eq 'a') {
143 17 return \@out;
144 } elsif ($returns eq 'c') {
145 4 return;
146 } elsif ($returns eq 'f') {
147 14 ▶ if ($pid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) { } 10 4 14
148 10 return $kidfh;
149 } else {
150 4 exit;
151 }
152 } else {
153 0 die "Invalid returns, must be a|c|f|l";
154 }
155 }
156  
157 5 ▶sub aduxa { _dux('a', 'a', @_) }
Subroutine aduxa: called CC=1 SCAR=0.0
Pod documented
158 2 ▶sub cduxa { _dux('c', 'a', @_) }
Subroutine cduxa: called CC=1 SCAR=0.0
Pod documented
159 5 ▶sub fduxa { _dux('f', 'a', @_) }
Subroutine fduxa: called CC=1 SCAR=0.0
Pod documented
160 5 ▶sub lduxa { _dux('l', 'a', @_) }
Subroutine lduxa: called CC=1 SCAR=0.0
Pod documented
161  
162 1 ▶sub aduxc { _dux('a', 'c', @_) }
Subroutine aduxc: called CC=1 SCAR=0.0
Pod documented
163 1 ▶sub cduxc { _dux('c', 'c', @_) }
Subroutine cduxc: called CC=1 SCAR=0.0
Pod documented
164 1 ▶sub fduxc { _dux('f', 'c', @_) }
Subroutine fduxc: called CC=1 SCAR=0.0
Pod documented
165 1 ▶sub lduxc { _dux('l', 'c', @_) }
Subroutine lduxc: called CC=1 SCAR=0.0
Pod documented
166  
167 5 ▶sub aduxf { _dux('a', 'f', @_) }
Subroutine aduxf: called CC=1 SCAR=0.0
Pod documented
168 2 ▶sub cduxf { _dux('c', 'f', @_) }
Subroutine cduxf: called CC=1 SCAR=0.0
Pod documented
169 3 ▶sub fduxf { _dux('f', 'f', @_) }
Subroutine fduxf: called CC=1 SCAR=0.0
Pod documented
170 4 ▶sub lduxf { _dux('l', 'f', @_) }
Subroutine lduxf: called CC=1 SCAR=0.0
Pod documented
171  
172 5 ▶sub aduxl { _dux('a', 'l', @_) }
Subroutine aduxl: called CC=1 SCAR=0.0
Pod documented
173 1 ▶sub cduxl { _dux('c', 'l', @_) }
Subroutine cduxl: called CC=1 SCAR=0.0
Pod documented
174 5 ▶sub fduxl { _dux('f', 'l', @_) }
Subroutine fduxl: called CC=1 SCAR=0.0
Pod documented
175 5 ▶sub lduxl { _dux('l', 'l', @_) }
Subroutine lduxl: called CC=1 SCAR=0.0
Pod documented
176  
177 55 ▶sub _idux {
Subroutine _idux: called CC=15 SCAR=28.6
178 55 my $accepts = shift;
179 55 my $returns = shift;
180  
181 55 my $func = shift;
182  
183 55 my %args;
184 my @items;
185  
186 55 ▶ if ($accepts eq 's') {
Branch: 3 branches
BranchTrueFalseTotal
if ($accepts eq 's') { } 4 51 55
elsif ($accepts eq 'a') { } 51 0 51
elsif ($accepts eq 'l') { } 0 0 0
187 4 @items = ($_[0]);
188 } elsif ($accepts eq 'a') {
189 51 @items = @{ $_[0] };
190 } elsif ($accepts eq 'l') {
191 0 @items = @_;
192 } else {
193 0 die "Invalid accepts, must be a|l|s";
194 }
195  
196 55 ▶ if (ref($func) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $func eq "ARRAY") 51 4 55
197 51 $args{$_} = $func->[1]{$_} for keys %{$func->[1]};
198 51 $func = $func->[0];
199 }
200  
201 55 my $pkg = "Data::Unixish::$func";
202 55 ▶ load $pkg unless package_exists($pkg);
Branch: 1 branch
BranchTrueFalseTotal
unless package_exists($pkg) 2 53 55
203 55 my $funcleaf = $func; $funcleaf =~ s/.+:://;
204 55 my $funcname_i = "Data::Unixish::$func\::_${funcleaf}_item";
205 55 ▶ die "Subroutine &$funcname_i not defined" unless defined &$funcname_i;
Branch: 1 branch
BranchTrueFalseTotal
unless defined &$funcname_i 0 55 55
206 55 my $funcname_b = "Data::Unixish::$func\::_${funcleaf}_begin";
207 55 my $funcname_e = "Data::Unixish::$func\::_${funcleaf}_end";
208  
209 55 my @res;
210 {
211 49 ▶ no strict 'refs'; ## no critic: TestingAndDebugging::ProhibitNoStrict
Subroutine BEGIN: called CC=1 SCAR=0.0
212 55 ▶ my @bres; @bres = $funcname_b->(\%args) if defined &$funcname_b;
Branch: 1 branch
BranchTrueFalseTotal
if defined &$funcname_b 30 25 55
213 55 for (@items) {
214 225 push @res, $funcname_i->($_, \%args);
215 }
216 55 ▶ $funcname_e->(\%args, @bres) if defined &$funcname_e;
Branch: 1 branch
BranchTrueFalseTotal
if defined &$funcname_e 11 44 55
217 }
218  
219 55 ▶ if ($returns eq 's') {
Branch: 3 branches
BranchTrueFalseTotal
if ($returns eq 's') { } 4 51 55
elsif ($returns eq 'l') { } 0 51 51
elsif ($returns eq 'a') { } 51 0 51
220 4 return $res[0];
221 } elsif ($returns eq 'l') {
222 0 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
223 0 return @res;
224 } else {
225 0 return $res[0];
226 }
227 } elsif ($returns eq 'a') {
228 51 return \@res;
229 } else {
230 0 die "Invalid returns, must be a|l|s";
231 }
232 }
233  
234 4 ▶sub siduxs { _idux('s', 's', @_) }
Subroutine siduxs: called CC=1 SCAR=0.0
Pod documented
235  
236 51 ▶sub aiduxa { _idux('a', 'a', @_) }
Subroutine aiduxa: called CC=1 SCAR=0.0
Pod documented
237 0 ▶sub aiduxl { _idux('a', 'l', @_) }
Subroutine aiduxl: not called CC=1 SCAR=6.9
Pod documented
238  
239 0 ▶sub liduxa { _idux('l', 'a', @_) }
Subroutine liduxa: not called CC=1 SCAR=6.9
Pod documented
240 0 ▶sub liduxl { _idux('l', 'l', @_) }
Subroutine liduxl: not called CC=1 SCAR=6.9
Pod documented
241  
242 49 1;
243 # ABSTRACT: Implementation for Unixish, a data transformation framework
244  
245 __END__
« Data/Unixish/wc.pm ↑ Summary Data/Unixish/Apply.pm »