6 our($VERSION, @ISA, @EXPORT, @EXPORT_OK, $Too_Big);
13 @EXPORT = qw(compare);
14 @EXPORT_OK = qw(cmp compare_text);
16 $Too_Big = 1024 * 1024 * 2;
19 # Version of File::Compare
20 return $File::Compare::VERSION;
24 croak("Usage: compare( file1, file2 [, buffersize]) ")
25 unless(@_ == 2 || @_ == 3);
27 my ($from,$to,$size) = @_;
28 my $text_mode = defined($size) && (ref($size) eq 'CODE' || $size < 0);
30 my ($fromsize,$closefrom,$closeto);
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;
51 (UNIVERSAL::isa($to,'GLOB') || UNIVERSAL::isa($to,'IO::Handle'))) {
53 } elsif (ref(\$to) eq 'GLOB') {
56 open(TO,"<$to") or goto fail_open2;
57 binmode TO unless $text_mode;
61 if (!$text_mode && $closefrom && $closeto) {
62 # If both are opened files we know they differ if their size differ
63 goto fail_inner if $fromsize != -s TO;
69 while (defined($fline = <FROM>)) {
70 goto fail_inner unless defined($tline = <TO>);
72 # $size contains ref to comparison function
73 goto fail_inner if &$size($fline, $tline);
75 goto fail_inner if $fline ne $tline;
78 goto fail_inner if defined($tline = <TO>);
81 unless (defined($size) && $size > 0) {
82 $size = $fromsize || -s TO || 0;
83 $size = 1024 if $size < 512;
84 $size = $Too_Big if $size > $Too_Big;
87 my ($fr,$tr,$fbuf,$tbuf);
89 while(defined($fr = read(FROM,$fbuf,$size)) && $fr > 0) {
90 unless (defined($tr = read(TO,$tbuf,$fr)) && $tbuf eq $fbuf) {
94 goto fail_inner if defined($tr = read(TO,$tbuf,$size)) && $tr > 0;
97 close(TO) || goto fail_open2 if $closeto;
98 close(FROM) || goto fail_open1 if $closefrom;
102 # All of these contortions try to preserve error messages...
104 close(TO) || goto fail_open2 if $closeto;
105 close(FROM) || goto fail_open1 if $closefrom;
114 $! = $status unless $!;
124 my ($from,$to,$cmp) = @_;
125 croak("Usage: compare_text( file1, file2 [, cmp-function])")
126 unless @_ == 2 || @_ == 3;
127 croak("Third arg to compare_text() function must be a code reference")
128 if @_ == 3 && ref($cmp) ne 'CODE';
130 # Using a negative buffer size puts compare into text_mode too
131 $cmp = -1 unless defined $cmp;
132 compare($from, $to, $cmp);
141 File::Compare - Compare files or filehandles
147 if (compare("file1","file2") == 0) {
148 print "They're equal\n";
153 The File::Compare::compare function compares the contents of two
154 sources, each of which can be a file or a file handle. It is exported
155 from File::Compare by default.
157 File::Compare::cmp is a synonym for File::Compare::compare. It is
158 exported from File::Compare only by request.
160 File::Compare::compare_text does a line by line comparison of the two
161 files. It stops as soon as a difference is detected. compare_text()
162 accepts an optional third argument: This must be a CODE reference to
163 a line comparison function, which returns 0 when both lines are considered
166 compare_text($file1, $file2)
168 is basically equivalent to
170 compare_text($file1, $file2, sub {$_[0] ne $_[1]} )
174 File::Compare::compare return 0 if the files are equal, 1 if the
175 files are unequal, or -1 if an error was encountered.
179 File::Compare was written by Nick Ing-Simmons.
180 Its original documentation was written by Chip Salzenberg.