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 croak("Usage: compare( file1, file2 [, buffersize]) ")
20 unless(@_ == 2 || @_ == 3);
22 my ($from,$to,$size) = @_;
23 my $text_mode = defined($size) && (ref($size) eq 'CODE' || $size < 0);
25 my ($fromsize,$closefrom,$closeto);
28 croak("from undefined") unless (defined $from);
29 croak("to undefined") unless (defined $to);
32 (UNIVERSAL::isa($from,'GLOB') || UNIVERSAL::isa($from,'IO::Handle'))) {
34 } elsif (ref(\$from) eq 'GLOB') {
37 open(FROM,"<$from") or goto fail_open1;
46 (UNIVERSAL::isa($to,'GLOB') || UNIVERSAL::isa($to,'IO::Handle'))) {
48 } elsif (ref(\$to) eq 'GLOB') {
51 open(TO,"<$to") or goto fail_open2;
52 binmode TO unless $text_mode;
56 if (!$text_mode && $closefrom && $closeto) {
57 # If both are opened files we know they differ if their size differ
58 goto fail_inner if $fromsize != -s TO;
64 while (defined($fline = <FROM>)) {
65 goto fail_inner unless defined($tline = <TO>);
67 # $size contains ref to comparison function
68 goto fail_inner if &$size($fline, $tline);
70 goto fail_inner if $fline ne $tline;
73 goto fail_inner if defined($tline = <TO>);
76 unless (defined($size) && $size > 0) {
77 $size = $fromsize || -s TO || 0;
78 $size = 1024 if $size < 512;
79 $size = $Too_Big if $size > $Too_Big;
82 my ($fr,$tr,$fbuf,$tbuf);
84 while(defined($fr = read(FROM,$fbuf,$size)) && $fr > 0) {
85 unless (defined($tr = read(TO,$tbuf,$fr)) && $tbuf eq $fbuf) {
89 goto fail_inner if defined($tr = read(TO,$tbuf,$size)) && $tr > 0;
92 close(TO) || goto fail_open2 if $closeto;
93 close(FROM) || goto fail_open1 if $closefrom;
97 # All of these contortions try to preserve error messages...
99 close(TO) || goto fail_open2 if $closeto;
100 close(FROM) || goto fail_open1 if $closefrom;
109 $! = $status unless $!;
119 my ($from,$to,$cmp) = @_;
120 croak("Usage: compare_text( file1, file2 [, cmp-function])")
121 unless @_ == 2 || @_ == 3;
122 croak("Third arg to compare_text() function must be a code reference")
123 if @_ == 3 && ref($cmp) ne 'CODE';
125 # Using a negative buffer size puts compare into text_mode too
126 $cmp = -1 unless defined $cmp;
127 compare($from, $to, $cmp);
136 File::Compare - Compare files or filehandles
142 if (compare("file1","file2") == 0) {
143 print "They're equal\n";
148 The File::Compare::compare function compares the contents of two
149 sources, each of which can be a file or a file handle. It is exported
150 from File::Compare by default.
152 File::Compare::cmp is a synonym for File::Compare::compare. It is
153 exported from File::Compare only by request.
155 File::Compare::compare_text does a line by line comparison of the two
156 files. It stops as soon as a difference is detected. compare_text()
157 accepts an optional third argument: This must be a CODE reference to
158 a line comparison function, which returns 0 when both lines are considered
161 compare_text($file1, $file2)
163 is basically equivalent to
165 compare_text($file1, $file2, sub {$_[0] ne $_[1]} )
169 File::Compare::compare return 0 if the files are equal, 1 if the
170 files are unequal, or -1 if an error was encountered.
174 File::Compare was written by Nick Ing-Simmons.
175 Its original documentation was written by Chip Salzenberg.