This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
allow custom comparison function in File::Compare::compare_text()
[perl5.git] / lib / File / Compare.pm
CommitLineData
5f05dabc
PP
1package File::Compare;
2
3use strict;
4use vars qw($VERSION @ISA @EXPORT @EXPORT_OK $Too_Big *FROM *TO);
5
6require Exporter;
7use Carp;
5f05dabc 8
3724d6f4 9$VERSION = '1.1002';
5f05dabc
PP
10@ISA = qw(Exporter);
11@EXPORT = qw(compare);
3724d6f4 12@EXPORT_OK = qw(cmp compare_text);
5f05dabc
PP
13
14$Too_Big = 1024 * 1024 * 2;
15
16sub VERSION {
17 # Version of File::Compare
18 return $File::Compare::VERSION;
19}
20
21sub compare {
22 croak("Usage: compare( file1, file2 [, buffersize]) ")
23 unless(@_ == 2 || @_ == 3);
24
3724d6f4 25 my ($from,$to,$size) = @_;
167e09eb 26 my $text_mode = defined($size) && (ref($size) eq 'CODE' || $size < 0);
3724d6f4
JD
27
28 my ($fromsize,$closefrom,$closeto);
29 local (*FROM, *TO);
5f05dabc
PP
30
31 croak("from undefined") unless (defined $from);
32 croak("to undefined") unless (defined $to);
33
d704f39a
G
34 if (ref($from) &&
35 (UNIVERSAL::isa($from,'GLOB') || UNIVERSAL::isa($from,'IO::Handle'))) {
5f05dabc
PP
36 *FROM = *$from;
37 } elsif (ref(\$from) eq 'GLOB') {
38 *FROM = $from;
39 } else {
40 open(FROM,"<$from") or goto fail_open1;
3724d6f4
JD
41 unless ($text_mode) {
42 binmode FROM;
43 $fromsize = -s FROM;
44 }
5f05dabc
PP
45 $closefrom = 1;
46 }
47
d704f39a
G
48 if (ref($to) &&
49 (UNIVERSAL::isa($to,'GLOB') || UNIVERSAL::isa($to,'IO::Handle'))) {
5f05dabc
PP
50 *TO = *$to;
51 } elsif (ref(\$to) eq 'GLOB') {
52 *TO = $to;
53 } else {
54 open(TO,"<$to") or goto fail_open2;
3724d6f4 55 binmode TO unless $text_mode;
5f05dabc
PP
56 $closeto = 1;
57 }
58
3724d6f4 59 if (!$text_mode && $closefrom && $closeto) {
387d8d95
CS
60 # If both are opened files we know they differ if their size differ
61 goto fail_inner if $fromsize != -s TO;
62 }
63
3724d6f4
JD
64 if ($text_mode) {
65 local $/ = "\n";
66 my ($fline,$tline);
67 while (defined($fline = <FROM>)) {
167e09eb
JD
68 goto fail_inner unless defined($tline = <TO>);
69 if (ref $size) {
70 # $size contains ref to comparison function
71 goto fail_inner if &$size($fline, $tline);
72 } else {
73 goto fail_inner if $fline ne $tline;
3724d6f4
JD
74 }
75 }
76 goto fail_inner if defined($tline = <TO>);
5f05dabc 77 }
3724d6f4
JD
78 else {
79 unless (defined($size) && $size > 0) {
80 $size = $fromsize;
81 $size = 1024 if $size < 512;
82 $size = $Too_Big if $size > $Too_Big;
83 }
5f05dabc 84
3724d6f4
JD
85 my ($fr,$tr,$fbuf,$tbuf);
86 $fbuf = $tbuf = '';
87 while(defined($fr = read(FROM,$fbuf,$size)) && $fr > 0) {
88 unless (defined($tr = read(TO,$tbuf,$fr)) && $tbuf eq $fbuf) {
89 goto fail_inner;
90 }
5f05dabc 91 }
3724d6f4 92 goto fail_inner if defined($tr = read(TO,$tbuf,$size)) && $tr > 0;
5f05dabc 93 }
5f05dabc
PP
94
95 close(TO) || goto fail_open2 if $closeto;
96 close(FROM) || goto fail_open1 if $closefrom;
97
98 return 0;
99
100 # All of these contortions try to preserve error messages...
101 fail_inner:
102 close(TO) || goto fail_open2 if $closeto;
103 close(FROM) || goto fail_open1 if $closefrom;
104
105 return 1;
106
107 fail_open2:
108 if ($closefrom) {
3724d6f4 109 my $status = $!;
5f05dabc
PP
110 $! = 0;
111 close FROM;
112 $! = $status unless $!;
113 }
114 fail_open1:
115 return -1;
116}
117
118*cmp = \&compare;
119
167e09eb
JD
120sub compare_text {
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';
126
127 # Using a negative buffer size puts compare into text_mode too
128 $cmp = -1 unless defined $cmp;
129 compare($from, $to, $cmp);
130}
3724d6f4 131
5f05dabc
PP
1321;
133
134__END__
135
136=head1 NAME
137
138File::Compare - Compare files or filehandles
139
140=head1 SYNOPSIS
141
142 use File::Compare;
143
144 if (compare("file1","file2") == 0) {
145 print "They're equal\n";
146 }
147
148=head1 DESCRIPTION
149
150The File::Compare::compare function compares the contents of two
151sources, each of which can be a file or a file handle. It is exported
152from File::Compare by default.
153
154File::Compare::cmp is a synonym for File::Compare::compare. It is
155exported from File::Compare only by request.
156
3724d6f4 157File::Compare::compare_text does a line by line comparison of the two
167e09eb
JD
158files. It stops as soon as a difference is detected. compare_text()
159accepts an optional third argument: This must be a CODE reference to
160a line comparison function, which returns 0 when both lines are considered
161equal. For example:
162
163 compare_text($file1, $file2)
164
165is basically equivalent to
166
167 compare_text($file1, $file2, sub {$_[0] ne $_[1]} )
3724d6f4 168
5f05dabc
PP
169=head1 RETURN
170
171File::Compare::compare return 0 if the files are equal, 1 if the
172files are unequal, or -1 if an error was encountered.
173
174=head1 AUTHOR
175
176File::Compare was written by Nick Ing-Simmons.
177Its original documentation was written by Chip Salzenberg.
178
179=cut
180