| 1 | package App::CSVUtils::csv_cmp; | ||||||||||||||||||||||||||||
| 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 | 1 | ▶ | use Log::ger; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 1 | our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY | |||||||||||||||||||||||||||
| 9 | 1 | our $DATE = '2026-07-09'; # DATE | |||||||||||||||||||||||||||
| 10 | 1 | our $DIST = 'App-CSVUtils'; # DIST | |||||||||||||||||||||||||||
| 11 | 1 | our $VERSION = '1.038'; # VERSION | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use App::CSVUtils qw( | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | gen_csv_util | ||||||||||||||||||||||||||||
| 15 | 1 | ); | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | gen_csv_util( | ||||||||||||||||||||||||||||
| 18 | name => 'csv_cmp', | ||||||||||||||||||||||||||||
| 19 | summary => 'Compare two CSV files value by value', | ||||||||||||||||||||||||||||
| 20 | description => <<'MARKDOWN', | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | This utility is modelled after the Unix command `cmp`; it compares two CSV files | ||||||||||||||||||||||||||||
| 23 | value by value and ignore quoting (and can be instructed to ignore whitespaces, | ||||||||||||||||||||||||||||
| 24 | case difference). | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | If all the values of two CSV files are identical, then utility will exit with | ||||||||||||||||||||||||||||
| 27 | code 0. If a value differ, this utility will stop, print the difference and exit | ||||||||||||||||||||||||||||
| 28 | with code 1. | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | If `-l` (`--detail`) option is specified, all differences will be reported. Note | ||||||||||||||||||||||||||||
| 31 | that in `cmp` Unix command, the `-l` option is called `--verbose`. The detailed | ||||||||||||||||||||||||||||
| 32 | report is in the form of CSV: | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | rownum,fieldnum,value1,value2 | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | where `rownum` begins at 1 (for header row), `fieldnum` begins at 1 (first | ||||||||||||||||||||||||||||
| 37 | field), `value1` is the value in first CSV file, `value2` is the value in the | ||||||||||||||||||||||||||||
| 38 | second CSV file. | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | Other notes: | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | * If none of the field selection options are used, it means all fields are | ||||||||||||||||||||||||||||
| 43 | included (equivalent to `--include-all-fields`). | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | * Field selection will be performed on the first CSV file, then the indexes will | ||||||||||||||||||||||||||||
| 46 | be used for the second CSV file. | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | MARKDOWN | ||||||||||||||||||||||||||||
| 49 | add_args => { | ||||||||||||||||||||||||||||
| 50 | %App::CSVUtils::argspecsopt_field_selection, | ||||||||||||||||||||||||||||
| 51 | %App::CSVUtils::argspecsopt_show_selected_fields, | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | detail => { | ||||||||||||||||||||||||||||
| 54 | summary => 'Report all differences instead of just the first one', | ||||||||||||||||||||||||||||
| 55 | schema => 'true*', | ||||||||||||||||||||||||||||
| 56 | cmdline_aliases => {l=>{}}, | ||||||||||||||||||||||||||||
| 57 | }, | ||||||||||||||||||||||||||||
| 58 | quiet => { | ||||||||||||||||||||||||||||
| 59 | summary => 'Do not report, just signal via exit code', | ||||||||||||||||||||||||||||
| 60 | schema => 'true*', | ||||||||||||||||||||||||||||
| 61 | cmdline_aliases => {q=>{}}, | ||||||||||||||||||||||||||||
| 62 | }, | ||||||||||||||||||||||||||||
| 63 | ignore_case => { | ||||||||||||||||||||||||||||
| 64 | summary => 'Ignore case difference', | ||||||||||||||||||||||||||||
| 65 | schema => 'bool*', | ||||||||||||||||||||||||||||
| 66 | cmdline_aliases => {i=>{}}, | ||||||||||||||||||||||||||||
| 67 | }, | ||||||||||||||||||||||||||||
| 68 | ignore_leading_ws => { | ||||||||||||||||||||||||||||
| 69 | summary => 'Ignore leading whitespaces', | ||||||||||||||||||||||||||||
| 70 | schema => 'bool*', | ||||||||||||||||||||||||||||
| 71 | }, | ||||||||||||||||||||||||||||
| 72 | ignore_trailing_ws => { | ||||||||||||||||||||||||||||
| 73 | summary => 'Ignore trailing whitespaces', | ||||||||||||||||||||||||||||
| 74 | schema => 'bool*', | ||||||||||||||||||||||||||||
| 75 | }, | ||||||||||||||||||||||||||||
| 76 | ignore_ws => { | ||||||||||||||||||||||||||||
| 77 | summary => 'Ignore leading & trailing whitespaces', | ||||||||||||||||||||||||||||
| 78 | schema => 'bool*', | ||||||||||||||||||||||||||||
| 79 | }, | ||||||||||||||||||||||||||||
| 80 | }, | ||||||||||||||||||||||||||||
| 81 | tags => [ | ||||||||||||||||||||||||||||
| 82 | 'accepts-regex', # for selecting fields | ||||||||||||||||||||||||||||
| 83 | 'category:comparing', | ||||||||||||||||||||||||||||
| 84 | ], | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | examples => [ | ||||||||||||||||||||||||||||
| 87 | { | ||||||||||||||||||||||||||||
| 88 | summary => 'Compare two identical files, will output nothing and exits 0', | ||||||||||||||||||||||||||||
| 89 | argv => ['file.csv', 'file.csv'], | ||||||||||||||||||||||||||||
| 90 | test => 0, | ||||||||||||||||||||||||||||
| 91 | 'x.doc.show_result' => 0, | ||||||||||||||||||||||||||||
| 92 | }, | ||||||||||||||||||||||||||||
| 93 | { | ||||||||||||||||||||||||||||
| 94 | summary => 'Compare two CSV files case-insensitively (-i), show detailed report (-l)', | ||||||||||||||||||||||||||||
| 95 | argv => ['file1.csv', 'file2.csv', '-il'], | ||||||||||||||||||||||||||||
| 96 | test => 0, | ||||||||||||||||||||||||||||
| 97 | 'x.doc.show_result' => 0, | ||||||||||||||||||||||||||||
| 98 | }, | ||||||||||||||||||||||||||||
| 99 | ], | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | reads_multiple_csv => 1, | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 0 | ▶ | on_begin => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 104 | 0 | my $r = shift; | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 0 | ▶ | unless ($r->{util_args}{input_filenames} && @{ $r->{util_args}{input_filenames} } == 2) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$r->{"util_args"}{"input_filenames"} and @{$r->{"util_args"}{"input_filenames"};} == 2
MC/DC0%0/2
$r->{"util_args"}{"input_filenames"} and @{$r->{"util_args"}{"input_filenames"};} == 2
$r->{"util_args"}{"input_filenames"}
@{$r->{"util_args"}{"input_filenames"};} == 2
Truth table0%0/3
A and B
A= $r->{"util_args"}{"input_filenames"}B= @{$r->{"util_args"}{"input_filenames"};} == 2
| |||||||||||||||||||||||||||||
| 107 | 0 | die [400, "Please specify exactly two files to compare"]; | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | # no point in generating detailed report if we're not going to show it | ||||||||||||||||||||||||||||
| 110 | 0 | ▶ | $r->{util_args}{detail} = 0 if $r->{util_args}{quiet}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | }, | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 0 | ▶ | before_open_input_files => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 114 | 0 | my $r = shift; | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | # we add the following keys to the stash | ||||||||||||||||||||||||||||
| 117 | 0 | $r->{all_input_rows} = [[], []]; # including header row. format: [ [row1 of csv1, row2 of csv1...], [row1 of csv2, row2 of csv2, ...] ] | |||||||||||||||||||||||||||
| 118 | 0 | $r->{selected_fields_idx_array_sorted} = undef; | |||||||||||||||||||||||||||
| 119 | }, | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 0 | ▶ | on_input_header_row => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 122 | 0 | my $r = shift; | |||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 0 | push @{ $r->{all_input_rows}[ $r->{input_filenum}-1 ] }, $r->{input_fields}; | |||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 0 | ▶ | if ($r->{input_filenum} == 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | # set selected_fields_idx_array_sorted | ||||||||||||||||||||||||||||
| 128 | 0 | my $res = App::CSVUtils::_select_fields($r->{input_fields}, $r->{input_fields_idx}, $r->{util_args}, 'all'); | |||||||||||||||||||||||||||
| 129 | 0 | ▶ | die $res unless $res->[0] == 100; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | 0 | my $selected_fields = $res->[2][0]; | |||||||||||||||||||||||||||
| 131 | 0 | my $selected_fields_idx_array = $res->[2][1]; | |||||||||||||||||||||||||||
| 132 | 0 | ▶ | die [412, "At least one field must be selected"] | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | unless @$selected_fields; | ||||||||||||||||||||||||||||
| 134 | 0 | $r->{selected_fields_idx_array_sorted} = [sort { $b <=> $a } @$selected_fields_idx_array]; | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 0 | ▶ | if ($r->{util_args}{show_selected_fields}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | 0 | $r->{wants_skip_files}++; | |||||||||||||||||||||||||||
| 138 | 0 | $r->{result} = [200, "OK", $selected_fields]; | |||||||||||||||||||||||||||
| 139 | 0 | return; | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | }, | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 0 | ▶ | on_input_data_row => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 145 | 0 | my $r = shift; | |||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 0 | push @{ $r->{all_input_rows}[ $r->{input_filenum}-1 ] }, $r->{input_row}; | |||||||||||||||||||||||||||
| 148 | }, | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 0 | ▶ | after_close_input_files => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=25 SCAR=64.8
| |||||||||||||||||||||||||||||
| 151 | 0 | my $r = shift; | |||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 0 | $r->{output_fields} = ["rownum","fieldnum","value1","value2"]; | |||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 0 | my $exit_code = 0; | |||||||||||||||||||||||||||
| 156 | 0 | my $numrows1 = @{ $r->{all_input_rows}[0] }; | |||||||||||||||||||||||||||
| 157 | 0 | my $numrows2 = @{ $r->{all_input_rows}[1] }; | |||||||||||||||||||||||||||
| 158 | 0 | my $numfields1 = @{ $r->{all_input_rows}[0][0] }; | |||||||||||||||||||||||||||
| 159 | 0 | my $numfields2 = @{ $r->{all_input_rows}[1][0] }; | |||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 0 | ▶ | if ($numfields1 > $numfields2) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 162 | warn "csv-cmp: second CSV only has $numfields2 field(s) (vs $numfields1)\n" | ||||||||||||||||||||||||||||
| 163 | 0 | ▶ | unless $r->{util_args}{quiet}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | 0 | $exit_code = 1; | |||||||||||||||||||||||||||
| 165 | 0 | ▶ | goto DONE unless $r->{util_args}{detail}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 166 | } elsif ($numfields1 < $numfields2) { | ||||||||||||||||||||||||||||
| 167 | warn "csv-cmp: first CSV only has $numfields1 field(s) (vs $numfields2)\n" | ||||||||||||||||||||||||||||
| 168 | 0 | ▶ | unless $r->{util_args}{quiet}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 169 | 0 | $exit_code = 1; | |||||||||||||||||||||||||||
| 170 | 0 | ▶ | goto DONE unless $r->{util_args}{detail}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 0 | ▶ | my $numrows_min = $numrows1 < $numrows2 ? $numrows1 : $numrows2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 174 | 0 | for my $rownum (1 .. $numrows_min) { | |||||||||||||||||||||||||||
| 175 | 0 | for my $j (@{ $r->{selected_fields_idx_array_sorted} }) { | |||||||||||||||||||||||||||
| 176 | 0 | my $fieldnum = $j+1; | |||||||||||||||||||||||||||
| 177 | 0 | my $origvalue1 = my $value1 = $r->{all_input_rows}[0][ $rownum-1 ][ $fieldnum-1 ]; | |||||||||||||||||||||||||||
| 178 | 0 | my $origvalue2 = my $value2 = $r->{all_input_rows}[1][ $rownum-1 ][ $fieldnum-1 ]; | |||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 0 | ▶ | if ($r->{util_args}{ignore_case}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 181 | 0 | $value1 = lc $value1; | |||||||||||||||||||||||||||
| 182 | 0 | $value2 = lc $value2; | |||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | 0 | ▶ | if ($r->{util_args}{ignore_ws} || $r->{util_args}{ignore_leading_ws}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$r->{"util_args"}{"ignore_ws"} or $r->{"util_args"}{"ignore_leading_ws"}
MC/DC0%0/2
$r->{"util_args"}{"ignore_ws"} or $r->{"util_args"}{"ignore_leading_ws"}
$r->{"util_args"}{"ignore_ws"}
$r->{"util_args"}{"ignore_leading_ws"}
Truth table0%0/3
A or B
A= $r->{"util_args"}{"ignore_ws"}B= $r->{"util_args"}{"ignore_leading_ws"}
| |||||||||||||||||||||||||||||
| 185 | 0 | $value1 =~ s/\A\s+//s; | |||||||||||||||||||||||||||
| 186 | 0 | $value2 =~ s/\A\s+//s; | |||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | 0 | ▶ | if ($r->{util_args}{ignore_ws} || $r->{util_args}{ignore_trailing_ws}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$r->{"util_args"}{"ignore_ws"} or $r->{"util_args"}{"ignore_trailing_ws"}
MC/DC0%0/2
$r->{"util_args"}{"ignore_ws"} or $r->{"util_args"}{"ignore_trailing_ws"}
$r->{"util_args"}{"ignore_ws"}
$r->{"util_args"}{"ignore_trailing_ws"}
Truth table0%0/3
A or B
A= $r->{"util_args"}{"ignore_ws"}B= $r->{"util_args"}{"ignore_trailing_ws"}
| |||||||||||||||||||||||||||||
| 189 | 0 | $value1 =~ s/\s+\z//s; | |||||||||||||||||||||||||||
| 190 | 0 | $value2 =~ s/\s+\z//s; | |||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | 0 | ▶ | if ($value1 ne $value2) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 194 | 0 | $exit_code = 1; | |||||||||||||||||||||||||||
| 195 | 0 | ▶ | if ($r->{util_args}{detail}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 196 | $r->{code_print_row}->([$rownum, $fieldnum, $origvalue1, $origvalue2]) | ||||||||||||||||||||||||||||
| 197 | 0 | ▶ | unless $r->{util_args}{quiet}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 198 | } else { | ||||||||||||||||||||||||||||
| 199 | warn "csv-cmp: Value differ at rownum $rownum fieldnum $fieldnum: '$origvalue1' vs '$origvalue2'\n" | ||||||||||||||||||||||||||||
| 200 | 0 | ▶ | unless $r->{util_args}{quiet}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 201 | 0 | goto DONE; | |||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||
| 204 | } # for field | ||||||||||||||||||||||||||||
| 205 | } # for row | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | 0 | ▶ | if ($numrows1 > $numrows2) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 208 | warn "csv-cmp: EOF: second CSV only has $numrows2 row(s) (vs $numrows1)\n" | ||||||||||||||||||||||||||||
| 209 | 0 | ▶ | unless $r->{util_args}{quiet}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 210 | 0 | $exit_code = 1; | |||||||||||||||||||||||||||
| 211 | 0 | ▶ | goto DONE unless $r->{util_args}{detail}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 212 | } elsif ($numrows1 < $numrows2) { | ||||||||||||||||||||||||||||
| 213 | warn "csv-cmp: EOF: first CSV only has $numrows1 row(s) (vs $numrows2)\n" | ||||||||||||||||||||||||||||
| 214 | 0 | ▶ | unless $r->{util_args}{quiet}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 215 | 0 | $exit_code = 1; | |||||||||||||||||||||||||||
| 216 | 0 | ▶ | goto DONE unless $r->{util_args}{detail}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | DONE: | ||||||||||||||||||||||||||||
| 220 | 0 | $r->{result} = [200, "OK", "", {"cmdline.exit_code"=>$exit_code}]; | |||||||||||||||||||||||||||
| 221 | }, | ||||||||||||||||||||||||||||
| 222 | 1 | ); | |||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | 1 | 1; | |||||||||||||||||||||||||||
| 225 | # ABSTRACT: Compare two CSV files value by value | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | __END__ | ||||||||||||||||||||||||||||