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, $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;
47 if (ref($to) && (isa($to,'GLOB') || isa($to,'IO::Handle'))) {
49 } elsif (ref(\$to) eq 'GLOB') {
52 open(TO,"<$to") or goto fail_open2;
58 $size = shift(@_) + 0;
59 croak("Bad buffer size for compare: $size\n") unless ($size > 0);
62 $size = 1024 if ($size < 512);
63 $size = $Too_Big if ($size > $Too_Big);
68 while(defined($fr = read(FROM,$fbuf,$size)) && $fr > 0) {
69 unless (defined($tr = read(TO,$tbuf,$fr)) and $tbuf eq $fbuf) {
73 goto fail_inner if (defined($tr = read(TO,$tbuf,$size)) && $tr > 0);
75 close(TO) || goto fail_open2 if $closeto;
76 close(FROM) || goto fail_open1 if $closefrom;
80 # All of these contortions try to preserve error messages...
82 close(TO) || goto fail_open2 if $closeto;
83 close(FROM) || goto fail_open1 if $closefrom;
92 $! = $status unless $!;
106 File::Compare - Compare files or filehandles
112 if (compare("file1","file2") == 0) {
113 print "They're equal\n";
118 The File::Compare::compare function compares the contents of two
119 sources, each of which can be a file or a file handle. It is exported
120 from File::Compare by default.
122 File::Compare::cmp is a synonym for File::Compare::compare. It is
123 exported from File::Compare only by request.
127 File::Compare::compare return 0 if the files are equal, 1 if the
128 files are unequal, or -1 if an error was encountered.
132 File::Compare was written by Nick Ing-Simmons.
133 Its original documentation was written by Chip Salzenberg.