Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

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!

Replies are listed 'Best First'.
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!

      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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://688232]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2017-02-21 20:20 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (318 votes). Check out past polls.