CSVUtils/csv_rtrim.pm

filter: Statementstmt 43.5% 17 / 39 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 57.1% 4 / 7 Podpod n/a totaltotal 38.1% 21 / 55 CC 7 SCAR 23.2
CC
7
Cov
36%
__ANON__
20.0
__ANON__
12.0
__ANON__
6.0
CRAP 10.2
« CSVUtils/csv_ltrim.pm ↑ Summary CSVUtils/csv_concat.pm »
1 package App::CSVUtils::csv_rtrim;
2  
3 1 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
8 1 our $DATE = '2026-07-09'; # DATE
9 1 our $DIST = 'App-CSVUtils'; # DIST
10 1 our $VERSION = '1.038'; # VERSION
11  
12 1 ▶use App::CSVUtils qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
13 gen_csv_util
14 1 );
15  
16 gen_csv_util(
17 name => 'csv_rtrim',
18 summary => 'Right-trim (remove trailing whitespace) values and/or fields in CSV',
19 description => <<'MARKDOWN',
20  
21 Whitespace includes space, tab, and newline.
22  
23 MARKDOWN
24 add_args => {
25 trim_fields => {
26 schema => 'bool*',
27 summary => 'Whether also to trim the field names',
28 },
29 trim_values => {
30 schema => 'bool*',
31 summary => 'Whether to trim the values in the data rows',
32 default => 1,
33 },
34 },
35 tags => ['category:munging', 'modifies-values'],
36  
37 examples => [
38 {
39 summary => 'Right-trim values in a CSV',
40 argv => ['FILE.csv'],
41 test => 0,
42 'x.doc.show_result' => 0,
43 },
44 ],
45 links => [
46 {url=>'prog:csv-ltrim'},
47 {url=>'prog:csv-trim'},
48 ],
49  
50 0 ▶ on_begin => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
51 0 my $r = shift;
52  
53 # XXX schema
54 0 ▶ $r->{util_args}{trim_values} //= 1;
Condition0%0/2
$r->{"util_args"}{"trim_values"} //= 1
l!l
00
MC/DC0%0/1
$r->{"util_args"}{"trim_values"} //= 1
$r->{"util_args"}{"trim_values"}
Truth table0%0/2
A
A= $r->{"util_args"}{"trim_values"}
Aresult
11
00
55 },
56  
57 0 ▶ on_input_header_row => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
58 0 my $r = shift;
59  
60 0 $r->{output_fields} = [];
61 0 $r->{output_fields_idx} = {};
62  
63 0 my $i = -1;
64 0 for my $f (@{ $r->{input_fields} }) {
65 0 ▶ if ($r->{util_args}{trim_fields}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{"util_args"}{"trim_fields"}) 0 0 0
66 0 my $f0 = $f;
67 0 $i++;
68 0 $f =~ s/\s+\z//s;
69 die [500, "Can't rtrim field '$f0' to '$f': will cause duplicate fields"]
70 0 ▶ if exists $r->{output_fields_idx}{$f};
Branch: 1 branch
BranchTrueFalseTotal
if exists $r->{"output_fields_idx"}{$f} 0 0 0
71 }
72 0 push @{$r->{output_fields}}, $f;
73 0 $r->{output_fields_idx}{$f} = $i;
74 }
75 },
76  
77 0 ▶ on_input_data_row => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
78 0 my $r = shift;
79  
80 0 ▶ if ($r->{util_args}{trim_values}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{"util_args"}{"trim_values"}) 0 0 0
81 0 for my $f (@{$r->{input_row}}) {
82 0 $f =~ s/\s+\z//s;
83 }
84 }
85  
86 0 $r->{code_print_row}->($r->{input_row});
87 },
88 1 );
89  
90 1 1;
91 # ABSTRACT: Right-trim (remove trailing whitespace) values and/or fields in CSV
92  
93 __END__
« CSVUtils/csv_ltrim.pm ↑ Summary CSVUtils/csv_concat.pm »