5 our($VERSION, @ISA, @EXPORT, @EXPORT_OK, $Too_Big);
12 @EXPORT = qw(compare);
13 @EXPORT_OK = qw(cmp compare_text);
15 $Too_Big = 1024 * 1024 * 2;
18 # Version of File::Compare
19 return $File::Compare
::VERSION
;
23 croak
("Usage: compare( file1, file2 [, buffersize]) ")
24 unless(@_ == 2 || @_ == 3);
26 my ($from,$to,$size) = @_;
27 my $text_mode = defined($size) && (ref($size) eq 'CODE' || $size < 0);
29 my ($fromsize,$closefrom,$closeto);
32 croak
("from undefined") unless (defined $from);
33 croak
("to undefined") unless (defined $to);
36 (UNIVERSAL
::isa
($from,'GLOB') || UNIVERSAL
::isa
($from,'IO::Handle'))) {
38 } elsif (ref(\
$from) eq 'GLOB') {
41 open(FROM
,"<$from") or goto fail_open1
;
50 (UNIVERSAL
::isa
($to,'GLOB') || UNIVERSAL
::isa
($to,'IO::Handle'))) {
52 } elsif (ref(\
$to) eq 'GLOB') {
55 open(TO
,"<$to") or goto fail_open2
;
56 binmode TO
unless $text_mode;
60 if (!$text_mode && $closefrom && $closeto) {
61 # If both are opened files we know they differ if their size differ
62 goto fail_inner
if $fromsize != -s TO
;
68 while (defined($fline = <FROM
>)) {
69 goto fail_inner
unless defined($tline = <TO
>);
71 # $size contains ref to comparison function
72 goto fail_inner
if &$size($fline, $tline);
74 goto fail_inner
if $fline ne $tline;
77 goto fail_inner
if defined($tline = <TO
>);
80 unless (defined($size) && $size > 0) {
81 $size = $fromsize || -s TO
|| 0;
82 $size = 1024 if $size < 512;
83 $size = $Too_Big if $size > $Too_Big;
86 my ($fr,$tr,$fbuf,$tbuf);
88 while(defined($fr = read(FROM
,$fbuf,$size)) && $fr > 0) {
89 unless (defined($tr = read(TO
,$tbuf,$fr)) && $tbuf eq $fbuf) {
93 goto fail_inner
if defined($tr = read(TO
,$tbuf,$size)) && $tr > 0;
96 close(TO
) || goto fail_open2
if $closeto;
97 close(FROM
) || goto fail_open1
if $closefrom;
101 # All of these contortions try to preserve error messages...
103 close(TO
) || goto fail_open2
if $closeto;
104 close(FROM
) || goto fail_open1
if $closefrom;
113 $! = $status unless $!;
123 my ($from,$to,$cmp) = @_;
124 croak
("Usage: compare_text( file1, file2 [, cmp-function])")
125 unless @_ == 2 || @_ == 3;
126 croak
("Third arg to compare_text() function must be a code reference")
127 if @_ == 3 && ref($cmp) ne 'CODE';
129 # Using a negative buffer size puts compare into text_mode too
130 $cmp = -1 unless defined $cmp;
131 compare
($from, $to, $cmp);
140 File::Compare - Compare files or filehandles
146 if (compare("file1","file2") == 0) {
147 print "They're equal\n";
152 The File::Compare::compare function compares the contents of two
153 sources, each of which can be a file or a file handle. It is exported
154 from File::Compare by default.
156 File::Compare::cmp is a synonym for File::Compare::compare. It is
157 exported from File::Compare only by request.
159 File::Compare::compare_text does a line by line comparison of the two
160 files. It stops as soon as a difference is detected. compare_text()
161 accepts an optional third argument: This must be a CODE reference to
162 a line comparison function, which returns 0 when both lines are considered
165 compare_text($file1, $file2)
167 is basically equivalent to
169 compare_text($file1, $file2, sub {$_[0] ne $_[1]} )
173 File::Compare::compare return 0 if the files are equal, 1 if the
174 files are unequal, or -1 if an error was encountered.
178 File::Compare was written by Nick Ing-Simmons.
179 Its original documentation was written by Chip Salzenberg.