=head1 DESCRIPTION Compare 2 text files =cut $usage = "perl -w ... file1 [ file2 ]"; =head1 COMMAND LINE PARAMETERS perl -w ... file1 [ file2 ] =cut sub errx { print STDERR $_[0], "\n"; exit 1; } sub comparelines { my $a = shift; my $b = shift; my $len1 = length $a; my $len2 = length $b; print STDERR "lines lengths: $len1, $len2\n"; CHAR: for (my $i =0; $i < $len1 && $i < $len2; $i++) { my ($cc,$dd) = (substr($a,$i,1), substr($b,$i,1)); if ($cc eq $dd) { print $cc; } else { printf STDERR "\nmismatch at char %d: '%s' (%d) and '%s' (%d)\n", $i+1, $cc, ord($cc), $dd, ord($dd); last CHAR; } } } ## ## get the command-line parameters ## $afile = shift or &errx($usage); if (0) { while (@ARGV+0 && $ARGV[0] =~ /^-/) { my $aa = $ARGV[0]; last if $aa eq '--'; if ($aa eq '--foo') { $foo = $ARGV[1]; shift @ARGV; shift @ARGV; } elsif ($aa eq '-d') { $debug = 1; shift @ARGV; } else { print STDERR "bad arg: $aa\n"; &errx($usage); } } print "foo=$foo"; } ## ## read in the first file ## open(AFILE, "< $afile") or &errx("cannot open $afile: $!"); @alines = (); ALINE: while () { print STDERR "ALINE: $_" if $debug; push @alines, $_; } printf("file1: %d lines\n", 0+@alines); close(AFILE); ## ## get the second file ## $chomped = 0; $lineno = 0; DLINE: while (<>) { $lineno++; chomp if $chomped; #print STDERR "line $.\n"; if ($lineno > (0+@alines)) { errx "line $lineno: second file is bigger"; } if ($_ ne $alines[$lineno-1]) { print STDERR "file1: ", $alines[$lineno-1], "\n"; print STDERR "file2: ", $_, "\n"; comparelines $alines[$lineno-1], $_; errx "line $lineno: mismatch"; } } printf("file2: %d lines\n", $lineno); exit 0;