App-CSVUtils

 view release on metacpan or  search on metacpan

lib/App/CSVUtils/csv_munge_field.pm  view on Meta::CPAN

package App::CSVUtils::csv_munge_field;

use 5.010001;
use strict;
use warnings;

our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
our $DATE = '2025-02-04'; # DATE
our $DIST = 'App-CSVUtils'; # DIST
our $VERSION = '1.036'; # VERSION

use App::CSVUtils qw(
                        gen_csv_util
                        compile_eval_code
                        eval_code
                );

gen_csv_util(
    name => 'csv_munge_field',
    summary => 'Munge a field in every row of CSV file with Perl code',
    description => <<'_',

Perl code (-e) will be called for each row (excluding the header row) and `$_`
will contain the value of the field, and the Perl code is expected to modify it.
`$main::row` will contain the current row array. `$main::rownum` contains the
row number (2 means the first data row). `$main::csv` is the <pm:Text::CSV_XS>
object. `$main::fields_idx` is also available for additional information.

To munge multiple fields, use <prog:csv-munge-rows>.

_
    add_args => {
        %App::CSVUtils::argspec_field_1,
        %App::CSVUtils::argspec_eval_2,
    },
    tags => ['category:munging', 'modifies-field'],

    examples => [
        {
            summary => 'Square a number field in CSV',
            argv => ['file.csv', 'num', '$_ = $_*$_'],
            test => 0,
            'x.doc.show_result' => 0,
        },
    ],

    on_input_header_row => sub {
        my $r = shift;

        # check that selected field exists in the header
        my $field_idx = $r->{input_fields_idx}{ $r->{util_args}{field} };
        die [404, "Field '$r->{util_args}{field}' not found in CSV"]
            unless defined $field_idx;

        # we add the following keys to the stash
        $r->{code} = compile_eval_code($r->{util_args}{eval}, 'eval');
        $r->{field_idx} = $field_idx;
    },

    on_input_data_row => sub {
        my $r = shift;

        my $topic;
        eval { $topic = eval_code($r->{code}, $r, $r->{input_row}[$r->{field_idx}], 'return_topic') };
        die [500, "Error while munging row ".
             "#$r->{input_rownum} field '$r->{util_args}{field}': $@\n"] if $@;
        $r->{input_row}->[ $r->{field_idx} ] = $topic;
        $r->{code_print_row}->($r->{input_row});
    },
);

1;
# ABSTRACT: Munge a field in every row of CSV file with Perl code

__END__

=pod

=encoding UTF-8

=head1 NAME

App::CSVUtils::csv_munge_field - Munge a field in every row of CSV file with Perl code

=head1 VERSION

This document describes version 1.036 of App::CSVUtils::csv_munge_field (from Perl distribution App-CSVUtils), released on 2025-02-04.

=head1 FUNCTIONS


=head2 csv_munge_field

Usage:

 csv_munge_field(%args) -> [$status_code, $reason, $payload, \%result_meta]

Munge a field in every row of CSV file with Perl code.

Examples:

=over

=item * Square a number field in CSV:

 csv_munge_field(input_filename => "file.csv", field => "num", eval => "\$_ = \$_*\$_");

=back

Perl code (-e) will be called for each row (excluding the header row) and C<$_>
will contain the value of the field, and the Perl code is expected to modify it.
C<$main::row> will contain the current row array. C<$main::rownum> contains the



( run in 0.847 second using v1.01-cache-2.11-cpan-39bf76dae61 )