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