Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: file comparison

by Perlbotics (Archbishop)
on Jun 26, 2009 at 19:21 UTC ( [id://775147]=note: print w/replies, xml ) Need Help??


in reply to file comparison

You could compute and compare hash-digests (e.g. Digest::MD5) of files having identical size. The probability that two different files have the same digest is very low. If you decide for a bit-by-bit comparison, you should do that on rather big chunks of data. See sysread to get an idea.

Update: As a measure of precaution, you should also take care to really read the files from disk or network but not from the OS's file cache. Don't know how to do that using Windows, though. *nix has sync

Update2: As a response to this node below: You could try something along...

use strict; use Digest::MD5; sub get_md5 { my $file = shift; open (my $fh, '<', $file) or die "cannot open $file - $1"; binmode($fh); my $md5 = Digest::MD5->new; $md5->addfile($fh); close($fh) or die "cannot close $file - $!"; return $md5->hexdigest; # TODO: think about caching results... } sub files_equal_by_md5 { my ($file1, $file2) = @_; # files differ in size? return 0 if (-s $file1 != -s $file2); my $digest1 = get_md5($file1); my $digest2 = get_md5($file2); return $digest1 eq $digest2 ? 1 : 0; } die "usage: $0 file1 file2\n compares file1 and file 2\n" unless @ARGV +==2; print files_equal_by_md5($ARGV[0],$ARGV[1]) ? "files are equal" : "different files", "\n";
HTH

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://775147]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-04-20 02:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found