better version of change#3038
[p5sagit/p5-mst-13.2.git] / lib / File / Compare.pm
CommitLineData
5f05dabc 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 10@ISA = qw(Exporter);
11@EXPORT = qw(compare);
3724d6f4 12@EXPORT_OK = qw(cmp compare_text);
5f05dabc 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) = @_;
26 my $text_mode = defined($size) && $size < 0;
27
28 my ($fromsize,$closefrom,$closeto);
29 local (*FROM, *TO);
5f05dabc 30
31 croak("from undefined") unless (defined $from);
32 croak("to undefined") unless (defined $to);
33
d704f39a 34 if (ref($from) &&
35 (UNIVERSAL::isa($from,'GLOB') || UNIVERSAL::isa($from,'IO::Handle'))) {
5f05dabc 36 *FROM = *$from;
37 } elsif (ref(\$from) eq 'GLOB') {
38 *FROM = $from;
39 } else {
40 open(FROM,"<$from") or goto fail_open1;
3724d6f4 41 unless ($text_mode) {
42 binmode FROM;
43 $fromsize = -s FROM;
44 }
5f05dabc 45 $closefrom = 1;
46 }
47
d704f39a 48 if (ref($to) &&
49 (UNIVERSAL::isa($to,'GLOB') || UNIVERSAL::isa($to,'IO::Handle'))) {
5f05dabc 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 56 $closeto = 1;
57 }
58
3724d6f4 59 if (!$text_mode && $closefrom && $closeto) {
387d8d95 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 64 if ($text_mode) {
65 local $/ = "\n";
66 my ($fline,$tline);
67 while (defined($fline = <FROM>)) {
68 unless (defined($tline = <TO>) && $fline eq $tline) {
69 goto fail_inner;
70 }
71 }
72 goto fail_inner if defined($tline = <TO>);
5f05dabc 73 }
3724d6f4 74 else {
75 unless (defined($size) && $size > 0) {
76 $size = $fromsize;
77 $size = 1024 if $size < 512;
78 $size = $Too_Big if $size > $Too_Big;
79 }
5f05dabc 80
3724d6f4 81 my ($fr,$tr,$fbuf,$tbuf);
82 $fbuf = $tbuf = '';
83 while(defined($fr = read(FROM,$fbuf,$size)) && $fr > 0) {
84 unless (defined($tr = read(TO,$tbuf,$fr)) && $tbuf eq $fbuf) {
85 goto fail_inner;
86 }
5f05dabc 87 }
3724d6f4 88 goto fail_inner if defined($tr = read(TO,$tbuf,$size)) && $tr > 0;
5f05dabc 89 }
5f05dabc 90
91 close(TO) || goto fail_open2 if $closeto;
92 close(FROM) || goto fail_open1 if $closefrom;
93
94 return 0;
95
96 # All of these contortions try to preserve error messages...
97 fail_inner:
98 close(TO) || goto fail_open2 if $closeto;
99 close(FROM) || goto fail_open1 if $closefrom;
100
101 return 1;
102
103 fail_open2:
104 if ($closefrom) {
3724d6f4 105 my $status = $!;
5f05dabc 106 $! = 0;
107 close FROM;
108 $! = $status unless $!;
109 }
110 fail_open1:
111 return -1;
112}
113
114*cmp = \&compare;
115
3724d6f4 116# Using a negative buffer size puts compare into text_mode
117sub compare_text { compare(@_[0..1], -1) }
118
5f05dabc 1191;
120
121__END__
122
123=head1 NAME
124
125File::Compare - Compare files or filehandles
126
127=head1 SYNOPSIS
128
129 use File::Compare;
130
131 if (compare("file1","file2") == 0) {
132 print "They're equal\n";
133 }
134
135=head1 DESCRIPTION
136
137The File::Compare::compare function compares the contents of two
138sources, each of which can be a file or a file handle. It is exported
139from File::Compare by default.
140
141File::Compare::cmp is a synonym for File::Compare::compare. It is
142exported from File::Compare only by request.
143
3724d6f4 144File::Compare::compare_text does a line by line comparison of the two
145files. It stops as soon as a difference is detected.
146
5f05dabc 147=head1 RETURN
148
149File::Compare::compare return 0 if the files are equal, 1 if the
150files are unequal, or -1 if an error was encountered.
151
152=head1 AUTHOR
153
154File::Compare was written by Nick Ing-Simmons.
155Its original documentation was written by Chip Salzenberg.
156
157=cut
158