| 1 | package App::CSVUtils::csv_map; | ||||||||||||||||||||||||||||
| 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 | compile_eval_code | ||||||||||||||||||||||||||||
| 15 | eval_code | ||||||||||||||||||||||||||||
| 16 | 2 | ); | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | gen_csv_util( | ||||||||||||||||||||||||||||
| 19 | name => 'csv_map', | ||||||||||||||||||||||||||||
| 20 | summary => 'Return result of Perl code for every row', | ||||||||||||||||||||||||||||
| 21 | description => <<'MARKDOWN', | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | This is like Perl's `map` performed over rows of CSV. In `$_`, your Perl code | ||||||||||||||||||||||||||||
| 24 | will find the CSV row as an arrayref (or, if you specify `-H`, as a hashref). | ||||||||||||||||||||||||||||
| 25 | `$main::row` is also set to the row (always as arrayref). `$main::rownum` | ||||||||||||||||||||||||||||
| 26 | contains the row number (2 means the first data row). `$main::csv` is the | ||||||||||||||||||||||||||||
| 27 | <pm:Text::CSV_XS> object. `$main::fields_idx` is also available for additional | ||||||||||||||||||||||||||||
| 28 | information. | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | Your code is then free to return a string based on some operation against these | ||||||||||||||||||||||||||||
| 31 | data. This utility will then print out the resulting string. | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | MARKDOWN | ||||||||||||||||||||||||||||
| 34 | add_args => { | ||||||||||||||||||||||||||||
| 35 | %App::CSVUtils::argspecopt_hash, | ||||||||||||||||||||||||||||
| 36 | %App::CSVUtils::argspec_eval, | ||||||||||||||||||||||||||||
| 37 | add_newline => { | ||||||||||||||||||||||||||||
| 38 | summary => 'Whether to make sure each string ends with newline', | ||||||||||||||||||||||||||||
| 39 | 'summary.alt.bool.not' => 'Do not add newline to each output', | ||||||||||||||||||||||||||||
| 40 | schema => 'bool*', | ||||||||||||||||||||||||||||
| 41 | default => 1, | ||||||||||||||||||||||||||||
| 42 | }, | ||||||||||||||||||||||||||||
| 43 | }, | ||||||||||||||||||||||||||||
| 44 | tags => ['category:iterating', 'accepts-code'], | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | examples => [ | ||||||||||||||||||||||||||||
| 47 | { | ||||||||||||||||||||||||||||
| 48 | summary => 'Create SQL insert statements (escaping is left as an exercise for users)', | ||||||||||||||||||||||||||||
| 49 | argv => ['-He', '"INSERT INTO mytable (id,amount) VALUES ($_->{id}, $_->{amount});"', 'file.csv'], | ||||||||||||||||||||||||||||
| 50 | test => 0, | ||||||||||||||||||||||||||||
| 51 | 'x.doc.show_result' => 0, | ||||||||||||||||||||||||||||
| 52 | }, | ||||||||||||||||||||||||||||
| 53 | ], | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | writes_csv => 0, | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 3 | ▶ | on_begin => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 58 | 3 | my $r = shift; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | # for when we are called directly as a function without wrapper to set | ||||||||||||||||||||||||||||
| 61 | # defaults etc. | ||||||||||||||||||||||||||||
| 62 | 3 | ▶ | $r->{util_args}{add_newline} //= 1; | ||||||||||||||||||||||||||
|
Condition100%2/2
$r->{"util_args"}{"add_newline"} //= 1
MC/DC100%1/1
$r->{"util_args"}{"add_newline"} //= 1
$r->{"util_args"}{"add_newline"}
Truth table100%2/2
A
A= $r->{"util_args"}{"add_newline"}
| |||||||||||||||||||||||||||||
| 63 | }, | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 3 | ▶ | on_input_header_row => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 66 | 3 | my $r = shift; | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | # we add the following keys to the stash | ||||||||||||||||||||||||||||
| 69 | 3 | $r->{code} = compile_eval_code($r->{util_args}{eval}, 'eval'); | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 3 | ▶ | $r->{wants_input_row_as_hashref} = 1 if $r->{util_args}{hash}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | }, | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 9 | ▶ | on_input_data_row => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.3
| |||||||||||||||||||||||||||||
| 75 | 9 | my $r = shift; | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 9 | ▶ | my $rowres = eval_code($r->{code}, $r, $r->{wants_input_row_as_hashref} ? $r->{input_row_as_hashref} : $r->{input_row}) // ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
eval_code($r->{"code"}, $r, $r->{"wants_input_row_as_hashref"} ? $r->{"input_row_as_hashref"} : $r->{"input_row"}) // ""
MC/DC0%0/1
eval_code($r->{"code"}, $r, $r->{"wants_input_row_as_hashref"} ? $r->{"input_row_as_hashref"} : $r->{"input_row"}) // ""
eval_code($r->{"code"}, $r, $r->{"wants_input_row_as_hashref"} ? $r->{"input_row_as_hashref"} : $r->{"input_row"})
Truth table50%1/2
A
A= eval_code($r->{"code"}, $r, $r->{"wants_input_row_as_hashref"} ? $r->{"input_row_as_hashref"} : $r->{"input_row"})
| |||||||||||||||||||||||||||||
| 78 | 9 | ▶ | $rowres .= "\n" if $r->{util_args}{add_newline} && $rowres !~ /\R\z/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$r->{"util_args"}{"add_newline"} and not $rowres =~ /\R\z/
MC/DC50%1/2
$r->{"util_args"}{"add_newline"} and not $rowres =~ /\R\z/
$r->{"util_args"}{"add_newline"}
not $rowres =~ /\R\z/
Truth table66%2/3
A and not(B)
A= $r->{"util_args"}{"add_newline"}B= not $rowres =~ /\R\z/
| |||||||||||||||||||||||||||||
| 79 | 9 | $r->{code_print}->($rowres); | |||||||||||||||||||||||||||
| 80 | }, | ||||||||||||||||||||||||||||
| 81 | 2 | ); | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 2 | 1; | |||||||||||||||||||||||||||
| 84 | # ABSTRACT: Return result of Perl code for every row | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | __END__ | ||||||||||||||||||||||||||||