CSVUtils/csv_replace_newline.pm

filter: Statementstmt 92.1% 35 / 38 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 66.6% 6 / 9 Podpod n/a totaltotal 87.2% 41 / 47 CC 3 SCAR 4.9
CC
3
Cov
92%
__ANON__
2.0
__ANON__
2.0
__ANON__
2.0
CRAP 1.6
« CSVUtils/csv_dump.pm ↑ Summary CSVUtils/csv_each_row.pm »
1 package App::CSVUtils::csv_replace_newline;
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 2 );
15  
16 gen_csv_util(
17 name => 'csv_replace_newline',
18 summary => 'Replace newlines in CSV values',
19 description => <<'MARKDOWN',
20  
21 Some CSV parsers or applications cannot handle multiline CSV values. This
22 utility can be used to convert the newline to something else. There are a few
23 choices: replace newline with space (`--with-space`, the default), remove
24 newline (`--with-nothing`), replace with encoded representation
25 (`--with-backslash-n`), or with characters of your choice (`--with 'blah'`).
26  
27 MARKDOWN
28 add_args => {
29 with => {
30 schema => 'str*',
31 default => ' ',
32 cmdline_aliases => {
33 0 ▶ with_space => { is_flag=>1, code=>sub { $_[0]{with} = ' ' } },
Subroutine __ANON__: not called CC=1 SCAR=6.9
34 0 ▶ with_nothing => { is_flag=>1, code=>sub { $_[0]{with} = '' } },
Subroutine __ANON__: not called CC=1 SCAR=6.9
35 0 ▶ with_backslash_n => { is_flag=>1, code=>sub { $_[0]{with} = "\\n" } },
Subroutine __ANON__: not called CC=1 SCAR=6.9
36 },
37 },
38 },
39 tags => ['category:munging', 'modifies-rows'],
40  
41 examples => [
42 {
43 summary => 'Replace newline in a CSV file to space',
44 argv => ['file.csv'],
45 test => 0,
46 'x.doc.show_result' => 0,
47 },
48 ],
49  
50 1 ▶ on_input_header_row => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
51 1 my $r = shift;
52  
53 1 $r->{output_fields} = [];
54 1 my $with = $r->{util_args}{with};
55 1 for my $j (0 .. $#{ $r->{input_fields} }) {
56 2 my $val = $r->{input_fields}[$j];
57 2 $val =~ s/[\015\012]+/$with/g;
58 2 push @{ $r->{output_fields} }, $val;
59 }
60 },
61  
62 2 ▶ on_input_data_row => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
63 2 my $r = shift;
64  
65 2 my $row = [];
66 2 my $with = $r->{util_args}{with};
67 2 for my $j (0 .. $#{ $r->{input_fields} }) {
68 4 my $val = $r->{input_row}[$j];
69 4 $val =~ s/[\015\012]+/$with/g;
70 4 push @$row, $val;
71 }
72  
73 2 $r->{code_print_row}->($row);
74 },
75 2 );
76  
77 2 1;
78 # ABSTRACT: Replace newlines in CSV values
79  
80 __END__
« CSVUtils/csv_dump.pm ↑ Summary CSVUtils/csv_each_row.pm »