Commit | Line | Data |
5f05dabc |
1 | package File::Compare; |
2 | |
3b825e41 |
3 | use 5.006; |
5f05dabc |
4 | use strict; |
b395063c |
5 | use warnings; |
17f410f9 |
6 | our($VERSION, @ISA, @EXPORT, @EXPORT_OK, $Too_Big); |
5f05dabc |
7 | |
8 | require Exporter; |
9 | use Carp; |
5f05dabc |
10 | |
d6a466d7 |
11 | $VERSION = '1.1003'; |
5f05dabc |
12 | @ISA = qw(Exporter); |
13 | @EXPORT = qw(compare); |
3724d6f4 |
14 | @EXPORT_OK = qw(cmp compare_text); |
5f05dabc |
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 | |
3724d6f4 |
27 | my ($from,$to,$size) = @_; |
167e09eb |
28 | my $text_mode = defined($size) && (ref($size) eq 'CODE' || $size < 0); |
3724d6f4 |
29 | |
30 | my ($fromsize,$closefrom,$closeto); |
31 | local (*FROM, *TO); |
5f05dabc |
32 | |
33 | croak("from undefined") unless (defined $from); |
34 | croak("to undefined") unless (defined $to); |
35 | |
d704f39a |
36 | if (ref($from) && |
37 | (UNIVERSAL::isa($from,'GLOB') || UNIVERSAL::isa($from,'IO::Handle'))) { |
5f05dabc |
38 | *FROM = *$from; |
39 | } elsif (ref(\$from) eq 'GLOB') { |
40 | *FROM = $from; |
41 | } else { |
42 | open(FROM,"<$from") or goto fail_open1; |
3724d6f4 |
43 | unless ($text_mode) { |
44 | binmode FROM; |
45 | $fromsize = -s FROM; |
46 | } |
5f05dabc |
47 | $closefrom = 1; |
48 | } |
49 | |
d704f39a |
50 | if (ref($to) && |
51 | (UNIVERSAL::isa($to,'GLOB') || UNIVERSAL::isa($to,'IO::Handle'))) { |
5f05dabc |
52 | *TO = *$to; |
53 | } elsif (ref(\$to) eq 'GLOB') { |
54 | *TO = $to; |
55 | } else { |
56 | open(TO,"<$to") or goto fail_open2; |
3724d6f4 |
57 | binmode TO unless $text_mode; |
5f05dabc |
58 | $closeto = 1; |
59 | } |
60 | |
3724d6f4 |
61 | if (!$text_mode && $closefrom && $closeto) { |
387d8d95 |
62 | # If both are opened files we know they differ if their size differ |
63 | goto fail_inner if $fromsize != -s TO; |
64 | } |
65 | |
3724d6f4 |
66 | if ($text_mode) { |
67 | local $/ = "\n"; |
68 | my ($fline,$tline); |
69 | while (defined($fline = <FROM>)) { |
167e09eb |
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; |
3724d6f4 |
76 | } |
77 | } |
78 | goto fail_inner if defined($tline = <TO>); |
5f05dabc |
79 | } |
3724d6f4 |
80 | else { |
81 | unless (defined($size) && $size > 0) { |
5c99b74d |
82 | $size = $fromsize || -s TO || 0; |
3724d6f4 |
83 | $size = 1024 if $size < 512; |
84 | $size = $Too_Big if $size > $Too_Big; |
85 | } |
5f05dabc |
86 | |
3724d6f4 |
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 | } |
5f05dabc |
93 | } |
3724d6f4 |
94 | goto fail_inner if defined($tr = read(TO,$tbuf,$size)) && $tr > 0; |
5f05dabc |
95 | } |
5f05dabc |
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) { |
3724d6f4 |
111 | my $status = $!; |
5f05dabc |
112 | $! = 0; |
113 | close FROM; |
114 | $! = $status unless $!; |
115 | } |
116 | fail_open1: |
117 | return -1; |
118 | } |
119 | |
17f410f9 |
120 | sub cmp; |
5f05dabc |
121 | *cmp = \&compare; |
122 | |
167e09eb |
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 | } |
3724d6f4 |
134 | |
5f05dabc |
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 | |
3724d6f4 |
160 | File::Compare::compare_text does a line by line comparison of the two |
167e09eb |
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]} ) |
3724d6f4 |
171 | |
5f05dabc |
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 | |