CSVUtils/csv_munge_rows.pm

filter: Statementstmt 100% 34 / 34 Branchbran 90.0% 9 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 98.0% 49 / 50 CC 7 SCAR 13.0
CC
7
Cov
98%
__ANON__
6.0
__ANON__
2.0
BEGIN
1.0
CRAP 3.7
« CSVUtils/csv_uniq.pm ↑ Summary CSVUtils/csv_pick_fields.pm »
1 package App::CSVUtils::csv_munge_rows;
2  
3 2 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
8 2 our $DATE = '2026-07-09'; # DATE
9 2 our $DIST = 'App-CSVUtils'; # DIST
10 2 our $VERSION = '1.038'; # VERSION
11  
12 2 ▶use App::CSVUtils qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
13 gen_csv_util
14 compile_eval_code
15 eval_code
16 2 );
17  
18 gen_csv_util(
19 name => 'csv_munge_rows',
20 summary => 'Modify CSV data rows using Perl code',
21 description => <<'MARKDOWN',
22  
23 Perl code (-e) will be called for each row (excluding the header row) and `$_`
24 will contain the row (arrayref, or hashref if `-H` is specified). The Perl code
25 is expected to modify it.
26  
27 Aside from `$_`, `$main::row` will contain the current row array.
28 `$main::rownum` contains the row number (2 means the first data row).
29 `$main::csv` is the <pm:Text::CSV_XS> object. `$main::fields_idx` is also
30 available for additional information.
31  
32 The modified `$_` will be rendered back to CSV row.
33  
34 You cannot add new fields using this utility. To do so, use
35 <prog:csv-add-fields>. You also cannot delete fields (they just become empty
36 string if you delete the field in the eval code). To delete fields, use
37 <prog:csv-delete-fields>.
38  
39 Note that you can also munge a single field using <prog:csv-munge-field>.
40  
41 MARKDOWN
42 add_args => {
43 %App::CSVUtils::argspec_eval_1,
44 %App::CSVUtils::argspecopt_hash,
45 },
46 tags => ['category:munging', 'modifies-rows'],
47  
48 examples => [
49 {
50 summary => 'Modify two fields in a CSV',
51 argv => ['-He', '$_->{field1} *= 2; $_->{field2} =~ s/foo/bar/', 'file.csv'],
52 test => 0,
53 'x.doc.show_result' => 0,
54 },
55 ],
56  
57 3 ▶ on_input_header_row => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
58 3 my $r = shift;
59  
60 # we add the following keys to the stash
61 3 $r->{code} = compile_eval_code($r->{util_args}{eval}, 'eval');
62  
63 2 ▶ $r->{wants_input_row_as_hashref} = 1 if $r->{util_args}{hash};
Branch: 1 branch
BranchTrueFalseTotal
if $r->{"util_args"}{"hash"} 1 1 2
64 },
65  
66 6 ▶ on_input_data_row => sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
67 6 my $r = shift;
68  
69 6 ▶ my $topic; eval { $topic = eval_code($r->{code}, $r, $r->{wants_input_row_as_hashref} ? $r->{input_row_as_hashref} : $r->{input_row}, 'return_topic') };
Branch: 1 branch
BranchTrueFalseTotal
$r->{'wants_input_row_as_hashref'} ? : 3 3 6
70 6 ▶ die [500, "Error while munging row #$r->{input_rownum}: $@\n"] if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 6 6
71 # convert back hashref row to arrayref
72 6 my $newrow;
73 6 ▶ if ($r->{util_args}{hash}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{'util_args'}{'hash'}) { } 3 3 6
74 3 $newrow = [('') x @{ $r->{input_fields} }];
75 3 for my $field (keys %$topic) {
76 9 ▶ next unless exists $r->{input_fields_idx}{$field}; # ignore created fields
Branch: 1 branch
BranchTrueFalseTotal
unless exists $r->{"input_fields_idx"}{$field} 3 6 9
77 6 $newrow->[$r->{input_fields_idx}{$field}] = $topic->{$field};
78 }
79 } else {
80 3 $newrow = $topic;
81 }
82 6 $r->{code_print_row}->($newrow);
83 },
84 2 );
85  
86 2 1;
87 # ABSTRACT: Modify CSV data rows using Perl code
88  
89 __END__
« CSVUtils/csv_uniq.pm ↑ Summary CSVUtils/csv_pick_fields.pm »