X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FFile%2FCompare.pm;h=0b73d7c76570925aea187ba67642eb42091f0171;hb=d84c672d23e941bae9d05e88fa42ae466c892856;hp=0ee84bdba5a99ac348baf500e421f63aad3ad9d2;hpb=3724d6f49c146fc4418f91f2550cb38405d01c90;p=p5sagit%2Fp5-mst-13.2.git diff --git a/lib/File/Compare.pm b/lib/File/Compare.pm index 0ee84bd..0b73d7c 100644 --- a/lib/File/Compare.pm +++ b/lib/File/Compare.pm @@ -1,29 +1,26 @@ package File::Compare; +use 5.006; use strict; -use vars qw($VERSION @ISA @EXPORT @EXPORT_OK $Too_Big *FROM *TO); +use warnings; +our($VERSION, @ISA, @EXPORT, @EXPORT_OK, $Too_Big); require Exporter; use Carp; -$VERSION = '1.1002'; +$VERSION = '1.1003'; @ISA = qw(Exporter); @EXPORT = qw(compare); @EXPORT_OK = qw(cmp compare_text); $Too_Big = 1024 * 1024 * 2; -sub VERSION { - # Version of File::Compare - return $File::Compare::VERSION; -} - sub compare { croak("Usage: compare( file1, file2 [, buffersize]) ") unless(@_ == 2 || @_ == 3); my ($from,$to,$size) = @_; - my $text_mode = defined($size) && $size < 0; + my $text_mode = defined($size) && (ref($size) eq 'CODE' || $size < 0); my ($fromsize,$closefrom,$closeto); local (*FROM, *TO); @@ -65,15 +62,19 @@ sub compare { local $/ = "\n"; my ($fline,$tline); while (defined($fline = )) { - unless (defined($tline = ) && $fline eq $tline) { - goto fail_inner; + goto fail_inner unless defined($tline = ); + if (ref $size) { + # $size contains ref to comparison function + goto fail_inner if &$size($fline, $tline); + } else { + goto fail_inner if $fline ne $tline; } } goto fail_inner if defined($tline = ); } else { unless (defined($size) && $size > 0) { - $size = $fromsize; + $size = $fromsize || -s TO || 0; $size = 1024 if $size < 512; $size = $Too_Big if $size > $Too_Big; } @@ -111,10 +112,20 @@ sub compare { return -1; } +sub cmp; *cmp = \&compare; -# Using a negative buffer size puts compare into text_mode -sub compare_text { compare(@_[0..1], -1) } +sub compare_text { + my ($from,$to,$cmp) = @_; + croak("Usage: compare_text( file1, file2 [, cmp-function])") + unless @_ == 2 || @_ == 3; + croak("Third arg to compare_text() function must be a code reference") + if @_ == 3 && ref($cmp) ne 'CODE'; + + # Using a negative buffer size puts compare into text_mode too + $cmp = -1 unless defined $cmp; + compare($from, $to, $cmp); +} 1; @@ -142,12 +153,21 @@ File::Compare::cmp is a synonym for File::Compare::compare. It is exported from File::Compare only by request. File::Compare::compare_text does a line by line comparison of the two -files. It stops as soon as a difference is detected. +files. It stops as soon as a difference is detected. compare_text() +accepts an optional third argument: This must be a CODE reference to +a line comparison function, which returns 0 when both lines are considered +equal. For example: + + compare_text($file1, $file2) + +is basically equivalent to + + compare_text($file1, $file2, sub {$_[0] ne $_[1]} ) =head1 RETURN -File::Compare::compare return 0 if the files are equal, 1 if the -files are unequal, or -1 if an error was encountered. +File::Compare::compare and its sibling functions return 0 if the files +are equal, 1 if the files are unequal, or -1 if an error was encountered. =head1 AUTHOR