<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package File::Compare 1.1008;

use v5.12;
use warnings;

use Exporter 'import';

our @EXPORT = qw(compare);
our @EXPORT_OK = qw(cmp compare_text);

our $Too_Big = 1024 * 1024 * 2;

sub croak {
    require Carp;
    goto &amp;Carp::croak;
}

sub compare {
    croak("Usage: compare( file1, file2 [, buffersize]) ")
      unless(@_ == 2 || @_ == 3);

    my ($from, $to, $size) = @_;
    my $text_mode = defined($size) &amp;&amp; (ref($size) eq 'CODE' || $size &lt; 0);

    my ($fromsize, $closefrom, $closeto);
    local (*FROM, *TO);

    croak("from undefined") unless (defined $from);
    croak("to undefined") unless (defined $to);

    if (ref($from) &amp;&amp; 
        (UNIVERSAL::isa($from, 'GLOB') || UNIVERSAL::isa($from, 'IO::Handle'))) {
	*FROM = *$from;
    } elsif (ref(\$from) eq 'GLOB') {
	*FROM = $from;
    } else {
	open(FROM, '&lt;', $from) or goto fail_open1;
	unless ($text_mode) {
	    binmode FROM;
	    $fromsize = -s FROM;
	}
	$closefrom = 1;
    }

    if (ref($to) &amp;&amp;
        (UNIVERSAL::isa($to, 'GLOB') || UNIVERSAL::isa($to, 'IO::Handle'))) {
	*TO = *$to;
    } elsif (ref(\$to) eq 'GLOB') {
	*TO = $to;
    } else {
	open(TO, '&lt;', $to) or goto fail_open2;
	binmode TO unless $text_mode;
	$closeto = 1;
    }

    if (!$text_mode &amp;&amp; $closefrom &amp;&amp; $closeto) {
	# If both are opened files we know they differ if their size differ
	goto fail_inner if $fromsize != -s TO;
    }

    if ($text_mode) {
	local $/ = "\n";
	my ($fline, $tline);
	while (defined($fline = &lt;FROM&gt;)) {
	    goto fail_inner unless defined($tline = &lt;TO&gt;);
	    if (ref $size) {
		# $size contains ref to comparison function
		goto fail_inner if &amp;$size($fline, $tline);
	    } else {
		goto fail_inner if $fline ne $tline;
	    }
	}
	goto fail_inner if defined($tline = &lt;TO&gt;);
    }
    else {
	unless (defined($size) &amp;&amp; $size &gt; 0) {
	    $size = $fromsize || -s TO || 0;
	    $size = 1024 if $size &lt; 512;
	    $size = $Too_Big if $size &gt; $Too_Big;
	}

	my ($fr, $tr, $fbuf, $tbuf);
	$fbuf = $tbuf = '';
	while(defined($fr = read(FROM, $fbuf, $size)) &amp;&amp; $fr &gt; 0) {
	    unless (defined($tr = read(TO, $tbuf, $fr)) &amp;&amp; $tbuf eq $fbuf) {
		goto fail_inner;
	    }
	}
	goto fail_inner if defined($tr = read(TO, $tbuf, $size)) &amp;&amp; $tr &gt; 0;
    }

    close(TO) || goto fail_open2 if $closeto;
    close(FROM) || goto fail_open1 if $closefrom;

    return 0;
    
  # All of these contortions try to preserve error messages...
  fail_inner:
    close(TO) || goto fail_open2 if $closeto;
    close(FROM) || goto fail_open1 if $closefrom;

    return 1;

  fail_open2:
    if ($closefrom) {
	my $status = $!;
	$! = 0;
	close FROM;
	$! = $status unless $!;
    }
  fail_open1:
    return -1;
}

sub cmp;
*cmp = \&amp;compare;

sub compare_text {
    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 &amp;&amp; ref($cmp) ne 'CODE';

    # Using a negative buffer size puts compare into text_mode too
    compare($from, $to, $cmp // -1);
}

1;

__END__

=head1 NAME

File::Compare - Compare files or filehandles

=head1 SYNOPSIS

  	use File::Compare;

	if (compare("file1", "file2") == 0) {
	    print "They're equal\n";
	}

=head1 DESCRIPTION

The C&lt;File::Compare::compare&gt; function compares the contents of two
sources, each of which can be a file or a file handle.  It is exported
from C&lt;File::Compare&gt; by default.

C&lt;File::Compare::cmp&gt; is a synonym for C&lt;File::Compare::compare&gt;.  It is
exported from C&lt;File::Compare&gt; only by request.

C&lt;File::Compare::compare_text&gt; does a line by line comparison of the two
files. It stops as soon as a difference is detected. C&lt;compare_text()&gt;
accepts an optional third argument: This must be a CODE reference to
a line comparison function, which returns C&lt;0&gt; when both lines are considered
equal. For example:

    compare_text($file1, $file2)

is basically equivalent to

    compare_text($file1, $file2, sub {$_[0] ne $_[1]} )

=head1 RETURN

C&lt;File::Compare::compare&gt; and its sibling functions return C&lt;0&gt; if the files
are equal, C&lt;1&gt; if the files are unequal, or C&lt;-1&gt; if an error was encountered.

=head1 AUTHOR

C&lt;File::Compare&gt; was written by Nick Ing-Simmons.
Its original documentation was written by Chip Salzenberg.
</pre></body></html>