4 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK $Too_Big *FROM *TO);
11 @EXPORT = qw(compare);
14 $Too_Big = 1024 * 1024 * 2;
17 # Version of File::Compare
18 return $File::Compare::VERSION;
22 croak("Usage: compare( file1, file2 [, buffersize]) ")
23 unless(@_ == 2 || @_ == 3);
29 my ($size, $fromsize, $status, $fr, $tr, $fbuf, $tbuf);
33 croak("from undefined") unless (defined $from);
34 croak("to undefined") unless (defined $to);
37 (UNIVERSAL::isa($from,'GLOB') || UNIVERSAL::isa($from,'IO::Handle'))) {
39 } elsif (ref(\$from) eq 'GLOB') {
42 open(FROM,"<$from") or goto fail_open1;
49 (UNIVERSAL::isa($to,'GLOB') || UNIVERSAL::isa($to,'IO::Handle'))) {
51 } elsif (ref(\$to) eq 'GLOB') {
54 open(TO,"<$to") or goto fail_open2;
59 if ($closefrom && $closeto) {
60 # If both are opened files we know they differ if their size differ
61 goto fail_inner if $fromsize != -s TO;
65 $size = shift(@_) + 0;
66 croak("Bad buffer size for compare: $size\n") unless ($size > 0);
69 $size = 1024 if ($size < 512);
70 $size = $Too_Big if ($size > $Too_Big);
75 while(defined($fr = read(FROM,$fbuf,$size)) && $fr > 0) {
76 unless (defined($tr = read(TO,$tbuf,$fr)) and $tbuf eq $fbuf) {
80 goto fail_inner if (defined($tr = read(TO,$tbuf,$size)) && $tr > 0);
82 close(TO) || goto fail_open2 if $closeto;
83 close(FROM) || goto fail_open1 if $closefrom;
87 # All of these contortions try to preserve error messages...
89 close(TO) || goto fail_open2 if $closeto;
90 close(FROM) || goto fail_open1 if $closefrom;
99 $! = $status unless $!;
113 File::Compare - Compare files or filehandles
119 if (compare("file1","file2") == 0) {
120 print "They're equal\n";
125 The File::Compare::compare function compares the contents of two
126 sources, each of which can be a file or a file handle. It is exported
127 from File::Compare by default.
129 File::Compare::cmp is a synonym for File::Compare::compare. It is
130 exported from File::Compare only by request.
134 File::Compare::compare return 0 if the files are equal, 1 if the
135 files are unequal, or -1 if an error was encountered.
139 File::Compare was written by Nick Ing-Simmons.
140 Its original documentation was written by Chip Salzenberg.