This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
The latest JPL from the anoncvs.
[perl5.git] / lib / File / Compare.pm
1 package File::Compare;
2
3 use 5.6.0;
4 use strict;
5 use warnings;
6 our($VERSION, @ISA, @EXPORT, @EXPORT_OK, $Too_Big);
7
8 require Exporter;
9 use Carp;
10
11 $VERSION = '1.1002';
12 @ISA = qw(Exporter);
13 @EXPORT = qw(compare);
14 @EXPORT_OK = qw(cmp compare_text);
15
16 $Too_Big = 1024 * 1024 * 2;
17
18 sub VERSION {
19     # Version of File::Compare
20     return $File::Compare::VERSION;
21 }
22
23 sub compare {
24     croak("Usage: compare( file1, file2 [, buffersize]) ")
25       unless(@_ == 2 || @_ == 3);
26
27     my ($from,$to,$size) = @_;
28     my $text_mode = defined($size) && (ref($size) eq 'CODE' || $size < 0);
29
30     my ($fromsize,$closefrom,$closeto);
31     local (*FROM, *TO);
32
33     croak("from undefined") unless (defined $from);
34     croak("to undefined") unless (defined $to);
35
36     if (ref($from) && 
37         (UNIVERSAL::isa($from,'GLOB') || UNIVERSAL::isa($from,'IO::Handle'))) {
38         *FROM = *$from;
39     } elsif (ref(\$from) eq 'GLOB') {
40         *FROM = $from;
41     } else {
42         open(FROM,"<$from") or goto fail_open1;
43         unless ($text_mode) {
44             binmode FROM;
45             $fromsize = -s FROM;
46         }
47         $closefrom = 1;
48     }
49
50     if (ref($to) &&
51         (UNIVERSAL::isa($to,'GLOB') || UNIVERSAL::isa($to,'IO::Handle'))) {
52         *TO = *$to;
53     } elsif (ref(\$to) eq 'GLOB') {
54         *TO = $to;
55     } else {
56         open(TO,"<$to") or goto fail_open2;
57         binmode TO unless $text_mode;
58         $closeto = 1;
59     }
60
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;
64     }
65
66     if ($text_mode) {
67         local $/ = "\n";
68         my ($fline,$tline);
69         while (defined($fline = <FROM>)) {
70             goto fail_inner unless defined($tline = <TO>);
71             if (ref $size) {
72                 # $size contains ref to comparison function
73                 goto fail_inner if &$size($fline, $tline);
74             } else {
75                 goto fail_inner if $fline ne $tline;
76             }
77         }
78         goto fail_inner if defined($tline = <TO>);
79     }
80     else {
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;
85         }
86
87         my ($fr,$tr,$fbuf,$tbuf);
88         $fbuf = $tbuf = '';
89         while(defined($fr = read(FROM,$fbuf,$size)) && $fr > 0) {
90             unless (defined($tr = read(TO,$tbuf,$fr)) && $tbuf eq $fbuf) {
91                 goto fail_inner;
92             }
93         }
94         goto fail_inner if defined($tr = read(TO,$tbuf,$size)) && $tr > 0;
95     }
96
97     close(TO) || goto fail_open2 if $closeto;
98     close(FROM) || goto fail_open1 if $closefrom;
99
100     return 0;
101     
102   # All of these contortions try to preserve error messages...
103   fail_inner:
104     close(TO) || goto fail_open2 if $closeto;
105     close(FROM) || goto fail_open1 if $closefrom;
106
107     return 1;
108
109   fail_open2:
110     if ($closefrom) {
111         my $status = $!;
112         $! = 0;
113         close FROM;
114         $! = $status unless $!;
115     }
116   fail_open1:
117     return -1;
118 }
119
120 sub cmp;
121 *cmp = \&compare;
122
123 sub compare_text {
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';
129
130     # Using a negative buffer size puts compare into text_mode too
131     $cmp = -1 unless defined $cmp;
132     compare($from, $to, $cmp);
133 }
134
135 1;
136
137 __END__
138
139 =head1 NAME
140
141 File::Compare - Compare files or filehandles
142
143 =head1 SYNOPSIS
144
145         use File::Compare;
146
147         if (compare("file1","file2") == 0) {
148             print "They're equal\n";
149         }
150
151 =head1 DESCRIPTION
152
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.
156
157 File::Compare::cmp is a synonym for File::Compare::compare.  It is
158 exported from File::Compare only by request.
159
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
164 equal. For example:
165
166     compare_text($file1, $file2)
167
168 is basically equivalent to
169
170     compare_text($file1, $file2, sub {$_[0] ne $_[1]} )
171
172 =head1 RETURN
173
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.
176
177 =head1 AUTHOR
178
179 File::Compare was written by Nick Ing-Simmons.
180 Its original documentation was written by Chip Salzenberg.
181
182 =cut
183