Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

File::Compare Binary Files

by ddexter (Initiate)
on May 23, 2008 at 22:03 UTC ( #688232=perlquestion: print w/ replies, xml ) Need Help??
ddexter has asked for the wisdom of the Perl Monks concerning the following question:

Hi everyone,
My question is about the functionality of the File::Compare module.

If I do

compare("file1", "file2");

(where file1 and file2 are binary files) will the compare function check to see if the binary files are actually the same, or will it just compare the characters in the file?

If compare doesn't have the functionality I'm looking for, I have been considering

if(system("diff file1 file2 > /dev/null") == 0) {they are the same...}

Any suggestions about a better way are also appreciated. Thank you!

Comment on File::Compare Binary Files
Select or Download Code
Re: File::Compare Binary Files
by moritz (Cardinal) on May 23, 2008 at 22:55 UTC
    Looking at the source reveals that File::Compare actually does a binary comparison (and the doc states that there is a separate compare_text sub). So use that.

    BTW diff is also text-oriented, for binary files you'd use the unix utility cmp instead.

Re: File::Compare Binary Files
by skx (Parson) on May 24, 2008 at 11:18 UTC

    This is a suggestion of another method, rather than a comment on the module you mention. When I want to compare (binary) files I do two things:

    • Use stat to get the filesize.
    • Use Digest::SHA1 to get a checksum of the file contents.

    Sure collisions are possible, but for my cases I find that if the SHA1 hash and file sizes match then the files are identical enough!

    Steve
    --
      If you just want to compare two files (and no n-way comparisons) File::Compare can be much faster than calculating a hash because it can abort at the first difference, while the hash algorithm always has to go through both files entirely.
Re: File::Compare Binary Files
by ddexter (Initiate) on May 27, 2008 at 16:49 UTC
    Thank you all for your help!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://688232]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (13)
As of 2014-10-22 15:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (119 votes), past polls