0b73d7c76570925aea187ba67642eb42091f0171
our($VERSION, @ISA, @EXPORT, @EXPORT_OK, $Too_Big);
@EXPORT_OK = qw(cmp compare_text);
$Too_Big = 1024 * 1024 * 2;
croak
("Usage: compare( file1, file2 [, buffersize]) ")
unless(@_ == 2 || @_ == 3);
my ($from,$to,$size) = @_;
my $text_mode = defined($size) && (ref($size) eq 'CODE' || $size < 0);
my ($fromsize,$closefrom,$closeto);
croak
("from undefined") unless (defined $from);
croak
("to undefined") unless (defined $to);
(UNIVERSAL
::isa
($from,'GLOB') || UNIVERSAL
::isa
($from,'IO::Handle'))) {
} elsif (ref(\
$from) eq 'GLOB') {
open(FROM
,"<$from") or goto fail_open1
;
(UNIVERSAL
::isa
($to,'GLOB') || UNIVERSAL
::isa
($to,'IO::Handle'))) {
} elsif (ref(\
$to) eq 'GLOB') {
open(TO
,"<$to") or goto fail_open2
;
binmode TO
unless $text_mode;
if (!$text_mode && $closefrom && $closeto) {
# If both are opened files we know they differ if their size differ
goto fail_inner
if $fromsize != -s TO
;
while (defined($fline = <FROM
>)) {
goto fail_inner
unless defined($tline = <TO
>);
# $size contains ref to comparison function
goto fail_inner
if &$size($fline, $tline);
goto fail_inner
if $fline ne $tline;
goto fail_inner
if defined($tline = <TO
>);
unless (defined($size) && $size > 0) {
$size = $fromsize || -s TO
|| 0;
$size = 1024 if $size < 512;
$size = $Too_Big if $size > $Too_Big;
my ($fr,$tr,$fbuf,$tbuf);
while(defined($fr = read(FROM
,$fbuf,$size)) && $fr > 0) {
unless (defined($tr = read(TO
,$tbuf,$fr)) && $tbuf eq $fbuf) {
goto fail_inner
if defined($tr = read(TO
,$tbuf,$size)) && $tr > 0;
close(TO
) || goto fail_open2
if $closeto;
close(FROM
) || goto fail_open1
if $closefrom;
# All of these contortions try to preserve error messages...
close(TO
) || goto fail_open2
if $closeto;
close(FROM
) || goto fail_open1
if $closefrom;
my ($from,$to,$cmp) = @_;
croak
("Usage: compare_text( file1, file2 [, cmp-function])")
unless @_ == 2 || @_ == 3;
croak
("Third arg to compare_text() function must be a code reference")
if @_ == 3 && ref($cmp) ne 'CODE';
# Using a negative buffer size puts compare into text_mode too
$cmp = -1 unless defined $cmp;
compare
($from, $to, $cmp);
File::Compare - Compare files or filehandles
if (compare("file1","file2") == 0) {
The File::Compare::compare function compares the contents of two
sources, each of which can be a file or a file handle. It is exported
from File::Compare by default.
File::Compare::cmp is a synonym for File::Compare::compare. It is
exported from File::Compare only by request.
File::Compare::compare_text does a line by line comparison of the two
files. It stops as soon as a difference is detected. compare_text()
accepts an optional third argument: This must be a CODE reference to
a line comparison function, which returns 0 when both lines are considered
compare_text($file1, $file2)
is basically equivalent to
compare_text($file1, $file2, sub {$_[0] ne $_[1]} )
File::Compare::compare and its sibling functions return 0 if the files
are equal, 1 if the files are unequal, or -1 if an error was encountered.
File::Compare was written by Nick Ing-Simmons.
Its original documentation was written by Chip Salzenberg.