4 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK $Too_Big *FROM *TO);
12 @EXPORT = qw(compare);
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);
30 my ($size, $fromsize, $status, $fr, $tr, $fbuf, $tbuf);
34 croak("from undefined") unless (defined $from);
35 croak("to undefined") unless (defined $to);
37 if (ref($from) && (isa($from,'GLOB') || isa($from,'IO::Handle'))) {
39 } elsif (ref(\$from) eq 'GLOB') {
42 open(FROM,"<$from") or goto fail_open1;
48 if (ref($to) && (isa($to,'GLOB') || isa($to,'IO::Handle'))) {
50 } elsif (ref(\$to) eq 'GLOB') {
53 open(TO,"<$to") or goto fail_open2;
58 if ($closefrom && $closeto) {
59 # If both are opened files we know they differ if their size differ
60 goto fail_inner if $fromsize != -s TO;
64 $size = shift(@_) + 0;
65 croak("Bad buffer size for compare: $size\n") unless ($size > 0);
68 $size = 1024 if ($size < 512);
69 $size = $Too_Big if ($size > $Too_Big);
74 while(defined($fr = read(FROM,$fbuf,$size)) && $fr > 0) {
75 unless (defined($tr = read(TO,$tbuf,$fr)) and $tbuf eq $fbuf) {
79 goto fail_inner if (defined($tr = read(TO,$tbuf,$size)) && $tr > 0);
81 close(TO) || goto fail_open2 if $closeto;
82 close(FROM) || goto fail_open1 if $closefrom;
86 # All of these contortions try to preserve error messages...
88 close(TO) || goto fail_open2 if $closeto;
89 close(FROM) || goto fail_open1 if $closefrom;
98 $! = $status unless $!;
112 File::Compare - Compare files or filehandles
118 if (compare("file1","file2") == 0) {
119 print "They're equal\n";
124 The File::Compare::compare function compares the contents of two
125 sources, each of which can be a file or a file handle. It is exported
126 from File::Compare by default.
128 File::Compare::cmp is a synonym for File::Compare::compare. It is
129 exported from File::Compare only by request.
133 File::Compare::compare return 0 if the files are equal, 1 if the
134 files are unequal, or -1 if an error was encountered.
138 File::Compare was written by Nick Ing-Simmons.
139 Its original documentation was written by Chip Salzenberg.