line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package App::CSVUtils::csv_dump; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
4205
|
use 5.010001; |
|
1
|
|
|
|
|
3
|
|
4
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
26
|
|
5
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
67
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY |
8
|
|
|
|
|
|
|
our $DATE = '2023-03-31'; # DATE |
9
|
|
|
|
|
|
|
our $DIST = 'App-CSVUtils'; # DIST |
10
|
|
|
|
|
|
|
our $VERSION = '1.023'; # VERSION |
11
|
|
|
|
|
|
|
|
12
|
1
|
|
|
1
|
|
5
|
use App::CSVUtils qw(gen_csv_util); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
259
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
gen_csv_util( |
15
|
|
|
|
|
|
|
name => 'csv_dump', |
16
|
|
|
|
|
|
|
summary => 'Dump CSV as data structure (array of array/hash)', |
17
|
|
|
|
|
|
|
description => <<'_', |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
This utility reads CSV file then dumps it as a text table, or as JSON if you |
20
|
|
|
|
|
|
|
specify the `--format=json` or `--json` option. |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
_ |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
add_args => { |
25
|
|
|
|
|
|
|
hash => { |
26
|
|
|
|
|
|
|
summary => 'Dump CSV as array of hashrefs instead of array of arrayrefs', |
27
|
|
|
|
|
|
|
schema => 'true*', |
28
|
|
|
|
|
|
|
cmdline_aliases => {H=>{}}, |
29
|
|
|
|
|
|
|
}, |
30
|
|
|
|
|
|
|
}, |
31
|
|
|
|
|
|
|
tags => ['category:converting', 'outputs-data-structure'], |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
examples => [ |
34
|
|
|
|
|
|
|
{ |
35
|
|
|
|
|
|
|
summary => 'Convert CSV to a text table', |
36
|
|
|
|
|
|
|
argv => ['file.csv'], |
37
|
|
|
|
|
|
|
test => 0, |
38
|
|
|
|
|
|
|
'x.doc.show_result' => 0, |
39
|
|
|
|
|
|
|
}, |
40
|
|
|
|
|
|
|
{ |
41
|
|
|
|
|
|
|
summary => 'Convert CSV to JSON', |
42
|
|
|
|
|
|
|
src => '[[prog]] file.csv --json', |
43
|
|
|
|
|
|
|
src_plang => 'bash', |
44
|
|
|
|
|
|
|
test => 0, |
45
|
|
|
|
|
|
|
'x.doc.show_result' => 0, |
46
|
|
|
|
|
|
|
}, |
47
|
|
|
|
|
|
|
], |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
on_input_header_row => sub { |
50
|
|
|
|
|
|
|
my $r = shift; |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
# this is a key we add ourselves |
53
|
|
|
|
|
|
|
$r->{output_rows} //= []; |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
if ($r->{util_args}{hash}) { |
56
|
|
|
|
|
|
|
$r->{wants_input_row_as_hashref} = 1; |
57
|
|
|
|
|
|
|
} else { |
58
|
|
|
|
|
|
|
push @{ $r->{output_rows} }, $r->{input_row}; |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
}, |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
on_input_data_row => sub { |
63
|
|
|
|
|
|
|
my $r = shift; |
64
|
|
|
|
|
|
|
push @{ $r->{output_rows} }, |
65
|
|
|
|
|
|
|
$r->{util_args}{hash} ? $r->{input_row_as_hashref} : $r->{input_row}; |
66
|
|
|
|
|
|
|
}, |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
writes_csv => 0, |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
on_end => sub { |
71
|
|
|
|
|
|
|
my $r = shift; |
72
|
|
|
|
|
|
|
$r->{result} = [200, "OK", $r->{output_rows}]; |
73
|
|
|
|
|
|
|
}, |
74
|
|
|
|
|
|
|
); |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
1; |
77
|
|
|
|
|
|
|
# ABSTRACT: Dump CSV as data structure (array of array/hash) |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
__END__ |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
=pod |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
=encoding UTF-8 |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=head1 NAME |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
App::CSVUtils::csv_dump - Dump CSV as data structure (array of array/hash) |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
=head1 VERSION |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
This document describes version 1.023 of App::CSVUtils::csv_dump (from Perl distribution App-CSVUtils), released on 2023-03-31. |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
=head1 FUNCTIONS |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=head2 csv_dump |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
Usage: |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
csv_dump(%args) -> [$status_code, $reason, $payload, \%result_meta] |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
Dump CSV as data structure (array of arrayE<sol>hash). |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
Examples: |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=over |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
=item * Convert CSV to a text table: |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
csv_dump(input_filename => "file.csv"); |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
=back |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
This utility reads CSV file then dumps it as a text table, or as JSON if you |
115
|
|
|
|
|
|
|
specify the C<--format=json> or C<--json> option. |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
This function is not exported. |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
Arguments ('*' denotes required arguments): |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
=over 4 |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
=item * B<hash> => I<true> |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
Dump CSV as array of hashrefs instead of array of arrayrefs. |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
=item * B<input_escape_char> => I<str> |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
Specify character to escape value in field in input CSV, will be passed to Text::CSV_XS. |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
Defaults to C<\\> (backslash). Overrides C<--input-tsv> option. |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
=item * B<input_filename> => I<filename> (default: "-") |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
Input CSV file. |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
Use C<-> to read from stdin. |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
Encoding of input file is assumed to be UTF-8. |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
=item * B<input_header> => I<bool> (default: 1) |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
Specify whether input CSV has a header row. |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
By default, the first row of the input CSV will be assumed to contain field |
146
|
|
|
|
|
|
|
names (and the second row contains the first data row). When you declare that |
147
|
|
|
|
|
|
|
input CSV does not have header row (C<--no-input-header>), the first row of the |
148
|
|
|
|
|
|
|
CSV is assumed to contain the first data row. Fields will be named C<field1>, |
149
|
|
|
|
|
|
|
C<field2>, and so on. |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
=item * B<input_quote_char> => I<str> |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
Specify field quote character in input CSV, will be passed to Text::CSV_XS. |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
Defaults to C<"> (double quote). Overrides C<--input-tsv> option. |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=item * B<input_sep_char> => I<str> |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
Specify field separator character in input CSV, will be passed to Text::CSV_XS. |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
Defaults to C<,> (comma). Overrides C<--input-tsv> option. |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
=item * B<input_tsv> => I<true> |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
Inform that input file is in TSV (tab-separated) format instead of CSV. |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
Overriden by C<--input-sep-char>, C<--input-quote-char>, C<--input-escape-char> |
168
|
|
|
|
|
|
|
options. If one of those options is specified, then C<--input-tsv> will be |
169
|
|
|
|
|
|
|
ignored. |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=back |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
Returns an enveloped result (an array). |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
First element ($status_code) is an integer containing HTTP-like status code |
177
|
|
|
|
|
|
|
(200 means OK, 4xx caller error, 5xx function error). Second element |
178
|
|
|
|
|
|
|
($reason) is a string containing error message, or something like "OK" if status is |
179
|
|
|
|
|
|
|
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 |
180
|
|
|
|
|
|
|
element (%result_meta) is called result metadata and is optional, a hash |
181
|
|
|
|
|
|
|
that contains extra information, much like how HTTP response headers provide additional metadata. |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
Return value: (any) |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
=head1 HOMEPAGE |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
Please visit the project's homepage at L<https://metacpan.org/release/App-CSVUtils>. |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
=head1 SOURCE |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
Source repository is at L<https://github.com/perlancar/perl-App-CSVUtils>. |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
=head1 AUTHOR |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
perlancar <perlancar@cpan.org> |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
=head1 CONTRIBUTING |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
To contribute, you can send patches by email/via RT, or send pull requests on |
201
|
|
|
|
|
|
|
GitHub. |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
Most of the time, you don't need to build the distribution yourself. You can |
204
|
|
|
|
|
|
|
simply modify the code, then test via: |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
% prove -l |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
If you want to build the distribution (e.g. to try to install it locally on your |
209
|
|
|
|
|
|
|
system), you can install L<Dist::Zilla>, |
210
|
|
|
|
|
|
|
L<Dist::Zilla::PluginBundle::Author::PERLANCAR>, |
211
|
|
|
|
|
|
|
L<Pod::Weaver::PluginBundle::Author::PERLANCAR>, and sometimes one or two other |
212
|
|
|
|
|
|
|
Dist::Zilla- and/or Pod::Weaver plugins. Any additional steps required beyond |
213
|
|
|
|
|
|
|
that are considered a bug and can be reported to me. |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
This software is copyright (c) 2023, 2022, 2021, 2020, 2019, 2018, 2017, 2016 by perlancar <perlancar@cpan.org>. |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
220
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
=head1 BUGS |
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=App-CSVUtils> |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
When submitting a bug or request, please include a test-file or a |
227
|
|
|
|
|
|
|
patch to an existing test-file that illustrates the bug or desired |
228
|
|
|
|
|
|
|
feature. |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
=cut |