Perl find size of file with Checksum ( unpack)

Posted on 2012-08-17
Last Modified: 2012-08-17
I need to find and some some file sizes from a perl script there is some issues with this compare using just the files size "$x = -s $y;

I need to use Checksum to do an unpack to get what I need:

For example I can get the size I need from command line such as:

cat file.text | perl -e '$/=undef;print q{Checksum is }, (unpack(q{%32c*},<>) % 65535), qq{\n}'

Open in new window

But don't seem to be putting this in the script to work correctly.

How can I do this from a script if I have a file in the script such as:

$sum = '/tmp/file1.txt';

Question by:bt707

    Author Comment

    From perldoc -f unpack:

            For example, the following computes the same number as the
            System V sum program:

                $checksum = do {
                    local $/;  # slurp!
                    unpack("%32W*",<>) % 65535;

    I could use this but can't seem to put in what I want is to check?

    The size of:
    $sum = '/tmp/file1.txt';

    Do I need to open and read $sum the put it in the check somehow?

    LVL 28

    Expert Comment

    Why not use one of the many available digest modules.

    Here are a few commonly used digest modules used to calculate a checksum.
    Digest::CRC - Generic CRC functions
    Digest::MD5 - Perl interface to the MD5 Algorithm
    Digest::Adler32::XS - Generate Adler32 digests efficiently
    LVL 28

    Expert Comment


    Author Comment


    I need a module that is already on the server, it takes forever to get a change request approved just to update a new module to the server.

    I checked those, so do see one that is on the server, let me see what I need to do with that one and see if it gives what I need.

    perl -MDigest::MD5  -e 'print $Digest::MD5::VERSION'

    LVL 28

    Accepted Solution

    Digest::MD5 should do what you need, but you could also look at:
    Digest - Modules that calculate message digests
    Digest::SHA - Perl extension for SHA-1/224/256/384/512

    That are both core modules so they should already be installed on your server, assuming you have a recent perl version.

    Digest was first released with perl v5.7.3

    Digest::SHA was first released with perl v5.9.3

    Author Closing Comment

    I have:

    This is perl, v5.8.4 built for sun4-solaris-64int

    so should have what you pointed out, just got tied up with other issues, but it looks like the info you gave should do just what I need.


    Featured Post

    Live: Real-Time Solutions, Start Here

    Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

    Join & Write a Comment

    Suggested Solutions

    Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
    A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (,  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    25 Experts available now in Live!

    Get 1:1 Help Now