🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.perl.misc
Article #287

Re: Calculate a file's checksum with Perl?

#287
From: David Cassell
Date: Thu, 20 Jan 2000 00:00
45 lines
2393 bytes
Alfonso PALACIOS SUREDA wrote:
> 
> I would like to calculate a file's checsum using Perl. Ine the Perl doc
> they say:
> 
> ==========================
> For example, the following computes the same number as the System V sum
> program:
> while (<>) {
>   $checksum += unpack("%16C*", $_);
> }
> $checksum %= 65536;
> ==========================
> 
> But when I try this, the checksum I get is slightly different from the
> one I get whith the "sum" command on my Solaris platform.

It should be slightly different.  This has at least one typo in it, and
has been changed in later docs.  You may want to be sure in future to 
read the online docs which come with perl, instead of docs in books,
however authoritative the title may sound.

I believe it is the GNU checksum which is emulated by the above, with
the exception that the '16' should really be a '32'.

> - Can I still use this number as a checksum, like System V's?

Yes, you just can't compare it directly to the SysV checksum.

> - What do the 'pack' and 'unpack' functions do, anyway? They don't say
> much in the Perl doc.

There's about 2 pages on pack() and about 1.5 pages on unpack() in
my documentation.  Do you want more than that?

Okay, the docs are pretty sparse if you've never seen these before in
another programming language.  pack() let you take a bunch of values 
and shove them into a string.. and unpack does the reverse.  Try a
few examples and see what you get.

David
-- 
David Cassell, OAO Corp.                      cassell@mail.cor.epa.gov
Senior Computing Specialist
mathematical statistician
Technical Details
Message-ID: <3887A587.7C9C7531@mail.cor.epa.gov>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <388720EF.D7364F8@alcatel.fr>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 112333,e5f5de2a0ef43657
X-Google-Attributes: gid112333,public
X-Google-Thread: fa0ae,e5f5de2a0ef43657
X-Google-Attributes: gidfa0ae,public
From: David Cassell <cassell@mail.cor.epa.gov>
Subject: Re: Calculate a file's checksum with Perl?
Date: 2000/01/20
Message-ID: <3887A587.7C9C7531@mail.cor.epa.gov>
X-Deja-AN: 575503063
Content-Transfer-Encoding: 7bit
References: <388720EF.D7364F8@alcatel.fr>
Content-Type: text/plain; charset=us-ascii
X-Complaints-To: abuse@nero.net
X-Trace: news.NERO.NET 948413829 4260 204.47.148.179 (21 Jan 2000 00:17:09 GMT)
Organization: OAO Corp.
Mime-Version: 1.0
NNTP-Posting-Date: 21 Jan 2000 00:17:09 GMT
Newsgroups: comp.lang.perl.misc,fr.comp.lang.perl