4 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK $Too_Big *FROM *TO);
11 @EXPORT = qw(compare);
12 @EXPORT_OK = qw(cmp compare_text);
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);
25 my ($from,$to,$size) = @_;
26 my $text_mode = defined($size) && (ref($size) eq 'CODE' || $size < 0);
28 my ($fromsize,$closefrom,$closeto);
31 croak("from undefined") unless (defined $from);
32 croak("to undefined") unless (defined $to);
35 (UNIVERSAL::isa($from,'GLOB') || UNIVERSAL::isa($from,'IO::Handle'))) {
37 } elsif (ref(\$from) eq 'GLOB') {
40 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;
55 binmode TO unless $text_mode;
59 if (!$text_mode && $closefrom && $closeto) {
60 # If both are opened files we know they differ if their size differ
61 goto fail_inner if $fromsize != -s TO;
67 while (defined($fline = <FROM>)) {
68 goto fail_inner unless defined($tline = <TO>);
70 # $size contains ref to comparison function
71 goto fail_inner if &$size($fline, $tline);
73 goto fail_inner if $fline ne $tline;
76 goto fail_inner if defined($tline = <TO>);
79 unless (defined($size) && $size > 0) {
81 $size = 1024 if $size < 512;
82 $size = $Too_Big if $size > $Too_Big;
85 my ($fr,$tr,$fbuf,$tbuf);
87 while(defined($fr = read(FROM,$fbuf,$size)) && $fr > 0) {
88 unless (defined($tr = read(TO,$tbuf,$fr)) && $tbuf eq $fbuf) {
92 goto fail_inner if defined($tr = read(TO,$tbuf,$size)) && $tr > 0;
95 close(TO) || goto fail_open2 if $closeto;
96 close(FROM) || goto fail_open1 if $closefrom;
100 # All of these contortions try to preserve error messages...
102 close(TO) || goto fail_open2 if $closeto;
103 close(FROM) || goto fail_open1 if $closefrom;
112 $! = $status unless $!;
121 my ($from,$to,$cmp) = @_;
122 croak("Usage: compare_text( file1, file2 [, cmp-function])")
123 unless @_ == 2 || @_ == 3;
124 croak("Third arg to compare_text() function must be a code reference")
125 if @_ == 3 && ref($cmp) ne 'CODE';
127 # Using a negative buffer size puts compare into text_mode too
128 $cmp = -1 unless defined $cmp;
129 compare($from, $to, $cmp);
138 File::Compare - Compare files or filehandles
144 if (compare("file1","file2") == 0) {
145 print "They're equal\n";
150 The File::Compare::compare function compares the contents of two
151 sources, each of which can be a file or a file handle. It is exported
152 from File::Compare by default.
154 File::Compare::cmp is a synonym for File::Compare::compare. It is
155 exported from File::Compare only by request.
157 File::Compare::compare_text does a line by line comparison of the two
158 files. It stops as soon as a difference is detected. compare_text()
159 accepts an optional third argument: This must be a CODE reference to
160 a line comparison function, which returns 0 when both lines are considered
163 compare_text($file1, $file2)
165 is basically equivalent to
167 compare_text($file1, $file2, sub {$_[0] ne $_[1]} )
171 File::Compare::compare return 0 if the files are equal, 1 if the
172 files are unequal, or -1 if an error was encountered.
176 File::Compare was written by Nick Ing-Simmons.
177 Its original documentation was written by Chip Salzenberg.