1*0Sstevel@tonic-gatepackage TestCompare; 2*0Sstevel@tonic-gate 3*0Sstevel@tonic-gateuse vars qw(@ISA @EXPORT $MYPKG); 4*0Sstevel@tonic-gate#use strict; 5*0Sstevel@tonic-gate#use diagnostics; 6*0Sstevel@tonic-gateuse Carp; 7*0Sstevel@tonic-gateuse Exporter; 8*0Sstevel@tonic-gateuse File::Basename; 9*0Sstevel@tonic-gateuse File::Spec; 10*0Sstevel@tonic-gateuse FileHandle; 11*0Sstevel@tonic-gate 12*0Sstevel@tonic-gate@ISA = qw(Exporter); 13*0Sstevel@tonic-gate@EXPORT = qw(&testcmp); 14*0Sstevel@tonic-gate$MYPKG = eval { (caller)[0] }; 15*0Sstevel@tonic-gate 16*0Sstevel@tonic-gate##-------------------------------------------------------------------------- 17*0Sstevel@tonic-gate 18*0Sstevel@tonic-gate=head1 NAME 19*0Sstevel@tonic-gate 20*0Sstevel@tonic-gatetestcmp -- compare two files line-by-line 21*0Sstevel@tonic-gate 22*0Sstevel@tonic-gate=head1 SYNOPSIS 23*0Sstevel@tonic-gate 24*0Sstevel@tonic-gate $is_diff = testcmp($file1, $file2); 25*0Sstevel@tonic-gate 26*0Sstevel@tonic-gateor 27*0Sstevel@tonic-gate 28*0Sstevel@tonic-gate $is_diff = testcmp({-cmplines => \&mycmp}, $file1, $file2); 29*0Sstevel@tonic-gate 30*0Sstevel@tonic-gate=head2 DESCRIPTION 31*0Sstevel@tonic-gate 32*0Sstevel@tonic-gateCompare two text files line-by-line and return 0 if they are the 33*0Sstevel@tonic-gatesame, 1 if they differ. Each of $file1 and $file2 may be a filenames, 34*0Sstevel@tonic-gateor a filehandles (in which case it must already be open for reading). 35*0Sstevel@tonic-gate 36*0Sstevel@tonic-gateIf the first argument is a hashref, then the B<-cmplines> key in the 37*0Sstevel@tonic-gatehash may have a subroutine reference as its corresponding value. 38*0Sstevel@tonic-gateThe referenced user-defined subroutine should be a line-comparator 39*0Sstevel@tonic-gatefunction that takes two pre-chomped text-lines as its arguments 40*0Sstevel@tonic-gate(the first is from $file1 and the second is from $file2). It should 41*0Sstevel@tonic-gatereturn 0 if it considers the two lines equivalent, and non-zero 42*0Sstevel@tonic-gateotherwise. 43*0Sstevel@tonic-gate 44*0Sstevel@tonic-gate=cut 45*0Sstevel@tonic-gate 46*0Sstevel@tonic-gate##-------------------------------------------------------------------------- 47*0Sstevel@tonic-gate 48*0Sstevel@tonic-gatesub testcmp( $ $ ; $) { 49*0Sstevel@tonic-gate my %opts = ref($_[0]) eq 'HASH' ? %{shift()} : (); 50*0Sstevel@tonic-gate my ($file1, $file2) = @_; 51*0Sstevel@tonic-gate my ($fh1, $fh2) = ($file1, $file2); 52*0Sstevel@tonic-gate unless (ref $fh1) { 53*0Sstevel@tonic-gate $fh1 = FileHandle->new($file1, "r") or die "Can't open $file1: $!"; 54*0Sstevel@tonic-gate } 55*0Sstevel@tonic-gate unless (ref $fh2) { 56*0Sstevel@tonic-gate $fh2 = FileHandle->new($file2, "r") or die "Can't open $file2: $!"; 57*0Sstevel@tonic-gate } 58*0Sstevel@tonic-gate 59*0Sstevel@tonic-gate my $cmplines = $opts{'-cmplines'} || undef; 60*0Sstevel@tonic-gate my ($f1text, $f2text) = ("", ""); 61*0Sstevel@tonic-gate my ($line, $diffs) = (0, 0); 62*0Sstevel@tonic-gate 63*0Sstevel@tonic-gate while ( defined($f1text) and defined($f2text) ) { 64*0Sstevel@tonic-gate defined($f1text = <$fh1>) and chomp($f1text); 65*0Sstevel@tonic-gate defined($f2text = <$fh2>) and chomp($f2text); 66*0Sstevel@tonic-gate ++$line; 67*0Sstevel@tonic-gate last unless ( defined($f1text) and defined($f2text) ); 68*0Sstevel@tonic-gate $diffs = (ref $cmplines) ? &$cmplines($f1text, $f2text) 69*0Sstevel@tonic-gate : ($f1text ne $f2text); 70*0Sstevel@tonic-gate last if $diffs; 71*0Sstevel@tonic-gate } 72*0Sstevel@tonic-gate close($fh1) unless (ref $file1); 73*0Sstevel@tonic-gate close($fh2) unless (ref $file2); 74*0Sstevel@tonic-gate 75*0Sstevel@tonic-gate $diffs = 1 if (defined($f1text) or defined($f2text)); 76*0Sstevel@tonic-gate if ( defined($f1text) and defined($f2text) ) { 77*0Sstevel@tonic-gate ## these two lines must be different 78*0Sstevel@tonic-gate warn "$file1 and $file2 differ at line $line\n"; 79*0Sstevel@tonic-gate } 80*0Sstevel@tonic-gate elsif (defined($f1text) and (! defined($f1text))) { 81*0Sstevel@tonic-gate ## file1 must be shorter 82*0Sstevel@tonic-gate warn "$file1 is shorter than $file2\n"; 83*0Sstevel@tonic-gate } 84*0Sstevel@tonic-gate elsif (defined $f2text) { 85*0Sstevel@tonic-gate ## file2 must be longer 86*0Sstevel@tonic-gate warn "$file1 is shorter than $file2\n"; 87*0Sstevel@tonic-gate } 88*0Sstevel@tonic-gate return $diffs; 89*0Sstevel@tonic-gate} 90*0Sstevel@tonic-gate 91*0Sstevel@tonic-gate1; 92