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