bin/diff_spreadsheets

filter: Statementstmt 29.2% 273 / 933 Branchbran 11.2% 45 / 400 Conditioncond 12.6% 23 / 182 MC/DCmcdc 0.0% 0 / 110 Subroutinesub 41.1% 44 / 107 Podpod n/a totaltotal 22.2% 385 / 1732 CC 258 SCAR 55.2
CC
258
Cov
23%
_pairup_columns
756.0
_native_output
462.0
show_row
462.0
CRAP 249.8
↑ Summary /usr/local/bin/perldoc »
1 #!/usr/bin/env perl
2 # License: Public Domain or CC0
3 # See https://creativecommons.org/publicdomain/zero/1.0/
4 # The author, Jim Avera (jim.avera at gmail) has waived all copyright and
5 # related or neighboring rights. Attribution is requested but is not required.
6 3 ▶use strict; use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 our $VERSION = '1.028'; # VERSION
9 3 our $DATE = '2026-01-13'; # DATE
10  
11 #use 5.12; # for unicode_strings
12 3 ▶use v5.18; # for lexical_subs
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 ▶use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use feature qw(state say lexical_subs fc);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use feature qw(unicode_strings unicode_eval evalbytes);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶no warnings "experimental::lexical_subs";
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 3 ▶use Carp; $Carp::MaxArgNums = 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 3 ▶use Data::Dumper::Interp 6.007 qw/:DEFAULT dviso/;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 3 ▶use Path::Tiny 0.144;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 #use File::Basename qw(basename dirname fileparse);
23 #use File::Path qw(make_path remove_tree);
24 #use File::Temp qw(tempfile tempdir);
25 #use File::Copy ();
26 #use File::Spec;
27 #use File::Spec::Functions qw(canonpath catfile catdir rootdir tmpdir);
28 3 ▶use File::Spec::Functions qw(tmpdir);
Subroutine BEGIN: called CC=1 SCAR=0.0
29 3 ▶use FindBin qw/$Bin $Script/;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 3 ▶use Getopt::Long qw/GetOptions/;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 3 ▶use Guard qw(guard scope_guard);
Subroutine BEGIN: called CC=1 SCAR=0.0
32 3 ▶use List::Util qw/min max any first/;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 3 ▶use List::MoreUtils qw/indexes/;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 3 ▶use Scalar::Util qw/openhandle/;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 3 ▶use Pod::Usage qw/pod2usage/;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 3 ▶use Spreadsheet::Edit 1000.020 qw(title2ident);
Subroutine BEGIN: called CC=1 SCAR=0.0
37 3 ▶use Spreadsheet::Edit::IO 1000.020 qw/convert_spreadsheet
Subroutine BEGIN: called CC=1 SCAR=0.0
38 3 sheetname_from_spec filepath_from_spec form_spec_with_sheetname/;
39 3 ▶use Spreadsheet::Edit::Log ':btw';
Subroutine BEGIN: called CC=1 SCAR=0.0
40 3 ▶use Term::ReadKey ();
Subroutine BEGIN: called CC=1 SCAR=0.0
41 3 ▶use Encode 3.00 qw/decode/; # for ONLY_PRAGMA_WARNINGS
Subroutine BEGIN: called CC=1 SCAR=0.0
42 3 require PerlIO;
43 0 ▶sub oops(@) { unshift @_, "oops "; require Carp; goto &Carp::confess; }
Subroutine oops: not called CC=1 SCAR=6.9
44 #$SIG{__WARN__} = sub{ Carp::cluck @_ };
45  
46 sub main::Differ::compile_if_regex(@); #forward
47  
48 # Replace invalid/undesirable filename characters with underscore
49 0 ▶sub sanitize_filename(_) { local $_ = shift; s/[^-._[:word:]]/_/g; s/_$//; $_ }
Subroutine sanitize_filename: not called CC=1 SCAR=6.9
50  
51 3 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 3 my ($visible_space, $RArrow);
54  
55 # By default set output encoding to match the user's terminal/locale,
56 # and suppress the "...does not mapt to..." warnings if unsupported characters
57 # are displayed e.g. in spreadsheet data diff displays (\x{hex} escapes will
58 # be displayed for un-encodeable characters).
59 # May be overridden by the --output-encoding option!
60  
61 # https://rt.cpan.org/Public/Bug/Display.html?id=88592
62 3 $PerlIO::encoding::fallback |= Encode::ONLY_PRAGMA_WARNINGS;
63 3 ▶no warnings 'utf8';
Subroutine BEGIN: called CC=1 SCAR=0.0
64 3 ▶use open ':std', ':locale';
Subroutine BEGIN: called CC=1 SCAR=0.0
65  
66 3 select STDERR; $| = 1; select STDOUT; $| = 1;
67  
68 3 my $stdout_encoding;
69 0 ▶sub decode_foruser($) {
Subroutine decode_foruser: not called CC=2 SCAR=17.9
70 0 my $octets = shift;
71 0 ▶ $stdout_encoding
Branch: 1 branch
BranchTrueFalseTotal
$stdout_encoding ? : 0 0 0
72 ? decode($stdout_encoding, $octets, Encode::FB_DEFAULT|Encode::LEAVE_SRC)
73 : $octets
74 }
75 0 ▶sub encode_foruser($) {
Subroutine encode_foruser: not called CC=2 SCAR=17.9
76 0 my $chars = shift;
77 0 ▶ $stdout_encoding
Branch: 1 branch
BranchTrueFalseTotal
$stdout_encoding ? : 0 0 0
78 ? encode($stdout_encoding, $chars, Encode::FB_DEFAULT|Encode::LEAVE_SRC)
79 : $chars
80 }
81  
82 #-------- Get Arguments ---------------
83 2 ▶sub call_pod2usage {
Subroutine call_pod2usage: called CC=2 SCAR=7.1
84 2 ▶ confess "bug" if (scalar(@_) % 2) != 0;
Branch: 1 branch
BranchTrueFalseTotal
if scalar(@_) % 2 != 0 0 2 2
85 2 my %opts = @_;
86 # if (! $opts{-msg}) {
87 # if (my $podpath = pod_where({-inc => 1},"App::diff_spreadsheets")) {
88 # $opts{-input} = $podpath;
89 # } else {
90 # warn "Could not find App::diff_spreadsheets in \@INC\n",
91 # "\@INC:\n", join("\n ",@INC), "\n";
92 # }
93 # }
94 2 pod2usage(\%opts);
95 }
96  
97 1 ▶sub badargs_exit(@) {
Subroutine badargs_exit: called CC=1 SCAR=0.0
98 1 call_pod2usage(-output => \*STDERR, -exitval => 2, @_);
99 }
100  
101 # We could use Term::Encoding to detect the terminal's encoding
102 # but that would create a possibly-undesirable dependency.
103 # We just assume UTF-8, which these days is probably correct.
104 #my $rightarrow = '->';
105 #my $rightarrow = "\N{RIGHTWARDS ARROW}\N{THIN SPACE}";
106 #my $rightarrow = "\N{RIGHTWARDS ARROW}\N{NARROW NO-BREAK SPACE}";
107 #my $rightarrow = "\N{RIGHTWARDS ARROW}\N{HAIR SPACE}";
108  
109 3 ▶sub _get_terminal_width() { # returns undef if unknowable
Subroutine _get_terminal_width: called CC=9 SCAR=24.8
110 3 ▶ if (u($ENV{COLUMNS}) =~ /^[1-9]\d*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (&u($ENV{'COLUMNS'}) =~ /^[1-9]\d*$/u) { } 0 3 3
111 0 return $ENV{COLUMNS}; # overrides actual terminal width
112 } else {
113 3 local *_; # Try to avoid clobbering special filehandle "_"
114 # This does not actualy work; https://github.com/Perl/perl5/issues/19142
115  
116 my $fh =
117 -t STDOUT ? *STDOUT :
118 -t STDERR ? *STDERR :
119 # under Windows the filehandle must be an *output* handle
120 3 ▶ do{my $fh; for("/dev/tty",'CONOUT$') { last if open $fh, $_ } $fh}
Branch: 3 branches
BranchTrueFalseTotal
-t STDOUT ? : 0 3 3
-t STDERR ? : 0 3 3
if open $fh, $_ 0 6 6
Condition16%1/6
do { my $fh; foreach $_ ("/dev/tty", "CONOUT\$") { last if open $fh, $_; } ; $fh } or -t STDIN and *STDIN
l!l&&r!l&&!r
300
-t STDIN and *STDIN
!ll&&!rl&&r
000
MC/DC0%0/3
do { my $fh; foreach $_ ("/dev/tty", "CONOUT\$") { last if open $fh, $_; } ; $fh } or -t STDIN and *STDIN
do { my $fh; foreach $_ ("/dev/tty", "CONOUT\$") { last if open $fh, $_; } ; $fh } -t STDIN *STDIN
Truth table25%1/4
A or B and C
A= do { my $fh; foreach $_ ("/dev/tty", "CONOUT\$") { last if open $fh, $_; } ; $fh }B= -t STDINC= *STDIN
ABCresult
1XX1
0111
00X0
0100
121 || (-t STDIN && *STDIN)
122 ;
123 3 my ($width, $height);
124 3 ▶ if ($fh) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fh) 3 0 3
125 # Some platforms (bsd?) carp if the terminal size can not be determined.
126 # We don't want to see any such warnings. Also there might be a
127 # __WARN__ trap which we don't want to trigger
128 #
129 # Sigh. It never ends! On some platforms (different libc?)
130 # "stty" directly prints "stdin is not a tty" which we can not trap.
131 # Probably this is a bug in Term::Readkey where it should redirect
132 # such messages to /dev/null. So we have to do it here.
133 3 require Capture::Tiny;
134 3 ▶ () = Capture::Tiny::capture_merged(sub{
Subroutine __ANON__: called CC=1 SCAR=0.0
135 3 delete local $INC{__WARN__};
136 3 delete local $INC{__DIE__};
137 3 ($width, $height) = eval{ Term::ReadKey::GetTerminalSize($fh) };
138 3 });
139 }
140 3 return $width; # possibly undef (sometimes seems to be zero ?!?)
141 }
142 }
143  
144 # 0 or undef to not wrap long lines
145 3 ▶my $maxwidth = _get_terminal_width || 80;
Condition50%1/2
&_get_terminal_width() || 80
l!l
03
MC/DC0%0/1
&_get_terminal_width() || 80
&_get_terminal_width()
Truth table50%1/2
A
A= &_get_terminal_width()
Aresult
11
00
146  
147 3 my %opts = (
148 sort_rows => 1,
149 quote_char => '"',
150 sep_char => ',',
151 encoding => 'UTF-8',
152 trunc_title_width => max(20, int($maxwidth/3)),
153 );
154 3 my $method = "native";
155 3 my $help;
156  
157 0 ▶sub dashopt($) {
Subroutine dashopt: not called CC=2 SCAR=17.9
158 0 my $optname = shift;
159 0 ▶ length($optname) == 1 ? "-$optname" : "--$optname";
Branch: 1 branch
BranchTrueFalseTotal
length $optname == 1 ? : 0 0 0
160 }
161 0 ▶sub combined_optval($@) { # value is optional
Subroutine combined_optval: not called CC=3 SCAR=24.8
162 0 my ($nilval, $optname, $val) = @_;
163 0 ▶ if ($val eq $nilval) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val eq $nilval) { } 0 0 0
164 0 return dashopt($optname);
165 } else {
166 # e.g. -w20 or --width=20
167 0 ▶ return(length($optname) == 1 ? "-$optname${val}" : "--$optname=$val");
Branch: 1 branch
BranchTrueFalseTotal
length $optname == 1 ? : 0 0 0
168 }
169 }
170  
171 my @diff_opts;
172 0 ▶sub diffopt_noval { push @diff_opts, dashopt($_[0]); }
Subroutine diffopt_noval: not called CC=1 SCAR=6.9
173 0 ▶sub diffopt_withval { push @diff_opts, dashopt($_[0]), $_[1]; }
Subroutine diffopt_withval: not called CC=1 SCAR=6.9
174 0 ▶sub diffopt_comboptstr { push @diff_opts, combined_optval("",@_) }
Subroutine diffopt_comboptstr: not called CC=1 SCAR=6.9
175 0 ▶sub diffopt_comboptnum { push @diff_opts, combined_optval(0, @_) }
Subroutine diffopt_comboptnum: not called CC=1 SCAR=6.9
176  
177 ### MAIN ###
178  
179 3 ▶badargs_exit(-msg => "Usage: $Script file1 file2\n -h for help") if @ARGV==0;
Branch: 1 branch
BranchTrueFalseTotal
if @ARGV == 0 1 2 3
180 2 Getopt::Long::Configure ("default", "gnu_getopt", "auto_version");
181 GetOptions(
182 # diff options:
183 26 ▶ (map { ((/=/ ? $_ : "$_=s") => \&diffopt_withval) }
Branch: 1 branch
BranchTrueFalseTotal
/=/u ? : 4 22 26
184 qw/C|c|context=i D F GTYPE-group-format horizon-lines I ifdef
185 ignore-matching-lines label line-format LTYPE-group-format
186 show-function-line tabsize=i
187 /),
188 60 (map { ($_ => \&diffopt_noval) }
189 qw/a B b brief d E e ed expand-tabs i ignore-blank-lines ignore-case
190 ignore-space-change ignore-tab-expansion initial-tab l minimal n
191 p paginate q rcs report-identical-files s show-c-function
192 speed-large-files strip-trailing-cr T t text/),
193 # valid only for 'git diff':
194 2 ▶ (map { ((/[:=]/ ? $_ : "$_=s") => \&diffopt_comboptstr) }
Branch: 1 branch
BranchTrueFalseTotal
/[:=]/u ? : 0 2 2
195 qw/color/),
196 2 ▶ (map { ((/[:=]/ ? $_ : oops) => \&diffopt_comboptnum) }
Branch: 1 branch
BranchTrueFalseTotal
/[:=]/u ? : 2 0 2
197 qw/u|U|unified:i/),
198 6 ▶ (map { ((/=/ ? $_ : "$_=s") => \&diffopt_comboptstr) }
Branch: 1 branch
BranchTrueFalseTotal
/=/u ? : 0 6 6
199 qw/word-diff-regex word-diff ws-error-highlight/),
200 4 (map { ($_ => \&diffopt_noval) }
201 qw/w|ignore-all-space y|side-by-side/),
202 0 ▶ "W|width=i" => sub{ $maxwidth = $_[1]; diffopt_comboptnum(@_); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
203  
204 # our options:
205 0 ▶ "always-show-columns=s" => sub{ push @{$opts{always_show_columns}}, $_[1]},
Subroutine __ANON__: not called CC=1 SCAR=6.9
206 0 ▶ "columns=s" => sub{ push @{$opts{columns}}, $_[1] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
207 0 ▶ "debug" => sub{ $opts{debug} = $_[1] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
208 0 ▶ "encoding=s" => sub{ $opts{encoding} = $_[1] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
209 0 ▶ "first-data-row=i" => sub{ $opts{first_data_row} = $_[1] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
210 0 ▶ "hash-func=s" => sub{ $opts{hash_func} = $_[1] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
211 0 ▶ "hashid-func=s" => sub{ $opts{hashid_func} = $_[1] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
212 "h|help" => \$help,
213 0 ▶ "id-columns=s" => sub{ push @{$opts{id_columns}}, $_[1] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
214 0 ▶ "keep-temps!" => sub{ $opts{keep_temps} = $_[1] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
215 "m|method=s" => \$method,
216 0 ▶ "output-encoding=s" => sub{ $opts{output_encoding} = $_[1] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
217 0 ▶ "quiet|silent" => sub{ $opts{silent} = $_[1] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
218 0 ▶ "quote-char=s" => sub{ $opts{quote_char} = $_[1] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
219 0 ▶ "sep-char=s" => sub{ $opts{sep_char} = $_[1] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
220 0 ▶ "setup-code=s" => sub{ $opts{setup_code} = $_[1] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
221 0 ▶ "sheets=s" => sub{ push @{$opts{sheet_names}}, $_[1] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
222 0 ▶ "show-empties" => sub{ $opts{show_empties} = $_[1] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
223 0 ▶ "sort-rows!" => sub{ $opts{sort_rows} = $_[1] }, # allow --no-sort
Subroutine __ANON__: not called CC=1 SCAR=6.9
224 0 ▶ "suppress-common-lines" => sub{ $opts{suppress_common_lines} = 1; push @diff_opts, "-$_[0]"; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
225 0 ▶ "title-row=i" => sub{ $opts{title_row} = $_[1] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
226 0 ▶ "v|verbose" => sub{ $opts{verbose} = $_[1] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
227 0 ▶ "Z|ignore-trailing-space" => sub{ $opts{ign_trailing_spaces} = 1; push @diff_opts, "-$_[0]"; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
228 2 ▶) or badargs_exit(-msg => "$Script -h for help");
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions(map({/=/u ? $_ : "$_=s", \&main::diffopt_withval;} "C|c|context=i", "D", "F", "GTYPE-group-format", "horizon-lines", "I", "ifdef", "ignore-matching-lines", "label", "line-format", "LTYPE-group-format", "show-function-line", "tabsize=i"), map({$_, \&main::diffopt_noval;} "a", "B", "b", "brief", "d", "E", "e", "ed", "expand-tabs", "i", "ignore-blank-lines", "ignore-case", "ignore-space-change", "ignore-tab-expansion", "initial-tab", "l", "minimal", "n", "p", "paginate", "q", "rcs", "report-identical-files", "s", "show-c-function", "speed-large-files", "strip-trailing-cr", "T", "t", "text"), map({/[:=]/u ? $_ : "$_=s", \&main::diffopt_comboptstr;} "color"), map({/[:=]/u ? $_ : &oops(), \&main::diffopt_comboptnum;} "u|U|unified:i"), map({/=/u ? $_ : "$_=s", \&main::diffopt_comboptstr;} "word-diff-regex", "word-diff", "ws-error-highlight"), map({$_, \&main::diffopt_noval;} "w|ignore-all-space", "y|side-by-side"), "W|width=i", sub { $maxwidth = $_[1]; diffopt_comboptnum(@_); } , "always-show-columns=s", sub { push @{$opts{"always_show_columns"};}, $_[1]; } , "columns=s", sub { push @{$opts{"columns"};}, $_[1]; } , "debug", sub { $opts{"debug"} = $_[1]; } , "encoding=s", sub { $opts{"encoding"} = $_[1]; } , "first-data-row=i", sub { $opts{"first_data_row"} = $_[1]; } , "hash-func=s", sub { $opts{"hash_func"} = $_[1]; } , "hashid-func=s", sub { $opts{"hashid_func"} = $_[1]; } , "h|help", \$help, "id-columns=s", sub { push @{$opts{"id_columns"};}, $_[1]; } , "keep-temps!", sub { $opts{"keep_temps"} = $_[1]; } , "m|method=s", \$method, "output-encoding=s", sub { $opts{"output_encoding"} = $_[1]; } , "quiet|silent", sub { $opts{"silent"} = $_[1]; } , "quote-char=s", sub { $opts{"quote_char"} = $_[1]; } , "sep-char=s", sub { $opts{"sep_char"} = $_[1]; } , "setup-code=s", sub { $opts{"setup_code"} = $_[1]; } , "sheets=s", sub { push @{$opts{"sheet_names"};}, $_[1]; } , "show-empties", sub { $opts{"show_empties"} = $_[1]; } , "sort-rows!", sub { $opts{"sort_rows"} = $_[1]; } , "suppress-common-lines", sub { $opts{"suppress_common_lines"} = 1; push @diff_opts, "-$_[0]"; } , "title-row=i", sub { $opts{"title_row"} = $_[1]; } , "v|verbose", sub { $opts{"verbose"} = $_[1]; } , "Z|ignore-trailing-space", sub { $opts{"ign_trailing_spaces"} = 1; push @diff_opts, "-$_[0]"; } ) or &badargs_exit("-msg", "$main::Script -h for help") 2 0 2
229  
230 2 ▶call_pod2usage(-verbose => 2, -output => \*STDOUT) if $help;
Branch: 1 branch
BranchTrueFalseTotal
if $help 1 1 2
231  
232 0 foreach (@diff_opts) { s/^-(\w\w+)/--$1/ } # change -longopt to --longopt
233  
234 1 foreach my $key (qw/columns id_columns always_show_columns sheet_names/) {
235 4 ▶ next unless $opts{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{$key} 4 0 4
236 0 @{$opts{$key}} = map{ split/(?<!\\),/ } @{$opts{$key}}
237 }
238 1 ▶$opts{verbose} //= $opts{debug};
Condition33%1/3
$opts{"verbose"} //= $opts{"debug"}
l!l&&r!l&&!r
001
MC/DC0%0/2
$opts{"verbose"} //= $opts{"debug"}
$opts{"verbose"} $opts{"debug"}
Truth table33%1/3
A //= B
A= $opts{"verbose"}B= $opts{"debug"}
ABresult
1X1
011
000
239  
240 1 ▶if ($opts{output_encoding}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"output_encoding"}) 0 1 1
241 0 ▶ my $crlf = grep(/crlf/, PerlIO::get_layers(*STDOUT)) ? ":crlf" : "";
Branch: 1 branch
BranchTrueFalseTotal
grep(/crlf/u, &PerlIO::get_layers(\*STDOUT)) ? : 0 0 0
242 0 my $arg = ":raw${crlf}:encoding($opts{output_encoding})";
243 0 ▶ warn ">binmode STDOUT/ERR '$arg'\n" if $opts{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"debug"} 0 0 0
244 0 ▶ binmode(\*STDOUT, $arg) or die "binmode $arg : $!";
Branch: 1 branch
BranchTrueFalseTotal
binmode \*STDOUT, $arg or die "binmode $arg : $!" 0 0 0
245 0 ▶ binmode(\*STDERR, $arg) or die "binmode $arg : $!";
Branch: 1 branch
BranchTrueFalseTotal
binmode \*STDERR, $arg or die "binmode $arg : $!" 0 0 0
246 }
247 1 ▶($stdout_encoding) = map{ /encoding\((.+)\)/ ? ($1) : () }
Branch: 1 branch
BranchTrueFalseTotal
/encoding\((.+)\)/u ? : 1 4 5
248 PerlIO::get_layers(*STDOUT);
249  
250 1 ▶($visible_space, $RArrow) =
Branch: 1 branch
BranchTrueFalseTotal
$stdout_encoding =~ /utf/iu ? : 0 1 1
251 ($stdout_encoding =~ /utf/i)
252 3 ▶ ? ("\N{MIDDLE DOT}", "\N{RIGHTWARDS ARROW}\N{HAIR SPACE}")
Subroutine BEGIN: called CC=1 SCAR=0.0
253 : (".", "-->") ;
254  
255 1 our ($hash_func_code, $hashid_func_code, $setup_code_code);
256 1 foreach my $argname (qw(hashid-func hash-func setup-code)) {
257 3 (my $optskey = $argname) =~ s/-/_/g;
258 3 ▶ next unless defined $opts{$optskey};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts{$optskey} 3 0 3
259 0 my $source = "package Usercode;" . $opts{$optskey};
260 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
261 0 ${"${optskey}_code"} = eval $source;
262 0 ▶ die "Syntax error in Perl code for --$argname option:\n$@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
263 0 die "--$argname did not produce a sub ref (got ",u(${"${optskey}_code"}),"\n"
264 0 ▶ unless ref(${"${optskey}_code"}) eq "CODE";
Branch: 1 branch
BranchTrueFalseTotal
unless ref ${"${optskey}_code";} eq "CODE" 0 0 0
265 }
266 0 ▶$hashid_func_code //= sub{ Carp::cluck "bug" if grep{! defined} @{$_[0]}; join ",", @{$_[0]} };
Subroutine __ANON__: not called CC=2 SCAR=14.3
Branch: 1 branch
BranchTrueFalseTotal
if grep {not defined $_;} @{$_[0];} 0 0 0
Condition33%1/3
$main::hashid_func_code //= sub { Carp::cluck("bug") if grep {not defined $_;} @{$_[0];}; join ",", @{$_[0];}; }
l!l&&r!l&&!r
010
MC/DC0%0/2
$main::hashid_func_code //= sub { Carp::cluck("bug") if grep {not defined $_;} @{$_[0];}; join ",", @{$_[0];}; }
$main::hashid_func_code sub { Carp::cluck("bug") if grep {not defined $_;} @{$_[0];}; join ",", @{$_[0];}; }
Truth table33%1/3
A //= B
A= $main::hashid_func_codeB= sub { Carp::cluck("bug") if grep {not defined $_;} @{$_[0];}; join ",", @{$_[0];}; }
ABresult
1X1
011
000
267 0 ▶$hash_func_code //= sub{ Carp::cluck "bug" if grep{! defined} @{$_[0]}; join ",", @{$_[0]} };
Subroutine __ANON__: not called CC=2 SCAR=14.3
Branch: 1 branch
BranchTrueFalseTotal
if grep {not defined $_;} @{$_[0];} 0 0 0
Condition33%1/3
$main::hash_func_code //= sub { Carp::cluck("bug") if grep {not defined $_;} @{$_[0];}; join ",", @{$_[0];}; }
l!l&&r!l&&!r
010
MC/DC0%0/2
$main::hash_func_code //= sub { Carp::cluck("bug") if grep {not defined $_;} @{$_[0];}; join ",", @{$_[0];}; }
$main::hash_func_code sub { Carp::cluck("bug") if grep {not defined $_;} @{$_[0];}; join ",", @{$_[0];}; }
Truth table33%1/3
A //= B
A= $main::hash_func_codeB= sub { Carp::cluck("bug") if grep {not defined $_;} @{$_[0];}; join ",", @{$_[0];}; }
ABresult
1X1
011
000
268 1 ▶$setup_code_code //= sub{};
Subroutine __ANON__: not called CC=1 SCAR=1.2
Condition33%1/3
$main::setup_code_code //= sub { }
l!l&&r!l&&!r
010
MC/DC0%0/2
$main::setup_code_code //= sub { }
$main::setup_code_code sub { }
Truth table33%1/3
A //= B
A= $main::setup_code_codeB= sub { }
ABresult
1X1
011
000
269  
270 1 ▶badargs_exit(-msg => "Two files must be specified") if @ARGV != 2;
Branch: 1 branch
BranchTrueFalseTotal
if @ARGV != 2 0 1 1
271 1 $opts{origpath1} = $ARGV[0];
272 1 $opts{origpath2} = $ARGV[1];
273  
274 1 ▶if ($opts{keep_temps}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{'keep_temps'}) { } 0 1 1
275 0 my $dir = path( catfile(tmpdir(), "dstmp") ); # /tmp/dstmp
276 0 $dir->remove_tree;
277 0 ▶ mkdir $dir->canonpath or die "mkdir $dir : $!";
Branch: 1 branch
BranchTrueFalseTotal
mkdir $dir->canonpath or die "mkdir $dir : $!" 0 0 0
278 0 warn "> Created ",qsh($dir),"\n";
279 0 $opts{tempdir} = $dir;
280 } else {
281 1 $opts{tempdir} = Path::Tiny->tempdir("diffspread_XXXXX", CLEANUP=>1);
282 }
283  
284 # Extract the possibly-multiple "sheets" from each spreadsheet into
285 # separate .csv files and process the corresponding pairs.
286 # If a specific sheet was specified, then only that sheet will be extracted.
287 1 foreach my $N (1,2) {
288 2 my $origpath = $opts{"origpath$N"};
289 2 my $sheet_from_path = sheetname_from_spec($origpath);
290 2 ▶ if (defined $sheet_from_path) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sheet_from_path) 0 2 2
291 die "--sheets argument not allowed because filename specifies a sheet:\n",
292 " ", qsh($origpath), "\n"
293 0 ▶ if $opts{sheet_names};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"sheet_names"} 0 0 0
294 }
295 2 my $dir = $opts{tempdir}->child("--INFILE$N--")->mkdir;
296 my $h = convert_spreadsheet($origpath, cvt_to=>"csv",
297 allsheets => !$sheet_from_path,
298 outpath => $dir->canonpath,
299 silent => $opts{silent},
300 verbose => $opts{debug},
301 debug => $opts{debug},
302 2 );
303 2 $opts{cvt_from} = $h->{cvt_from};
304 2 ▶ if ($h->{cvt_from} =~ /csv/i && !openhandle($origpath)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h->{'cvt_from'} =~ /csv/iu and not &openhandle($origpath)) { } 2 0 2
Condition33%1/3
$h->{"cvt_from"} =~ /csv/iu and not &openhandle($origpath)
!ll&&!rl&&r
002
MC/DC0%0/2
$h->{"cvt_from"} =~ /csv/iu and not &openhandle($origpath)
$h->{"cvt_from"} =~ /csv/iu not &openhandle($origpath)
Truth table33%1/3
A and not(B)
A= $h->{"cvt_from"} =~ /csv/iuB= not &openhandle($origpath)
ABresult
0X0
100
111
305 # Input was already a CSV (possibly detected by peeking at actual content).
306 # The "converted" file has a bogus name; use the original path instead.
307 2 $opts{"csvpaths$N"} = [ $origpath ];
308 } else {
309 0 my @csvpaths = sort $dir->children;
310 0 if (0 and @csvpaths == 1) { ## FIXME WHY IS THIS DISABLED ???
311 ##Answer: Because we want to see the sheetname even if user did not specify it
312 # Only one sheet in this file: Rename the csv to just show the
313 # spreadsheet file name without a redundant [sheetname] suffix.
314 my $new_tmpfname = $dir->child(path($origpath)->basename.".csv")->canonpath;
315 rename $csvpaths[0], $new_tmpfname or die;
316 $opts{"csvpaths$N"} = [ $new_tmpfname ];
317 } else {
318 0 $opts{"csvpaths$N"} = \@csvpaths;
319 }
320 }
321 2 my %sheet2csvpath; # sheetname => csvpath
322 2 foreach (@{ $opts{"csvpaths$N"} }) {
323 2 my $sheetname = path($_)->basename(qr/\.csv/i);
324 2 ▶ die if exists $sheet2csvpath{$sheetname};
Branch: 1 branch
BranchTrueFalseTotal
if exists $sheet2csvpath{$sheetname} 0 2 2
325 2 $sheet2csvpath{$sheetname} = $_;
326 };
327 2 $opts{"sheet2csvpath$N"} = \%sheet2csvpath;
328 }
329  
330 1 ▶my $onlyone = @{ $opts{"csvpaths1"} } == 1 && @{ $opts{"csvpaths2"} } == 1;
Condition33%1/3
@{$opts{'csvpaths1'};} == 1 && @{$opts{'csvpaths2'};} == 1
!ll&&!rl&&r
001
MC/DC0%0/2
@{$opts{'csvpaths1'};} == 1 && @{$opts{'csvpaths2'};} == 1
@{$opts{'csvpaths1'};} == 1 @{$opts{'csvpaths2'};} == 1
Truth table33%1/3
A && B
A= @{$opts{'csvpaths1'};} == 1B= @{$opts{'csvpaths2'};} == 1
ABresult
0X0
100
111
331  
332 # If --sheets arg(s) were given, diff only pairs of so-named sheets;
333 #
334 # Otherwise if each file contains only one sheet then diff them regardless
335 # of their names;
336 #
337 # But with a multi-sheet file diff only pairs of same-named sheets
338 # and warn about any sheet names which exist only in one file.
339  
340 1 my $status = 0;
341 1 $opts{sheetname_str} = "";
342 1 my @pairs; # { n1 n2 label1 label2 sheetname_str }
343 1 ▶if (@{$opts{sheet_names}//[]} > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (@{$opts{'sheet_names'} // [];} > 0) { } 0 1 1
elsif ($onlyone) { } 1 0 1
Condition50%1/2
$opts{"sheet_names"} // []
l!l
01
MC/DC0%0/1
$opts{"sheet_names"} // []
$opts{"sheet_names"}
Truth table50%1/2
A
A= $opts{"sheet_names"}
Aresult
11
00
344 # Sheet names were specified; corresponding sheets might have different
345 # names if the user specified a regex.
346 0 foreach (@{$opts{sheet_names}}) {
347 0 my $spec = main::Differ::compile_if_regex($_);
348 0 ▶ if (ref($spec)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $spec) { } 0 0 0
349 0 my @names1 = grep{ /$spec/s } keys %{ $opts{sheet2csvpath1} };
350 0 ▶ die "$spec matches no sheet name in $opts{origpath1}\n"
Branch: 1 branch
BranchTrueFalseTotal
unless @names1 > 0 0 0 0
351 unless @names1 > 0;
352 0 ▶ die "'$spec' matches multiple sheet names in $opts{origpath1}\n"
Branch: 1 branch
BranchTrueFalseTotal
if @names1 > 1 0 0 0
353 if @names1 > 1;
354 0 my @names2 = grep{ /$spec/s } keys %{ $opts{sheet2csvpath2} };
355 0 ▶ die "$spec matches no sheet name in $opts{origpath2}\n"
Branch: 1 branch
BranchTrueFalseTotal
unless @names2 > 0 0 0 0
356 unless @names2 > 0;
357 0 ▶ die "'$spec' matches multiple sheet names in $opts{origpath2}\n"
Branch: 1 branch
BranchTrueFalseTotal
if @names2 > 1 0 0 0
358 if @names2 > 1;
359 0 ▶ push @pairs, { n1 => $names1[0], n2 => $names2[0],
Branch: 1 branch
BranchTrueFalseTotal
$names1[0] eq $names2[0] ? : 0 0 0
360 sheetname_str => $names1[0] eq $names2[0]
361 ? vis($names1[0])
362 : vis($names1[0])."/".vis($names2[0])
363 };
364 } else {
365 die "sheet '$spec' does not exist in $opts{origpath1}\n"
366 0 ▶ unless $opts{sheet2csvpath1}{$spec};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"sheet2csvpath1"}{$spec} 0 0 0
367 die "sheet '$spec' does not exist in $opts{origpath2}\n"
368 0 ▶ unless $opts{sheet2csvpath2}{$spec};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"sheet2csvpath2"}{$spec} 0 0 0
369 0 push @pairs, { n1 => $spec, n2 => $spec, sheetname_str => vis($spec) };
370 }
371 }
372 }
373 elsif ($onlyone) {
374 # User did not specity a sheet name, but there is only one in each file.
375 # Compare them regardless of their names.
376 1 ▶ my ($name1, $bug1) = keys %{ $opts{sheet2csvpath1} }; die "bug1" if $bug1;
Branch: 1 branch
BranchTrueFalseTotal
if $bug1 0 1 1
377 1 $opts{path1} = $opts{sheet2csvpath1}{$name1};
378 1 ▶ my ($name2, $bug2) = keys %{ $opts{sheet2csvpath2} }; die "bug2" if $bug2;
Branch: 1 branch
BranchTrueFalseTotal
if $bug2 0 1 1
379 # set the displayed names to just the spreadsheet paths sans sheetnames.
380 push @pairs, { n1 => $name1, n2 => $name2,
381 1 label1 => $opts{origpath1}, label2 => $opts{origpath2} };
382 }
383 else {
384 # User did not specity which sheets, but there are several
385 0 foreach my $name (sort keys %{ $opts{sheet2csvpath1} }) {
386 0 ▶ unless ($opts{sheet2csvpath2}{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts{"sheet2csvpath2"}{$name}) 0 0 0
387 0 say "*** sheet '$name' exists ONLY in ",qsh($opts{origpath1}),"\n";
388 0 $status = max($status, 2);
389 0 next;
390 }
391 0 push @pairs, { n1 => $name, n2 => $name, sheetname_str => vis($name) };
392 }
393 0 foreach my $name (sort keys %{ $opts{sheet2csvpath2} }) {
394 0 ▶ unless ($opts{sheet2csvpath1}{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts{"sheet2csvpath1"}{$name}) 0 0 0
395 0 say "*** sheet '$name' exists ONLY in ",qsh($opts{origpath2}),"\n";
396 0 $status = max($status, 2);
397 0 next;
398 }
399 }
400 }
401  
402 1 foreach my $h (@pairs) {
403 my ($name1, $name2, $label1, $label2, $sheetname_str)
404 1 = @$h{qw/n1 n2 label1 label2 sheetname_str/};
405 # If no labels provided, default to "/path/to/file.xls[sheetname]"
406 1 ▶ $label1 //= form_spec_with_sheetname($opts{origpath1}, $name1);
Condition33%1/3
$label1 //= &form_spec_with_sheetname($opts{"origpath1"}, $name1)
l!l&&r!l&&!r
100
MC/DC0%0/2
$label1 //= &form_spec_with_sheetname($opts{"origpath1"}, $name1)
$label1 &form_spec_with_sheetname($opts{"origpath1"}, $name1)
Truth table33%1/3
A //= B
A= $label1B= &form_spec_with_sheetname($opts{"origpath1"}, $name1)
ABresult
1X1
011
000
407 1 ▶ $label2 //= form_spec_with_sheetname($opts{origpath2}, $name2);
Condition33%1/3
$label2 //= &form_spec_with_sheetname($opts{"origpath2"}, $name2)
l!l&&r!l&&!r
100
MC/DC0%0/2
$label2 //= &form_spec_with_sheetname($opts{"origpath2"}, $name2)
$label2 &form_spec_with_sheetname($opts{"origpath2"}, $name2)
Truth table33%1/3
A //= B
A= $label2B= &form_spec_with_sheetname($opts{"origpath2"}, $name2)
ABresult
1X1
011
000
408  
409 1 ▶ $opts{path1} = $opts{sheet2csvpath1}{$name1} // die;
Condition50%1/2
$opts{"sheet2csvpath1"}{$name1} // die()
l!l
10
MC/DC0%0/1
$opts{"sheet2csvpath1"}{$name1} // die()
$opts{"sheet2csvpath1"}{$name1}
Truth table50%1/2
A
A= $opts{"sheet2csvpath1"}{$name1}
Aresult
11
00
410 1 ▶ $opts{path2} = $opts{sheet2csvpath2}{$name2} // die;
Condition50%1/2
$opts{"sheet2csvpath2"}{$name2} // die()
l!l
10
MC/DC0%0/1
$opts{"sheet2csvpath2"}{$name2} // die()
$opts{"sheet2csvpath2"}{$name2}
Truth table50%1/2
A
A= $opts{"sheet2csvpath2"}{$name2}
Aresult
11
00
411 1 $opts{label1} = $label1;
412 1 $opts{label2} = $label2;
413 1 ▶ $opts{sheetname_str} = $sheetname_str // "";
Condition50%1/2
$sheetname_str // ""
l!l
01
MC/DC0%0/1
$sheetname_str // ""
$sheetname_str
Truth table50%1/2
A
A= $sheetname_str
Aresult
11
00
414 1 $status = max($status,
415 &process_pair # *THE WORK HAPPENS HERE*
416 );
417 }
418  
419 1 exit $status;
420  
421 1 ▶sub process_pair {
Subroutine process_pair: called CC=20 SCAR=52.2
422 # N.B. %opts includes "origpathN" and "pathN" (the latter refers to a csv),
423 # "sheetnameN", and "labelN".
424 # "sheetname_str" is "" or "sheet-pair description" to print before diffs.
425 #
426 # ---- Read data into memory, delete ignored columns, etc. ----
427 1 my $obj = main::Differ->new( %opts );
428  
429 # Set data_source() to a unique human-readable representation of each
430 # sheet. This is the caller-specified $opts{labelN}, typically
431 # "path/to/file.xls[sheetname]" or just "path/to/file",
432 # omitting any common ancestor directory parts.
433 1 my $abbr1 = path($obj->{label1});
434 1 my $abbr2 = path($obj->{label2});
435 1 ▶ if ($abbr1->volume eq $abbr2->volume && $abbr1 ne $abbr2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($abbr1->volume eq $abbr2->volume and $abbr1 ne $abbr2) 0 1 1
Condition33%1/3
$abbr1->volume eq $abbr2->volume and $abbr1 ne $abbr2
!ll&&!rl&&r
010
MC/DC0%0/2
$abbr1->volume eq $abbr2->volume and $abbr1 ne $abbr2
$abbr1->volume eq $abbr2->volume $abbr1 ne $abbr2
Truth table33%1/3
A and B
A= $abbr1->volume eq $abbr2->volumeB= $abbr1 ne $abbr2
ABresult
0X0
100
111
436 # Elide common ancestors.
437 # N.B. ->stringify() gives paths using / even on windows
438 0 my @pieces1 = split /\//, $abbr1->absolute()->stringify;
439 0 my @pieces2 = split /\//, $abbr2->absolute()->stringify;
440 0 ▶ while ($pieces1[0] eq $pieces2[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pieces1[0] eq $pieces2[0]) 0 0 0
441 0 ▶ oops dviso 'label1 & label2 do not have unique RHSs!\n%$obj'
Branch: 1 branch
BranchTrueFalseTotal
if @pieces1 == 1 or @pieces2 == 1 0 0 0
Condition0%0/3
@pieces1 == 1 or @pieces2 == 1
l!l&&r!l&&!r
000
MC/DC0%0/2
@pieces1 == 1 or @pieces2 == 1
@pieces1 == 1 @pieces2 == 1
Truth table0%0/3
A or B
A= @pieces1 == 1B= @pieces2 == 1
ABresult
1X1
011
000
442 if @pieces1==1 or @pieces2==1;
443 0 shift @pieces1; shift @pieces2;
444 }
445 0 $abbr1 = join "/", @pieces1;
446 0 $abbr2 = join "/", @pieces2;
447 }
448 1 $obj->{sheet1}->data_source($abbr1);
449 1 $obj->{sheet2}->data_source($abbr2);
450  
451 # Preliminary raw comparison
452 PRECHECK: {
453 1 my $rows1 = $obj->{sheet1}->rows();
454 1 my $rows2 = $obj->{sheet2}->rows();
455 1 my $num_cols = $obj->{sheet1}->num_cols();
456 1 ▶ last PRECHECK unless $obj->{sheet2}->num_cols() == $num_cols
Branch: 1 branch
BranchTrueFalseTotal
unless $obj->{"sheet2"}->num_cols == $num_cols and @$rows1 == @$rows2 0 1 1
Condition33%1/3
$obj->{"sheet2"}->num_cols == $num_cols and @$rows1 == @$rows2
!ll&&!rl&&r
001
MC/DC0%0/2
$obj->{"sheet2"}->num_cols == $num_cols and @$rows1 == @$rows2
$obj->{"sheet2"}->num_cols == $num_cols @$rows1 == @$rows2
Truth table33%1/3
A and B
A= $obj->{"sheet2"}->num_cols == $num_colsB= @$rows1 == @$rows2
ABresult
0X0
100
111
457 && @$rows1 == @$rows2;
458 1 for my $rx (0..$#$rows1) {
459 0 my $r1 = $rows1->[$rx];
460 0 my $r2 = $rows2->[$rx];
461 0 ▶ last PRECHECK if defined first {$r1->[$_] ne $r2->[$_]} 0..($num_cols-1);
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if defined &first(sub { $r1->[$_] ne $r2->[$_]; } , 0 .. $num_cols - 1) 0 0 0
462 }
463 warn "> Paired sheets contain identical data, skipping fancier algos\n"
464 1 ▶ if $opts{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"debug"} 0 1 1
465 1 ▶warn dvis '## $rows1\n $rows2\n' if $opts{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"debug"} 0 1 1
466 1 return 0
467 }
468  
469 0 ▶ if ($method ne "native") {
Branch: 1 branch
BranchTrueFalseTotal
if ($method ne "native") 0 0 0
470 0 foreach my $key (qw/id_columns always_show_columns hashid_func hash_func/) {
471 0 ▶ if ($opts{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{$key}) 0 0 0
472 0 (my $optname = $key) =~ s/_/-/g;
473 0 die "--${optname} option applies only to the 'native' method\n"
474 }
475 }
476 }
477 0 ▶ if ($method eq "diff") {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq "diff") 0 0 0
478 0 $obj->compare_using_diff(['diff', '-u', @diff_opts]);
479 0 return $obj->exit_status;
480 }
481 0 ▶ if ($method eq "tkdiff") {
Branch: 4 branches
BranchTrueFalseTotal
if ($method eq 'tkdiff') { } 0 0 0
elsif ($method eq 'git') { } 0 0 0
elsif ($method eq 'gitchars') { } 0 0 0
elsif ($method eq 'native') { } 0 0 0
482 0 $obj->compare_using_diff([$method, @diff_opts]);
483 0 return $obj->exit_status;
484 }
485 elsif ($method eq "git") {
486 0 $obj->compare_using_diff([qw/git diff --no-index --color-words/, @diff_opts]);
487 0 return $obj->exit_status;
488 }
489 elsif ($method eq "gitchars") {
490 0 $obj->compare_using_diff([qw/git diff --no-index --word-diff=color --word-diff-regex=./, @diff_opts]);
491 0 return $obj->exit_status;
492 }
493 elsif ($method eq "native") {
494 0 $obj->compare_native();
495 0 return $obj->exit_status;
496 }
497 else {
498 0 die "Don't know comparison method '$method'\n";
499 }
500 }
501  
502  
503 ########################################
504 #
505 #
506 #
507 ########################################
508 package main::Differ;
509  
510 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
511 0 ▶sub oops(@) { unshift @_, "oops "; require Carp; goto &Carp::confess; }
Subroutine oops: not called CC=1 SCAR=6.9
512 3 ▶use Path::Tiny 0.144;
Subroutine BEGIN: called CC=1 SCAR=0.0
513 #use File::Temp qw(tempfile tempdir);
514 #use File::Basename qw(basename dirname fileparse);
515 #use File::Path qw(make_path remove_tree);
516 #use File::Spec::Functions qw(canonpath catfile catdir rootdir tmpdir);
517 3 ▶use List::Util qw(first any min max any sum0);
Subroutine BEGIN: called CC=1 SCAR=0.0
518 3 ▶use List::Util qw(min max any first);
Subroutine BEGIN: called CC=1 SCAR=0.0
519 3 ▶use Guard qw(guard scope_guard);
Subroutine BEGIN: called CC=1 SCAR=0.0
520 3 ▶use Spreadsheet::Edit 1000.006 qw(:DEFAULT logmsg cx2let let2cx);
Subroutine BEGIN: called CC=1 SCAR=0.0
521 3 ▶use Data::Dumper::Interp 6.007;
Subroutine BEGIN: called CC=1 SCAR=0.0
522 3 ▶use Spreadsheet::Edit::Log ':btw';
Subroutine BEGIN: called CC=1 SCAR=0.0
523  
524 sub _visualize($);
525 sub _title_or_origABC($$);
526  
527 0 my $seen = {};
528 0 ▶sub warnonce(@) {
Subroutine warnonce: not called CC=2 SCAR=17.9
529 0 my $msg = join "",@_;
530 0 ▶ return if $seen->{$msg}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen->{$msg}++ 0 0 0
531 0 warn $msg;
532 }
533  
534 0 ▶sub cx2origcx($$) {
Subroutine cx2origcx: not called CC=4 SCAR=30.0
535 0 my ($sheet, $currcx) = @_;
536 # 0 1 2 3 [4] [5] 6 7 [8] 9 10 original
537 # 0 1 2 3 4 5 6 7 after deletes
538 0 ▶ my $deleted_cxs = $sheet->attributes->{DELETED_CXS} // confess("bug");
Condition0%0/2
$sheet->attributes->{"DELETED_CXS"} // confess("bug")
l!l
00
MC/DC0%0/1
$sheet->attributes->{"DELETED_CXS"} // confess("bug")
$sheet->attributes->{"DELETED_CXS"}
Truth table0%0/2
A
A= $sheet->attributes->{"DELETED_CXS"}
Aresult
11
00
539 0 my $nskipped = 0;
540 0 my $oldcx = $currcx;
541 0 for my $dcx (@$deleted_cxs) {
542 0 ▶ return($currcx + $nskipped) if $dcx >= $oldcx;
Branch: 1 branch
BranchTrueFalseTotal
if $dcx >= $oldcx 0 0 0
543 0 $oldcx = $dcx;
544 0 ++$nskipped;
545 }
546 0 return($currcx + $nskipped);
547 }
548 0 ▶sub cx2origlet($$) { cx2let(&cx2origcx) }
Subroutine cx2origlet: not called CC=1 SCAR=6.9
549  
550 2 ▶sub compile_if_regex(@) { # compile "/.../msix" strings to qr/.../msix
Subroutine compile_if_regex: called CC=6 SCAR=31.0
551 2 my @specs = @_;
552 2 foreach (@specs) {
553 0 ▶ if (m#^(/.*/[a-z]*)\z|^m([/\[\{\(\<].*)\z#s) {
Branch: 1 branch
BranchTrueFalseTotal
if (m"^(/.*/[a-z]*)\z|^m([/\[\{\(\<].*)\z"su) 0 0 0
554 0 ▶ my $regex = eval "qr${1}" // do{
Condition0%0/3
eval "qr$1" // do { $@ =~ s/ at \(eval.*//gmu; die "$@ in $_\n" }
l!l&&r!l&&!r
000
MC/DC0%0/2
eval "qr$1" // do { $@ =~ s/ at \(eval.*//gmu; die "$@ in $_\n" }
eval "qr$1" do { $@ =~ s/ at \(eval.*//gmu; die "$@ in $_\n" }
Truth table0%0/3
A // B
A= eval "qr$1"B= do { $@ =~ s/ at \(eval.*//gmu; die "$@ in $_\n" }
ABresult
1X1
011
000
555 0 $@ =~ s/ at \(eval.*//mg;
556 0 die "$@ in $_\n";
557 };
558 0 $_ = $regex
559 }
560 }
561 2 ▶ wantarray ? @specs :
Branch: 2 branches
BranchTrueFalseTotal
wantarray ? : 2 0 2
@specs > 1 ? : 0 0 0
562 @specs > 1 ? confess("multiple results") :
563 $specs[0]
564 }
565  
566 0 ▶sub __truncate($$) {
Subroutine __truncate: not called CC=4 SCAR=30.0
567 0 my ($aref, $maxwid) = @_;
568 0 my $changed;
569 0 foreach (@$aref) {
570 0 ▶ if (length($_) > $maxwid) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $_ > $maxwid) 0 0 0
571 0 $changed++;
572 # cut before the first actual newline or after visualized newline
573 0 s/\A.+?(?:\\n|(?=\n))\K.*/.../s;
574 0 ▶ if (length($_) > $maxwid) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $_ > $maxwid) 0 0 0
575 0 my $numdots = max(3, length($_)-$maxwid);
576 0 substr($_, $maxwid - $numdots) = ("." x $numdots);
577 }
578 #warn dvis '##TT __truncated $_ (len=',length($_),")\n";
579 }
580 }
581 $changed
582 0 }
583  
584 0 ▶sub __visualize($) {
Subroutine __visualize: not called CC=3 SCAR=24.8
585 0 local $_ = shift;
586 0 s/\n/\\n/sg;
587 0 s/\t/\\t/g;
588 0 s/([^[:print:]])/ sprintf "\\x{%02x}", ord($1) /eg;
589 0 ▶ if ($opts{ign_leading_spaces}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{'ign_leading_spaces'}) { } 0 0 0
590 0 s/^( +)//;
591 } else {
592 # make leading spaces visible
593 0 s/^( +)/$visible_space x length($1)/e;
594 }
595 0 ▶ if ($opts{ign_trailing_spaces}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{'ign_trailing_spaces'}) { } 0 0 0
596 0 s/( +)$//;
597 } else {
598 # make trailing spaces visible
599 0 s/( +)$/$visible_space x length($1)/e;
600 }
601 0 $_
602 }
603  
604 # Read the two files (only one sheet each) into memory and delete
605 # columns to be ignored. In each Spreadsheet::Edit sheet :
606 #
607 # To find corresponding columns after deleting ignored columns, even if
608 # the user specified absolute column letters:
609 #
610 # For each in {always_show_columns} and {id_columns} :
611 # alias <uniqueident> to the column and set
612 # $sheet->attributes->{TRACKING}{ spec => [list of <uniqueident>] }
613 # (each 'spec' might be a regex matching multiple values).
614 #
615 # Set attributes->{DELETED_CXS} => [ original cxs ]
616 #
617 1 ▶sub new {
Subroutine new: called CC=38 SCAR=61.1
618 1 my $class = shift;
619 1 my %hash = @_;
620 1 ▶ warn __PACKAGE__,"->new",hvis(%hash),"\n" if $hash{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $hash{"debug"} 0 1 1
621  
622 1 ▶ $hash{always_show_columns} //= $hash{id_columns};
Condition33%1/3
$hash{"always_show_columns"} //= $hash{"id_columns"}
l!l&&r!l&&!r
001
MC/DC0%0/2
$hash{"always_show_columns"} //= $hash{"id_columns"}
$hash{"always_show_columns"} $hash{"id_columns"}
Truth table33%1/3
A //= B
A= $hash{"always_show_columns"}B= $hash{"id_columns"}
ABresult
1X1
011
000
623  
624 1 ▶ my @columns = @{ $hash{columns} // [] }; # copy; will be mutated
Condition50%1/2
$hash{"columns"} // []
l!l
01
MC/DC0%0/1
$hash{"columns"} // []
$hash{"columns"}
Truth table50%1/2
A
A= $hash{"columns"}
Aresult
11
00
625 1 ▶ my $always_show_columns = $hash{always_show_columns} // [];
Condition50%1/2
$hash{"always_show_columns"} // []
l!l
01
MC/DC0%0/1
$hash{"always_show_columns"} // []
$hash{"always_show_columns"}
Truth table50%1/2
A
A= $hash{"always_show_columns"}
Aresult
11
00
626 1 ▶ my $id_columns = $hash{id_columns} // [];
Condition50%1/2
$hash{"id_columns"} // []
l!l
01
MC/DC0%0/1
$hash{"id_columns"} // []
$hash{"id_columns"}
Truth table50%1/2
A
A= $hash{"id_columns"}
Aresult
11
00
627 1 my $negated;
628 1 ▶ if (@columns) {
Branch: 1 branch
BranchTrueFalseTotal
if (@columns) 0 1 1
629 0 $negated = $columns[0] =~ s/^-//;
630 0 foreach (@columns[1..$#columns]) {
631 0 ▶ die "You can not mix negated and non-negated --columns COLSPECS (",
Branch: 1 branch
BranchTrueFalseTotal
unless s/^-//u == $negated 0 0 0
632 vis("$columns[0]")," and ",vis($_),")\n"
633 unless s/^-// == $negated;
634 }
635 }
636 ##my @sheet_new_opts = ($hash{debug} ? (verbose => 1) : (silent => 1)); # no alias warns
637 ##my @sheet_new_opts = (silent => 1); # no alias warns
638 ##my @sheet_new_opts = (silent => 1); # no alias warns
639 1 for my $N (1, 2) {
640 # Read into memory
641 2 ▶ my $path = $hash{"path$N"} // confess "'path$N' option is required";
Condition50%1/2
$hash{"path$N"} // confess("'path${N}' option is required")
l!l
20
MC/DC0%0/1
$hash{"path$N"} // confess("'path${N}' option is required")
$hash{"path$N"}
Truth table50%1/2
A
A= $hash{"path$N"}
Aresult
11
00
642 ##my $sh = Spreadsheet::Edit->new(@sheet_new_opts);
643 2 my $sh = Spreadsheet::Edit->new();
644 $sh->options(silent => $hash{silent},
645 verbose => $hash{debug},
646 2 debug => $hash{debug});
647  
648 2 my $title_row_spec = $hash{title_row}; # TODO: Allow separate specification
649  
650 $sh->read_spreadsheet({
651 use_gnumeric => 1,
652 quote_char => $hash{quote_char},
653 sep_char => $hash{sep_char},
654 encoding => $hash{encoding},
655 2 data_source => $hash{"label$N"}, # "path.xls[sheet]"
656 title_rx => undef, # don't detect titles yet
657 }, $path
658 );
659  
660 2 ▶ if (defined($title_row_spec) && $title_row_spec == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $title_row_spec and $title_row_spec == 0) { } 0 2 2
elsif (defined $title_row_spec) { } 0 2 2
Condition33%1/3
defined $title_row_spec and $title_row_spec == 0
!ll&&!rl&&r
200
MC/DC0%0/2
defined $title_row_spec and $title_row_spec == 0
defined $title_row_spec $title_row_spec == 0
Truth table33%1/3
A and B
A= defined $title_row_specB= $title_row_spec == 0
ABresult
0X0
100
111
661 # User explicitly specified no titles
662 }
663 elsif (defined $title_row_spec) {
664 # A title row was specified; use it and allow croaking on error
665 0 $sh->title_rx($title_row_spec-1); # 0-based row index
666 } else {
667 # Try to auto-detect a title row, requiring any specified columns,
668 # but assume there are no titles if an error occurred (which allows
669 # identical sheets to compare successfully, and otherwise diffs
670 # will be shown using absolute column letters only).
671 2 eval {
672 2 $sh->title_rx( { required => [compile_if_regex
673 @$always_show_columns,@$id_columns,@columns],
674 silent => 1, # don't show conflicts with letter codes
675 },
676 "auto"
677 );
678 };
679 2 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 2 2
680 0 ▶ oops "error but left title_rx defined!\n$@" if defined $sh->title_rx();
Branch: 1 branch
BranchTrueFalseTotal
if defined $sh->title_rx 0 0 0
681 0 ▶ oops $@ if $@ !~ /failed because/; # internal error?
Branch: 1 branch
BranchTrueFalseTotal
unless $@ =~ /failed because/u 0 0 0
682 }
683 }
684  
685 2 $hash{"sheet$N"} = $sh;
686 2 $sh->attributes->{ORIG_PATH} = $path;
687 }
688  
689 1 my $ncols = 0;
690 1 for my $N (1,2) {
691 2 my $sh = $hash{"sheet$N"};
692  
693 2 $ncols = max($ncols, $sh->num_cols);
694  
695 # Create aliases for columns which might move when non-compared
696 # columns are deleted later. The aliases will automatically track,
697 # relevant when the user specified absolute column letters.
698 # Each <spec> might be a regex which matches multiple columns!
699 2 my %cx2seq;
700 2 foreach my $spec (@$always_show_columns, @$id_columns,
701 @columns #unnecessary???
702 ) {
703 0 my @cxlist = $sh->spectocx(compile_if_regex($spec));
704  
705 0 ▶ if (defined(my $prev_aliases = $sh->attributes->{TRACKING}->{ $spec })) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $prev_aliases = $sh->attributes->{'TRACKING'}{$spec})) { } 0 0 0
706 # This same spec was seen before (perhaps in both always-show & id),
707 # and we already processed the cx(s) the spec specifies, i.e. @cxlist.
708  
709 0 my %seen_cx = map{($_ => 1)} @cxlist; # Sanity check...
710 0 for (@$prev_aliases) {
711 0 ▶ oops unless defined delete $seen_cx{ $sh->colx->{$_} };
Branch: 1 branch
BranchTrueFalseTotal
unless defined delete($seen_cx{$sh->colx->{$_}}) 0 0 0
712 }
713 0 ▶ oops if keys %seen_cx;
Branch: 1 branch
BranchTrueFalseTotal
if keys %seen_cx 0 0 0
714 } else {
715 my @aliaslist =
716 0 map { "__origcx${_}_".$cx2seq{$_}++."_".title2ident($spec) }
717 @cxlist;
718 0 $sh->attributes->{TRACKING}->{ $spec } = \@aliaslist;
719 0 for (0..$#aliaslist) {
720 0 $sh->alias($aliaslist[$_] => $cxlist[$_]);
721 0 ▶ oops unless $sh->colx->{$aliaslist[$_]} == $cxlist[$_];
Branch: 1 branch
BranchTrueFalseTotal
unless $sh->colx->{$aliaslist[$_]} == $cxlist[$_] 0 0 0
722 }
723 }
724 }
725  
726 # Pre-sort the files
727 2 ▶ if (@$id_columns && $hash{sort_rows}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$id_columns and $hash{"sort_rows"}) 0 2 2
Condition33%1/3
@$id_columns and $hash{"sort_rows"}
!ll&&!rl&&r
200
MC/DC0%0/2
@$id_columns and $hash{"sort_rows"}
@$id_columns $hash{"sort_rows"}
Truth table33%1/3
A and B
A= @$id_columnsB= $hash{"sort_rows"}
ABresult
0X0
100
111
728  
729 0 my $title_rx = $sh->title_rx;
730 $sh->first_data_rx(defined($hash{first_data_row})
731 0 ▶ ? ($hash{first_data_row}-1)
Branch: 1 branch
BranchTrueFalseTotal
defined $hash{'first_data_row'} ? : 0 0 0
Condition0%0/2
$title_rx // -1
l!l
00
MC/DC0%0/1
$title_rx // -1
$title_rx
Truth table0%0/2
A
A= $title_rx
Aresult
11
00
732 : (($title_rx // -1) + 1));
733  
734 # FIXME? Can the "Translate --always-show and --id-columns to cx..."
735 # be hoisted up here from compare_native()? Maybe not because
736 # that has to be done after deleting ignored columns (otoh maybe we can
737 # sort after deleting?) For now, repeating code from there...
738 my @key_cxs = map{
739 0 ▶ map{ $sh->colx->{$_} // oops }
Condition0%0/3
$sh->colx->{$_} // &oops()
l!l&&r!l&&!r
000
MC/DC0%0/2
$sh->colx->{$_} // &oops()
$sh->colx->{$_} &oops()
Truth table0%0/3
A // B
A= $sh->colx->{$_}B= &oops()
ABresult
1X1
011
000
740 0 ▶ @{ $sh->attributes->{TRACKING}->{ $_ } // oops }
Condition0%0/3
$sh->attributes->{"TRACKING"}{$_} // &oops()
l!l&&r!l&&!r
000
MC/DC0%0/2
$sh->attributes->{"TRACKING"}{$_} // &oops()
$sh->attributes->{"TRACKING"}{$_} &oops()
Truth table0%0/3
A // B
A= $sh->attributes->{"TRACKING"}{$_}B= &oops()
ABresult
1X1
011
000
741 }
742 0 @{$hash{id_columns}} ;
743  
744 $sh->attributes->{ORIGINAL_RXS} = [
745 0 ▶ $sh->sort_rows(sub{
Subroutine __ANON__: not called CC=3 SCAR=24.8
746 0 foreach my $cx (@key_cxs) {
747 0 my $r = $a->[$cx] cmp $b->[$cx];
748 0 ▶ return $r if $r;
Branch: 1 branch
BranchTrueFalseTotal
if $r 0 0 0
749 }
750 0
751 0 })
752 0 ];
753 }
754  
755 # Remove columns not being compared
756 2 ▶ if (@columns) {
Branch: 1 branch
BranchTrueFalseTotal
if (@columns) { } 0 2 2
757 0 my @cxlist = $sh->spectocx(compile_if_regex(@columns));
758 0 my %delete_cxs;
759 0 ▶ if ($negated) {
Branch: 1 branch
BranchTrueFalseTotal
if ($negated) { } 0 0 0
760 0 %delete_cxs = map{ $_ => 1 } @cxlist;
761 } else {
762 0 %delete_cxs = map{ $_ => 1 } 0..$sh->num_cols-1;
763 0 delete @delete_cxs{ @cxlist };
764 }
765 0 my @deleted_cxs = sort {$a <=> $b} keys %delete_cxs;
766 warn "negated=",vis($negated),
767 " delete cxs:",avis(@deleted_cxs),
768 0 " abcs:",avis(map{cx2let} @deleted_cxs),"\n"
769 0 ▶ if $hash{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $hash{"debug"} 0 0 0
770 0 $sh->delete_cols(@deleted_cxs);
771 0 $sh->attributes->{DELETED_CXS} = \@deleted_cxs;
772 } else {
773 2 $sh->attributes->{DELETED_CXS} = [];
774 }
775  
776 # Change newline etc. and non-printing to visible escapes
777 # (except in titles)
778 #$sh->apply_all(sub{
779 0 ▶ $sh->apply(sub{
Subroutine __ANON__: not called CC=2 SCAR=17.9
780 0 foreach (@{ $sh->crow }) {
781 0 $_ = __visualize($_);
782 }
783 2 });
784  
785 # Truncate overly-long titles
786 2 ▶ if (defined(my $title_row = $sh->title_row)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $title_row = $sh->title_row)) 0 2 2
787 0 ▶ if (__truncate($title_row, $hash{trunc_title_width})) {
Branch: 1 branch
BranchTrueFalseTotal
if (&__truncate($title_row, $hash{"trunc_title_width"})) 0 0 0
788 # Is this necessary (or wise) ???
789 0 $sh->title_rx({silent=>1}, # don't show column-alias warnings
790 $sh->title_rx); # re-compute %colx
791 }
792 }
793 }
794  
795 # Append empty columns if needed to make both sheets the same width
796 1 for my $N (1,2) {
797 2 my $sh = $hash{"sheet$N"};
798 0 ▶ while ($sh->num_cols < $ncols) { $sh->insert_col('>$', "") }
Branch: 1 branch
BranchTrueFalseTotal
if ($sh->num_cols < $ncols) 0 2 2
799 }
800 1 bless \%hash, $class;
801 }
802  
803 0 ▶sub _getncols($$) {
Subroutine _getncols: not called CC=3 SCAR=24.8
804 0 my ($sh, $rx) = @_;
805 0 ▶ $rx >= ($sh->title_rx//0) ? $sh->attributes->{NUMCOLS_USED} : $sh->num_cols
Branch: 1 branch
BranchTrueFalseTotal
$rx >= ($sh->title_rx // 0) ? : 0 0 0
Condition0%0/2
$sh->title_rx // 0
l!l
00
MC/DC0%0/1
$sh->title_rx // 0
$sh->title_rx
Truth table0%0/2
A
A= $sh->title_rx
Aresult
11
00
806 }
807  
808 0 ▶sub _title_or_origABC($$) {
Subroutine _title_or_origABC: not called CC=3 SCAR=24.8
809 0 my ($sh, $cx) = @_;
810 0 my $titlerow = $sh->title_row;
811 0 ▶ (defined($titlerow) && $titlerow->[$cx] ne "")
Branch: 1 branch
BranchTrueFalseTotal
defined $titlerow && $titlerow->[$cx] ne '' ? : 0 0 0
Condition0%0/3
defined $titlerow and $titlerow->[$cx] ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $titlerow and $titlerow->[$cx] ne ""
defined $titlerow $titlerow->[$cx] ne ""
Truth table0%0/3
A and B
A= defined $titlerowB= $titlerow->[$cx] ne ""
ABresult
0X0
100
111
812 ? $titlerow->[$cx]
813 : cx2origlet($sh,$cx)
814 }
815  
816 0 ▶sub _native_output {
Subroutine _native_output: not called CC=21 SCAR=61.4
817 0 ▶ my ($self, $cxlist1, $cxlist2, $diff, $dumbrun) = @_; oops unless @_==5;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 5 0 0 0
818 0 my $restricted_keycols = @{$self->{id_columns}} > 0;
819 0 my $changed;
820 0 $diff->Reset();
821 0 ▶ while( $diff->Next() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($diff->Next) 0 0 0
822 0 my @items1 = $diff->Items(1);
823 0 my @items2 = $diff->Items(2);
824 0 ▶ if ($diff->Same() && $restricted_keycols) {
Branch: 1 branch
BranchTrueFalseTotal
if ($diff->Same and $restricted_keycols) 0 0 0
Condition0%0/3
$diff->Same and $restricted_keycols
!ll&&!rl&&r
000
MC/DC0%0/2
$diff->Same and $restricted_keycols
$diff->Same $restricted_keycols
Truth table0%0/3
A and B
A= $diff->SameB= $restricted_keycols
ABresult
0X0
100
111
825 # Diff looked only at @id_columns and found no changes; there
826 # may be other changes, which we manually detect here.
827 0 ▶ die "bug" unless @items1 == @items2;
Branch: 1 branch
BranchTrueFalseTotal
unless @items1 == @items2 0 0 0
828 0 for my $i (0 .. $#items1) {
829 # Each item is [sheet#, rowref, original_rx]
830 0 my ($N1, $row1, $rx1) = @{$items1[$i]};
831 0 my ($N2, $row2, $rx2) = @{$items2[$i]};
832 0 my $hash1 = $hash_func_code->([@{$row1}[ @$cxlist1 ]], $rx1)
833 0 ▶ // die "ERROR: hash-func returned undef for row ".($rx1+1)." in ".$self->{"sheet$N1"}->data_source()."\n";
Condition0%0/2
&$main::hash_func_code([@{$row1;}[@$cxlist1]], $rx1) // die('ERROR: hash-func returned undef for row ' . ($rx1 + 1) . ' in ' . $$self{"sheet$N1"}->data_source . "\n")
l!l
00
MC/DC0%0/1
&$main::hash_func_code([@{$row1;}[@$cxlist1]], $rx1) // die('ERROR: hash-func returned undef for row ' . ($rx1 + 1) . ' in ' . $$self{"sheet$N1"}->data_source . "\n")
&$main::hash_func_code([@{$row1;}[@$cxlist1]], $rx1)
Truth table0%0/2
A
A= &$main::hash_func_code([@{$row1;}[@$cxlist1]], $rx1)
Aresult
11
00
834 0 my $hash2 = $hash_func_code->([@{$row2}[ @$cxlist2 ]], $rx2)
835 0 ▶ // die "ERROR: hash-func returned undef for row ".($rx2+1)." in ".$self->{"sheet$N2"}->data_source()."\n";
Condition0%0/2
&$main::hash_func_code([@{$row2;}[@$cxlist2]], $rx2) // die('ERROR: hash-func returned undef for row ' . ($rx2 + 1) . ' in ' . $$self{"sheet$N2"}->data_source . "\n")
l!l
00
MC/DC0%0/1
&$main::hash_func_code([@{$row2;}[@$cxlist2]], $rx2) // die('ERROR: hash-func returned undef for row ' . ($rx2 + 1) . ' in ' . $$self{"sheet$N2"}->data_source . "\n")
&$main::hash_func_code([@{$row2;}[@$cxlist2]], $rx2)
Truth table0%0/2
A
A= &$main::hash_func_code([@{$row2;}[@$cxlist2]], $rx2)
Aresult
11
00
836 0 ▶ if ($hash1 ne $hash2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hash1 ne $hash2) { } 0 0 0
837 0 $self->diff_rows($rx1, $rx2, "CHANGED", $dumbrun);
838 0 $changed = 1;
839 } else {
840 #print "NOT-CHANGED: ", verbrow_str($rx1,$rx2), "\n"
841 # if $debug && !$dumbrun;
842 }
843 }
844 }
845 next
846 0 ▶ if $diff->Same();
Branch: 1 branch
BranchTrueFalseTotal
if $diff->Same 0 0 0
847 0 $changed = 1;
848  
849 0 ▶ if (@items2 == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (@items2 == 0) { } 0 0 0
elsif (@items1 == 0) { } 0 0 0
850 # A hunk of rows deleted from self->{sheet2}
851 0 for my $i (0 .. $#items1) {
852 0 my ($N, $row, $orig_rx) = @{ $items1[$i] };
853 0 $self->show_row($N, $row, $orig_rx, "DELETED", $dumbrun);
854 }
855 }
856 elsif (@items1 == 0) {
857 # A hunk of rows added to self->{sheet2}
858 0 for my $i (0 .. $#items2) {
859 0 my ($N, $row, $orig_rx) = @{ $items2[$i] };
860 0 $self->show_row($N, $row, $orig_rx, "ADDED", $dumbrun);
861 }
862 }
863 else {
864 # A range replaced
865 0 my $lim = min($#items1, $#items2);
866 0 for my $i (0 .. $lim) {
867 0 ▶ if ($restricted_keycols) {
Branch: 1 branch
BranchTrueFalseTotal
if ($restricted_keycols) { } 0 0 0
868 # Did not match in ID columns, so they must be unrelated
869 # records; show as deletes and adds.
870 0 my ($N1, $row1, $rx1) = @{$items1[$i]};
871 0 my ($N2, $row2, $rx2) = @{$items2[$i]};
872 #??? When is it not the case that $N1==1 and $N2==2 ???
873 0 $self->show_row($N1, $row1, $rx1, "*Deleted", $dumbrun);
874 0 $self->show_row($N2, $row2, $rx2, "*Added", $dumbrun);
875 } else {
876 0 my (undef, undef, $rx1) = @{$items1[$i]};
877 0 my (undef, undef, $rx2) = @{$items2[$i]};
878 0 $self->diff_rows($rx1, $rx2, "Changed", $dumbrun)
879 }
880 }
881 0 for my $i ($lim+1 .. $#items1) {
882 0 my ($N1, $row1, $rx1) = @{$items1[$i]};
883 0 ▶ oops unless $N1==1;
Branch: 1 branch
BranchTrueFalseTotal
unless $N1 == 1 0 0 0
884 0 $self->show_row(1, $row1, $rx1, "Deleted*", $dumbrun);
885 }
886 0 for my $i ($lim+1 .. $#items2) {
887 0 my ($N2, $row2, $rx2) = @{$items2[$i]};
888 0 ▶ oops unless $N2==2;
Branch: 1 branch
BranchTrueFalseTotal
unless $N2 == 2 0 0 0
889 0 $self->show_row(2, $row2, $rx2, "Added*", $dumbrun);
890 }
891 }
892 }
893 0 return $changed
894 }#_native_output
895  
896 0 ▶sub _check_title_rows {
Subroutine _check_title_rows: not called CC=6 SCAR=37.4
897 0 my $self = shift;
898 0 my ($sh1, $sh2) = @$self{qw/sheet1 sheet2/};
899  
900 0 ▶ if (!defined($self->{title_row})) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{'title_row'}) { } 0 0 0
901 0 ▶ my sub _titlerow_msg {
Subroutine _titlerow_msg: not called CC=2 SCAR=17.9
902 0 my $sh = shift;
903 0 my $rx = $sh->title_rx;
904 0 ▶ defined($rx) ? "--title-row ".($rx+1) : "NO --title-row"
Branch: 1 branch
BranchTrueFalseTotal
defined $rx ? : 0 0 0
905 }
906 0 ▶ if ($self->{verbose}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"verbose"}) 0 0 0
907 0 ▶ if (u($sh1->title_rx) eq u($sh2->title_rx)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&u(scalar $sh1->title_rx) eq &u(scalar $sh2->title_rx)) { } 0 0 0
908 0 warn "> Auto-detected ", _titlerow_msg($sh1), " in both\n";
909 } else {
910 0 warn "> Auto-detected DIFFERENT title rows:\n";
911 0 warn " ",_titlerow_msg($sh1), " in ", $sh1->data_source, "\n";
912 0 warn " ",_titlerow_msg($sh2), " in ", $sh2->data_source, "\n";
913 }
914 }
915 } else {
916 confess "bug"
917 ,vis($sh1->attributes())
918 0 ▶ if $self->{title_row}==0 && defined( $sh1->title_rx );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"title_row"} == 0 and defined $sh1->title_rx 0 0 0
Condition0%0/3
$self->{"title_row"} == 0 and defined $sh1->title_rx
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"title_row"} == 0 and defined $sh1->title_rx
$self->{"title_row"} == 0 defined $sh1->title_rx
Truth table0%0/3
A and B
A= $self->{"title_row"} == 0B= defined $sh1->title_rx
ABresult
0X0
100
111
919 }
920 }#_check_title_rows
921  
922 # Find corresponding columns with the same title or fuzzy-matched titles
923 0 ▶sub _pairup_columns {
Subroutine _pairup_columns: not called CC=27 SCAR=66.3
924 0 my $self = shift;
925 0 my ($sh1, $sh2) = @$self{qw/sheet1 sheet2/};
926 # Match titles; if not all matched, try fuzzy alternatives, exhausting all
927 # possibilities at each "level of fuzzyness" before trying the next level.
928 #
929 # Matching by levels avoids wrongly matching a fuzzed title when the same
930 # string is a non-fuzzed (or less-fuzzed) title in the other file.
931 # For example, with
932 # f1=(Foo, Bar) f2=(Foo, foo, bar)
933 # "Foo" must be matched before trying fuzzed versions; otherwise
934 # f1-cx0 might be fuzzed to "foo" and wrongly match f2-cx1 when it
935 # should match f2-cx0.
936 #
937 # Note: Fuzzing can make mistakes. For example with
938 # f1=(F_oo,Bar) and f2=(Foo, f_oo), no titles match without fuzzing
939 # but it is unclear whether f1's "F_oo" should match "Foo" or "f_oo" in f2.
940 # Nevertheless, we will pick one and if wrong then the user will get a
941 # "change" in every row.
942  
943 0 ▶ my sub add_titles($$$) {
Subroutine add_titles: not called CC=9 SCAR=45.0
944 0 my ($hash, $level, $sh) = @_;
945 # Add titles (fuzzed to exactly the specified degree) to a hash unless
946 # already present, i.e. never trying a fuzzed title which matches
947 # a non-fuzzed (or less-fuzzed) title in the same file.
948 0 ▶ my $title_row = $sh->title_row
Condition0%0/2
$sh->title_row // (return)
l!l
00
MC/DC0%0/1
$sh->title_row // (return)
$sh->title_row
Truth table0%0/2
A
A= $sh->title_row
Aresult
11
00
949 // return;
950 0 foreach my $cx (0..$sh->num_cols-1) {
951 0 my $title = $title_row->[$cx];
952 0 my $t = $title;
953 0 ▶ { last if $level==0;
Branch: 1 branch
BranchTrueFalseTotal
if $level == 0 0 0 0
954 0 ▶ $t = fc($t); last if $level==1;
Branch: 1 branch
BranchTrueFalseTotal
if $level == 1 0 0 0
955 0 $t =~ s/[_. ]|\Q${visible_space}\E/-/g;
956 0 ▶ last if $level==2;
Branch: 1 branch
BranchTrueFalseTotal
if $level == 2 0 0 0
957 0 ($t = fc $title) =~ s/\s+|\Q${visible_space}\E//sg;
958 0 ▶ last if $level==3;
Branch: 1 branch
BranchTrueFalseTotal
if $level == 3 0 0 0
959 0 ▶ $t =~ s/\W//g; last if $level==4;
Branch: 1 branch
BranchTrueFalseTotal
if $level == 4 0 0 0
960 0 oops
961 }
962 0 ▶ unless(exists $hash->{$t}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $hash->{$t}) 0 0 0
963 0 $hash->{$t} = $cx;
964 #warn dvis '## $level $title -> $t\n';
965 }
966 }
967 }
968  
969 0 my (@corresp_titles1, @corresp_titles2);
970 0 my (@corresp_cxlist1, @corresp_cxlist2);
971 0 my ($title_row1, $title_row2) = ($sh1->title_row, $sh2->title_row);
972 0 my ($hash1, $hash2) = ({}, {});
973 0 my (%cx1found, %cx2found);
974 0 for my $level (0..4) {
975 0 add_titles($hash1, $level, $sh1);
976 0 add_titles($hash2, $level, $sh2);
977 0 ▶ while (my ($ft,$cx1) = each %$hash1) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$hash1) 0 0 0
978 0 ▶ next if $cx1found{$cx1};
Branch: 1 branch
BranchTrueFalseTotal
if $cx1found{$cx1} 0 0 0
979 0 ▶ if (defined(my $cx2 = delete $hash2->{$ft})) { # "matching" titles
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $cx2 = delete $hash2->{$ft})) 0 0 0
980 oops(dvis '$cx1 $cx2 $ft\n$hash1\n$hash2\%cx1found\n%cx2found')
981 0 ▶ if $cx2found{$cx2};
Branch: 1 branch
BranchTrueFalseTotal
if $cx2found{$cx2} 0 0 0
982 #$cx1found{$cx1} = $cx2found{$cx2} = 1;
983 0 $cx1found{$cx1} = $cx2found{$cx2} = "1,2:$cx1-$cx2";
984 0 delete $hash1->{$ft};
985 0 push @corresp_cxlist1, $cx1;
986 0 push @corresp_cxlist2, $cx2;
987 0 ▶ push @corresp_titles1, $title_row1->[$cx1] // oops;
Condition0%0/3
$title_row1->[$cx1] // &oops()
l!l&&r!l&&!r
000
MC/DC0%0/2
$title_row1->[$cx1] // &oops()
$title_row1->[$cx1] &oops()
Truth table0%0/3
A // B
A= $title_row1->[$cx1]B= &oops()
ABresult
1X1
011
000
988 0 ▶ push @corresp_titles2, $title_row2->[$cx2] // oops;
Condition0%0/3
$title_row2->[$cx2] // &oops()
l!l&&r!l&&!r
000
MC/DC0%0/2
$title_row2->[$cx2] // &oops()
$title_row2->[$cx2] &oops()
Truth table0%0/3
A // B
A= $title_row2->[$cx2]B= &oops()
ABresult
1X1
011
000
989 }
990 }
991 0 ▶ while (my ($ft,$cx2) = each %$hash2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$hash2) 0 0 0
992 0 ▶ next if $cx2found{$cx2};
Branch: 1 branch
BranchTrueFalseTotal
if $cx2found{$cx2} 0 0 0
993 0 ▶ if (defined(my $cx1 = delete $hash1->{$ft})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $cx1 = delete $hash1->{$ft})) 0 0 0
994 oops(dvis '$level $cx2 $cx1 $ft\n$hash2\n$hash1\n%cx2found\n%cx1found\n$title_row1->[$cx1] $title_row2->[$cx2]')
995 0 ▶ if $cx1found{$cx1};
Branch: 1 branch
BranchTrueFalseTotal
if $cx1found{$cx1} 0 0 0
996 #$cx2found{$cx2} = $cx1found{$cx1} = 2;
997 0 $cx1found{$cx1} = $cx2found{$cx2} = "2,1:$cx2-$cx1";
998 0 delete $hash2->{$ft};
999 0 push @corresp_cxlist1, $cx1;
1000 0 push @corresp_cxlist2, $cx2;
1001 0 ▶ push @corresp_titles1, $title_row1->[$cx1] // oops;
Condition0%0/3
$title_row1->[$cx1] // &oops()
l!l&&r!l&&!r
000
MC/DC0%0/2
$title_row1->[$cx1] // &oops()
$title_row1->[$cx1] &oops()
Truth table0%0/3
A // B
A= $title_row1->[$cx1]B= &oops()
ABresult
1X1
011
000
1002 0 ▶ push @corresp_titles2, $title_row2->[$cx2] // oops;
Condition0%0/3
$title_row2->[$cx2] // &oops()
l!l&&r!l&&!r
000
MC/DC0%0/2
$title_row2->[$cx2] // &oops()
$title_row2->[$cx2] &oops()
Truth table0%0/3
A // B
A= $title_row2->[$cx2]B= &oops()
ABresult
1X1
011
000
1003 }
1004 }
1005 0 ▶ last unless keys %$hash1; # stop if original titles all matched
Branch: 1 branch
BranchTrueFalseTotal
unless keys %$hash1 0 0 0
1006 }
1007 0 ▶ oops unless @corresp_cxlist1 == @corresp_cxlist2;
Branch: 1 branch
BranchTrueFalseTotal
unless @corresp_cxlist1 == @corresp_cxlist2 0 0 0
1008 0 ▶ oops unless @corresp_cxlist1 == @corresp_titles1;
Branch: 1 branch
BranchTrueFalseTotal
unless @corresp_cxlist1 == @corresp_titles1 0 0 0
1009 0 ▶ oops unless @corresp_cxlist1 == @corresp_titles2;
Branch: 1 branch
BranchTrueFalseTotal
unless @corresp_cxlist1 == @corresp_titles2 0 0 0
1010 0 { my @order = sort {$corresp_cxlist1[$a] <=> $corresp_cxlist1[$b]}
1011 0..$#corresp_cxlist1;
1012 0 @corresp_cxlist1 = @corresp_cxlist1[@order];
1013 0 @corresp_cxlist2 = @corresp_cxlist2[@order];
1014 0 @corresp_titles1 = @corresp_titles1[@order];
1015 0 @corresp_titles2 = @corresp_titles2[@order];
1016 }
1017 # Assume un-matched titles *in the same column* correspond
1018 # (if there are no titles, then all columns correspond!)
1019 # A collective title is synthesized for each pair.
1020 # Otherwise unmatched titles are considered unpaired.
1021 0 my (@unpaired_cxlist1, @unpaired_cxlist2);
1022 0 for my $cx (0..($sh1->num_cols-1)) {
1023 0 ▶ next if defined $cx1found{$cx};
Branch: 1 branch
BranchTrueFalseTotal
if defined $cx1found{$cx} 0 0 0
1024 0 ▶ if (!defined $cx2found{$cx} && $cx < $sh2->num_cols) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $cx2found{$cx} and $cx < $sh2->num_cols) { } 0 0 0
Condition0%0/3
not defined $cx2found{$cx} and $cx < $sh2->num_cols
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $cx2found{$cx} and $cx < $sh2->num_cols
not defined $cx2found{$cx} $cx < $sh2->num_cols
Truth table0%0/3
not(A) and B
A= not defined $cx2found{$cx}B= $cx < $sh2->num_cols
ABresult
0X0
100
111
1025 0 $cx1found{$cx} = $cx2found{$cx} = 1;
1026 0 push @corresp_cxlist1, $cx;
1027 0 push @corresp_cxlist2, $cx;
1028 0 my $synthetic_title = "(col ".cx2let($cx).")";
1029 0 push @corresp_titles1, $synthetic_title;
1030 0 push @corresp_titles2, $synthetic_title;
1031 } else {
1032 0 push @unpaired_cxlist1, $cx;
1033 }
1034 }
1035 0 for my $cx (0..($sh2->num_cols-2)) {
1036 0 ▶ next if defined $cx2found{$cx};
Branch: 1 branch
BranchTrueFalseTotal
if defined $cx2found{$cx} 0 0 0
1037 0 push @unpaired_cxlist2, $cx;
1038 }
1039 0 ▶ oops unless @corresp_cxlist1 + @unpaired_cxlist1 == $sh1->num_cols;
Branch: 1 branch
BranchTrueFalseTotal
unless @corresp_cxlist1 + @unpaired_cxlist1 == $sh1->num_cols 0 0 0
1040 0 ▶ oops unless @corresp_cxlist1 + @unpaired_cxlist2 == $sh2->num_cols;
Branch: 1 branch
BranchTrueFalseTotal
unless @corresp_cxlist1 + @unpaired_cxlist2 == $sh2->num_cols 0 0 0
1041  
1042 0 $sh1->attributes->{CORRESP_TITLES } = \@corresp_titles1;
1043 0 $sh1->attributes->{CORRESP_CXLIST } = \@corresp_cxlist1;
1044 0 $sh1->attributes->{UNPAIRED_CXLIST } = \@unpaired_cxlist1;
1045  
1046 0 $sh2->attributes->{CORRESP_TITLES } = \@corresp_titles2;
1047 0 $sh2->attributes->{CORRESP_CXLIST } = \@corresp_cxlist2;
1048 0 $sh2->attributes->{UNPAIRED_CXLIST } = \@unpaired_cxlist2;
1049  
1050 warn dvis '$title_row1\n$title_row2\n'
1051 .'@corresp_titles1\n@corresp_titles2\n'
1052 .'@corresp_cxlist1\n@corresp_cxlist2\n'
1053 .'@unpaired_cxlist1\n@unpaired_cxlist2\n'
1054 0 ▶ if $opts{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"debug"} 0 0 0
1055  
1056 }#_pairup_columns
1057  
1058 0 ▶sub compare_native {
Subroutine compare_native: not called CC=20 SCAR=60.4
1059 0 my $self = shift;
1060 0 require Algorithm::Diff;
1061  
1062 0 for my $N (1, 2) {
1063 0 my $sh = $self->{"sheet$N"};
1064 # Determine widest row in rows not preceeding title row (if any),
1065 # excluding empty cells on the right end of a row.
1066 0 my $ndcols = 0;
1067 0 my $title_rx = $sh->title_rx;
1068 0 ▶ $sh->apply_all(sub{
Subroutine __ANON__: not called CC=5 SCAR=34.0
1069 0 ▶ return if defined($title_rx) && $sh->rx < $title_rx;
Branch: 1 branch
BranchTrueFalseTotal
if defined $title_rx and $sh->rx < $title_rx 0 0 0
Condition0%0/3
defined $title_rx and $sh->rx < $title_rx
!ll&&!rl&&r
000
MC/DC0%0/2
defined $title_rx and $sh->rx < $title_rx
defined $title_rx $sh->rx < $title_rx
Truth table0%0/3
A and B
A= defined $title_rxB= $sh->rx < $title_rx
ABresult
0X0
100
111
1070 0 my $n = $sh->num_cols;
1071 0 ▶ while ($n > 0 && $sh->crow->[$n-1] eq "") { --$n }
Branch: 1 branch
BranchTrueFalseTotal
if ($n > 0 and $sh->crow->[$n - 1] eq "") 0 0 0
Condition0%0/3
$n > 0 and $sh->crow->[$n - 1] eq ""
!ll&&!rl&&r
000
MC/DC0%0/2
$n > 0 and $sh->crow->[$n - 1] eq ""
$n > 0 $sh->crow->[$n - 1] eq ""
Truth table0%0/3
A and B
A= $n > 0B= $sh->crow->[$n - 1] eq ""
ABresult
0X0
100
111
1072 0 $ndcols = max($ndcols, $n);
1073 0 });
1074 0 $sh->attributes->{NUMCOLS_USED} = $ndcols;
1075  
1076 # Translate --always-show and --id columns to cx values for each sheet
1077 0 foreach(['always_show_columns','ALWAYS_SHOW'],
1078 ['id_columns','ID_CXS']) {
1079 0 my ($option_key, $attr_key) = @$_;
1080 0 foreach my $spec (@{$self->{$option_key}}) {
1081 0 ▶ my $identlist = $sh->attributes->{TRACKING}->{ $spec } // confess;
Condition0%0/2
$sh->attributes->{"TRACKING"}{$spec} // confess()
l!l
00
MC/DC0%0/1
$sh->attributes->{"TRACKING"}{$spec} // confess()
$sh->attributes->{"TRACKING"}{$spec}
Truth table0%0/2
A
A= $sh->attributes->{"TRACKING"}{$spec}
Aresult
11
00
1082 0 foreach (@$identlist) {
1083 0 ▶ my $cx = $sh->colx->{$_}
Condition0%0/2
$sh->colx->{$_} // die("Can not use ignored column '${_}' ($spec) for $option_key", " (", $sh->data_source, ")\n")
l!l
00
MC/DC0%0/1
$sh->colx->{$_} // die("Can not use ignored column '${_}' ($spec) for $option_key", " (", $sh->data_source, ")\n")
$sh->colx->{$_}
Truth table0%0/2
A
A= $sh->colx->{$_}
Aresult
11
00
1084 // die "Can not use ignored column '$_' ($spec) for $option_key",
1085 " (",$sh->data_source,")\n";
1086  
1087 0 $sh->attributes->{$attr_key}->{$cx} = 1; # e.g. attributes->{ID_CXS}
1088 }
1089 }
1090 }
1091 }
1092  
1093 0 $self->_check_title_rows();
1094  
1095 # Set attributes CORRESP_TITLES, CORRESP_CXLIST, and UNPAIRED_CXLIST
1096 0 $self->_pairup_columns();
1097  
1098 0 my $restricted_keycols = @{$self->{id_columns}} > 0;
1099  
1100 0 ▶ my @cxlist1 = ( @{ $self->{sheet1}->attributes->{CORRESP_CXLIST} // [] },
Condition0%0/2
$self->{"sheet1"}->attributes->{"CORRESP_CXLIST"} // []
l!l
00
MC/DC0%0/1
$self->{"sheet1"}->attributes->{"CORRESP_CXLIST"} // []
$self->{"sheet1"}->attributes->{"CORRESP_CXLIST"}
Truth table0%0/2
A
A= $self->{"sheet1"}->attributes->{"CORRESP_CXLIST"}
Aresult
11
00
1101 0 @{ $self->{sheet1}->attributes->{UNPAIRED_CXLIST} }
1102 );
1103 0 ▶ my @cxlist2 = ( @{ $self->{sheet2}->attributes->{CORRESP_CXLIST} // [] },
Condition0%0/2
$self->{"sheet2"}->attributes->{"CORRESP_CXLIST"} // []
l!l
00
MC/DC0%0/1
$self->{"sheet2"}->attributes->{"CORRESP_CXLIST"} // []
$self->{"sheet2"}->attributes->{"CORRESP_CXLIST"}
Truth table0%0/2
A
A= $self->{"sheet2"}->attributes->{"CORRESP_CXLIST"}
Aresult
11
00
1104 0 @{ $self->{sheet2}->attributes->{UNPAIRED_CXLIST} }
1105 );
1106  
1107 # FIXME: What is difference between unmatched-in-same-columm
1108 # pairs in CORRESP_CXLIST and UNPAIRED_CXLIST ??
1109 # If *all* unmatched were just put into UNPAIRED_CXLIST in cx order,
1110 # I think they would line up exactly the same way as we get here...
1111  
1112 # Corresponding columns, limited to --id-columns if specified, otherwise all
1113 0 my (@idcxlist1, @idcxlist2);
1114 0 ▶ if ($restricted_keycols) {
Branch: 1 branch
BranchTrueFalseTotal
if ($restricted_keycols) { } 0 0 0
1115 0 @idcxlist1 = sort {$a <=> $b}
1116 0 keys %{ $self->{sheet1}->attributes->{ID_CXS} };
1117 0 @idcxlist2 = sort {$a <=> $b}
1118 0 keys %{ $self->{sheet2}->attributes->{ID_CXS} };
1119 } else {
1120 0 @idcxlist1 = @cxlist1;
1121 0 @idcxlist2 = @cxlist2;
1122 }
1123 0 my $max_idcxlist_max = max($#idcxlist1, $#idcxlist2);
1124 0 my @idpadding1 = ("") x ($max_idcxlist_max - $#idcxlist1);
1125 0 my @idpadding2 = ("") x ($max_idcxlist_max - $#idcxlist2);
1126  
1127 # Call user-specified global setup code (defaults to nop).
1128 # This could edit the Spreadsheet::Edit sheet contents and/or
1129 # save information in globals (declared with "our") for later use
1130 # by hashid-func or hash-func. All these user-defined functions
1131 # are compiled into package Usercode.
1132 # FIXME: Probably too late for this to be useful. UNDOCUMENTING for now...
1133 $setup_code_code->( $self->{"sheet1"},
1134 0 $self->{"sheet2"},
1135 \@idcxlist1,
1136 \@idcxlist2,
1137 );
1138  
1139 0 ▶ my $keygen = sub{
Subroutine __ANON__: not called CC=4 SCAR=30.0
1140 # Form a hash of the contents of the "id columns" in a given row
1141 # (or all columns in the absence of --id-column arguments).
1142 #
1143 # The Diff algorithm is run using only these hash values, and so rows
1144 # which represent the same record (i.e. same 'id' values) will
1145 # be reported as "unchanged" even though they may differ in other columns,
1146 # and other kinds of hunks represent additions or deletions of identities
1147 # to the data.
1148 #
1149 # OTOH, if there ARE NO ID columns, then the hash covers all columns
1150 # and only completely-identical rows are reported as "unchanged"
1151 # and changes may be reported as add, delete, or replace in a somewhat
1152 # unpredictable way (which is why using --id-columns is helpful).
1153 #
1154 # This processing occurs in _native_output()
1155 #
1156 # If one file has fewer columns than the other, the narrower rows are
1157 # padded with empty strings.
1158 0 my ($N, $row, $orig_rx) = @{ $_[0] };
1159 0 my $sh = $self->{"sheet$N"};
1160 0 ▶ if ($orig_rx eq u($sh->title_rx)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($orig_rx eq &u(scalar $sh->title_rx)) 0 0 0
1161 # Return a special token to force title rows to "match". This makes
1162 # changes in titles appear as a "change" and not "delete" + "add".
1163 0 return " __TITLE_ROW__ ";
1164 }
1165 0 ▶ my $idcxlist = ($N==1 ? \@idcxlist1 : \@idcxlist2);
Branch: 1 branch
BranchTrueFalseTotal
$N == 1 ? : 0 0 0
1166 0 ▶ my $idpadding = ($N==1 ? \@idpadding1 : \@idpadding2);
Branch: 1 branch
BranchTrueFalseTotal
$N == 1 ? : 0 0 0
1167  
1168 0 my $s = $hashid_func_code->([@{$row}[ @$idcxlist ], @$idpadding], $orig_rx);
1169 0 $s;
1170 0 };
1171  
1172 # Pre-screen all records, ignoring any for which hashid-func returns
1173 # undef, but remembering the original rx values for display to the user.
1174 #
1175 # Note: An alternate implementation would be to pass the hash strings
1176 # directly to Algorithm::Diff with no keyGen for faster Diff operation
1177 # (Diff is optimized for this case), remembering original rx values in
1178 # a side hash. However this would double memory usage and performance
1179 # probably matters only with very large spreadsheets when conserving memory
1180 # is arguably more important than conserving CPU.
1181  
1182 # Two arrays of [sheet#, rowref, original_rx], one for each sheet:
1183 0 my @diffargs = ([], []);
1184 0 foreach my $N (1,2) {
1185 0 my $sh = $self->{"sheet$N"};
1186 0 my $rows = $sh->rows;
1187 0 ▶ for (my $rx=0; $rx <= $#$rows; $rx++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rx <= $rows->$#*) 0 0 0
1188 0 my $row = $rows->[$rx];
1189 0 my $diffitem = [$N, $row, $rx]; # [sheet#, rowref, original_rx]
1190 0 ▶ push @{ $diffargs[$N-1] }, $diffitem if defined($keygen->($diffitem));
Branch: 1 branch
BranchTrueFalseTotal
if defined &$keygen($diffitem) 0 0 0
1191 }
1192 }
1193  
1194 0 ▶ warn dvis '$diffargs[0]\n$diffargs[1]\n' if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
1195  
1196 0 my $diff = Algorithm::Diff->new(
1197 $diffargs[0],
1198 $diffargs[1],
1199 { keyGen => $keygen }
1200 );
1201  
1202 # dumbrun==true to determine the width of the widest displayed title
1203 # without actually printing anything
1204 0 $self->{widest_title_width} = 0;
1205 0 my $changed = $self->_native_output(\@cxlist1, \@cxlist2, $diff, 1);
1206  
1207 0 ▶ if ($changed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($changed) { } 0 0 0
1208 0 ▶ if ($self->{sheetname_str} ne "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"sheetname_str"} ne "") 0 0 0
1209 0 my $wid = length($self->{sheetname_str});
1210 0 my $sep = "*" x (10 + $wid + 4);
1211 0 print "\n$sep\n";
1212 0 print "*** SHEET ",$self->{sheetname_str}, " ***\n";
1213 0 print "$sep\n";
1214 }
1215 0 $diff->Reset;
1216 0 my $changed2 = $self->_native_output(\@cxlist1, \@cxlist2, $diff, 0);
1217 0 ▶ oops if $changed2 != $changed;
Branch: 1 branch
BranchTrueFalseTotal
if $changed2 != $changed 0 0 0
1218 }
1219 else {
1220 0 ▶ if ($opts{debug}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"debug"}) 0 0 0
1221 ###TEMP FIXME remove this after verified
1222 0 $diff->Reset;
1223 0 my $changed2 = $self->_native_output(\@cxlist1, \@cxlist2, $diff, 0);
1224 0 ▶ oops if $changed2;
Branch: 1 branch
BranchTrueFalseTotal
if $changed2 0 0 0
1225 }
1226 }
1227 0 ▶ $self->{exit_status} = ($changed ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$changed ? : 0 0 0
1228 }
1229  
1230 # Format a value for display as an indented block.
1231 # Newlines in the input are already converted to visible "\n".
1232 # Actual newlines are appended to these markers and indentation
1233 # inserted before second and subsequent lines. Quotes are not
1234 # included in the result.
1235 # Usage:
1236 # printf "%*s: '%s'\n", $twid, $title, fmt_value($valstr,$twid+3);
1237 0 ▶sub fmt_value($$) {
Subroutine fmt_value: not called CC=6 SCAR=37.4
1238 0 my ($str, $indent_width) = @_;
1239 0 ▶ oops if $str =~ /\n/s;
Branch: 1 branch
BranchTrueFalseTotal
if $str =~ /\n/su 0 0 0
1240 0 my $indent = " " x $indent_width;
1241 0 $str =~ s/\\n/\\n\n${indent}/gs;
1242 0 ▶ if ($maxwidth) { # fold
Branch: 1 branch
BranchTrueFalseTotal
if ($maxwidth) 0 0 0
1243 0 my $first_mw = $maxwidth - $indent_width;
1244 0 ▶ if (length($str) > $first_mw) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $str > $first_mw) 0 0 0
1245 0 ▶ oops "maxwidth $maxwidth is too narrow\nfmw=$first_mw iw=$indent_width <<$str>>"
Branch: 1 branch
BranchTrueFalseTotal
if $first_mw < 20 0 0 0
1246 if $first_mw < 20; # sanity
1247 0 $str =~ s/\A([^\n]{$first_mw})([^\n]+)/$1\n${indent}$2/m;
1248 0 ▶ while ($str =~ s/^([^\n]{$maxwidth})([^\n]+)/$1\n${indent}$2/m) { }
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ s/^([^\n]{$maxwidth})([^\n]+)/$1\n$indent$2/mu) 0 0 0
1249 }
1250 }
1251 $str
1252 0 }
1253 #BEGIN {
1254 #$maxwidth = 30; for my $w (0..$maxwidth*3) {
1255 # my ($i, $NextL) = (0, "A");
1256 # my $s = "";
1257 # for (0..$w-1) {
1258 # my $d = ($i++ % 10);
1259 # if ($d==0 && $i > 1) { $d = $NextL++ }
1260 # $s .= $d;
1261 # if (($_ % 62)==9) { $s .= "\\n" }
1262 # }
1263 # print "Input : $s\n";
1264 # print "Output: ", fmt_value($s,8), "\n";
1265 #}
1266 #die "Test exit";
1267 #}
1268  
1269 0 ▶sub show_cell {
Subroutine show_cell: not called CC=20 SCAR=60.4
1270 0 ▶ my ($self, $title, $v1, $v2, $dumbrun) = @_; oops unless @_ == 5;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 5 0 0 0
1271  
1272 0 my $widest_title_width = $self->{widest_title_width};
1273 0 ▶ if ($dumbrun) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dumbrun) 0 0 0
1274 # $title has been visualized with newline replaced by literal '\n',
1275 # and they will be displayed just that way, without any wrapping.
1276 0 my $tw = length($title);
1277 0 ▶ if ($tw > $widest_title_width) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tw > $widest_title_width) 0 0 0
1278 0 $self->{widest_title_width} = $tw;
1279 }
1280 return
1281 0 }
1282  
1283 0 my $twidth = (2+$widest_title_width); # 2 for indent
1284  
1285 0 my $v1str = fmt_value($v1, $twidth+3); # +3 for the ": '"
1286  
1287 0 ▶ my $v2str = fmt_value($v2, $twidth+3)
Branch: 1 branch
BranchTrueFalseTotal
if defined $v2 and $v1 ne $v2 0 0 0
Condition0%0/3
defined $v2 and $v1 ne $v2
!ll&&!rl&&r
000
MC/DC0%0/2
defined $v2 and $v1 ne $v2
defined $v2 $v1 ne $v2
Truth table0%0/3
A and B
A= defined $v2B= $v1 ne $v2
ABresult
0X0
100
111
1288 if defined($v2) && $v1 ne $v2;
1289  
1290 0 ▶ if (! defined $v2) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $v2) { } 0 0 0
elsif ($v1 eq $v2) { } 0 0 0
1291 0 printf "%*s: '%s'\n", $twidth, $title, $v1str;
1292 }
1293 elsif ($v1 eq $v2) {
1294 0 printf "%*s: '%s' (unchanged)\n", $twidth, $title, $v1str;
1295 }
1296 else {
1297 0 my $v1_nl = 1 + @{[ $v1str =~ /\n/sg ]};
1298 0 my $v2_nl = 1 + @{[ $v2str =~ /\n/sg ]};
1299 0 ▶ if ($v2_nl <= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v2_nl <= 1) { } 0 0 0
1300 0 my $s = sprintf "%*s: '%s'", $twidth, $title, $v1str;
1301 0 ▶ if ($v2_nl==1
Branch: 1 branch
BranchTrueFalseTotal
if ($v2_nl == 1 and !$maxwidth || length($s) + 4 + length($RArrow) + length($v2str) <= $maxwidth) { } 0 0 0
Condition0%0/6
$v2_nl == 1 and !$maxwidth || length($s) + 4 + length($RArrow) + length($v2str) <= $maxwidth
!ll&&!rl&&r
000
not $maxwidth or length($s) + 4 + length($RArrow) + length($v2str) <= $maxwidth
l!l&&r!l&&!r
000
MC/DC0%0/3
$v2_nl == 1 and !$maxwidth || length($s) + 4 + length($RArrow) + length($v2str) <= $maxwidth
$v2_nl == 1 not $maxwidth length($s) + 4 + length($RArrow) + length($v2str) <= $maxwidth
Truth table0%0/4
A and not(B) or C
A= $v2_nl == 1B= not $maxwidthC= length($s) + 4 + length($RArrow) + length($v2str) <= $maxwidth
ABCresult
0XX0
1000
11X1
1011
1302 && (!$maxwidth
1303 || length($s)+4+length($RArrow)+length($v2str) <= $maxwidth)
1304 ) {
1305 # COL TITLE: 'old text' -> 'new text' (on one line)
1306 0 $s .= " $RArrow '$v2str'";
1307 #$s .= "\n<<<". dvis '$v1 $v2 $v1str $v2str>>>';
1308 }
1309 else {
1310 # COL TITLE: 'old text'
1311 # -> 'new text'
1312 0 $s .= sprintf "\n%*s $RArrow '%s'",
1313 $twidth-length($RArrow), "",
1314 $v2str;
1315 }
1316 0 print $s, "\n";
1317 } else {
1318 # Long multi-line blocks -- show ala 'diff -u infinity' :
1319 # COL TITLE: unchanged line
1320 # + added line
1321 # - deleted line
1322 # unchanged line
1323 0 my @lines1 = split /\\n/, $v1;
1324 0 my @lines2 = split /\\n/, $v2;
1325 0 my $diff = Algorithm::Diff->new(\@lines1, \@lines2);
1326 0 my $title_str = sprintf "%*s:", $twidth, $title;
1327 0 my $indent_str = sprintf "%*s ", $twidth, "";
1328 0 my $pfx = $title_str;
1329 0 ▶ while(my $posn = $diff->Next()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $posn = $diff->Next) 0 0 0
1330 0 my $d = $diff->Diff;
1331 0 ▶ next if $d==0 && $opts{suppress_common_lines};
Branch: 1 branch
BranchTrueFalseTotal
if $d == 0 and $opts{"suppress_common_lines"} 0 0 0
Condition0%0/3
$d == 0 and $opts{"suppress_common_lines"}
!ll&&!rl&&r
000
MC/DC0%0/2
$d == 0 and $opts{"suppress_common_lines"}
$d == 0 $opts{"suppress_common_lines"}
Truth table0%0/3
A and B
A= $d == 0B= $opts{"suppress_common_lines"}
ABresult
0X0
100
111
1332 0 foreach ($diff->Items(1)) {
1333 0 ▶ print $pfx, (($d&1) ? "- " : " "), fmt_value($_, $twidth+3), "\n";
Branch: 1 branch
BranchTrueFalseTotal
$d & 1 ? : 0 0 0
1334 0 $pfx = $indent_str;
1335 }
1336 0 ▶ next if $d==0; # both lists are the same
Branch: 1 branch
BranchTrueFalseTotal
if $d == 0 0 0 0
1337 0 foreach ($diff->Items(2)) {
1338 0 ▶ print $pfx, (($d&2) ? "+ " : " "), fmt_value($_, $twidth+3), "\n";
Branch: 1 branch
BranchTrueFalseTotal
$d & 2 ? : 0 0 0
1339 0 $pfx = $indent_str;
1340 }
1341 }
1342 }
1343 }
1344 } #show_cell
1345 0 ▶sub row_header($$;$) {
Subroutine row_header: not called CC=3 SCAR=24.8
1346 0 my ($verb, $rx1, $rx2) = @_;
1347 0 my $s = "$verb row ".($rx1+1);
1348 0 ▶ $s .= " (row ".($rx2+1)." in 2nd file)" if defined($rx2) && $rx1 != $rx2;
Branch: 1 branch
BranchTrueFalseTotal
if defined $rx2 and $rx1 != $rx2 0 0 0
Condition0%0/3
defined $rx2 and $rx1 != $rx2
!ll&&!rl&&r
000
MC/DC0%0/2
defined $rx2 and $rx1 != $rx2
defined $rx2 $rx1 != $rx2
Truth table0%0/3
A and B
A= defined $rx2B= $rx1 != $rx2
ABresult
0X0
100
111
1349 0 sprintf "\n-------- %s %s\n", $s, ('-' x ($maxwidth-10-length($s)));
1350 }
1351 0 ▶sub show_row {
Subroutine show_row: not called CC=21 SCAR=61.4
1352 #my ($self, $N, $rx, $verb, $dumbrun) = @_; oops if @_ != 5;
1353 0 ▶ my ($self, $N, $row, $curr_rx, $verb, $dumbrun) = @_; oops if @_ != 6;
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 6 0 0 0
1354 0 my $sh = $self->{"sheet$N"};
1355 0 ▶ my $orig_rx = $sh->attributes->{ORIGINAL_RXS}->[$curr_rx] // $curr_rx;
Condition0%0/3
$sh->attributes->{"ORIGINAL_RXS"}[$curr_rx] // $curr_rx
l!l&&r!l&&!r
000
MC/DC0%0/2
$sh->attributes->{"ORIGINAL_RXS"}[$curr_rx] // $curr_rx
$sh->attributes->{"ORIGINAL_RXS"}[$curr_rx] $curr_rx
Truth table0%0/3
A // B
A= $sh->attributes->{"ORIGINAL_RXS"}[$curr_rx]B= $curr_rx
ABresult
1X1
011
000
1356 0 ▶ unless ($dumbrun) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dumbrun) 0 0 0
1357 0 print row_header($verb, $orig_rx);
1358 }
1359 0 ▶ my sub ABC_label {
Subroutine ABC_label: not called CC=2 SCAR=17.9
1360 0 my $cx = shift;
1361 0 ▶ defined($sh->title_rx()) ? "(".cx2origlet($sh,$cx).")" : cx2let($cx);
Branch: 1 branch
BranchTrueFalseTotal
defined $sh->title_rx ? : 0 0 0
1362 }
1363 0 my $title_rx = $sh->title_rx;
1364 0 my $corresp_titles = $sh->attributes->{CORRESP_TITLES};
1365 0 my $always_show = $sh->attributes->{ALWAYS_SHOW};
1366 0 my %notseen = map{ ($_=>1) } (0.._getncols($sh,$curr_rx)-1);
1367 0 my $all_empty = 1;
1368  
1369 0 for my $show_always_shown (1, 0) { # Show --id-columns etc. first
1370 0 ▶ if (defined $corresp_titles) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $corresp_titles) 0 0 0
1371 0 my $corresp_cxlist = $sh->attributes->{CORRESP_CXLIST};
1372 0 for my $i (0 .. $#$corresp_cxlist) {
1373 0 my $cx = $corresp_cxlist->[$i];
1374 0 ▶ next if ((!$always_show->{$cx}) ^ (!$show_always_shown));
Branch: 1 branch
BranchTrueFalseTotal
if !$always_show->{$cx} ^ !$show_always_shown 0 0 0
1375 0 my $value = $row->[$cx];
1376 0 ▶ if ($value ne "" || $self->{show_empties} || $always_show->{ $cx }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value ne "" or $self->{"show_empties"} or $always_show->{$cx}) 0 0 0
Condition0%0/6
$value ne "" or $self->{"show_empties"} or $always_show->{$cx}
l!l&&r!l&&!r
000
$value ne "" or $self->{"show_empties"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$value ne "" or $self->{"show_empties"} or $always_show->{$cx}
$value ne "" $self->{"show_empties"} $always_show->{$cx}
Truth table0%0/4
A or B or C
A= $value ne ""B= $self->{"show_empties"}C= $always_show->{$cx}
ABCresult
1XX1
01X1
0011
0000
1377 0 ▶ my $label = $curr_rx <= ($title_rx//-1)
Branch: 1 branch
BranchTrueFalseTotal
$curr_rx <= ($title_rx // -1) ? : 0 0 0
Condition0%0/2
$title_rx // -1
l!l
00
MC/DC0%0/1
$title_rx // -1
$title_rx
Truth table0%0/2
A
A= $title_rx
Aresult
11
00
1378 ? ABC_label($cx) : $corresp_titles->[$i];
1379 0 $self->show_cell($label, $value, undef, $dumbrun);
1380 0 $all_empty = 0;
1381 }
1382 0 delete $notseen{$cx};
1383 }
1384 }
1385 0 for my $cx (@{ $sh->attributes->{UNPAIRED_CXLIST} }) {
1386 0 ▶ next if (!!$always_show->{ $cx } ^ !!$show_always_shown);
Branch: 1 branch
BranchTrueFalseTotal
if !!$always_show->{$cx} ^ !!$show_always_shown 0 0 0
1387 0 my $value = $row->[$cx];
1388 0 ▶ if ($value ne "" || $self->{show_empties} || $always_show->{ $cx }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value ne "" or $self->{"show_empties"} or $always_show->{$cx}) 0 0 0
Condition0%0/6
$value ne "" or $self->{"show_empties"} or $always_show->{$cx}
l!l&&r!l&&!r
000
$value ne "" or $self->{"show_empties"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$value ne "" or $self->{"show_empties"} or $always_show->{$cx}
$value ne "" $self->{"show_empties"} $always_show->{$cx}
Truth table0%0/4
A or B or C
A= $value ne ""B= $self->{"show_empties"}C= $always_show->{$cx}
ABCresult
1XX1
01X1
0011
0000
1389 0 $self->show_cell(ABC_label($cx), $value, undef, $dumbrun);
1390 0 $all_empty = 0;
1391 }
1392 0 delete $notseen{$cx};
1393 }
1394 }
1395 0 ▶ die dvis 'bug\n %notseen\n $sh->attributes()\n ' if %notseen;
Branch: 1 branch
BranchTrueFalseTotal
if %notseen 0 0 0
1396 0 ▶ print " (empty row)\n" if $all_empty && !$dumbrun;
Branch: 1 branch
BranchTrueFalseTotal
if $all_empty and not $dumbrun 0 0 0
Condition0%0/3
$all_empty and not $dumbrun
!ll&&!rl&&r
000
MC/DC0%0/2
$all_empty and not $dumbrun
$all_empty not $dumbrun
Truth table0%0/3
A and not(B)
A= $all_emptyB= not $dumbrun
ABresult
0X0
100
111
1397 }
1398 0 ▶sub diff_rows {
Subroutine diff_rows: not called CC=21 SCAR=61.4
1399 0 my ($self, $rx1, $rx2, $verb, $dumbrun) = @_;
1400 0 ▶ oops unless @_ == 5;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 5 0 0 0
1401 0 my ($sheet1, $sheet2) = @$self{qw/sheet1 sheet2/};
1402 0 ▶ my $orig_rx1 = $sheet1->attributes->{ORIGINAL_RXS}->[$rx1] // $rx1;
Condition0%0/3
$sheet1->attributes->{"ORIGINAL_RXS"}[$rx1] // $rx1
l!l&&r!l&&!r
000
MC/DC0%0/2
$sheet1->attributes->{"ORIGINAL_RXS"}[$rx1] // $rx1
$sheet1->attributes->{"ORIGINAL_RXS"}[$rx1] $rx1
Truth table0%0/3
A // B
A= $sheet1->attributes->{"ORIGINAL_RXS"}[$rx1]B= $rx1
ABresult
1X1
011
000
1403 0 ▶ my $orig_rx2 = $sheet2->attributes->{ORIGINAL_RXS}->[$rx2] // $rx2;
Condition0%0/3
$sheet2->attributes->{"ORIGINAL_RXS"}[$rx2] // $rx2
l!l&&r!l&&!r
000
MC/DC0%0/2
$sheet2->attributes->{"ORIGINAL_RXS"}[$rx2] // $rx2
$sheet2->attributes->{"ORIGINAL_RXS"}[$rx2] $rx2
Truth table0%0/3
A // B
A= $sheet2->attributes->{"ORIGINAL_RXS"}[$rx2]B= $rx2
ABresult
1X1
011
000
1404 0 my $headerline = row_header($verb, $orig_rx1, $orig_rx2);
1405 0 my $header_printed = $dumbrun; #suppress during dumb run
1406 0 ▶ my $row1 = $sheet1->[$rx1] // die "BUG: Undef row in sheet1 at rx1=$rx1\n";
Condition0%0/2
$sheet1->[$rx1] // die("BUG: Undef row in sheet1 at rx1=$rx1\n")
l!l
00
MC/DC0%0/1
$sheet1->[$rx1] // die("BUG: Undef row in sheet1 at rx1=$rx1\n")
$sheet1->[$rx1]
Truth table0%0/2
A
A= $sheet1->[$rx1]
Aresult
11
00
1407 0 ▶ my $row2 = $sheet2->[$rx2] // die "BUG: Undef row in sheet2 at rx2=$rx2\n";
Condition0%0/2
$sheet2->[$rx2] // die("BUG: Undef row in sheet2 at rx2=$rx2\n")
l!l
00
MC/DC0%0/1
$sheet2->[$rx2] // die("BUG: Undef row in sheet2 at rx2=$rx2\n")
$sheet2->[$rx2]
Truth table0%0/2
A
A= $sheet2->[$rx2]
Aresult
11
00
1408 0 my $ncols1 = _getncols($sheet1, $rx1);
1409 0 my $ncols2 = _getncols($sheet2, $rx2);
1410 0 my %notseen1 = map{ ($_=>1) } (0..$ncols1-1);
1411 0 my %notseen2 = map{ ($_=>1) } (0..$ncols2-1);
1412  
1413 0 my $corresp_titles = $sheet1->attributes->{CORRESP_TITLES};
1414 0 ▶ if (defined $corresp_titles) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $corresp_titles) 0 0 0
1415 0 my $corresp_cxlist1 = $sheet1->attributes->{CORRESP_CXLIST};
1416 0 my $corresp_cxlist2 = $sheet2->attributes->{CORRESP_CXLIST};
1417 0 for my $show_always_shown (1, 0) { # Show --id-columns etc. first
1418 0 for my $i (0 .. $#$corresp_cxlist1) {
1419 0 my $cx1 = $corresp_cxlist1->[$i];
1420 0 ▶ next if ((!$sheet1->attributes->{ALWAYS_SHOW}->{$cx1}) ^ (!$show_always_shown));
Branch: 1 branch
BranchTrueFalseTotal
if !$sheet1->attributes->{"ALWAYS_SHOW"}{$cx1} ^ !$show_always_shown 0 0 0
1421 0 my $cx2 = $corresp_cxlist2->[$i];
1422 0 delete $notseen1{$cx1};
1423 0 delete $notseen2{$cx2};
1424 0 ▶ if (($row1->[$cx1] ne $row2->[$cx2])
Branch: 1 branch
BranchTrueFalseTotal
if ($row1->[$cx1] ne $row2->[$cx2] or $sheet1->attributes->{"ALWAYS_SHOW"}{$cx1}) 0 0 0
Condition0%0/3
$row1->[$cx1] ne $row2->[$cx2] or $sheet1->attributes->{"ALWAYS_SHOW"}{$cx1}
l!l&&r!l&&!r
000
MC/DC0%0/2
$row1->[$cx1] ne $row2->[$cx2] or $sheet1->attributes->{"ALWAYS_SHOW"}{$cx1}
$row1->[$cx1] ne $row2->[$cx2] $sheet1->attributes->{"ALWAYS_SHOW"}{$cx1}
Truth table0%0/3
A or B
A= $row1->[$cx1] ne $row2->[$cx2]B= $sheet1->attributes->{"ALWAYS_SHOW"}{$cx1}
ABresult
1X1
011
000
1425 || $sheet1->attributes->{ALWAYS_SHOW}->{ $cx1 }) {
1426 0 ▶ print $headerline unless $header_printed++;
Branch: 1 branch
BranchTrueFalseTotal
unless $header_printed++ 0 0 0
1427 0 $self->show_cell($corresp_titles->[$i], $row1->[$cx1], $row2->[$cx2], $dumbrun);
1428 }
1429 }
1430 }
1431 }
1432 # "Singleton" columns have no corresponding column in the other sheet.
1433 # Show their cells only if not empty because they don't actually represent
1434 # different content except when they contain something.
1435 0 my $title1_row = $sheet1->title_row;
1436 0 for my $cx1 (@{ $sheet1->attributes->{UNPAIRED_CXLIST} }) {
1437 0 ▶ if ((my $content = $row1->[$cx1]) ne "") {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $content = $row1->[$cx1]) ne "") 0 0 0
1438 0 ▶ print $headerline unless $header_printed++;
Branch: 1 branch
BranchTrueFalseTotal
unless $header_printed++ 0 0 0
1439 0 $self->show_cell("f1."._title_or_origABC($sheet1,$cx1), $content, undef, $dumbrun);
1440 }
1441 0 delete $notseen1{$cx1};
1442 }
1443 0 my $title2_row = $sheet2->title_row;
1444 0 for my $cx2 (@{ $sheet2->attributes->{UNPAIRED_CXLIST} }) {
1445 0 ▶ if ((my $content = $row2->[$cx2]) ne "") {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $content = $row2->[$cx2]) ne "") 0 0 0
1446 0 ▶ print $headerline unless $header_printed++;
Branch: 1 branch
BranchTrueFalseTotal
unless $header_printed++ 0 0 0
1447 0 $self->show_cell("f2."._title_or_origABC($sheet2,$cx2), $content, undef, $dumbrun);
1448 }
1449 0 delete $notseen2{$cx2};
1450 }
1451 0 ▶ die dvis 'bug $rx1 $rx2 $verb %notseen1 %notseen2\n$sheet1->attributes()\n$sheet2->attributes()'
Branch: 1 branch
BranchTrueFalseTotal
if keys %notseen1 or keys %notseen2 0 0 0
Condition0%0/3
keys %notseen1 or keys %notseen2
l!l&&r!l&&!r
000
MC/DC0%0/2
keys %notseen1 or keys %notseen2
keys %notseen1 keys %notseen2
Truth table0%0/3
A or B
A= keys %notseen1B= keys %notseen2
ABresult
1X1
011
000
1452 if keys(%notseen1) or keys(%notseen2);
1453 }
1454  
1455 0 ▶sub compare_using_diff {
Subroutine compare_using_diff: not called CC=11 SCAR=48.8
1456 0 my ($self, $cmd) = @_;
1457 0 ▶ confess "usage: obj->compare_using_diff([command, args...])"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cmd eq "ARRAY" and $cmd->[0] =~ /diff|git/iu 0 0 0
Condition0%0/3
ref $cmd eq "ARRAY" and $cmd->[0] =~ /diff|git/iu
!ll&&!rl&&r
000
MC/DC0%0/2
ref $cmd eq "ARRAY" and $cmd->[0] =~ /diff|git/iu
ref $cmd eq "ARRAY" $cmd->[0] =~ /diff|git/iu
Truth table0%0/3
A and B
A= ref $cmd eq "ARRAY"B= $cmd->[0] =~ /diff|git/iu
ABresult
0X0
100
111
1458 unless ref($cmd) eq "ARRAY" && $cmd->[0] =~ /diff|git/i;
1459  
1460 # # With standard diff, we have no way to suppress the header if there are
1461 # # no diffs, so always print it (it will be "" if no header is ever
1462 # # appropriate).
1463 # confess "bug" unless defined $self->{sheetname_str};
1464 # print $self->{sheetname_str};
1465  
1466 # tkdiff does not support the --label option! # 4/1/23: Ditto for git diff.
1467 #
1468 # Write the possibly-edited-for-visibiliity data to temp .csv files, and
1469 # create symlinks to these csv files named using the "labelN" strings,
1470 # which usually contain the original input paths with common ancestors
1471 # eliminated.
1472 #
1473 # To make 'diff' output look similar to what would appear if run directly
1474 # on the original input files, 'diff' is run in a temp dir which contains
1475 # subdir trees which mimic the original input paths and ending with
1476 # symlinks to the temp .csv files. For example, if the original inputs are
1477 # /home/sam/foo/fee/../file1.xlsx!Sheet1
1478 # /home/sam/bar/bee/file2.xlsx!Sheet3
1479 # then the fake tree would contain
1480 # foo/
1481 # file1.xlsx!Sheet1 -> (symlink to 1st temp .csv file)
1482 # fee/
1483 # bar/
1484 # bee/
1485 # file2.xlsx!Sheet2 -> (symlink to 2nd temp .csv file)
1486 # Then 'diff' is executed with the current directory set to the fake tree
1487 # root, and passed arguments 'foo/fee/file1.xlsx!Sheet1' 'bar/bee/...'.
1488 # Diff's output will show these fake paths.
1489 #
1490 # If the "labelN" strings contain ".." subdirs, then the fake tree root
1491 # is buried below additional temp subdirs sufficiently deep to ensure
1492 # that only our temporary subdirs are traversed.
1493  
1494 0 my $tdirdepth = 0;
1495 0 foreach my $N (1,2) {
1496 0 my $sh = $self->{"sheet$N"};
1497 0 local $_ = path($sh->data_source())->stringify;
1498 0 s/^\Q${ \Path::Tiny->rootdir() }\E//; # ensure not absolute
1499 0 s/[\<\>\:\"\\\|\?\*\P{PosixPrint}]/_/g; # ensure only legal characters
1500 0 $self->{"fakerpath$N"} = $_;
1501 0 my @tmp = (m#(?:^|/)..(?=$|/)#g);
1502 0 $tdirdepth = max($tdirdepth, scalar @tmp);
1503 }
1504 0 ▶ my $tdir = $opts{tempdir} //= Path::Tiny->tempdir("Differ_XXXXX", CLEANUP=>1);
Condition0%0/3
$opts{"tempdir"} //= "Path::Tiny"->tempdir("Differ_XXXXX", "CLEANUP", 1)
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{"tempdir"} //= "Path::Tiny"->tempdir("Differ_XXXXX", "CLEANUP", 1)
$opts{"tempdir"} "Path::Tiny"->tempdir("Differ_XXXXX", "CLEANUP", 1)
Truth table0%0/3
A //= B
A= $opts{"tempdir"}B= "Path::Tiny"->tempdir("Differ_XXXXX", "CLEANUP", 1)
ABresult
1X1
011
000
1505 # Run diff in a sufficiently-deep subdir that ../ in fakerpaths will remain
1506 # within our fake hierarchy.
1507 0 my $diff_cwd = $tdir->child("diffdir");
1508 0 ▶ if ($tdirdepth > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tdirdepth > 0) 0 0 0
1509 0 $diff_cwd = $tdir->child(("sub") x $tdirdepth);
1510 }
1511 0 $diff_cwd->mkdir;
1512  
1513 0 foreach my $N (1,2) {
1514 # Write the temp csv in the fake relpath subdir which will contain
1515 # containing the fake original filename
1516 0 my $fakerpath = path($self->{"fakerpath$N"});
1517 0 my $fakerdir = $fakerpath->parent;
1518 0 my $tmpcsv_path = $diff_cwd->child($fakerdir)->child("TEMPCSV${N}.csv");
1519 0 $tmpcsv_path->parent->mkdir;
1520 0 $self->{"sheet$N"}->write_csv( $tmpcsv_path->canonpath );
1521 # Rename the .csv to the original filename e.g. "File.xlsx!Sheet1"
1522 0 my $tmpfake_path = $diff_cwd->child($fakerdir)->child($fakerpath->basename);
1523 warn "> rename ",$tmpcsv_path->canonpath, "\n ",$tmpfake_path->canonpath,"\n"
1524 0 ▶ if $opts{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"debug"} 0 0 0
1525 0 ▶ rename $tmpcsv_path->canonpath, $tmpfake_path->canonpath
Branch: 1 branch
BranchTrueFalseTotal
rename $tmpcsv_path->canonpath, $tmpfake_path->canonpath or die &ivis("rename \$tmpcsv_path -> \$tmpfake_path (->canonpath) : "), $! 0 0 0
1526 or die ivis 'rename $tmpcsv_path -> $tmpfake_path (->canonpath) : ',$!
1527 }
1528  
1529 0 my $old_cwd = path(".")->absolute;
1530 0 ▶ scope_guard { chdir $old_cwd->canonpath
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
chdir $old_cwd->canonpath or die "Could not restore cwd to $old_cwd : $!" 0 0 0
1531 0 or die "Could not restore cwd to $old_cwd : $!"; };
1532 0 ▶ chdir($diff_cwd->canonpath) or die "chdir $diff_cwd : $!";
Branch: 1 branch
BranchTrueFalseTotal
chdir $diff_cwd->canonpath or die "chdir $diff_cwd : $!" 0 0 0
1533  
1534 my @cmd = (@$cmd, path($self->{"fakerpath1"})->canonpath,
1535 0 path($self->{"fakerpath2"})->canonpath);
1536 0 ▶ if ($opts{debug}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"debug"}) 0 0 0
1537 0 warn "> chdir $diff_cwd\n";
1538 0 warn "> @cmd\n";
1539 }
1540 0 system @cmd;
1541 0 $self->{exit_status} = (($? >> 8) | ($? & 0xFF));
1542 }
1543  
1544 0 ▶sub exit_status { $_[0]->{exit_status} }
Subroutine exit_status: not called CC=1 SCAR=6.9
1545  
1546 __END__
↑ Summary /usr/local/bin/perldoc »