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