File Coverage

blib/lib/App/CSVUtils/csv_csv.pm
Criterion Covered Total %
statement 11 11 100.0
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 15 15 100.0


line stmt bran cond sub pod time code
1             package App::CSVUtils::csv_csv;
2              
3 1     1   5489 use 5.010001;
  1         4  
4 1     1   9 use strict;
  1         3  
  1         30  
5 1     1   5 use warnings;
  1         3  
  1         99  
6              
7             our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
8             our $DATE = '2023-07-25'; # DATE
9             our $DIST = 'App-CSVUtils'; # DIST
10             our $VERSION = '1.030'; # VERSION
11              
12 1         183 use App::CSVUtils qw(
13             gen_csv_util
14 1     1   7 );
  1         2  
15              
16             gen_csv_util(
17             name => 'csv_csv',
18             summary => 'Convert CSV to CSV',
19             description => <<'_',
20              
21             Why convert CSV to CSV? When you want to change separator/quote/escape
22             character, for one. Or you want to remove header or add one.
23              
24             Example:
25              
26             # in.csv
27             name,age
28             andi,12
29             budi,13
30              
31             % csv-csv in.csv --output-sep-char ';'
32             name;age
33             andi;12
34             budi;13
35              
36             _
37             add_args => {
38             },
39             tags => ['category:converting','category:munging'],
40              
41             on_input_data_row => sub {
42             my $r = shift;
43              
44             $r->{code_print_row}->($r->{input_row});
45             },
46             );
47              
48             1;
49             # ABSTRACT: Convert CSV to CSV
50              
51             __END__
52              
53             =pod
54              
55             =encoding UTF-8
56              
57             =head1 NAME
58              
59             App::CSVUtils::csv_csv - Convert CSV to CSV
60              
61             =head1 VERSION
62              
63             This document describes version 1.030 of App::CSVUtils::csv_csv (from Perl distribution App-CSVUtils), released on 2023-07-25.
64              
65             =head1 FUNCTIONS
66              
67              
68             =head2 csv_csv
69              
70             Usage:
71              
72             csv_csv(%args) -> [$status_code, $reason, $payload, \%result_meta]
73              
74             Convert CSV to CSV.
75              
76             Why convert CSV to CSV? When you want to change separator/quote/escape
77             character, for one. Or you want to remove header or add one.
78              
79             Example:
80              
81             # in.csv
82             name,age
83             andi,12
84             budi,13
85            
86             % csv-csv in.csv --output-sep-char ';'
87             name;age
88             andi;12
89             budi;13
90              
91             This function is not exported.
92              
93             Arguments ('*' denotes required arguments):
94              
95             =over 4
96              
97             =item * B<inplace> => I<true>
98              
99             Output to the same file as input.
100              
101             Normally, you output to a different file than input. If you try to output to the
102             same file (C<-o INPUT.csv -O>) you will clobber the input file; thus the utility
103             prevents you from doing it. However, with this C<--inplace> option, you can
104             output to the same file. Like perl's C<-i> option, this will first output to a
105             temporary file in the same directory as the input file then rename to the final
106             file at the end. You cannot specify output file (C<-o>) when using this option,
107             but you can specify backup extension with C<-b> option.
108              
109             Some caveats:
110              
111             =over
112              
113             =item * if input file is a symbolic link, it will be replaced with a regular file;
114              
115             =item * renaming (implemented using C<rename()>) can fail if input filename is too long;
116              
117             =item * value specified in C<-b> is currently not checked for acceptable characters;
118              
119             =item * things can also fail if permissions are restrictive;
120              
121             =back
122              
123             =item * B<inplace_backup_ext> => I<str> (default: "")
124              
125             Extension to add for backup of input file.
126              
127             In inplace mode (C<--inplace>), if this option is set to a non-empty string, will
128             rename the input file using this extension as a backup. The old existing backup
129             will be overwritten, if any.
130              
131             =item * B<input_escape_char> => I<str>
132              
133             Specify character to escape value in field in input CSV, will be passed to Text::CSV_XS.
134              
135             Defaults to C<\\> (backslash). Overrides C<--input-tsv> option.
136              
137             =item * B<input_filename> => I<filename> (default: "-")
138              
139             Input CSV file.
140              
141             Use C<-> to read from stdin.
142              
143             Encoding of input file is assumed to be UTF-8.
144              
145             =item * B<input_header> => I<bool> (default: 1)
146              
147             Specify whether input CSV has a header row.
148              
149             By default, the first row of the input CSV will be assumed to contain field
150             names (and the second row contains the first data row). When you declare that
151             input CSV does not have header row (C<--no-input-header>), the first row of the
152             CSV is assumed to contain the first data row. Fields will be named C<field1>,
153             C<field2>, and so on.
154              
155             =item * B<input_quote_char> => I<str>
156              
157             Specify field quote character in input CSV, will be passed to Text::CSV_XS.
158              
159             Defaults to C<"> (double quote). Overrides C<--input-tsv> option.
160              
161             =item * B<input_sep_char> => I<str>
162              
163             Specify field separator character in input CSV, will be passed to Text::CSV_XS.
164              
165             Defaults to C<,> (comma). Overrides C<--input-tsv> option.
166              
167             =item * B<input_tsv> => I<true>
168              
169             Inform that input file is in TSV (tab-separated) format instead of CSV.
170              
171             Overriden by C<--input-sep-char>, C<--input-quote-char>, C<--input-escape-char>
172             options. If one of those options is specified, then C<--input-tsv> will be
173             ignored.
174              
175             =item * B<output_always_quote> => I<bool> (default: 0)
176              
177             Whether to always quote values.
178              
179             When set to false (the default), values are quoted only when necessary:
180              
181             field1,field2,"field three contains comma (,)",field4
182              
183             When set to true, then all values will be quoted:
184              
185             "field1","field2","field three contains comma (,)","field4"
186              
187             =item * B<output_escape_char> => I<str>
188              
189             Specify character to escape value in field in output CSV, will be passed to Text::CSV_XS.
190              
191             This is like C<--input-escape-char> option but for output instead of input.
192              
193             Defaults to C<\\> (backslash). Overrides C<--output-tsv> option.
194              
195             =item * B<output_filename> => I<filename>
196              
197             Output filename.
198              
199             Use C<-> to output to stdout (the default if you don't specify this option).
200              
201             Encoding of output file is assumed to be UTF-8.
202              
203             =item * B<output_header> => I<bool>
204              
205             Whether output CSV should have a header row.
206              
207             By default, a header row will be output I<if> input CSV has header row. Under
208             C<--output-header>, a header row will be output even if input CSV does not have
209             header row (value will be something like "col0,col1,..."). Under
210             C<--no-output-header>, header row will I<not> be printed even if input CSV has
211             header row. So this option can be used to unconditionally add or remove header
212             row.
213              
214             =item * B<output_quote_char> => I<str>
215              
216             Specify field quote character in output CSV, will be passed to Text::CSV_XS.
217              
218             This is like C<--input-quote-char> option but for output instead of input.
219              
220             Defaults to C<"> (double quote). Overrides C<--output-tsv> option.
221              
222             =item * B<output_quote_empty> => I<bool> (default: 0)
223              
224             Whether to quote empty values.
225              
226             When set to false (the default), empty values are not quoted:
227              
228             field1,field2,,field4
229              
230             When set to true, then empty values will be quoted:
231              
232             field1,field2,"",field4
233              
234             =item * B<output_sep_char> => I<str>
235              
236             Specify field separator character in output CSV, will be passed to Text::CSV_XS.
237              
238             This is like C<--input-sep-char> option but for output instead of input.
239              
240             Defaults to C<,> (comma). Overrides C<--output-tsv> option.
241              
242             =item * B<output_tsv> => I<bool>
243              
244             Inform that output file is TSV (tab-separated) format instead of CSV.
245              
246             This is like C<--input-tsv> option but for output instead of input.
247              
248             Overriden by C<--output-sep-char>, C<--output-quote-char>, C<--output-escape-char>
249             options. If one of those options is specified, then C<--output-tsv> will be
250             ignored.
251              
252             =item * B<overwrite> => I<bool>
253              
254             Whether to override existing output file.
255              
256              
257             =back
258              
259             Returns an enveloped result (an array).
260              
261             First element ($status_code) is an integer containing HTTP-like status code
262             (200 means OK, 4xx caller error, 5xx function error). Second element
263             ($reason) is a string containing error message, or something like "OK" if status is
264             200. Third element ($payload) is the actual result, but usually not present when enveloped result is an error response ($status_code is not 2xx). Fourth
265             element (%result_meta) is called result metadata and is optional, a hash
266             that contains extra information, much like how HTTP response headers provide additional metadata.
267              
268             Return value: (any)
269              
270             =head1 HOMEPAGE
271              
272             Please visit the project's homepage at L<https://metacpan.org/release/App-CSVUtils>.
273              
274             =head1 SOURCE
275              
276             Source repository is at L<https://github.com/perlancar/perl-App-CSVUtils>.
277              
278             =head1 AUTHOR
279              
280             perlancar <perlancar@cpan.org>
281              
282             =head1 CONTRIBUTING
283              
284              
285             To contribute, you can send patches by email/via RT, or send pull requests on
286             GitHub.
287              
288             Most of the time, you don't need to build the distribution yourself. You can
289             simply modify the code, then test via:
290              
291             % prove -l
292              
293             If you want to build the distribution (e.g. to try to install it locally on your
294             system), you can install L<Dist::Zilla>,
295             L<Dist::Zilla::PluginBundle::Author::PERLANCAR>,
296             L<Pod::Weaver::PluginBundle::Author::PERLANCAR>, and sometimes one or two other
297             Dist::Zilla- and/or Pod::Weaver plugins. Any additional steps required beyond
298             that are considered a bug and can be reported to me.
299              
300             =head1 COPYRIGHT AND LICENSE
301              
302             This software is copyright (c) 2023, 2022, 2021, 2020, 2019, 2018, 2017, 2016 by perlancar <perlancar@cpan.org>.
303              
304             This is free software; you can redistribute it and/or modify it under
305             the same terms as the Perl 5 programming language system itself.
306              
307             =head1 BUGS
308              
309             Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=App-CSVUtils>
310              
311             When submitting a bug or request, please include a test-file or a
312             patch to an existing test-file that illustrates the bug or desired
313             feature.
314              
315             =cut