App-CSVUtils
view release on metacpan or search on metacpan
lib/App/CSVUtils/csv_check_rows.pm view on Meta::CPAN
package App::CSVUtils::csv_check_rows;
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
);
gen_csv_util(
name => 'csv_check_rows',
summary => 'Check CSV rows',
description => <<'_',
This utility performs the following checks:
For header row:
For data rows:
- There are the same number of values as the number of fields (no missing
values, no extraneous values)
For each failed check, an error message will be printed to stderr. And if there
is any error, the exit code will be non-zero. If there is no error, the utility
outputs nothing and exits with code zero.
There will be options to add some additional checks in the future.
Note that parsing errors, e.g. missing closing quotes on values, are currently
handled by <pm:Text::CSV_XS>.
_
add_args => {
},
tags => ['category:checking'],
examples => [
{
summary => 'Check CSV rows',
argv => ['file.csv'],
test => 0,
'x.doc.show_result' => 0,
},
],
writes_csv => 0,
on_input_header_row => sub {
my $r = shift;
$r->{wants_fill_rows} = 0;
# we add the following key(s) to the stash
$r->{num_errors} = 0;
},
on_input_data_row => sub {
my $r = shift;
if (@{ $r->{input_row} } != @{ $r->{input_fields} }) {
warn "csv-check-rows: Row #$r->{input_rownum}: There are too few/many values (".scalar(@{ $r->{input_row} }).", should be ".scalar(@{ $r->{input_fields} }).")\n";
$r->{num_errors}++;
}
},
after_close_input_files => sub {
my $r = shift;
$r->{result} = $r->{num_errors} ? [400, "Some rows have error"] : [200, "All rows ok"];
},
);
1;
# ABSTRACT: Check CSV rows
__END__
=pod
=encoding UTF-8
=head1 NAME
App::CSVUtils::csv_check_rows - Check CSV rows
=head1 VERSION
This document describes version 1.036 of App::CSVUtils::csv_check_rows (from Perl distribution App-CSVUtils), released on 2025-02-04.
=head1 FUNCTIONS
=head2 csv_check_rows
Usage:
csv_check_rows(%args) -> [$status_code, $reason, $payload, \%result_meta]
Check CSV rows.
Examples:
=over
=item * Check CSV rows:
csv_check_rows(input_filename => "file.csv");
=back
This utility performs the following checks:
For header row:
For data rows:
=over
=item * There are the same number of values as the number of fields (no missing
values, no extraneous values)
=back
For each failed check, an error message will be printed to stderr. And if there
is any error, the exit code will be non-zero. If there is no error, the utility
outputs nothing and exits with code zero.
There will be options to add some additional checks in the future.
Note that parsing errors, e.g. missing closing quotes on values, are currently
handled by L<Text::CSV_XS>.
This function is not exported.
Arguments ('*' denotes required arguments):
=over 4
=item * B<input_escape_char> => I<str>
Specify character to escape value in field in input CSV, will be passed to Text::CSV_XS.
Defaults to C<\\> (backslash). Overrides C<--input-tsv> option.
=item * B<input_filename> => I<filename> (default: "-")
Input CSV file.
Use C<-> to read from stdin.
Encoding of input file is assumed to be UTF-8.
=item * B<input_header> => I<bool> (default: 1)
Specify whether input CSV has a header row.
By default, the first row of the input CSV will be assumed to contain field
names (and the second row contains the first data row). When you declare that
input CSV does not have header row (C<--no-input-header>), the first row of the
CSV is assumed to contain the first data row. Fields will be named C<field1>,
C<field2>, and so on.
=item * B<input_quote_char> => I<str>
Specify field quote character in input CSV, will be passed to Text::CSV_XS.
Defaults to C<"> (double quote). Overrides C<--input-tsv> option.
=item * B<input_sep_char> => I<str>
Specify field separator character in input CSV, will be passed to Text::CSV_XS.
Defaults to C<,> (comma). Overrides C<--input-tsv> option.
=item * B<input_skip_num_lines> => I<posint>
Number of lines to skip before header row.
This can be useful if you have a CSV files (usually some generated reports,
sometimes converted from spreadsheet) that have additional header lines or info
before the CSV header row.
See also the alternative option: C<--input-skip-until-pattern>.
( run in 3.397 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )