| 1 | package App::CSVUtils::csv_trim; | ||||||||||||
| 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_trim', | ||||||||||||
| 18 | summary => 'Trim (remove leading & 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 | examples => [ | ||||||||||||
| 36 | { | ||||||||||||
| 37 | summary => 'Trim values in a CSV', | ||||||||||||
| 38 | argv => ['FILE.csv'], | ||||||||||||
| 39 | test => 0, | ||||||||||||
| 40 | 'x.doc.show_result' => 0, | ||||||||||||
| 41 | }, | ||||||||||||
| 42 | ], | ||||||||||||
| 43 | links => [ | ||||||||||||
| 44 | {url=>'prog:csv-ltrim'}, | ||||||||||||
| 45 | {url=>'prog:csv-rtrim'}, | ||||||||||||
| 46 | ], | ||||||||||||
| 47 | |||||||||||||
| 48 | tags => ['category:munging', 'modifies-values'], | ||||||||||||
| 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
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"}
| |||||||||||||
| 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
| |||||||||||||
| 66 | 0 | my $f0 = $f; | |||||||||||
| 67 | 0 | $i++; | |||||||||||
| 68 | 0 | $f =~ s/\A\s+//s; | |||||||||||
| 69 | 0 | $f =~ s/\s+\z//s; | |||||||||||
| 70 | die [500, "Can't trim field '$f0' to '$f': will cause duplicate fields"] | ||||||||||||
| 71 | 0 | ▶ | if exists $r->{output_fields_idx}{$f}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 72 | } | ||||||||||||
| 73 | 0 | push @{$r->{output_fields}}, $f; | |||||||||||
| 74 | 0 | $r->{output_fields_idx}{$f} = $i; | |||||||||||
| 75 | } | ||||||||||||
| 76 | }, | ||||||||||||
| 77 | |||||||||||||
| 78 | 0 | ▶ | on_input_data_row => sub { | ||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||
| 79 | 0 | my $r = shift; | |||||||||||
| 80 | |||||||||||||
| 81 | 0 | ▶ | if ($r->{util_args}{trim_values}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 82 | 0 | for my $f (@{$r->{input_row}}) { | |||||||||||
| 83 | 0 | $f =~ s/\A\s+//s; | |||||||||||
| 84 | 0 | $f =~ s/\s+\z//s; | |||||||||||
| 85 | } | ||||||||||||
| 86 | } | ||||||||||||
| 87 | |||||||||||||
| 88 | 0 | $r->{code_print_row}->($r->{input_row}); | |||||||||||
| 89 | }, | ||||||||||||
| 90 | 1 | ); | |||||||||||
| 91 | |||||||||||||
| 92 | 1 | 1; | |||||||||||
| 93 | # ABSTRACT: Trim (remove leading & trailing whitespace) values and/or fields in CSV | ||||||||||||
| 94 | |||||||||||||
| 95 | __END__ | ||||||||||||