| 1 | package App::CSVUtils::csv_munge_field; | ||||||||||
| 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_munge_field', | ||||||||||
| 20 | summary => 'Munge a field in every row of CSV file with Perl code', | ||||||||||
| 21 | description => <<'MARKDOWN', | ||||||||||
| 22 | |||||||||||
| 23 | Perl code (-e) will be called for each row (excluding the header row) and `$_` | ||||||||||
| 24 | will contain the value of the field, and the Perl code is expected to modify it. | ||||||||||
| 25 | `$main::row` will contain the current row array. `$main::rownum` contains the | ||||||||||
| 26 | row number (2 means the first data row). `$main::csv` is the <pm:Text::CSV_XS> | ||||||||||
| 27 | object. `$main::fields_idx` is also available for additional information. | ||||||||||
| 28 | |||||||||||
| 29 | To munge multiple fields, use <prog:csv-munge-rows>. | ||||||||||
| 30 | |||||||||||
| 31 | MARKDOWN | ||||||||||
| 32 | add_args => { | ||||||||||
| 33 | %App::CSVUtils::argspec_field_1, | ||||||||||
| 34 | %App::CSVUtils::argspec_eval_2, | ||||||||||
| 35 | }, | ||||||||||
| 36 | tags => ['category:munging', 'modifies-field'], | ||||||||||
| 37 | |||||||||||
| 38 | examples => [ | ||||||||||
| 39 | { | ||||||||||
| 40 | summary => 'Square a number field in CSV', | ||||||||||
| 41 | argv => ['file.csv', 'num', '$_ = $_*$_'], | ||||||||||
| 42 | test => 0, | ||||||||||
| 43 | 'x.doc.show_result' => 0, | ||||||||||
| 44 | }, | ||||||||||
| 45 | ], | ||||||||||
| 46 | |||||||||||
| 47 | 3 | ▶ | on_input_header_row => sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 48 | 3 | my $r = shift; | |||||||||
| 49 | |||||||||||
| 50 | # check that selected field exists in the header | ||||||||||
| 51 | 3 | my $field_idx = $r->{input_fields_idx}{ $r->{util_args}{field} }; | |||||||||
| 52 | 3 | ▶ | die [404, "Field '$r->{util_args}{field}' not found in CSV"] | ||||||||
|
Branch: 1 branch
| |||||||||||
| 53 | unless defined $field_idx; | ||||||||||
| 54 | |||||||||||
| 55 | # we add the following keys to the stash | ||||||||||
| 56 | 2 | $r->{code} = compile_eval_code($r->{util_args}{eval}, 'eval'); | |||||||||
| 57 | 1 | $r->{field_idx} = $field_idx; | |||||||||
| 58 | }, | ||||||||||
| 59 | |||||||||||
| 60 | 3 | ▶ | on_input_data_row => sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||
| 61 | 3 | my $r = shift; | |||||||||
| 62 | |||||||||||
| 63 | 3 | my $topic; | |||||||||
| 64 | 3 | eval { $topic = eval_code($r->{code}, $r, $r->{input_row}[$r->{field_idx}], 'return_topic') }; | |||||||||
| 65 | 3 | ▶ | die [500, "Error while munging row ". | ||||||||
|
Branch: 1 branch
| |||||||||||
| 66 | "#$r->{input_rownum} field '$r->{util_args}{field}': $@\n"] if $@; | ||||||||||
| 67 | 3 | $r->{input_row}->[ $r->{field_idx} ] = $topic; | |||||||||
| 68 | 3 | $r->{code_print_row}->($r->{input_row}); | |||||||||
| 69 | }, | ||||||||||
| 70 | 2 | ); | |||||||||
| 71 | |||||||||||
| 72 | 2 | 1; | |||||||||
| 73 | # ABSTRACT: Munge a field in every row of CSV file with Perl code | ||||||||||
| 74 | |||||||||||
| 75 | __END__ | ||||||||||