CSVUtils/csv_check_rows.pm

filter: Statementstmt 56.6% 17 / 30 Branchbran 0.0% 0 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 57.1% 4 / 7 Podpod n/a totaltotal 51.2% 21 / 41 CC 3 SCAR 12.0
CC
3
Cov
50%
__ANON__
6.0
__ANON__
6.0
__ANON__
2.0
CRAP 3.3
« CSVUtils/csv_pick_fields.pm ↑ Summary CSVUtils/csv_shuf_rows.pm »
1 package App::CSVUtils::csv_check_rows;
2  
3 1 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
8 1 our $DATE = '2026-07-09'; # DATE
9 1 our $DIST = 'App-CSVUtils'; # DIST
10 1 our $VERSION = '1.038'; # VERSION
11  
12 1 ▶use App::CSVUtils qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
13 gen_csv_util
14 1 );
15  
16 gen_csv_util(
17 name => 'csv_check_rows',
18 summary => 'Check CSV rows',
19 description => <<'MARKDOWN',
20  
21 This utility performs the following checks:
22  
23 For header row:
24  
25 For data rows:
26  
27 - There are the same number of values as the number of fields (no missing
28 values, no extraneous values)
29  
30 For each failed check, an error message will be printed to stderr. And if there
31 is any error, the exit code will be non-zero. If there is no error, the utility
32 outputs nothing and exits with code zero.
33  
34 There will be options to add some additional checks in the future.
35  
36 Note that parsing errors, e.g. missing closing quotes on values, are currently
37 handled by <pm:Text::CSV_XS>.
38  
39 MARKDOWN
40 add_args => {
41 },
42 tags => ['category:checking'],
43  
44 examples => [
45 {
46 summary => 'Check CSV rows',
47 argv => ['file.csv'],
48 test => 0,
49 'x.doc.show_result' => 0,
50 },
51 ],
52  
53 writes_csv => 0,
54  
55 0 ▶ on_input_header_row => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
56 0 my $r = shift;
57  
58 0 $r->{wants_fill_rows} = 0;
59  
60 # we add the following key(s) to the stash
61 0 $r->{num_errors} = 0;
62 },
63  
64 0 ▶ on_input_data_row => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
65 0 my $r = shift;
66  
67 0 ▶ if (@{ $r->{input_row} } != @{ $r->{input_fields} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$r->{"input_row"};} != @{$r->{"input_fields"};}) 0 0 0
68 0 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";
69 0 $r->{num_errors}++;
70 }
71 },
72  
73 0 ▶ after_close_input_files => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
74 0 my $r = shift;
75  
76 0 ▶ $r->{result} = $r->{num_errors} ? [400, "Some rows have error"] : [200, "All rows ok"];
Branch: 1 branch
BranchTrueFalseTotal
$r->{'num_errors'} ? : 0 0 0
77 },
78 1 );
79  
80 1 1;
81 # ABSTRACT: Check CSV rows
82  
83 __END__
« CSVUtils/csv_pick_fields.pm ↑ Summary CSVUtils/csv_shuf_rows.pm »