CSVUtils.pm

filter: Statementstmt 84.9% 788 / 928 Branchbran 59.4% 202 / 340 Conditioncond 51.5% 66 / 128 MC/DCmcdc 28.0% 21 / 75 Subroutinesub 88.1% 111 / 126 Podpod 100% 3 / 3 totaltotal 74.4% 1191 / 1600 CC 250 SCAR 38.3
CC
250
Cov
76%
_complete_field_or_field_list
182.0
__ANON__
125.2
__ANON__
62.8
CRAP 46.2
« CSVUtils/csv_check_field_values.pm ↑ Summary CSVUtils/csv_gen.pm »
1 package App::CSVUtils;
2  
3 57 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 57 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 57 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 57 ▶use Log::ger;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 57 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 57 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 57 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
12 57 our $DATE = '2026-07-09'; # DATE
13 57 our $DIST = 'App-CSVUtils'; # DIST
14 57 our $VERSION = '1.038'; # VERSION
15  
16 57 our @EXPORT_OK = qw(
17 gen_csv_util
18 compile_eval_code
19 eval_code
20 );
21  
22 57 our %SPEC;
23  
24 57 our $sch_req_str_or_code = ['any*', of=>['str*', 'code*']];
25  
26 146 ▶sub _open_file_read {
Subroutine _open_file_read: called CC=3 SCAR=12.6
27 146 my $filename = shift;
28  
29 146 my ($fh, $err);
30 146 ▶ if ($filename eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename eq '-') { } 0 146 146
31 0 $fh = *STDIN;
32 } else {
33 146 ▶ open $fh, "<", $filename or do {
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<", $filename or do { $err = [500, "Can't open input filename '${filename}': $!"]; goto RETURN } 146 0 146
34 0 $err = [500, "Can't open input filename '$filename': $!"];
35 0 goto RETURN;
36 };
37 }
38 146 binmode $fh, ":encoding(utf8)";
39  
40 146 RETURN:
41 ($fh, $err);
42 }
43  
44 2 ▶sub _open_file_write {
Subroutine _open_file_write: called CC=3 SCAR=12.6
45 2 my $filename = shift;
46  
47 2 my ($fh, $err);
48 2 ▶ if ($filename eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename eq '-') { } 0 2 2
49 0 $fh = *STDOUT;
50 } else {
51 2 ▶ open $fh, ">", $filename or do {
Branch: 1 branch
BranchTrueFalseTotal
open $fh, ">", $filename or do { $err = [500, "Can't open output filename '${filename}': $!"]; goto RETURN } 2 0 2
52 0 $err = [500, "Can't open output filename '$filename': $!"];
53 0 goto RETURN;
54 };
55 }
56 2 binmode $fh, ":encoding(utf8)";
57  
58 2 RETURN:
59 ($fh, $err);
60 }
61  
62 0 ▶sub _return_or_write_file {
Subroutine _return_or_write_file: not called CC=7 SCAR=40.3
63 0 my ($res, $filename, $overwrite) = @_;
64 0 ▶ return $res if !defined($filename);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $filename 0 0 0
65  
66 0 my $fh;
67 0 ▶ if ($filename eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename eq '-') { } 0 0 0
68 0 $fh = \*STDOUT;
69 } else {
70 0 ▶ if (-f $filename) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $filename) 0 0 0
71 0 ▶ if ($overwrite) {
Branch: 1 branch
BranchTrueFalseTotal
if ($overwrite) { } 0 0 0
72 0 log_info "[csvutil] Overwriting output file $filename";
73 } else {
74 0 return [412, "Refusing to ovewrite existing output file '$filename', please select another path or specify --overwrite"];
75 }
76 }
77 0 ▶ open my $fh, ">", $filename or do {
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $filename or do { return [500, "Can't open output file '${filename}': $!"] } 0 0 0
78 0 return [500, "Can't open output file '$filename': $!"];
79 };
80 0 binmode $fh, ":encoding(utf8)";
81 0 print $fh $res->[2];
82 0 ▶ close $fh or warn "Can't write to '$filename': $!";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or warn "Can't write to '${filename}': $!" 0 0 0
83 0 return [$res->[0], $res->[1]];
84 }
85 }
86  
87 45 ▶sub compile_eval_code {
Subroutine compile_eval_code: called CC=5 SCAR=16.1
Pod documented
88 45 ▶ return $_[0] if ref $_[0] eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] eq "CODE" 2 43 45
89 43 my ($str, $label) = @_;
90 43 ▶ defined($str) && length($str) or die [400, "Please specify code ($label)"];
Branch: 1 branch
BranchTrueFalseTotal
defined $str and length $str or die [400, "Please specify code ($label)"] 43 0 43
Condition33%1/3
defined $str and length $str
!ll&&!rl&&r
0043
MC/DC0%0/2
defined $str and length $str
defined $str length $str
Truth table33%1/3
A and B
A= defined $strB= length $str
ABresult
0X0
100
111
91 43 $str = "package main; no strict; no warnings; sub { $str }";
92 43 log_trace "[csvutil] Compiling Perl code: $str";
93 43 ▶ my $code = eval $str; ## no critic: BuiltinFunctions::ProhibitStringyEval
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
94 43 ▶ die [400, "Can't compile code ($label) '$str': $@"] if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 3 40 43
95 40 $code;
96 }
97  
98 60 ▶sub eval_code {
Subroutine eval_code: called CC=2 SCAR=6.9
Pod documented
99 57 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
100 60 my ($code, $r, $value_for_topic, $return_topic) = @_;
101 60 local $_ = $value_for_topic;
102 60 local $main::r = $r;
103 60 local $main::row = $r->{input_row};
104 60 local $main::rownum = $r->{input_rownum};
105 60 local $main::data_rownum = $r->{input_data_rownum};
106 60 local $main::csv = $r->{input_parser};
107 60 local $main::fields_idx = $r->{input_fields_idx};
108 60 ▶ if ($return_topic) {
Branch: 1 branch
BranchTrueFalseTotal
if ($return_topic) { } 9 51 60
109 9 $code->($_);
110 9 $_;
111 } else {
112 51 $code->($_);
113 }
114 }
115  
116 0 ▶sub _get_field_idx {
Subroutine _get_field_idx: not called CC=4 SCAR=30.0
117 0 my ($field, $field_idxs) = @_;
118 0 ▶ defined($field) && length($field) or die "Please specify at least a field\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $field and length $field or die "Please specify at least a field\n" 0 0 0
Condition0%0/3
defined $field and length $field
!ll&&!rl&&r
000
MC/DC0%0/2
defined $field and length $field
defined $field length $field
Truth table0%0/3
A and B
A= defined $fieldB= length $field
ABresult
0X0
100
111
119 0 my $idx = $field_idxs->{$field};
120 die "Unknown field '$field' (known fields include: ".
121 0 ▶ join(", ", map { "'$_'" } sort {$field_idxs->{$a} <=> $field_idxs->{$b}}
Branch: 1 branch
BranchTrueFalseTotal
unless defined $idx 0 0 0
122 keys %$field_idxs).")\n" unless defined $idx;
123 0 $idx;
124 }
125  
126 0 ▶sub _get_csv_row {
Subroutine _get_csv_row: not called CC=4 SCAR=30.0
127 0 my ($csv, $row, $i, $outputs_header) = @_;
128 #use DD; print " "; dd $row;
129 0 ▶ return "" if $i == 1 && !$outputs_header;
Branch: 1 branch
BranchTrueFalseTotal
if $i == 1 and not $outputs_header 0 0 0
Condition0%0/3
$i == 1 and not $outputs_header
!ll&&!rl&&r
000
MC/DC0%0/2
$i == 1 and not $outputs_header
$i == 1 not $outputs_header
Truth table0%0/3
A and not(B)
A= $i == 1B= not $outputs_header
ABresult
0X0
100
111
130 0 ▶ my $status = $csv->combine(@$row)
Branch: 1 branch
BranchTrueFalseTotal
my $status = $csv->combine(@$row) or die "Error in line ${i}: " . $csv->error_input . "\n" 0 0 0
131 or die "Error in line $i: ".$csv->error_input."\n";
132 0 $csv->string . "\n";
133 }
134  
135 0 ▶sub _instantiate_parser_default {
Subroutine _instantiate_parser_default: not called CC=1 SCAR=6.9
136 0 require Text::CSV_XS;
137  
138 0 Text::CSV_XS->new({binary=>1});
139 }
140  
141 219 ▶sub _instantiate_parser {
Subroutine _instantiate_parser: called CC=11 SCAR=24.8
142 219 require Text::CSV_XS;
143  
144 219 my ($args, $prefix) = @_;
145 219 ▶ $prefix //= '';
Condition50%1/2
$prefix //= ""
l!l
2190
MC/DC0%0/1
$prefix //= ""
$prefix
Truth table50%1/2
A
A= $prefix
Aresult
11
00
146  
147 219 my %tcsv_opts = (binary=>1);
148 219 ▶ if (defined $args->{"${prefix}sep_char"} ||
Branch: 2 branches
BranchTrueFalseTotal
if (defined $$args{"${prefix}sep_char"} or defined $$args{"${prefix}quote_char"} or defined $$args{"${prefix}escape_char"}) { } 1 218 219
elsif ($$args{"${prefix}tsv"}) { } 1 217 218
Condition66%4/6
defined $$args{"${prefix}sep_char"} or defined $$args{"${prefix}quote_char"} or defined $$args{"${prefix}escape_char"}
l!l&&r!l&&!r
10218
defined $$args{"${prefix}sep_char"} or defined $$args{"${prefix}quote_char"}
l!l&&r!l&&!r
10218
MC/DC33%1/3
defined $$args{"${prefix}sep_char"} or defined $$args{"${prefix}quote_char"} or defined $$args{"${prefix}escape_char"}
defined $$args{"${prefix}sep_char"} defined $$args{"${prefix}quote_char"} defined $$args{"${prefix}escape_char"}
Truth table50%2/4
A or B or C
A= defined $$args{"${prefix}sep_char"}B= defined $$args{"${prefix}quote_char"}C= defined $$args{"${prefix}escape_char"}
ABCresult
1XX1
01X1
0011
0000
149 defined $args->{"${prefix}quote_char"} ||
150 defined $args->{"${prefix}escape_char"}) {
151 1 ▶ $tcsv_opts{"sep_char"} = $args->{"${prefix}sep_char"} if defined $args->{"${prefix}sep_char"};
Branch: 1 branch
BranchTrueFalseTotal
if defined $$args{"${prefix}sep_char"} 1 0 1
152 1 ▶ $tcsv_opts{"quote_char"} = $args->{"${prefix}quote_char"} if defined $args->{"${prefix}quote_char"};
Branch: 1 branch
BranchTrueFalseTotal
if defined $$args{"${prefix}quote_char"} 0 1 1
153 1 ▶ $tcsv_opts{"escape_char"} = $args->{"${prefix}escape_char"} if defined $args->{"${prefix}escape_char"};
Branch: 1 branch
BranchTrueFalseTotal
if defined $$args{"${prefix}escape_char"} 0 1 1
154 } elsif ($args->{"${prefix}tsv"}) {
155 1 $tcsv_opts{"sep_char"} = "\t";
156 1 $tcsv_opts{"quote_char"} = undef;
157 1 $tcsv_opts{"escape_char"} = undef;
158 }
159 219 ▶ $tcsv_opts{always_quote} = 1 if $args->{"${prefix}always_quote"};
Branch: 1 branch
BranchTrueFalseTotal
if $$args{"${prefix}always_quote"} 1 218 219
160 219 ▶ $tcsv_opts{quote_empty} = 1 if $args->{"${prefix}quote_empty"};
Branch: 1 branch
BranchTrueFalseTotal
if $$args{"${prefix}quote_empty"} 0 219 219
161  
162 219 Text::CSV_XS->new(\%tcsv_opts);
163 }
164  
165 96 ▶sub _instantiate_emitter {
Subroutine _instantiate_emitter: called CC=1 SCAR=0.0
166 96 my $args = shift;
167 96 _instantiate_parser($args, 'output_');
168 }
169  
170 0 ▶sub _complete_field_or_field_list {
Subroutine _complete_field_or_field_list: not called CC=13 SCAR=52.0
171 # return list of known fields of a CSV
172  
173 0 my $which = shift;
174  
175 0 my %args = @_;
176 0 ▶ my $word = $args{word} // '';
Condition0%0/2
$args{"word"} // ""
l!l
00
MC/DC0%0/1
$args{"word"} // ""
$args{"word"}
Truth table0%0/2
A
A= $args{"word"}
Aresult
11
00
177 0 my $cmdline = $args{cmdline};
178 0 my $r = $args{r};
179  
180 # we are not called from cmdline, bail
181 0 ▶ return undef unless $cmdline; ## no critic: Subroutines::ProhibitExplicitReturnUndef
Branch: 1 branch
BranchTrueFalseTotal
unless $cmdline 0 0 0
182  
183 # let's parse argv first
184 0 my $args;
185 {
186 # this is not activated yet
187 0 $r->{read_config} = 1;
188  
189 0 my $res = $cmdline->parse_argv($r);
190 #return undef unless $res->[0] == 200;
191  
192 0 ▶ $cmdline->_read_config($r) unless $r->{config};
Branch: 1 branch
BranchTrueFalseTotal
unless $r->{"config"} 0 0 0
193 0 $args = $res->[2];
194 }
195  
196 # user hasn't specified -f, bail
197 0 ▶ return {message=>"Please specify input filename first"} unless defined $args && $args->{input_filename};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args and $args->{"input_filename"} 0 0 0
Condition0%0/3
defined $args and $args->{"input_filename"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $args and $args->{"input_filename"}
defined $args $args->{"input_filename"}
Truth table0%0/3
A and B
A= defined $argsB= $args->{"input_filename"}
ABresult
0X0
100
111
198  
199 # user wants to read CSV from stdin, bail
200 0 ▶ return {message=>"Can't get field list when input is stdin"} if $args->{input_filename} eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"input_filename"} eq "-" 0 0 0
201  
202 # can the file be opened?
203 0 my $csv_parser = _instantiate_parser(\%args, 'input_');
204 0 ▶ open my($fh), "<encoding(utf8)", $args->{input_filename} or do {
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<encoding(utf8)", $args->{"input_filename"} or do { return [] } 0 0 0
205 #warn "csvutils: Cannot open file '$args->{input_filename}': $!\n";
206 0 return [];
207 };
208  
209 # can the header row be read?
210 0 ▶ my $row = $csv_parser->getline($fh) or return [];
Branch: 1 branch
BranchTrueFalseTotal
my $row = $csv_parser->getline($fh) or return [] 0 0 0
211  
212 0 ▶ if (defined $args->{input_header} && !$args->{input_header}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{"input_header"} and not $args->{"input_header"}) 0 0 0
Condition0%0/3
defined $args->{"input_header"} and not $args->{"input_header"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $args->{"input_header"} and not $args->{"input_header"}
defined $args->{"input_header"} not $args->{"input_header"}
Truth table0%0/3
A and not(B)
A= defined $args->{"input_header"}B= not $args->{"input_header"}
ABresult
0X0
100
111
213 0 $row = [map {"field$_"} 1 .. @$row];
214 }
215  
216 0 ▶ if ($which =~ /sort/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($which =~ /sort/) 0 0 0
217 0 $row = [map {($_,"-$_","+$_","~$_")} @$row];
218 }
219  
220 0 require Complete::Util;
221 0 ▶ if ($which =~ /field_list/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($which =~ /field_list/) { } 0 0 0
222 0 return Complete::Util::complete_comma_sep(
223 word => $word,
224 elems => $row,
225 uniq => 1,
226 );
227 } else {
228 0 return Complete::Util::complete_array_elem(
229 word => $word,
230 array => $row,
231 );
232 }
233 }
234  
235 0 ▶sub _complete_field {
Subroutine _complete_field: not called CC=1 SCAR=6.9
236 0 _complete_field_or_field_list('field', @_);
237 }
238  
239 0 ▶sub _complete_field_list {
Subroutine _complete_field_list: not called CC=1 SCAR=6.9
240 0 _complete_field_or_field_list('field_list', @_);
241 }
242  
243 0 ▶sub _complete_sort_field_list {
Subroutine _complete_sort_field_list: not called CC=1 SCAR=6.9
244 0 _complete_field_or_field_list('sort_field_list', @_);
245 }
246  
247 0 ▶sub _complete_sort_field {
Subroutine _complete_sort_field: not called CC=1 SCAR=6.9
248 0 _complete_field_or_field_list('sort_field', @_);
249 }
250  
251 0 ▶sub _array2hash {
Subroutine _array2hash: not called CC=2 SCAR=17.9
252 0 my ($row, $fields) = @_;
253 0 my $rowhash = {};
254 0 for my $i (0..$#{$fields}) {
255 0 $rowhash->{ $fields->[$i] } = $row->[$i];
256 }
257 0 $rowhash;
258 }
259  
260 # check that the first N values of a field are all defined and numeric. if there
261 # are now rows or less than N values, return true.
262 0 ▶sub _is_numeric_field {
Subroutine _is_numeric_field: not called CC=5 SCAR=34.0
263 0 require Scalar::Util::Numeric;
264  
265 0 my ($rows, $field_idx, $num_samples) = @_;
266 0 ▶ $num_samples //= 5;
Condition0%0/2
$num_samples //= 5
l!l
00
MC/DC0%0/1
$num_samples //= 5
$num_samples
Truth table0%0/2
A
A= $num_samples
Aresult
11
00
267  
268 0 my $is_numeric = 1;
269 0 for my $row (@$rows) {
270 0 my $val = $row->[$field_idx];
271 0 ▶ return 0 unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 0 0
272 0 ▶ return 0 unless Scalar::Util::Numeric::isnum($val);
Branch: 1 branch
BranchTrueFalseTotal
unless Scalar::Util::Numeric::isnum($val) 0 0 0
273 }
274 0 $is_numeric;
275 }
276  
277 # find a single field by name or index (1-based), return index (0-based). die
278 # when requested field does not exist.
279 17 ▶sub _find_field {
Subroutine _find_field: called CC=8 SCAR=24.2
280 17 my ($fields, $name_or_idx) = @_;
281  
282 # search by name first
283 17 for my $i (0 .. $#{$fields}) {
284 29 my $field = $fields->[$i];
285 29 ▶ return $i if $field eq $name_or_idx;
Branch: 1 branch
BranchTrueFalseTotal
if $field eq $name_or_idx 15 14 29
286 }
287  
288 2 ▶ if ($name_or_idx eq '0') {
Branch: 3 branches
BranchTrueFalseTotal
if ($name_or_idx eq '0') { } 0 2 2
elsif ($name_or_idx =~ /\A[1-9][0-9]*\z/) { } 1 1 2
elsif ($name_or_idx =~ /\A-[1-9][0-9]*\z/) { } 0 1 1
289 0 die [400, "Field index 0 is requested, you probably meant 1 for the first field?"];
290 } elsif ($name_or_idx =~ /\A[1-9][0-9]*\z/) {
291 1 ▶ if ($name_or_idx > @$fields) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name_or_idx > @$fields) { } 0 1 1
292 0 die [400, "There are only ".scalar(@$fields)." field(s) but field index $name_or_idx is requested"];
293 } else {
294 1 return $name_or_idx-1;
295 }
296 } elsif ($name_or_idx =~ /\A-[1-9][0-9]*\z/) {
297 0 ▶ if (-$name_or_idx > @$fields) {
Branch: 1 branch
BranchTrueFalseTotal
if (-$name_or_idx > @$fields) { } 0 0 0
298 0 die [400, "There are only ".scalar(@$fields)." field(s) but field index $name_or_idx is requested"];
299 } else {
300 0 return @$fields + $name_or_idx;
301 }
302 }
303  
304 # not found
305 die [404, "Unknown field name/index '$name_or_idx' (known fields include: ".
306 1 join(", ", map { "'$_'" } @$fields).")"];
307 }
308  
309 # select one or more fields with options like --include-field, etc
310 12 ▶sub _select_fields {
Subroutine _select_fields: called CC=25 SCAR=39.6
311 12 my ($fields, $field_idxs, $args, $default_select_choice) = @_;
312  
313 12 my @selected_fields;
314  
315 my $select_field_options_used;
316  
317 12 ▶ if (defined $args->{include_field_pat}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{"include_field_pat"}) 4 8 12
318 4 $select_field_options_used++;
319 4 for my $field (@$fields) {
320 10 ▶ if ($field =~ $args->{include_field_pat}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field =~ /$args->{"include_field_pat"}/) 10 0 10
321 10 push @selected_fields, $field;
322 }
323 }
324 }
325 12 ▶ if (defined $args->{exclude_field_pat}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{"exclude_field_pat"}) 1 11 12
326 1 $select_field_options_used++;
327 1 @selected_fields = grep { $_ !~ $args->{exclude_field_pat} }
328 @selected_fields;
329 }
330 12 ▶ if (defined $args->{include_fields}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{"include_fields"}) 8 4 12
331 8 $select_field_options_used++;
332 FIELD:
333 8 for my $field (@{ $args->{include_fields} }) {
334 13 ▶ unless (defined $field_idxs->{$field}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $field_idxs->{$field}) 4 9 13
335 4 ▶ return [400, "Unknown field '$field'"] unless $args->{ignore_unknown_fields};
Branch: 1 branch
BranchTrueFalseTotal
unless $args->{"ignore_unknown_fields"} 2 2 4
336 2 next FIELD;
337 }
338 9 ▶ next if grep { $field eq $_ } @selected_fields;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$field eq $_;} @selected_fields 0 9 9
339 9 push @selected_fields, $field;
340 }
341 }
342 10 ▶ if (defined $args->{exclude_fields}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{"exclude_fields"}) 2 8 10
343 2 $select_field_options_used++;
344 FIELD:
345 2 for my $field (@{ $args->{exclude_fields} }) {
346 2 ▶ unless (defined $field_idxs->{$field}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $field_idxs->{$field}) 0 2 2
347 0 ▶ return [400, "Unknown field '$field'"] unless $args->{ignore_unknown_fields};
Branch: 1 branch
BranchTrueFalseTotal
unless $args->{"ignore_unknown_fields"} 0 0 0
348 0 next FIELD;
349 }
350 2 @selected_fields = grep { $field ne $_ } @selected_fields;
351 }
352 }
353  
354 10 ▶ if (!$select_field_options_used && $default_select_choice) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $select_field_options_used and $default_select_choice) 0 10 10
Condition33%1/3
not $select_field_options_used and $default_select_choice
!ll&&!rl&&r
1000
MC/DC0%0/2
not $select_field_options_used and $default_select_choice
not $select_field_options_used $default_select_choice
Truth table33%1/3
not(A) and B
A= not $select_field_options_usedB= $default_select_choice
ABresult
0X0
100
111
355 0 ▶ if ($default_select_choice eq 'all') {
Branch: 4 branches
BranchTrueFalseTotal
if ($default_select_choice eq 'all') { } 0 0 0
elsif ($default_select_choice eq 'first') { } 0 0 0
elsif ($default_select_choice eq 'last') { } 0 0 0
elsif ($default_select_choice eq 'first-if-only-field') { } 0 0 0
356 0 @selected_fields = @$fields;
357 } elsif ($default_select_choice eq 'first') {
358 0 ▶ @selected_fields = ($fields->[0]) if @$fields;
Branch: 1 branch
BranchTrueFalseTotal
if @$fields 0 0 0
359 } elsif ($default_select_choice eq 'last') {
360 0 ▶ @selected_fields = ($fields->[-1]) if @$fields;
Branch: 1 branch
BranchTrueFalseTotal
if @$fields 0 0 0
361 } elsif ($default_select_choice eq 'first-if-only-field') {
362 0 ▶ @selected_fields = ($fields->[0]) if @$fields == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @$fields == 1 0 0 0
363 }
364 }
365  
366 10 ▶ if ($args->{show_selected_fields}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"show_selected_fields"}) 1 9 10
367 1 return [200, "OK", \@selected_fields];
368 }
369  
370 #my %selected_field_idxs;
371 #$selected_field_idxs{$_} = $fields_idx->{$_} for @selected_fields;
372  
373 9 my @selected_field_idxs_array;
374 9 push @selected_field_idxs_array, $field_idxs->{$_} for @selected_fields;
375  
376 9 [100, "Continue", [\@selected_fields, \@selected_field_idxs_array]];
377 }
378  
379 57 our $xcomp_csvfiles = [filename => {file_ext_filter => qr/^[tc]sv$/i}];
380  
381 57 our %argspecs_csv_input = (
382 input_header => {
383 summary => 'Specify whether input CSV has a header row',
384 'summary.alt.bool.not' => 'Specify that input CSV does not have a header row',
385 schema => 'bool*',
386 default => 1,
387 description => <<'MARKDOWN',
388  
389 By default, the first row of the input CSV will be assumed to contain field
390 names (and the second row contains the first data row). When you declare that
391 input CSV does not have header row (`--no-input-header`), the first row of the
392 CSV is assumed to contain the first data row. Fields will be named `field1`,
393 `field2`, and so on.
394  
395 MARKDOWN
396 cmdline_aliases => {
397 },
398 tags => ['category:input'],
399 },
400 input_skip_num_lines => {
401 summary => 'Number of lines to skip before header row',
402 schema => 'posint*',
403 description => <<'MARKDOWN',
404  
405 This can be useful if you have a CSV files (usually some generated reports,
406 sometimes converted from spreadsheet) that have additional header lines or info
407 before the CSV header row.
408  
409 See also the alternative option: `--input-skip-until-pattern`.
410  
411 MARKDOWN
412 tags => ['category:input'],
413 },
414 input_skip_until_pattern => {
415 summary => 'Skip rows until the first header row matches a regex pattern',
416 schema => 're_from_str*',
417 description => <<'MARKDOWN',
418  
419 This is an alternative to the `--input-skip-num-lines` and can be useful if you
420 have a CSV files (usually some generated reports, sometimes converted from
421 spreadsheet) that have additional header lines or info before the CSV header
422 row.
423  
424 With `--input-skip-num-lines`, you skip a fixed number of lines. With this
425 option, rows will be skipped until the first field matches the specified regex
426 pattern.
427  
428 MARKDOWN
429 tags => ['category:input'],
430 },
431 input_skip_before_num_data_rows => {
432 summary => 'Skip a certain number of data rows for each input file',
433 schema => 'uint*',
434 description => <<'MARKDOWN',
435  
436 This option can be used to skip the first certain number of data rows. If set to
437 1, for example, will only process 1 data row for each input file. This option is
438 a convenient alternative to composing with <pm:csv-tail>.
439  
440 MARKDOWN
441 tags => ['category:input'],
442 },
443 input_skip_file_after_num_data_rows => {
444 summary => 'Limit processing each input file to this number of data rows',
445 schema => 'uint*',
446 description => <<'MARKDOWN',
447  
448 This option can be used to limit processing only to a certain number of data
449 rows. If set to 1, for example, will only process 1 data row for each input
450 file. This option is a convenient alternative to composing with <pm:csv-head>.
451  
452 MARKDOWN
453 tags => ['category:input'],
454 },
455 input_tsv => {
456 summary => "Inform that input file is in TSV (tab-separated) format instead of CSV",
457 schema => 'true*',
458 description => <<'MARKDOWN',
459  
460 Overriden by `--input-sep-char`, `--input-quote-char`, `--input-escape-char`
461 options. If one of those options is specified, then `--input-tsv` will be
462 ignored.
463  
464 MARKDOWN
465 tags => ['category:input'],
466 },
467 input_sep_char => {
468 summary => 'Specify field separator character in input CSV, will be passed to Text::CSV_XS',
469 schema => ['str*', len=>1],
470 description => <<'MARKDOWN',
471  
472 Defaults to `,` (comma). Overrides `--input-tsv` option.
473  
474 MARKDOWN
475 tags => ['category:input'],
476 },
477 input_quote_char => {
478 summary => 'Specify field quote character in input CSV, will be passed to Text::CSV_XS',
479 schema => ['str*', len=>1],
480 description => <<'MARKDOWN',
481  
482 Defaults to `"` (double quote). Overrides `--input-tsv` option.
483  
484 MARKDOWN
485 tags => ['category:input'],
486 },
487 input_escape_char => {
488 summary => 'Specify character to escape value in field in input CSV, will be passed to Text::CSV_XS',
489 schema => ['str*', len=>1],
490 description => <<'MARKDOWN',
491  
492 Defaults to `\\` (backslash). Overrides `--input-tsv` option.
493  
494 MARKDOWN
495 tags => ['category:input'],
496 },
497 );
498  
499 57 our %argspecs_csv_output = (
500 output_header => {
501 summary => 'Whether output CSV should have a header row',
502 schema => 'bool*',
503 description => <<'MARKDOWN',
504  
505 By default, a header row will be output *if* input CSV has header row. Under
506 `--output-header`, a header row will be output even if input CSV does not have
507 header row (value will be something like "col0,col1,..."). Under
508 `--no-output-header`, header row will *not* be printed even if input CSV has
509 header row. So this option can be used to unconditionally add or remove header
510 row.
511  
512 MARKDOWN
513 tags => ['category:output'],
514 },
515 output_tsv => {
516 summary => "Inform that output file is TSV (tab-separated) format instead of CSV",
517 schema => 'bool*',
518 description => <<'MARKDOWN',
519  
520 This is like `--input-tsv` option but for output instead of input.
521  
522 Overriden by `--output-sep-char`, `--output-quote-char`, `--output-escape-char`
523 options. If one of those options is specified, then `--output-tsv` will be
524 ignored.
525  
526 MARKDOWN
527 tags => ['category:output'],
528 },
529 output_sep_char => {
530 summary => 'Specify field separator character in output CSV, will be passed to Text::CSV_XS',
531 schema => ['str*', len=>1],
532 description => <<'MARKDOWN',
533  
534 This is like `--input-sep-char` option but for output instead of input.
535  
536 Defaults to `,` (comma). Overrides `--output-tsv` option.
537  
538 MARKDOWN
539 tags => ['category:output'],
540 },
541 output_quote_char => {
542 summary => 'Specify field quote character in output CSV, will be passed to Text::CSV_XS',
543 schema => ['str*', len=>1],
544 description => <<'MARKDOWN',
545  
546 This is like `--input-quote-char` option but for output instead of input.
547  
548 Defaults to `"` (double quote). Overrides `--output-tsv` option.
549  
550 MARKDOWN
551 tags => ['category:output'],
552 },
553 output_escape_char => {
554 summary => 'Specify character to escape value in field in output CSV, will be passed to Text::CSV_XS',
555 schema => ['str*', len=>1],
556 description => <<'MARKDOWN',
557  
558 This is like `--input-escape-char` option but for output instead of input.
559  
560 Defaults to `\\` (backslash). Overrides `--output-tsv` option.
561  
562 MARKDOWN
563 tags => ['category:output'],
564 },
565 output_always_quote => {
566 summary => 'Whether to always quote values',
567 schema => 'bool*',
568 default => 0,
569 description => <<'MARKDOWN',
570  
571 When set to false (the default), values are quoted only when necessary:
572  
573 field1,field2,"field three contains comma (,)",field4
574  
575 When set to true, then all values will be quoted:
576  
577 "field1","field2","field three contains comma (,)","field4"
578  
579 MARKDOWN
580 tags => ['category:output'],
581 },
582 output_quote_empty => {
583 summary => 'Whether to quote empty values',
584 schema => 'bool*',
585 default => 0,
586 description => <<'MARKDOWN',
587  
588 When set to false (the default), empty values are not quoted:
589  
590 field1,field2,,field4
591  
592 When set to true, then empty values will be quoted:
593  
594 field1,field2,"",field4
595  
596 MARKDOWN
597 tags => ['category:output'],
598 },
599 );
600  
601 57 our %argspecopt_input_filename = (
602 input_filename => {
603 summary => 'Input CSV file',
604 description => <<'MARKDOWN',
605  
606 Use `-` to read from stdin.
607  
608 Encoding of input file is assumed to be UTF-8.
609  
610 MARKDOWN
611 schema => 'filename*',
612 default => '-',
613 'x.completion' => $xcomp_csvfiles,
614 tags => ['category:input'],
615 },
616 );
617  
618 57 our %argspecopt_input_filenames = (
619 input_filenames => {
620 'x.name.is_plural' => 1,
621 'x.name.singular' => 'input_filename',
622 summary => 'Input CSV files',
623 description => <<'MARKDOWN',
624  
625 Use `-` to read from stdin.
626  
627 Encoding of input file is assumed to be UTF-8.
628  
629 MARKDOWN
630 schema => ['array*', of=>'filename*'],
631 default => ['-'],
632 'x.completion' => $xcomp_csvfiles,
633 tags => ['category:input'],
634 },
635 );
636  
637 57 our %argspecopt_overwrite = (
638 overwrite => {
639 summary => 'Whether to override existing output file',
640 schema => 'bool*',
641 cmdline_aliases=>{O=>{}},
642 tags => ['category:output'],
643 },
644 );
645  
646 57 our %argspecsopt_inplace = (
647 inplace => {
648 summary => 'Output to the same file as input',
649 schema => 'true*',
650 description => <<'MARKDOWN',
651  
652 Normally, you output to a different file than input. If you try to output to the
653 same file (`-o INPUT.csv -O`) you will clobber the input file; thus the utility
654 prevents you from doing it. However, with this `--inplace` option, you can
655 output to the same file. Like perl's `-i` option, this will first output to a
656 temporary file in the same directory as the input file then rename to the final
657 file at the end. You cannot specify output file (`-o`) when using this option,
658 but you can specify backup extension with `-b` option.
659  
660 Some caveats:
661  
662 - if input file is a symbolic link, it will be replaced with a regular file;
663 - renaming (implemented using `rename()`) can fail if input filename is too long;
664 - value specified in `-b` is currently not checked for acceptable characters;
665 - things can also fail if permissions are restrictive;
666  
667 MARKDOWN
668 tags => ['category:output'],
669 },
670 inplace_backup_ext => {
671 summary => 'Extension to add for backup of input file',
672 schema => 'str*',
673 default => '',
674 description => <<'MARKDOWN',
675  
676 In inplace mode (`--inplace`), if this option is set to a non-empty string, will
677 rename the input file using this extension as a backup. The old existing backup
678 will be overwritten, if any.
679  
680 MARKDOWN
681 cmdline_aliases => {b=>{}},
682 tags => ['category:output'],
683 },
684 );
685  
686 57 our %argspecopt_output_filename = (
687 output_filename => {
688 summary => 'Output filename',
689 description => <<'MARKDOWN',
690  
691 Use `-` to output to stdout (the default if you don't specify this option).
692  
693 Encoding of output file is assumed to be UTF-8.
694  
695 MARKDOWN
696 schema => 'filename*',
697 cmdline_aliases=>{o=>{}},
698 tags => ['category:output'],
699 },
700 );
701  
702 57 our %argspecopt_output_filenames = (
703 output_filenames => {
704 summary => 'Output filenames',
705 description => <<'MARKDOWN',
706  
707 Use `-` to output to stdout (the default if you don't specify this option).
708  
709 Encoding of output file is assumed to be UTF-8.
710  
711 MARKDOWN
712 schema => ['array*', of=>'filename*'],
713 cmdline_aliases=>{o=>{}},
714 tags => ['category:output'],
715 },
716 );
717  
718 57 our %argspecopt_field = (
719 field => {
720 summary => 'Field name',
721 schema => 'str*',
722 cmdline_aliases => { f=>{} },
723 completion => \&_complete_field,
724 },
725 );
726  
727 57 our %argspecopt_field_1 = (
728 field => {
729 summary => 'Field name',
730 schema => 'str*',
731 pos => 1,
732 cmdline_aliases => { f=>{} },
733 completion => \&_complete_field,
734 },
735 );
736  
737 57 our %argspec_field_1 = (
738 field => {
739 summary => 'Field name',
740 schema => 'str*',
741 cmdline_aliases => { f=>{} },
742 req => 1,
743 pos => 1,
744 completion => \&_complete_field,
745 },
746 );
747  
748 57 our %argspec_fields_1plus = (
749 fields => {
750 'x.name.is_plural' => 1,
751 'x.name.singular' => 'field',
752 summary => 'Field names',
753 schema => ['array*', of=>['str*', min_len=>1], min_len=>1],
754 req => 1,
755 pos => 1,
756 slurpy => 1,
757 cmdline_aliases => {f=>{}},
758 element_completion => \&_complete_field,
759 },
760 );
761  
762 # without completion, for adding new field
763 57 our %argspec_field_1_nocomp = (
764 field => {
765 summary => 'Field name',
766 schema => 'str*',
767 cmdline_aliases => { f=>{} },
768 req => 1,
769 pos => 1,
770 },
771 );
772  
773 # without completion, for adding new fields
774 57 our %argspec_fields_1plus_nocomp = (
775 fields => {
776 'x.name.is_plural' => 1,
777 'x.name.singular' => 'field',
778 summary => 'Field names',
779 'summary.alt.plurality.singular' => 'Field name',
780 schema => ['array*', of=>['str*', min_len=>1], min_len=>1],
781 cmdline_aliases => { f=>{} },
782 req => 1,
783 pos => 1,
784 slurpy => 1,
785 },
786 );
787  
788 57 our %argspec_fields = (
789 fields => {
790 'x.name.is_plural' => 1,
791 'x.name.singular' => 'field',
792 summary => 'Field names',
793 schema => ['array*', of=>['str*', min_len=>1], min_len=>1],
794 req => 1,
795 cmdline_aliases => {f=>{}},
796 element_completion => \&_complete_field,
797 },
798 );
799  
800 57 our %argspecopt_fields = (
801 fields => {
802 'x.name.is_plural' => 1,
803 'x.name.singular' => 'field',
804 summary => 'Field names',
805 schema => ['array*', of=>['str*', min_len=>1], min_len=>1],
806 cmdline_aliases => {f=>{}},
807 element_completion => \&_complete_field,
808 },
809 );
810  
811 our %argspecsopt_field_selection = (
812 include_fields => {
813 'x.name.is_plural' => 1,
814 'x.name.singular' => 'include_field',
815 summary => 'Field names to include, takes precedence over --exclude-field-pat',
816 schema => ['array*', of=>'str*'],
817 cmdline_aliases => {
818 f => {},
819 field => {}, # backward compatibility
820 },
821 element_completion => \&_complete_field,
822 tags => ['category:field-selection'],
823 },
824 include_field_pat => {
825 summary => 'Field regex pattern to select, overidden by --exclude-field-pat',
826 schema => 're*',
827 cmdline_aliases => {
828 field_pat => {}, # backward compatibility
829 0 ▶ include_all_fields => { summary => 'Shortcut for --field-pat=.*, effectively selecting all fields', is_flag=>1, code => sub { $_[0]{include_field_pat} = '.*' } },
Subroutine __ANON__: not called CC=1 SCAR=6.9
830 0 ▶ a => { summary => 'Shortcut for --field-pat=.*, effectively selecting all fields', is_flag=>1, code => sub { $_[0]{include_field_pat} = '.*' } },
Subroutine __ANON__: not called CC=1 SCAR=6.9
831 },
832 tags => ['category:field-selection'],
833 },
834 exclude_fields => {
835 'x.name.is_plural' => 1,
836 'x.name.singular' => 'exclude_field',
837 summary => 'Field names to exclude, takes precedence over --fields',
838 schema => ['array*', of=>'str*'],
839 cmdline_aliases => {
840 F => {},
841 },
842 element_completion => \&_complete_field,
843 tags => ['category:field-selection'],
844 },
845 exclude_field_pat => {
846 summary => 'Field regex pattern to exclude, takes precedence over --field-pat',
847 schema => 're*',
848 cmdline_aliases => {
849 0 ▶ exclude_all_fields => { summary => 'Shortcut for --exclude-field-pat=.*, effectively excluding all fields', is_flag=>1, code => sub { $_[0]{exclude_field_pat} = '.*' } },
Subroutine __ANON__: not called CC=1 SCAR=6.9
850 0 ▶ A => { summary => 'Shortcut for --exclude-field-pat=.*, effectively excluding all fields', is_flag=>1, code => sub { $_[0]{exclude_field_pat} = '.*' } },
Subroutine __ANON__: not called CC=1 SCAR=6.9
851 },
852 57 tags => ['category:field-selection'],
853 },
854 ignore_unknown_fields => {
855 summary => 'When unknown fields are specified in --include-field (--field) or --exclude-field options, ignore them instead of throwing an error',
856 schema => 'bool*',
857 },
858 show_selected_fields => {
859 summary => 'Show selected fields and then immediately exit',
860 schema => 'true*',
861 },
862 );
863  
864 57 our %argspec_eval = (
865 eval => {
866 summary => 'Perl code',
867 schema => $sch_req_str_or_code,
868 cmdline_aliases => { e=>{} },
869 req => 1,
870 },
871 );
872  
873 57 our %argspecopt_eval = (
874 eval => {
875 summary => 'Perl code',
876 schema => $sch_req_str_or_code,
877 cmdline_aliases => { e=>{} },
878 },
879 );
880  
881 57 our %argspec_eval_1 = (
882 eval => {
883 summary => 'Perl code',
884 schema => $sch_req_str_or_code,
885 cmdline_aliases => { e=>{} },
886 req => 1,
887 pos => 1,
888 },
889 );
890  
891 57 our %argspec_eval_2 = (
892 eval => {
893 summary => 'Perl code',
894 schema => $sch_req_str_or_code,
895 cmdline_aliases => { e=>{} },
896 req => 1,
897 pos => 2,
898 },
899 );
900  
901 57 our %argspecopt_eval_2 = (
902 eval => {
903 summary => 'Perl code',
904 schema => $sch_req_str_or_code,
905 cmdline_aliases => { e=>{} },
906 pos => 2,
907 },
908 );
909  
910 57 our %argspecsopt_sortsub = (
911 by_sortsub => {
912 schema => 'str*',
913 description => <<'MARKDOWN',
914  
915 When sorting rows, usually combined with `--key` because most Sort::Sub routine
916 expects a string to be compared against.
917  
918 When sorting fields, the Sort::Sub routine will get the field name as argument.
919  
920 MARKDOWN
921 summary => 'Sort using a Sort::Sub routine',
922 'x.completion' => ['sortsub_spec'],
923 },
924 sortsub_args => {
925 summary => 'Arguments to pass to Sort::Sub routine',
926 schema => ['hash*', of=>'str*'],
927 },
928 );
929  
930 57 our %argspecopt_key = (
931 key => {
932 summary => 'Generate sort keys with this Perl code',
933 description => <<'MARKDOWN',
934  
935 If specified, then will compute sort keys using Perl code and sort using the
936 keys. Relevant when sorting using `--by-code` or `--by-sortsub`. If specified,
937 then instead of row when sorting rows, the code (or Sort::Sub routine) will
938 receive these sort keys to sort against.
939  
940 The code will receive the row (arrayref, or if -H is specified, hashref) as the
941 argument.
942  
943 MARKDOWN
944 schema => $sch_req_str_or_code,
945 cmdline_aliases => {k=>{}},
946 },
947 );
948  
949 57 our %argspecs_sort_rows = (
950 reverse => {
951 schema => ['bool', is=>1],
952 cmdline_aliases => {r=>{}},
953 },
954 ci => {
955 schema => ['bool', is=>1],
956 cmdline_aliases => {i=>{}},
957 },
958 by_fields => {
959 summary => 'Sort by a list of field specifications',
960 'summary.alt.plurality.singular' => 'Add a sort field specification',
961 'x.name.is_plural' => 1,
962 'x.name.singular' => 'by_field',
963 description => <<'MARKDOWN',
964  
965 Each field specification is a field name with an optional prefix. `FIELD`
966 (without prefix) means sort asciibetically ascending (smallest to largest),
967 `~FIELD` means sort asciibetically descending (largest to smallest), `+FIELD`
968 means sort numerically ascending, `-FIELD` means sort numerically descending.
969  
970 MARKDOWN
971 schema => ['array*', of=>'str*'],
972 element_completion => \&_complete_sort_field,
973 },
974 by_code => {
975 summary => 'Sort by using Perl code',
976 schema => $sch_req_str_or_code,
977 description => <<'MARKDOWN',
978  
979 `$a` and `$b` (or the first and second argument) will contain the two rows to be
980 compared. Which are arrayrefs; or if `--hash` (`-H`) is specified, hashrefs; or
981 if `--key` is specified, whatever the code in `--key` returns.
982  
983 MARKDOWN
984 },
985 %argspecopt_key,
986 %argspecsopt_sortsub,
987 );
988  
989 57 our %argspecs_sort_fields = (
990 reverse => {
991 schema => ['bool', is=>1],
992 cmdline_aliases => {r=>{}},
993 },
994 ci => {
995 schema => ['bool', is=>1],
996 cmdline_aliases => {i=>{}},
997 },
998 by_examples => {
999 summary => 'Sort by a list of field names as examples',
1000 'summary.alt.plurality.singular' => 'Add a field to sort by example',
1001 'x.name.is_plural' => 1,
1002 'x.name.singular' => 'by_example',
1003 schema => ['array*', of=>'str*'],
1004 element_completion => \&_complete_field,
1005 },
1006 by_code => {
1007 summary => 'Sort fields using Perl code',
1008 schema => $sch_req_str_or_code,
1009 description => <<'MARKDOWN',
1010  
1011 `$a` and `$b` (or the first and second argument) will contain `[$field_name,
1012 $field_idx]`.
1013  
1014 MARKDOWN
1015 },
1016 %argspecsopt_sortsub,
1017 );
1018  
1019 57 our %argspecopt_with_data_rows = (
1020 with_data_rows => {
1021 summary => 'Whether to also output data rows',
1022 schema => 'bool',
1023 },
1024 );
1025  
1026 57 our %argspecopt_hash = (
1027 hash => {
1028 summary => 'Provide row in $_ as hashref instead of arrayref',
1029 schema => ['bool*', is=>1],
1030 cmdline_aliases => {H=>{}},
1031 },
1032 );
1033  
1034 # add a position to specified argument, if possible
1035 135 ▶sub _add_arg_pos {
Subroutine _add_arg_pos: called CC=13 SCAR=25.7
1036 135 my ($args, $argname, $is_slurpy) = @_;
1037  
1038 # argument already has a position, return
1039 135 ▶ return if defined $args->{$argname}{pos};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args->{$argname}{"pos"} 0 135 135
1040  
1041 # position of slurpy argument
1042 135 my $slurpy_pos;
1043 135 for (keys %$args) {
1044 2147 ▶ next unless $args->{$_}{slurpy};
Branch: 1 branch
BranchTrueFalseTotal
unless $args->{$_}{"slurpy"} 2132 15 2147
1045 15 $slurpy_pos = $args->{$_}{pos};
1046 15 last;
1047 }
1048  
1049 # there is already a slurpy arg, return
1050 135 ▶ return if $is_slurpy && defined $slurpy_pos;
Branch: 1 branch
BranchTrueFalseTotal
if $is_slurpy and defined $slurpy_pos 0 135 135
Condition66%2/3
$is_slurpy and defined $slurpy_pos
!ll&&!rl&&r
12780
MC/DC0%0/2
$is_slurpy and defined $slurpy_pos
$is_slurpy defined $slurpy_pos
Truth table66%2/3
A and B
A= $is_slurpyB= defined $slurpy_pos
ABresult
0X0
100
111
1051  
1052 # find the lowest position that's not available
1053 ARG:
1054 135 for my $j (0 .. scalar(keys %$args)-1) {
1055 191 ▶ last if defined $slurpy_pos && $j >= $slurpy_pos;
Branch: 1 branch
BranchTrueFalseTotal
if defined $slurpy_pos and $j >= $slurpy_pos 9 182 191
Condition100%3/3
defined $slurpy_pos and $j >= $slurpy_pos
!ll&&!rl&&r
17489
MC/DC100%2/2
defined $slurpy_pos and $j >= $slurpy_pos
defined $slurpy_pos $j >= $slurpy_pos
Truth table100%3/3
A and B
A= defined $slurpy_posB= $j >= $slurpy_pos
ABresult
0X0
100
111
1056 182 for (keys %$args) {
1057 2764 ▶ next ARG if defined $args->{$_}{pos} && $args->{$_}{pos} == $j;
Branch: 1 branch
BranchTrueFalseTotal
if defined $args->{$_}{"pos"} and $args->{$_}{"pos"} == $j 56 2708 2764
Condition100%3/3
defined $args->{$_}{"pos"} and $args->{$_}{"pos"} == $j
!ll&&!rl&&r
26238556
MC/DC100%2/2
defined $args->{$_}{"pos"} and $args->{$_}{"pos"} == $j
defined $args->{$_}{"pos"} $args->{$_}{"pos"} == $j
Truth table100%3/3
A and B
A= defined $args->{$_}{"pos"}B= $args->{$_}{"pos"} == $j
ABresult
0X0
100
111
1058 }
1059 126 $args->{$argname}{pos} = $j;
1060 126 ▶ $args->{$argname}{slurpy} = 1 if $is_slurpy;
Branch: 1 branch
BranchTrueFalseTotal
if $is_slurpy 8 118 126
1061 126 last;
1062 }
1063 }
1064  
1065 2 ▶sub _randext {
Subroutine _randext: called CC=2 SCAR=6.9
1066 2 state $charset = [0..9, "A".."Z","a".."z"];
1067 2 my $len = shift;
1068 2 my $ext = "";
1069 2 for (1..$len) { $ext .= $charset->[rand @$charset] }
1070 2 $ext;
1071 }
1072  
1073 $SPEC{gen_csv_util} = {
1074 57 v => 1.1,
1075 summary => 'Generate a CSV utility',
1076 description => <<'MARKDOWN',
1077  
1078 This routine is used to generate a CSV utility in the form of a <pm:Rinci>
1079 function (code and metadata). You can then produce a CLI from the Rinci function
1080 simply using <pm:Perinci::CmdLine::Gen> or, if you use <pm:Dist::Zilla>,
1081 <pm:Dist::Zilla::Plugin::GenPericmdScript> or, if on the command-line,
1082 <prog:gen-pericmd-script>.
1083  
1084 Using this routine, by providing just one or a few hooks and setting some
1085 parameters like a couple of extra arguments, you will get a complete CLI with
1086 decent POD/manpage, ability to read one or multiple CSV's and write one or
1087 multiple CSV's, some command-line options to customize how the input CSV's
1088 should be parsed and how the output CSV's should be formatted and named. Your
1089 CLI also has tab completion, usage and help message, and other features.
1090  
1091 To create a CSV utility, you specify a `name` (e.g. `csv_dump`; must be a valid
1092 unqualified Perl identifier/function name) and optionally `summary`,
1093 `description`, and other metadata like `links` or even `add_meta_props`. Then
1094 you specify one or more of `on_*` or `before_*` or `after_*` arguments to supply
1095 handlers (coderefs) for your CSV utility at various hook points.
1096  
1097  
1098 *THE HOOKS*
1099  
1100 All code for hooks should accept a single argument `r`. `r` is a stash (hashref)
1101 of various data, the keys of which will depend on which hook point being called.
1102 You can also add more keys to store data or for flow control (see hook
1103 documentation below for more details).
1104  
1105 The order of the hooks, in processing chronological order:
1106  
1107 * on_begin
1108  
1109 Called when utility begins, before reading CSV. You can use this hook e.g. to
1110 process arguments, set output filenames (if you allow custom output
1111 filenames).
1112  
1113 * before_read_input
1114  
1115 Called before opening any input CSV file. This hook is *still* called even if
1116 your utility sets `reads_csv` to false.
1117  
1118 At this point, the `input_filenames` stash key (as well as other keys like
1119 `input_filename`, `input_filenum`, etc) has not been set. You can use this
1120 hook e.g. to set a custom `input_filenames`.
1121  
1122 * before_open_input_files
1123  
1124 Called before an input CSV file is about to be opened, including for stdin
1125 (`-`). You can use this hook e.g. to check/preprocess input file. Flow control
1126 is available by setting `$r->{wants_skip_files}` to skip reading all the input
1127 files and go directly to the `after_read_input` hook.
1128  
1129 * before_open_input_file
1130  
1131 Called before each input CSV file is about to be opened, including for stdin
1132 (`-`). For the first file, called after `before_open_input_file` hook. You can
1133 use this hook e.g. to check/preprocess input file. Flow control is available
1134 by setting `$r->{wants_skip_file}` to skip reading a single input file and go
1135 to the next file, or `$r->{wants_skip_files}` to skip reading the rest of the
1136 files and go directly to the `after_read_input` hook.
1137  
1138 * on_input_header_row
1139  
1140 Called when receiving header row. Will be called for every input file, and
1141 called even when user specify `--no-input-header`, in which case the header
1142 row will be the generated `["field1", "field2", ...]`. You can use this hook
1143 e.g. to add/remove/rearrange fields.
1144  
1145 You can set `$r->{wants_fill_rows}` to a defined false if you do not want
1146 `$r->{input_rows}` to be filled with empty string elements when it contains
1147 less than the number of fields (in case of sparse values at the end). Normally
1148 you only want to do this when you want to do checking, e.g. in
1149 <prog:csv-check-rows>.
1150  
1151 * on_input_data_row
1152  
1153 Called when receiving each data row. You can use this hook e.g. to modify the
1154 row or print output (for line-by-line transformation or filtering).
1155  
1156 * after_close_input_file
1157  
1158 Called after each input file is closed, including for stdin (`-`) (although
1159 for stdin, the handle is not actually closed). Flow control is possible by
1160 setting `$r->{wants_skip_files}` to skip reading the rest of the files and go
1161 straight to the `after_close_input_files` hook.
1162  
1163 * after_close_input_files
1164  
1165 Called after the last input file is closed, after the last
1166 `after_close_input_file` hook, including for stdin (`-`) (although for stdin,
1167 the handle is not actually closed).
1168  
1169 * after_read_input
1170  
1171 Called after the last row of the last CSV file is read and the last file is
1172 closed. This hook is *still* called, if you set `reads_csv` option to false.
1173 At this point the stash keys related to CSV reading have all been cleared,
1174 including `input_filenames`, `input_filename`, `input_fh`, etc.
1175  
1176 You can use this hook e.g. to print output if you buffer the output.
1177  
1178 * on_end
1179  
1180 Called when utility is about to exit. You can use this hook e.g. to return the
1181 final result.
1182  
1183  
1184 *THE STASH*
1185  
1186 The common keys that `r` will contain:
1187  
1188 - `gen_args`, hash. The arguments used to generate the CSV utility.
1189  
1190 - `util_args`, hash. The arguments that your CSV utility accepts. Parsed from
1191 command-line arguments (or configuration files, or environment variables).
1192  
1193 - `name`, str. The name of the CSV utility. Which can also be retrieved via
1194 `gen_args`.
1195  
1196 - `code_print`, coderef. Routine provided for you to print something. Accepts a
1197 string. Takes care of opening the output files for you.
1198  
1199 - `code_print_row`, coderef. Routine provided for you to print a data row. You
1200 pass the row (either arrayref or hashref). Takes care of opening the output
1201 files for you, as well as printing header row the first time, if needed.
1202  
1203 - `code_print_header_row`, coderef. Routine provided for you to print header
1204 row. You don't need to pass any arguments. Will only print the header row once
1205 per output file if output header is enabled, even if called multiple times.
1206  
1207 If you are accepting CSV data (`reads_csv` gen argument set to true), the
1208 following keys will also be available (in `on_input_header_row` and
1209 `on_input_data_row` hooks):
1210  
1211 - `input_parser`, a <pm:Text::CSV_XS> instance for input parsing.
1212  
1213 - `input_fields`, array of str. Input CSV's field names.
1214  
1215 - `input_fields_idx`, hash with field name as keys and field index (0-based
1216 integer) as values.
1217  
1218 - `input_filenames`, array of str.
1219  
1220 - `input_filename`, str. The name of the current input file being read (`-` if
1221 reading from stdin).
1222  
1223 - `input_filenum`, uint. The number of the current input file, 1 being the first
1224 file, 2 for the second, and so on.
1225  
1226 - `input_fh`, the handle to the current file being read.
1227  
1228 - `input_rownum`, uint. The number of rows that have been read (reset after each
1229 input file). In `on_input_header_row` phase, this will be 1 since header row
1230 (including the generated one) is the first row. Then in `on_input_data_row`
1231 phase (called the first time for a file), it will be 2 for the first data row,
1232 even if physically it is the first row for CSV file that does not have a
1233 header.
1234  
1235 - `input_data_rownum`, uint. The number of data rows that have been read (reset
1236 after each input file). This will be equal to `input_rownum` less 1 if input
1237 file has header.
1238  
1239 - `input_row`, aos (array of str). The current input CSV row as an arrayref.
1240  
1241 - `input_row_as_hashref`, hos (hash of str). The current input CSV row as a
1242 hashref, with field names as hash keys and field values as hash values. This
1243 will only be calculated if utility wants it. Utility can express so by setting
1244 `$r->{wants_input_row_as_hashref}` to true, e.g. in the `on_begin` hook.
1245  
1246 - `input_header_row_count`, uint. Contains the number of actual header rows that
1247 have been read. If CLI user specifies `--no-input-header`, this will stay at
1248 zero. Will be reset for each CSV file.
1249  
1250 - `input_data_row_count`, int. Contains the number of actual data rows that have
1251 read. Will be reset for each CSV file.
1252  
1253 If you are outputting CSV (`writes_csv` gen argument set to true), the following
1254 keys will be available:
1255  
1256 - `output_emitter`, a <pm:Text::CSV_XS> instance for output.
1257  
1258 - `output_fields`, array of str. Should be set to list of output field names. If
1259 unset, will be set to be the same as `input_fields`.
1260  
1261 - `output_fields_idx`, hash with field names as keys and field indexes (0-based
1262 integer) as values. Normally you do not need to set this manually; you just
1263 need to set `output_fields` and this hash will be computed automatically for
1264 you just before the first output row is outputted.
1265  
1266 - `output_filenames`, array of str.
1267  
1268 - `output_filename`, str, name of current output file.
1269  
1270 - `output_filenum`, uint, the number of the current output file, 1 being the
1271 first file, 2 for the second, and so on.
1272  
1273 - `output_fh`, handle to the current output file.
1274  
1275 - `output_rownum`, uint. The number of rows that have been outputted (reset
1276 after each output file).
1277  
1278 - `output_data_rownum`, uint. The number of data rows that have been outputted
1279 (reset after each output file). This will be equal to `input_rownum` less 1 if
1280 input file has header.
1281  
1282 For other hook-specific keys, see the documentation for associated hook point.
1283  
1284  
1285 *ACCEPTING ADDITIONAL COMMAND-LINE OPTIONS/ARGUMENTS*
1286  
1287 As mentioned above, you will get additional command-line options/arguments in
1288 `$r->{util_args}` hashref. Some options/arguments are already added by
1289 `gen_csv_util`, e.g. `input_filename` or `input_filenames` along with
1290 `input_sep_char`, etc (when your utility declares `reads_csv`),
1291 `output_filename` or `output_filenames` along with `overwrite`,
1292 `output_sep_char`, etc (when your utility declares `writes_csv`).
1293  
1294 If you want to accept additional arguments/options, you specify them in
1295 `add_args` (hashref, with key being Each option/argument has to be specified
1296 first via `add_args` (as hashref, with key being argument name and value the
1297 argument specification as defined in <pm:Rinci::function>)). Some argument
1298 specifications have been defined in <pm:App::CSVUtils> and can be used. See
1299 existing utilities for examples.
1300  
1301  
1302 *READING CSV DATA*
1303  
1304 To read CSV data, normally your utility would provide handler for the
1305 `on_input_data_row` hook and sometimes additionally `on_input_header_row`.
1306  
1307  
1308 *OUTPUTTING STRING OR RETURNING RESULT*
1309  
1310 To output string, usually you call the provided routine `$r->{code_print}`. This
1311 routine will open the output files for you.
1312  
1313 You can also return enveloped result directly by setting `$r->{result}`.
1314  
1315  
1316 *OUTPUTTING CSV DATA*
1317  
1318 To output CSV data, usually you call the provided routine `$r->{code_print_row}`.
1319 This routine accepts a row (arrayref or hashref). This routine will open the
1320 output files for you when needed, as well as print header row automatically.
1321  
1322 You can also buffer rows from input to e.g. `$r->{output_rows}`, then call
1323 `$r->{code_print_row}` repeatedly in the `after_read_input` hook to print all the
1324 rows.
1325  
1326  
1327 *READING MULTIPLE CSV FILES*
1328  
1329 To read multiple CSV files, you first specify `reads_multiple_csv`. Then, you
1330 can supply handler for `on_input_header_row` and `on_input_data_row` as usual.
1331 If you want to do something before/after each input file, you can also supply
1332 handler for `before_open_input_file` or `after_close_input_file`.
1333  
1334  
1335 *WRITING TO MULTIPLE CSV FILES*
1336  
1337 Similarly, to write to many CSv files, you first specify `writes_multiple_csv`.
1338 Then, you can supply handler for `on_input_header_row` and `on_input_data_row`
1339 as usual. To switch to the next file, set
1340 `$r->{wants_switch_to_next_output_file}` to true, in which case the next call to
1341 `$r->{code_print_row}` will close the current file and open the next file.
1342  
1343  
1344 *CHANGING THE OUTPUT FIELDS*
1345  
1346 When calling `$r->{code_print_row}`, you can output whatever fields you want. By
1347 convention, you can set `$r->{output_fields}` and `$r->{output_fields_idx}` to
1348 let other handlers know about the output fields. For example, see the
1349 implementation of <prog:csv-concat>.
1350  
1351 MARKDOWN
1352 args => {
1353 name => {
1354 schema => 'perl::identifier::unqualified_ascii*',
1355 req => 1,
1356 tags => ['category:metadata'],
1357 },
1358 summary => {
1359 schema => 'str*',
1360 tags => ['category:metadata'],
1361 },
1362 description => {
1363 schema => 'str*',
1364 tags => ['category:metadata'],
1365 },
1366 links => {
1367 schema => ['array*', of=>'hash*'], # XXX defhashes
1368 tags => ['category:metadata'],
1369 },
1370 examples => {
1371 schema => ['array*'], # defhashes
1372 tags => ['category:metadata'],
1373 },
1374 add_meta_props => {
1375 summary => 'Add additional Rinci function metadata properties',
1376 schema => ['hash*'],
1377 tags => ['category:metadata'],
1378 },
1379 add_args => {
1380 schema => ['hash*'],
1381 tags => ['category:metadata'],
1382 },
1383 add_args_rels => {
1384 schema => ['hash*'],
1385 tags => ['category:metadata'],
1386 },
1387  
1388 reads_csv => {
1389 summary => 'Whether utility reads CSV data',
1390 'summary.alt.bool.not' => 'Specify that utility does not read CSV data',
1391 schema => 'bool*',
1392 default => 1,
1393 },
1394 reads_multiple_csv => {
1395 summary => 'Whether utility accepts CSV data',
1396 schema => 'bool*',
1397 description => <<'MARKDOWN',
1398  
1399 Setting this option to true will implicitly set the `reads_csv` option to true,
1400 obviously.
1401  
1402 MARKDOWN
1403 },
1404 writes_csv => {
1405 summary => 'Whether utility writes CSV data',
1406 'summary.alt.bool.not' => 'Specify that utility does not write CSV data',
1407 schema => 'bool*',
1408 default => 1,
1409 },
1410 writes_multiple_csv => {
1411 summary => 'Whether utility outputs CSV data',
1412 schema => 'bool*',
1413 description => <<'MARKDOWN',
1414  
1415 Setting this option to true will implicitly set the `writes_csv` option to true,
1416 obviously.
1417  
1418 MARKDOWN
1419 },
1420  
1421 on_begin => {
1422 schema => 'code*',
1423 },
1424 before_read_input => {
1425 schema => 'code*',
1426 },
1427 before_open_input_files => {
1428 schema => 'code*',
1429 },
1430 before_open_input_file => {
1431 schema => 'code*',
1432 },
1433 on_input_header_row => {
1434 schema => 'code*',
1435 },
1436 on_input_data_row => {
1437 schema => 'code*',
1438 },
1439 after_close_input_file => {
1440 schema => 'code*',
1441 },
1442 after_close_input_files => {
1443 schema => 'code*',
1444 },
1445 after_read_input => {
1446 schema => 'code*',
1447 },
1448 on_end => {
1449 schema => 'code*',
1450 },
1451 },
1452 result_naked => 1,
1453 result => {
1454 schema => 'bool*',
1455 },
1456 };
1457 82 ▶sub gen_csv_util {
Subroutine gen_csv_util: called CC=33 SCAR=37.4
Pod documented
1458 82 my %gen_args = @_;
1459  
1460 82 ▶ my $name = delete($gen_args{name}) or die "Please specify name";
Branch: 1 branch
BranchTrueFalseTotal
my $name = delete $gen_args{"name"} or die "Please specify name" 82 0 82
1461 82 ▶ my $summary = delete($gen_args{summary}) // '(No summary)';
Condition50%1/2
delete $gen_args{"summary"} // "(No summary)"
l!l
820
MC/DC0%0/1
delete $gen_args{"summary"} // "(No summary)"
delete $gen_args{"summary"}
Truth table50%1/2
A
A= delete $gen_args{"summary"}
Aresult
11
00
1462 82 ▶ my $description = delete($gen_args{description}) // '(No description)';
Condition100%2/2
delete $gen_args{"description"} // "(No description)"
l!l
766
MC/DC100%1/1
delete $gen_args{"description"} // "(No description)"
delete $gen_args{"description"}
Truth table100%2/2
A
A= delete $gen_args{"description"}
Aresult
11
00
1463 82 ▶ my $links = delete($gen_args{links}) // [];
Condition100%2/2
delete $gen_args{"links"} // []
l!l
1765
MC/DC100%1/1
delete $gen_args{"links"} // []
delete $gen_args{"links"}
Truth table100%2/2
A
A= delete $gen_args{"links"}
Aresult
11
00
1464 82 ▶ my $examples = delete($gen_args{examples}) // [];
Condition100%2/2
delete $gen_args{"examples"} // []
l!l
5131
MC/DC100%1/1
delete $gen_args{"examples"} // []
delete $gen_args{"examples"}
Truth table100%2/2
A
A= delete $gen_args{"examples"}
Aresult
11
00
1465 82 my $add_meta_props = delete $gen_args{add_meta_props};
1466 82 my $add_args = delete $gen_args{add_args};
1467 82 my $add_args_rels = delete $gen_args{add_args_rels};
1468 82 my $reads_multiple_csv = delete($gen_args{reads_multiple_csv});
1469 82 ▶ my $reads_csv = delete($gen_args{reads_csv}) // 1;
Condition100%2/2
delete $gen_args{"reads_csv"} // 1
l!l
280
MC/DC100%1/1
delete $gen_args{"reads_csv"} // 1
delete $gen_args{"reads_csv"}
Truth table100%2/2
A
A= delete $gen_args{"reads_csv"}
Aresult
11
00
1470 82 ▶ my $tags = [ @{ delete($gen_args{tags}) // [] } ];
Condition50%1/2
delete $gen_args{"tags"} // []
l!l
820
MC/DC0%0/1
delete $gen_args{"tags"} // []
delete $gen_args{"tags"}
Truth table50%1/2
A
A= delete $gen_args{"tags"}
Aresult
11
00
1471 82 ▶ $reads_csv = 1 if $reads_multiple_csv;
Branch: 1 branch
BranchTrueFalseTotal
if $reads_multiple_csv 7 75 82
1472 82 my $writes_multiple_csv = delete($gen_args{writes_multiple_csv});
1473 82 ▶ my $writes_csv = delete($gen_args{writes_csv}) // 1;
Condition100%2/2
delete $gen_args{"writes_csv"} // 1
l!l
2854
MC/DC100%1/1
delete $gen_args{"writes_csv"} // 1
delete $gen_args{"writes_csv"}
Truth table100%2/2
A
A= delete $gen_args{"writes_csv"}
Aresult
11
00
1474 82 ▶ $writes_csv = 1 if $writes_multiple_csv;
Branch: 1 branch
BranchTrueFalseTotal
if $writes_multiple_csv 1 81 82
1475 82 my $on_begin = delete $gen_args{on_begin};
1476 82 my $before_read_input = delete $gen_args{before_read_input};
1477 82 my $before_open_input_files = delete $gen_args{before_open_input_files};
1478 82 my $before_open_input_file = delete $gen_args{before_open_input_file};
1479 82 my $on_input_header_row = delete $gen_args{on_input_header_row};
1480 82 my $on_input_data_row = delete $gen_args{on_input_data_row};
1481 82 my $after_close_input_file = delete $gen_args{after_close_input_file};
1482 82 my $after_close_input_files = delete $gen_args{after_close_input_files};
1483 82 my $after_read_input = delete $gen_args{after_read_input};
1484 82 my $on_end = delete $gen_args{on_end};
1485  
1486 82 ▶ scalar(keys %gen_args) and die "Unknown argument(s): ".join(", ", keys %gen_args);
Branch: 1 branch
BranchTrueFalseTotal
scalar keys %gen_args and die 'Unknown argument(s): ' . join(", ", keys %gen_args) 0 82 82
1487  
1488 82 my $code;
1489 CREATE_CODE: {
1490 82 ▶ $code = sub {
Subroutine __ANON__: called CC=67 SCAR=48.3
1491 123 my %util_args = @_;
1492  
1493 123 ▶ my $has_header = $util_args{input_header} // 1;
Condition100%2/2
$util_args{"input_header"} // 1
l!l
4119
MC/DC100%1/1
$util_args{"input_header"} // 1
$util_args{"input_header"}
Truth table100%2/2
A
A= $util_args{"input_header"}
Aresult
11
00
1494 123 ▶ my $outputs_header = $util_args{output_header} // $has_header;
Condition33%1/3
$util_args{"output_header"} // $has_header
l!l&&r!l&&!r
01230
MC/DC0%0/2
$util_args{"output_header"} // $has_header
$util_args{"output_header"} $has_header
Truth table33%1/3
A // B
A= $util_args{"output_header"}B= $has_header
ABresult
1X1
011
000
1495  
1496 123 my $r = {
1497 gen_args => \%gen_args,
1498 util_args => \%util_args,
1499 name => $name,
1500 };
1501  
1502 # inside the main eval block, we call hook handlers. A handler can
1503 # throw an exception (which can be a string or an enveloped response
1504 # like [500, "some error message"], see Rinci::function). we trap
1505 # the exception so we can return the appropriate enveloped response.
1506 MAIN_EVAL:
1507 123 eval {
1508  
1509 # do some checking
1510 123 ▶ if ($util_args{inplace} && (!$reads_csv || !$writes_csv)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($util_args{"inplace"} and not $reads_csv && $writes_csv) 0 123 123
Condition50%3/6
$util_args{"inplace"} and not $reads_csv && $writes_csv
!ll&&!rl&&r
12120
$reads_csv && $writes_csv
!ll&&!rl&&r
002
MC/DC0%0/3
$util_args{"inplace"} and not $reads_csv && $writes_csv
$util_args{"inplace"} $reads_csv $writes_csv
Truth table50%2/4
A and not(B && C)
A= $util_args{"inplace"}B= $reads_csvC= $writes_csv
ABCresult
0XX0
1110
10X1
1101
1511 0 die [412, "--inplace cannot be specified when we do not read & write CSV"];
1512 }
1513  
1514 123 ▶ if ($on_begin) {
Branch: 1 branch
BranchTrueFalseTotal
if ($on_begin) 37 86 123
1515 37 log_trace "[csvutil] Calling on_begin hook handler ...";
1516 37 $on_begin->($r);
1517 }
1518  
1519 217 ▶ my $code_open_file = sub {
Subroutine __ANON__: called CC=31 SCAR=41.4
1520 # set output filenames, if not yet
1521 217 ▶ unless ($r->{output_filenames}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($r->{"output_filenames"}) 81 136 217
1522 81 my @output_filenames;
1523 81 ▶ if ($util_args{inplace}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($util_args{'inplace'}) { } 2 79 81
elsif ($writes_multiple_csv) { } 0 79 79
1524 2 for my $input_filename (@{ $r->{input_filenames} }) {
1525 2 my $output_filename;
1526 2 while (1) {
1527 2 $output_filename = $input_filename . "." . _randext(5);
1528 2 ▶ last unless -e $output_filename;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $output_filename 2 0 2
1529 }
1530 2 push @output_filenames, $output_filename;
1531 }
1532 } elsif ($writes_multiple_csv) {
1533 0 ▶ @output_filenames = @{ $util_args{output_filenames} // ['-'] };
Condition0%0/2
$util_args{"output_filenames"} // ["-"]
l!l
00
MC/DC0%0/1
$util_args{"output_filenames"} // ["-"]
$util_args{"output_filenames"}
Truth table0%0/2
A
A= $util_args{"output_filenames"}
Aresult
11
00
1534 } else {
1535 79 ▶ @output_filenames = ($util_args{output_filename} // '-');
Condition50%1/2
$util_args{"output_filename"} // "-"
l!l
079
MC/DC0%0/1
$util_args{"output_filename"} // "-"
$util_args{"output_filename"}
Truth table50%1/2
A
A= $util_args{"output_filename"}
Aresult
11
00
1536 }
1537  
1538 CHECK_OUTPUT_FILENAME_SAME_AS_INPUT_FILENAME: {
1539 81 my %seen_output_abs_path; # key = output filename
1540 81 ▶ last unless $reads_csv && $writes_csv;
Branch: 1 branch
BranchTrueFalseTotal
unless $reads_csv and $writes_csv 3 78 81
Condition66%2/3
$reads_csv and $writes_csv
!ll&&!rl&&r
0378
MC/DC50%1/2
$reads_csv and $writes_csv
$reads_csv $writes_csv
Truth table66%2/3
A and B
A= $reads_csvB= $writes_csv
ABresult
0X0
100
111
1541 78 for my $input_filename (@{ $r->{input_filenames} }) {
1542 84 ▶ next if $input_filename eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $input_filename eq "-" 0 84 84
1543 84 my $input_abs_path = Cwd::abs_path($input_filename);
1544 84 ▶ die [500, "Can't get absolute path of input filename '$input_filename'"] unless $input_abs_path;
Branch: 1 branch
BranchTrueFalseTotal
unless $input_abs_path 0 84 84
1545 84 for my $output_filename (@output_filenames) {
1546 84 ▶ next if $output_filename eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $output_filename eq "-" 82 2 84
1547 2 ▶ next if $seen_output_abs_path{$output_filename};
Branch: 1 branch
BranchTrueFalseTotal
if $seen_output_abs_path{$output_filename} 0 2 2
1548 2 my $output_abs_path = Cwd::abs_path($output_filename);
1549 2 ▶ die [500, "Can't get absolute path of output filename '$output_filename'"] unless $output_abs_path;
Branch: 1 branch
BranchTrueFalseTotal
unless $output_abs_path 0 2 2
1550 2 ▶ die [412, "Cannot set output filename to '$output_filename' ".
Branch: 2 branches
BranchTrueFalseTotal
if $output_abs_path eq $input_abs_path 0 2 2
$output_filename ne $output_abs_path ? : 0 0 0
1551 ($output_filename ne $output_abs_path ? "($output_abs_path) ":"").
1552 "because it is the same as input filename and input will be clobbered; use --inplace to avoid clobbering<"]
1553 if $output_abs_path eq $input_abs_path;
1554 }
1555 }
1556 } # CHECK_OUTPUT_FILENAME_SAME_AS_INPUT_FILENAME
1557  
1558 81 $r->{output_filenames} = \@output_filenames;
1559 81 ▶ $r->{output_num_of_files} //= scalar(@output_filenames);
Condition50%1/2
$r->{"output_num_of_files"} //= scalar @output_filenames
l!l
081
MC/DC0%0/1
$r->{"output_num_of_files"} //= scalar @output_filenames
$r->{"output_num_of_files"}
Truth table50%1/2
A
A= $r->{"output_num_of_files"}
Aresult
11
00
1560 } # set output filenames
1561  
1562 # open the next file, if not yet
1563 217 ▶ if (!$r->{output_fh} || $r->{wants_switch_to_next_output_file}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $r->{"output_fh"} or $r->{"wants_switch_to_next_output_file"}) 81 136 217
Condition66%2/3
not $r->{"output_fh"} or $r->{"wants_switch_to_next_output_file"}
l!l&&r!l&&!r
810136
MC/DC50%1/2
not $r->{"output_fh"} or $r->{"wants_switch_to_next_output_file"}
not $r->{"output_fh"} $r->{"wants_switch_to_next_output_file"}
Truth table66%2/3
not(A) or B
A= not $r->{"output_fh"}B= $r->{"wants_switch_to_next_output_file"}
ABresult
1X1
011
000
1564 81 ▶ $r->{output_filenum} //= 0;
Condition50%1/2
$r->{"output_filenum"} //= 0
l!l
081
MC/DC0%0/1
$r->{"output_filenum"} //= 0
$r->{"output_filenum"}
Truth table50%1/2
A
A= $r->{"output_filenum"}
Aresult
11
00
1565 81 $r->{output_filenum}++;
1566  
1567 81 $r->{output_rownum} = 0;
1568 81 $r->{output_data_rownum} = 0;
1569  
1570 # close the previous file, if any
1571 81 ▶ if ($r->{output_fh} && $r->{output_filename} ne '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{"output_fh"} and $r->{"output_filename"} ne "-") 0 81 81
Condition33%1/3
$r->{"output_fh"} and $r->{"output_filename"} ne "-"
!ll&&!rl&&r
8100
MC/DC0%0/2
$r->{"output_fh"} and $r->{"output_filename"} ne "-"
$r->{"output_fh"} $r->{"output_filename"} ne "-"
Truth table33%1/3
A and B
A= $r->{"output_fh"}B= $r->{"output_filename"} ne "-"
ABresult
0X0
100
111
1572 0 log_info "[csvutil] Closing output file '$r->{output_filename}' ...";
1573 0 ▶ close $r->{output_fh} or die [500, "Can't close output file '$r->{output_filename}': $!"];
Branch: 1 branch
BranchTrueFalseTotal
close $r->{"output_fh"} or die [500, qq[Can't close output file '$r->{"output_filename"}': $!]] 0 0 0
1574 0 delete $r->{has_printed_header};
1575 0 delete $r->{wants_switch_to_next_output_file};
1576 }
1577  
1578 # we have exhausted all the files, do nothing & return
1579 81 ▶ return if $r->{output_filenum} > @{ $r->{output_filenames} };
Branch: 1 branch
BranchTrueFalseTotal
if $r->{"output_filenum"} > @{$r->{"output_filenames"};} 0 81 81
1580  
1581 81 $r->{output_filename} = $r->{output_filenames}[ $r->{output_filenum}-1 ];
1582 log_info "[csvutil] [%d/%s] Opening output file %s ...",
1583 81 $r->{output_filenum}, $r->{output_num_of_files}, $r->{output_filename};
1584 81 ▶ if ($r->{output_filename} eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{'output_filename'} eq '-') { } 79 2 81
1585 79 $r->{output_fh} = \*STDOUT;
1586 } else {
1587 2 ▶ if (-f $r->{output_filename}) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $r->{"output_filename"}) 0 2 2
1588 0 ▶ if ($r->{util_args}{overwrite}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{'util_args'}{'overwrite'}) { } 0 0 0
1589 0 log_info "[csvutil] Will be overwriting output file %s", $r->{output_filename};
1590 } else {
1591 0 die [412, "Refusing to overwrite existing output file '$r->{output_filename}', choose another name or use --overwrite (-O)"];
1592 }
1593 }
1594 2 my ($fh, $err) = _open_file_write($r->{output_filename});
1595 2 ▶ die $err if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 2 2
1596 2 $r->{output_fh} = $fh;
1597 }
1598 } # open the next file
1599 123 }; # code_open_file
1600  
1601 9 ▶ my $code_print = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1602 9 my $str = shift;
1603 9 $code_open_file->();
1604 9 print { $r->{output_fh} } $str;
1605 123 }; # code_print
1606 123 $r->{code_print} = $code_print;
1607  
1608 123 ▶ if ($writes_csv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($writes_csv) 96 27 123
1609 96 my $output_emitter = _instantiate_emitter(\%util_args);
1610 96 $r->{output_emitter} = $output_emitter;
1611 96 $r->{has_printed_header} = 0;
1612  
1613 208 ▶ my $code_print_header_row = sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
1614 # set output fields, if not yet
1615 208 ▶ unless ($r->{output_fields}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($r->{"output_fields"}) 24 184 208
1616 # by default, use the
1617 24 $r->{output_fields} = $r->{input_fields};
1618 }
1619  
1620 # index the output fields, if not yet
1621 208 ▶ unless ($r->{output_fields_idx}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($r->{"output_fields_idx"}) 75 133 208
1622 75 $r->{output_fields_idx} = {};
1623 75 for my $j (0 .. $#{ $r->{output_fields} }) {
1624 206 $r->{output_fields_idx}{ $r->{output_fields}[$j] } = $j;
1625 }
1626 }
1627  
1628 208 $code_open_file->();
1629  
1630 # print header line, if not yet
1631 208 ▶ if ($outputs_header && !$r->{has_printed_header}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($outputs_header and not $r->{"has_printed_header"}) 76 132 208
Condition100%3/3
$outputs_header and not $r->{"has_printed_header"}
!ll&&!rl&&r
412876
MC/DC100%2/2
$outputs_header and not $r->{"has_printed_header"}
$outputs_header not $r->{"has_printed_header"}
Truth table100%3/3
A and not(B)
A= $outputs_headerB= not $r->{"has_printed_header"}
ABresult
0X0
100
111
1632 76 $r->{has_printed_header}++;
1633 76 $r->{output_emitter}->print($r->{output_fh}, $r->{output_fields});
1634 76 print { $r->{output_fh} } "\n";
1635 76 $r->{output_rownum}++;
1636 }
1637 96 };
1638 96 $r->{code_print_header_row} = $code_print_header_row;
1639  
1640 208 ▶ my $code_print_row = sub {
Subroutine __ANON__: called CC=5 SCAR=19.8
1641 208 my $row = shift;
1642  
1643 208 $code_print_header_row->();
1644  
1645 # print data line
1646 208 ▶ if ($row) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row) 208 0 208
1647 208 ▶ if (ref $row eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $row eq "HASH") 0 208 208
1648 0 my $row0 = $row;
1649 0 $row = [];
1650 0 for my $j (0 .. $#{ $r->{output_fields} }) {
1651 0 ▶ $row->[$j] = $row0->{ $r->{output_fields}[$j] } // '';
Condition0%0/2
$$row0{$r->{"output_fields"}[$j]} // ""
l!l
00
MC/DC0%0/1
$$row0{$r->{"output_fields"}[$j]} // ""
$$row0{$r->{"output_fields"}[$j]}
Truth table0%0/2
A
A= $$row0{$r->{"output_fields"}[$j]}
Aresult
11
00
1652 }
1653 }
1654 208 $r->{output_emitter}->print( $r->{output_fh}, $row );
1655 208 print { $r->{output_fh} } "\n";
1656 208 $r->{output_rownum}++;
1657 208 $r->{output_data_rownum}++;
1658 }
1659 96 }; # code_print_row
1660 96 $r->{code_print_row} = $code_print_row;
1661 } # if outputs csv
1662  
1663 123 ▶ if ($before_read_input) {
Branch: 1 branch
BranchTrueFalseTotal
if ($before_read_input) 0 123 123
1664 0 log_trace "[csvutil] Calling before_read_input handler ...";
1665 0 $before_read_input->($r);
1666 }
1667  
1668 READ_CSV: {
1669 123 ▶ last unless $reads_csv;
Branch: 1 branch
BranchTrueFalseTotal
unless $reads_csv 0 123 123
1670  
1671 123 my $input_parser = _instantiate_parser(\%util_args, 'input_');
1672 123 $r->{input_parser} = $input_parser;
1673  
1674 123 my @input_filenames;
1675 123 ▶ if ($reads_multiple_csv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reads_multiple_csv) { } 20 103 123
1676 20 ▶ @input_filenames = @{ $util_args{input_filenames} // ['-'] };
Condition50%1/2
$util_args{"input_filenames"} // ["-"]
l!l
200
MC/DC0%0/1
$util_args{"input_filenames"} // ["-"]
$util_args{"input_filenames"}
Truth table50%1/2
A
A= $util_args{"input_filenames"}
Aresult
11
00
1677 } else {
1678 103 ▶ @input_filenames = ($util_args{input_filename} // '-');
Condition50%1/2
$util_args{"input_filename"} // "-"
l!l
1030
MC/DC0%0/1
$util_args{"input_filename"} // "-"
$util_args{"input_filename"}
Truth table50%1/2
A
A= $util_args{"input_filename"}
Aresult
11
00
1679 }
1680 123 ▶ $r->{input_filenames} //= \@input_filenames;
Condition50%1/2
$r->{"input_filenames"} //= \@input_filenames
l!l
0123
MC/DC0%0/1
$r->{"input_filenames"} //= \@input_filenames
$r->{"input_filenames"}
Truth table50%1/2
A
A= $r->{"input_filenames"}
Aresult
11
00
1681  
1682 BEFORE_INPUT_FILENAME:
1683 123 $r->{input_filenum} = 0;
1684  
1685 INPUT_FILENAME:
1686 123 for my $input_filename (@input_filenames) {
1687 146 $r->{input_filenum}++;
1688 146 $r->{input_filename} = $input_filename;
1689 146 $r->{input_file_input_has_been_skipped} = 0;
1690  
1691 146 ▶ if ($r->{input_filenum} == 1 && $before_open_input_files) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{"input_filenum"} == 1 and $before_open_input_files) 3 143 146
Condition100%3/3
$r->{"input_filenum"} == 1 and $before_open_input_files
!ll&&!rl&&r
231203
MC/DC100%2/2
$r->{"input_filenum"} == 1 and $before_open_input_files
$r->{"input_filenum"} == 1 $before_open_input_files
Truth table100%3/3
A and B
A= $r->{"input_filenum"} == 1B= $before_open_input_files
ABresult
0X0
100
111
1692 3 log_trace "[csvutil] Calling before_open_input_files handler ...";
1693 3 $before_open_input_files->($r);
1694 3 ▶ if (delete $r->{wants_skip_files}) {
Branch: 1 branch
BranchTrueFalseTotal
if (delete $r->{"wants_skip_files"}) 0 3 3
1695 0 log_trace "[csvutil] Handler wants to skip files, skipping all input files";
1696 0 last READ_CSV;
1697 }
1698 }
1699  
1700 146 ▶ if ($before_open_input_file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($before_open_input_file) 0 146 146
1701 0 log_trace "[csvutil] Calling before_open_input_file handler ...";
1702 0 $before_open_input_file->($r);
1703 0 ▶ if (delete $r->{wants_skip_file}) {
Branch: 2 branches
BranchTrueFalseTotal
if (delete $r->{'wants_skip_file'}) { } 0 0 0
elsif (delete $r->{'wants_skip_files'}) { } 0 0 0
1704 0 log_trace "[csvutil] Handler wants to skip this file, moving on to the next file";
1705 0 next INPUT_FILENAME;
1706 } elsif (delete $r->{wants_skip_files}) {
1707 0 log_trace "[csvutil] Handler wants to skip all files, skipping all input files";
1708 0 last READ_CSV;
1709 }
1710 }
1711  
1712 log_info "[csvutil] [file %d/%d] Reading input file %s ...",
1713 146 $r->{input_filenum}, scalar(@input_filenames), $input_filename;
1714 146 my ($fh, $err) = _open_file_read($input_filename);
1715 146 ▶ die $err if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 146 146
1716 146 $r->{input_fh} = $r->{input_fhs}[ $r->{input_filenum}-1 ] = $fh;
1717  
1718 146 my $i;
1719 146 $r->{input_header_row_count} = 0;
1720 146 $r->{input_data_row_count} = 0;
1721 146 $r->{input_fields} = []; # array, field names in order
1722 146 $r->{input_field_idxs} = {}; # key=field name, value=index (0-based)
1723 146 my $row0;
1724 677 ▶ my $code_getline = sub {
Subroutine __ANON__: called CC=21 SCAR=33.0
1725 677 ▶ if ($r->{stdin_input_fields} && $r->{input_filename} eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{"stdin_input_fields"} and $r->{"input_filename"} eq "-") 0 677 677
Condition33%1/3
$r->{"stdin_input_fields"} and $r->{"input_filename"} eq "-"
!ll&&!rl&&r
67700
MC/DC0%0/2
$r->{"stdin_input_fields"} and $r->{"input_filename"} eq "-"
$r->{"stdin_input_fields"} $r->{"input_filename"} eq "-"
Truth table33%1/3
A and B
A= $r->{"stdin_input_fields"}B= $r->{"input_filename"} eq "-"
ABresult
0X0
100
111
1726 0 ▶ if ($i == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == 0) { } 0 0 0
1727 # we have read the header for stdin. since
1728 # we can't seek to the beginning, we return
1729 # the saved fields
1730 0 $r->{input_header_row_count}++;
1731 0 return $r->{stdin_input_fields};
1732 } else {
1733 0 my $row = $input_parser->getline($r->{input_fh});
1734 0 ▶ $r->{input_data_row_count}++ if $row;
Branch: 1 branch
BranchTrueFalseTotal
if $row 0 0 0
1735 0 return $row;
1736 }
1737 }
1738  
1739 # handle skipping lines before the first row
1740 677 ▶ unless ($r->{input_file_input_has_been_skipped}++) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($r->{"input_file_input_has_been_skipped"}++) 146 531 677
1741 146 ▶ if ($r->{util_args}{input_skip_num_lines}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($r->{'util_args'}{'input_skip_num_lines'}) { } 1 145 146
elsif ($r->{'util_args'}{'input_skip_until_pattern'}) { } 1 144 145
1742 1 for my $j (1 .. $r->{util_args}{input_skip_num_lines}) {
1743 3 my $line = readline($r->{input_fh});
1744 3 ▶ return unless $line;
Branch: 1 branch
BranchTrueFalseTotal
unless $line 0 3 3
1745 }
1746 } elsif ($r->{util_args}{input_skip_until_pattern}) {
1747 1 while (1) {
1748 4 my $row0 = $input_parser->getline($r->{input_fh});
1749 4 ▶ return unless $row0;
Branch: 1 branch
BranchTrueFalseTotal
unless $row0 0 4 4
1750 4 ▶ if ($row0->[0] =~ $r->{util_args}{input_skip_until_pattern}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row0->[0] =~ /$r->{"util_args"}{"input_skip_until_pattern"}/) 1 3 4
1751 # this is the header row
1752 1 $r->{input_header_row_count}++;
1753 1 return $row0;
1754 }
1755 }
1756 }
1757 }
1758  
1759 676 ▶ if ($i == 0 && !$has_header) {
Branch: 2 branches
BranchTrueFalseTotal
if ($i == 0 and not $has_header) { } 4 672 676
elsif ($i == 1 and not $has_header) { } 4 1213 1217
Condition100%3/3
$i == 0 and not $has_header
!ll&&!rl&&r
5311414
MC/DC100%2/2
$i == 0 and not $has_header
$i == 0 not $has_header
Truth table100%3/3
A and not(B)
A= $i == 0B= not $has_header
ABresult
0X0
100
111
1760 # this is the first line of a file and user
1761 # specifies there is no input header. we save
1762 # the line and return the generated field names
1763 # instead.
1764 4 $row0 = $input_parser->getline($r->{input_fh});
1765 4 ▶ return unless $row0;
Branch: 1 branch
BranchTrueFalseTotal
unless $row0 0 4 4
1766 4 return [map { "field$_" } 1..@$row0];
1767 ▶ } elsif ($i == 1 && !$has_header) {
Condition100%3/3
$i == 1 and not $has_header
!ll&&!rl&&r
5451234
MC/DC100%2/2
$i == 1 and not $has_header
$i == 1 not $has_header
Truth table100%3/3
A and not(B)
A= $i == 1B= not $has_header
ABresult
0X0
100
111
1768 # we return the saved first line
1769 4 ▶ $r->{input_data_row_count}++ if $row0;
Branch: 1 branch
BranchTrueFalseTotal
if $row0 4 0 4
1770 4 return $row0;
1771 }
1772 668 my $res = $input_parser->getline($r->{input_fh});
1773 668 ▶ if ($res) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res) 550 118 668
1774 550 ▶ $r->{input_header_row_count}++ if $i==0;
Branch: 1 branch
BranchTrueFalseTotal
if $i == 0 141 409 550
1775 550 ▶ $r->{input_data_row_count}++ if $i;
Branch: 1 branch
BranchTrueFalseTotal
if $i 409 141 550
1776 }
1777 668 $res;
1778  
1779 146 };
1780 146 $r->{code_getline} = $code_getline;
1781  
1782 146 $i = 0;
1783 INPUT_ROW:
1784 146 ▶ while ($r->{input_row} = $code_getline->()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{"input_row"} = &$code_getline()) 559 118 677
1785 559 $i++;
1786 559 $r->{input_rownum} = $i;
1787 559 ▶ $r->{input_data_rownum} = $has_header ? $i-1 : $i;
Branch: 1 branch
BranchTrueFalseTotal
$has_header ? : 541 18 559
1788  
1789 559 ▶ if (defined($r->{util_args}{input_skip_file_after_num_data_rows}) && $r->{input_data_rownum} > $r->{util_args}{input_skip_file_after_num_data_rows}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $r->{"util_args"}{"input_skip_file_after_num_data_rows"} and $r->{"input_data_rownum"} > $r->{"util_args"}{"input_skip_file_after_num_data_rows"}) 0 559 559
Condition33%1/3
defined $r->{"util_args"}{"input_skip_file_after_num_data_rows"} and $r->{"input_data_rownum"} > $r->{"util_args"}{"input_skip_file_after_num_data_rows"}
!ll&&!rl&&r
55900
MC/DC0%0/2
defined $r->{"util_args"}{"input_skip_file_after_num_data_rows"} and $r->{"input_data_rownum"} > $r->{"util_args"}{"input_skip_file_after_num_data_rows"}
defined $r->{"util_args"}{"input_skip_file_after_num_data_rows"} $r->{"input_data_rownum"} > $r->{"util_args"}{"input_skip_file_after_num_data_rows"}
Truth table33%1/3
A and B
A= defined $r->{"util_args"}{"input_skip_file_after_num_data_rows"}B= $r->{"input_data_rownum"} > $r->{"util_args"}{"input_skip_file_after_num_data_rows"}
ABresult
0X0
100
111
1790 0 log_trace "[csvutil] Stopping after reading $r->{util_args}{input_skip_file_after_num_data_rows} row(s) ...";
1791 0 next INPUT_FILENAME;
1792 }
1793  
1794 559 ▶ if ($i == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == 1) { } 146 413 559
1795 # gather the list of fields
1796 146 $r->{input_fields} = $r->{input_row};
1797 146 ▶ $r->{stdin_input_fields} //= $r->{input_row} if $input_filename eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $input_filename eq "-" 0 146 146
Condition0%0/3
$r->{"stdin_input_fields"} //= $r->{"input_row"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$r->{"stdin_input_fields"} //= $r->{"input_row"}
$r->{"stdin_input_fields"} $r->{"input_row"}
Truth table0%0/3
A //= B
A= $r->{"stdin_input_fields"}B= $r->{"input_row"}
ABresult
1X1
011
000
1798 146 $r->{orig_input_fields} = $r->{input_fields};
1799 146 $r->{input_fields_idx} = {};
1800 146 for my $j (0 .. $#{ $r->{input_fields} }) {
1801 388 $r->{input_fields_idx}{ $r->{input_fields}[$j] } = $j;
1802 }
1803  
1804 146 ▶ if ($on_input_header_row) {
Branch: 1 branch
BranchTrueFalseTotal
if ($on_input_header_row) 132 14 146
1805 132 log_trace "[csvutil] Calling on_input_header_row hook handler ...";
1806 132 $on_input_header_row->($r);
1807  
1808 123 ▶ if (delete $r->{wants_skip_file}) {
Branch: 2 branches
BranchTrueFalseTotal
if (delete $r->{'wants_skip_file'}) { } 9 114 123
elsif (delete $r->{'wants_skip_files'}) { } 1 113 114
1809 9 log_trace "[csvutil] Handler wants to skip this file, moving on to the next file";
1810 9 next INPUT_FILENAME;
1811 } elsif (delete $r->{wants_skip_files}) {
1812 1 log_trace "[csvutil] Handler wants to skip all files, skipping all input files";
1813 1 last READ_CSV;
1814 }
1815 }
1816  
1817 # reindex the fields, should the above hook
1818 # handler adds/removes fields. let's save the
1819 # old fields_idx to orig_fields_idx.
1820 127 $r->{orig_input_fields_idx} = $r->{input_fields_idx};
1821 127 $r->{input_fields_idx} = {};
1822 127 for my $j (0 .. $#{ $r->{input_fields} }) {
1823 339 $r->{input_fields_idx}{ $r->{input_fields}[$j] } = $j;
1824 }
1825  
1826 } else {
1827 413 ▶ if (defined($r->{util_args}{input_skip_before_num_data_rows}) && $r->{input_data_rownum} <= $r->{util_args}{input_skip_before_num_data_rows}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $r->{"util_args"}{"input_skip_before_num_data_rows"} and $r->{"input_data_rownum"} <= $r->{"util_args"}{"input_skip_before_num_data_rows"}) 0 413 413
Condition33%1/3
defined $r->{"util_args"}{"input_skip_before_num_data_rows"} and $r->{"input_data_rownum"} <= $r->{"util_args"}{"input_skip_before_num_data_rows"}
!ll&&!rl&&r
41300
MC/DC0%0/2
defined $r->{"util_args"}{"input_skip_before_num_data_rows"} and $r->{"input_data_rownum"} <= $r->{"util_args"}{"input_skip_before_num_data_rows"}
defined $r->{"util_args"}{"input_skip_before_num_data_rows"} $r->{"input_data_rownum"} <= $r->{"util_args"}{"input_skip_before_num_data_rows"}
Truth table33%1/3
A and B
A= defined $r->{"util_args"}{"input_skip_before_num_data_rows"}B= $r->{"input_data_rownum"} <= $r->{"util_args"}{"input_skip_before_num_data_rows"}
ABresult
0X0
100
111
1828 #log_trace "[csvutil] Skipping row (before input_skip_before_num_data_rows) ...";
1829 0 next INPUT_ROW;
1830 }
1831  
1832 # fill up the elements of row to the number of
1833 # fields, in case the row contains sparse values
1834 413 ▶ unless (defined $r->{wants_fill_rows} && !$r->{wants_fill_rows}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $r->{"wants_fill_rows"} and not $r->{"wants_fill_rows"}) 413 0 413
Condition33%1/3
defined $r->{"wants_fill_rows"} and not $r->{"wants_fill_rows"}
!ll&&!rl&&r
41300
MC/DC0%0/2
defined $r->{"wants_fill_rows"} and not $r->{"wants_fill_rows"}
defined $r->{"wants_fill_rows"} not $r->{"wants_fill_rows"}
Truth table33%1/3
A and not(B)
A= defined $r->{"wants_fill_rows"}B= not $r->{"wants_fill_rows"}
ABresult
0X0
100
111
1835 413 ▶ if (@{ $r->{input_row} } < @{ $r->{input_fields} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$r->{"input_row"};} < @{$r->{"input_fields"};}) 3 410 413
1836 3 splice @{ $r->{input_row} }, scalar(@{ $r->{input_row} }), 0, (("") x (@{ $r->{input_fields} } - @{ $r->{input_row} }));
1837 }
1838 }
1839  
1840 # generate the hashref version of row if utility
1841 # requires it
1842 413 ▶ if ($r->{wants_input_row_as_hashref}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{"wants_input_row_as_hashref"}) 34 379 413
1843 34 $r->{input_row_as_hashref} = {};
1844 34 for my $j (0 .. $#{ $r->{input_row} }) {
1845 # ignore extraneous data fields
1846 93 ▶ last if $j >= @{ $r->{input_fields} };
Branch: 1 branch
BranchTrueFalseTotal
if $j >= @{$r->{"input_fields"};} 0 93 93
1847 93 $r->{input_row_as_hashref}{ $r->{input_fields}[$j] } = $r->{input_row}[$j];
1848 }
1849 }
1850  
1851 413 ▶ if ($on_input_data_row) {
Branch: 1 branch
BranchTrueFalseTotal
if ($on_input_data_row) 413 0 413
1852 413 ▶ log_trace "[csvutil] Calling on_input_data_row hook handler (for first data row) ..." if $r->{input_rownum} <= 2;
Branch: 1 branch
BranchTrueFalseTotal
if $r->{"input_rownum"} <= 2 125 288 413
1853 413 $on_input_data_row->($r);
1854  
1855 408 ▶ if (delete $r->{wants_skip_file}) {
Branch: 2 branches
BranchTrueFalseTotal
if (delete $r->{'wants_skip_file'}) { } 2 406 408
elsif (delete $r->{'wants_skip_files'}) { } 2 404 406
1856 2 log_trace "[csvutil] Handler wants to skip this file, moving on to the next file";
1857 2 next INPUT_FILENAME;
1858 } elsif (delete $r->{wants_skip_files}) {
1859 2 log_trace "[csvutil] Handler wants to skip all files, skipping all input files";
1860 2 last READ_CSV;
1861 }
1862 }
1863 }
1864  
1865 } # while getline
1866  
1867 # XXX actually close filehandle except stdin
1868  
1869 118 ▶ if ($after_close_input_file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($after_close_input_file) 0 118 118
1870 0 log_trace "[csvutil] Calling after_close_input_file handler ...";
1871 0 $after_close_input_file->($r);
1872 0 ▶ if (delete $r->{wants_skip_files}) {
Branch: 1 branch
BranchTrueFalseTotal
if (delete $r->{"wants_skip_files"}) 0 0 0
1873 0 log_trace "[csvutil] Handler wants to skip reading all file, skipping";
1874 0 last READ_CSV;
1875 }
1876 }
1877 } # for input_filename
1878  
1879 106 ▶ if ($after_close_input_files) {
Branch: 1 branch
BranchTrueFalseTotal
if ($after_close_input_files) 34 72 106
1880 34 log_trace "[csvutil] Calling after_close_input_files handler ...";
1881 34 $after_close_input_files->($r);
1882 }
1883  
1884 } # READ_CSV
1885  
1886 # cleanup stash from csv-reading-related keys
1887 109 delete $r->{input_filenames};
1888 109 delete $r->{input_filenum};
1889 109 delete $r->{input_filename};
1890 109 delete $r->{input_fh};
1891 109 delete $r->{input_rownum};
1892 109 delete $r->{input_data_rownum};
1893 109 delete $r->{input_row};
1894 109 delete $r->{input_row_as_hashref};
1895 109 delete $r->{input_fields};
1896 109 delete $r->{input_fields_idx};
1897 109 delete $r->{orig_input_fields_idx};
1898 109 delete $r->{code_getline};
1899 109 delete $r->{wants_input_row_as_hashref};
1900  
1901 109 ▶ if ($after_read_input) {
Branch: 1 branch
BranchTrueFalseTotal
if ($after_read_input) 15 94 109
1902 15 log_trace "[csvutil] Calling after_read_input handler ...";
1903 15 $after_read_input->($r);
1904 }
1905  
1906 # cleanup stash from csv-outputting-related keys
1907 109 delete $r->{output_num_of_files};
1908 109 delete $r->{output_filenum};
1909 109 ▶ if ($r->{output_fh}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{"output_fh"}) 81 28 109
1910 81 ▶ if ($r->{output_filename} ne '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{"output_filename"} ne "-") 2 79 81
1911 2 log_info "[csvutil] Closing output file '$r->{output_filename}' ...";
1912 2 ▶ close $r->{output_fh} or die [500, "Can't close output file '$r->{output_filename}': $!"];
Branch: 1 branch
BranchTrueFalseTotal
close $r->{"output_fh"} or die [500, qq[Can't close output file '$r->{"output_filename"}': $!]] 2 0 2
1913 }
1914 81 delete $r->{output_fh};
1915 }
1916 109 ▶ if ($r->{util_args}{inplace}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{"util_args"}{"inplace"}) 2 107 109
1917 2 ▶ my $output_filenum = $r->{output_filenum} // 0;
Condition50%1/2
$r->{"output_filenum"} // 0
l!l
02
MC/DC0%0/1
$r->{"output_filenum"} // 0
$r->{"output_filenum"}
Truth table50%1/2
A
A= $r->{"output_filenum"}
Aresult
11
00
1918 2 my $i = -1;
1919 2 for my $output_filename (@{ $r->{output_filenames} }) {
1920 2 $i++;
1921 2 ▶ last if $i > $output_filenum;
Branch: 1 branch
BranchTrueFalseTotal
if $i > $output_filenum 0 2 2
1922 2 ▶ (my $input_filename = $output_filename) =~ s/\.\w{5}\z//
Branch: 1 branch
BranchTrueFalseTotal
(my $input_filename = $output_filename) =~ s/\.\w{5}\z// or die [500, "BUG: Can't get original input file '${output_filename}'"] 2 0 2
1923 or die [500, "BUG: Can't get original input file '$output_filename'"];
1924 2 ▶ if (length(my $ext = $r->{util_args}{inplace_backup_ext})) {
Branch: 1 branch
BranchTrueFalseTotal
if (length(my $ext = $r->{"util_args"}{"inplace_backup_ext"})) 1 1 2
1925 1 my $backup_filename = $input_filename . $ext;
1926 1 log_info "[csvutil] Backing up input file '$output_filename' -> '$backup_filename' ...";
1927 1 ▶ rename $input_filename, $backup_filename or die [500, "Can't rename '$input_filename' -> '$backup_filename': $!"];
Branch: 1 branch
BranchTrueFalseTotal
rename $input_filename, $backup_filename or die [500, "Can't rename '${input_filename}' -> '${backup_filename}': $!"] 1 0 1
1928 }
1929 2 log_info "[csvutil] Renaming from temporary output file '$output_filename' -> '$input_filename' ...";
1930 2 ▶ rename $output_filename, $input_filename or die [500, "Can't rename back '$output_filename' -> '$input_filename': $!"];
Branch: 1 branch
BranchTrueFalseTotal
rename $output_filename, $input_filename or die [500, "Can't rename back '${output_filename}' -> '${input_filename}': $!"] 2 0 2
1931 }
1932 }
1933 109 delete $r->{output_filenames};
1934 109 delete $r->{output_filename};
1935 109 delete $r->{output_rownum};
1936 109 delete $r->{output_data_rownum};
1937 109 delete $r->{code_print};
1938 109 delete $r->{code_print_row};
1939 109 delete $r->{code_print_header_row};
1940 109 delete $r->{has_printed_header};
1941 109 delete $r->{wants_switch_to_next_output_file};
1942  
1943 109 ▶ if ($on_end) {
Branch: 1 branch
BranchTrueFalseTotal
if ($on_end) 13 96 109
1944 13 log_trace "[csvutil] Calling on_end hook handler ...";
1945 13 $on_end->($r);
1946 }
1947  
1948 }; # MAIN_EVAL
1949  
1950 123 my $err = $@;
1951 123 ▶ if ($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 14 109 123
1952 14 ▶ $err = [500, $err] unless ref $err;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $err 0 14 14
1953 14 return $err;
1954 }
1955  
1956 RETURN_RESULT:
1957 109 ▶ if (!$r->{result}) {
Branch: 3 branches
BranchTrueFalseTotal
if (not $r->{'result'}) { } 93 16 109
elsif (not ref $r->{'result'}) { } 0 16 16
elsif (ref $r->{'result'} ne 'ARRAY') { } 0 16 16
1958 93 $r->{result} = [200];
1959 } elsif (!ref($r->{result})) {
1960 0 $r->{result} = [500, "BUG: Result (r->{result}) is set to a non-reference ($r->{result}), probably by one of the handlers"];
1961 } elsif (ref($r->{result}) ne 'ARRAY') {
1962 0 $r->{result} = [500, "BUG: Result (r->{result}) is not set to an enveloped result (arrayref) ($r->{result}), probably by one of the handlers"];
1963 }
1964 109 $r->{result};
1965 82 };
1966 } # CREATE_CODE
1967  
1968 82 my $meta;
1969 CREATE_META: {
1970  
1971 82 $meta = {
1972 v => 1.1,
1973 summary => $summary,
1974 description => $description,
1975 args => {},
1976 args_rels => {},
1977 links => $links,
1978 examples => $examples,
1979 tags => $tags,
1980 };
1981  
1982 CREATE_ARGS_PROP: {
1983 82 ▶ if ($add_args) {
Branch: 1 branch
BranchTrueFalseTotal
if ($add_args) 82 0 82
1984 82 $meta->{args}{$_} = $add_args->{$_} for keys %$add_args;
1985 }
1986  
1987 82 ▶ if ($reads_csv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reads_csv) 80 2 82
1988 80 $meta->{args}{$_} = {%{$argspecs_csv_input{$_}}} for keys %argspecs_csv_input;
1989  
1990 80 ▶ if ($reads_multiple_csv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reads_multiple_csv) { } 7 73 80
1991 7 $meta->{args}{input_filenames} = {%{$argspecopt_input_filenames{input_filenames}}};
1992 7 _add_arg_pos($meta->{args}, 'input_filenames', 'slurpy');
1993 7 push @$tags, 'reads-multiple-csv';
1994 } else {
1995 73 $meta->{args}{input_filename} = {%{$argspecopt_input_filename{input_filename}}};
1996 73 _add_arg_pos($meta->{args}, 'input_filename');
1997 }
1998  
1999 80 push @$tags, 'reads-csv';
2000 } # if reads_csv
2001  
2002 82 ▶ if ($writes_csv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($writes_csv) 55 27 82
2003 55 $meta->{args}{$_} = {%{$argspecs_csv_output{$_}}} for keys %argspecs_csv_output;
2004  
2005 55 ▶ if ($reads_csv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reads_csv) 53 2 55
2006 53 $meta->{args}{$_} = {%{$argspecsopt_inplace{$_}}} for keys %argspecsopt_inplace;
2007 53 ▶ $meta->{args_rels}{'dep_all&'} //= [];
Condition50%1/2
$meta->{"args_rels"}{"dep_all&"} //= []
l!l
053
MC/DC0%0/1
$meta->{"args_rels"}{"dep_all&"} //= []
$meta->{"args_rels"}{"dep_all&"}
Truth table50%1/2
A
A= $meta->{"args_rels"}{"dep_all&"}
Aresult
11
00
2008 53 push @{ $meta->{args_rels}{'dep_all&'} }, ['inplace_backup_ext', ['inplace']];
2009 53 ▶ $meta->{args_rels}{'choose_one&'} //= [];
Condition50%1/2
$meta->{"args_rels"}{"choose_one&"} //= []
l!l
053
MC/DC0%0/1
$meta->{"args_rels"}{"choose_one&"} //= []
$meta->{"args_rels"}{"choose_one&"}
Truth table50%1/2
A
A= $meta->{"args_rels"}{"choose_one&"}
Aresult
11
00
2010 53 push @{ $meta->{args_rels}{'choose_one&'} }, ['inplace', 'output_filename'];
2011 53 push @{ $meta->{args_rels}{'choose_one&'} }, ['inplace', 'output_filenames'];
2012 }
2013  
2014 55 ▶ if ($writes_multiple_csv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($writes_multiple_csv) { } 1 54 55
2015 1 $meta->{args}{output_filenames} = {%{$argspecopt_output_filenames{output_filenames}}};
2016 1 _add_arg_pos($meta->{args}, 'output_filenames', 'slurpy');
2017 1 ▶ if ($reads_csv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reads_csv) 1 0 1
2018 1 ▶ $meta->{args_rels}{'choose_one&'} //= [];
Condition50%1/2
$meta->{"args_rels"}{"choose_one&"} //= []
l!l
10
MC/DC0%0/1
$meta->{"args_rels"}{"choose_one&"} //= []
$meta->{"args_rels"}{"choose_one&"}
Truth table50%1/2
A
A= $meta->{"args_rels"}{"choose_one&"}
Aresult
11
00
2019 1 push @{ $meta->{args_rels}{'choose_one&'} }, [qw/output_filenames inplace/];
2020 }
2021 1 push @$tags, 'writes-multiple-csv';
2022 } else {
2023 54 $meta->{args}{output_filename} = {%{$argspecopt_output_filename{output_filename}}};
2024 54 _add_arg_pos($meta->{args}, 'output_filename');
2025 54 ▶ if ($reads_csv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reads_csv) 52 2 54
2026 52 ▶ $meta->{args_rels}{'choose_one&'} //= [];
Condition50%1/2
$meta->{"args_rels"}{"choose_one&"} //= []
l!l
520
MC/DC0%0/1
$meta->{"args_rels"}{"choose_one&"} //= []
$meta->{"args_rels"}{"choose_one&"}
Truth table50%1/2
A
A= $meta->{"args_rels"}{"choose_one&"}
Aresult
11
00
2027 52 push @{ $meta->{args_rels}{'choose_one&'} }, [qw/output_filename inplace/];
2028 }
2029 }
2030  
2031 55 $meta->{args}{overwrite} = {%{$argspecopt_overwrite{overwrite}}};
2032 55 ▶ $meta->{args_rels}{'dep_any&'} //= [];
Condition50%1/2
$meta->{"args_rels"}{"dep_any&"} //= []
l!l
055
MC/DC0%0/1
$meta->{"args_rels"}{"dep_any&"} //= []
$meta->{"args_rels"}{"dep_any&"}
Truth table50%1/2
A
A= $meta->{"args_rels"}{"dep_any&"}
Aresult
11
00
2033 55 push @{ $meta->{args_rels}{'dep_any&'} }, ['overwrite', ['output_filename', 'output_filenames']];
2034  
2035 55 push @$tags, 'writes-csv';
2036 } # if writes csv
2037  
2038 } # CREATE_ARGS_PROP
2039  
2040 CREATE_ARGS_RELS_PROP: {
2041 82 $meta->{args_rels} = {};
2042 82 ▶ if ($add_args_rels) {
Branch: 1 branch
BranchTrueFalseTotal
if ($add_args_rels) 14 68 82
2043 14 $meta->{args_rels}{$_} = $add_args_rels->{$_} for keys %$add_args_rels;
2044 }
2045 } # CREATE_ARGS_RELS_PROP
2046  
2047 82 ▶ if ($add_meta_props) {
Branch: 1 branch
BranchTrueFalseTotal
if ($add_meta_props) 0 82 82
2048 0 $meta->{$_} = $add_meta_props->{$_} for keys %$add_meta_props;
2049 }
2050  
2051 } # CREATE_META
2052  
2053 {
2054 82 my $package = caller();
2055 57 ▶ no strict 'refs'; ## no critic: TestingAndDebugging::ProhibitNoStrict
Subroutine BEGIN: called CC=1 SCAR=0.0
2056 82 *{"$package\::$name"} = $code;
2057 #use DD; dd $meta;
2058 82 ${"$package\::SPEC"}{$name} = $meta;
2059 }
2060  
2061 82 1;
2062 }
2063  
2064 57 1;
2065 # ABSTRACT: CLI utilities related to CSV
2066  
2067 __END__
« CSVUtils/csv_check_field_values.pm ↑ Summary CSVUtils/csv_gen.pm »