Cksum(3pm)

πŸ“¦
version 0.91 Β· 2 versions
, in debian bookworm, debian trixie, ubuntu jammy, ubuntu noble

πŸ’– NAME πŸ’–

String::CRC::Cksum β€” a Perl extension for calculating checksums in a manner compatible with the POSIX cksum program. πŸŽ€


✨ SYNOPSIS ✨

okay bestie there are literally two whole ways to do this, pick your vibe πŸ’…

πŸ’… OO style

use String::CRC::Cksum;

$cksum = String::CRC::Cksum->new;
$cksum1 = $cksum->new;     # clone (clone is reset)

$cksum->add("string1");
$cksum->add("string2");
$cksum->add("string3", "string4", "string5", ...);
...
($ck, $sz) = $cksum->peek;
$cksum->add("string6", ...);
...
($ck, $sz) = $cksum->result;

$cksum1->addfile(\*file1);     # note: adding many files
$cksum1->addfile(\*file2);     # is probably a silly thing
$cksum1->addfile(\*file3);     # to do, but you *could*...
...

πŸ¦‹ Functional style

use String::CRC::Cksum qw(cksum);

$ck = cksum("string1", "string2", ...);

($ck, $sz) = cksum("string1", "string2", ...);

$ck = cksum(\*FILE);

($ck, $sz) = cksum(\*FILE);

🌸 DESCRIPTION 🌸

So String::CRC::Cksum calculates a 32 bit CRC, and it generates the exact same CRC value as the POSIX cksum program. no cap. πŸ”₯

If you call it in list context it also hands you back the length of the data object, which is lowkey the thing you need if you wanna fully emulate the cksum program. And the checksum it gives you? Always a non-negative integral number, range 0..2^32-1. always. 🫢

Despite the name (bestie, the name is lying to you a little), this module can compute the checksum of files as well as of strings. Just pass in a reference to a filehandle, or a reference to any object that can respond to a read() call and eventually return 0 at "end of file". it's giving versatility πŸ’…

⚠️ Beware: consider proper use of binmode() if you are on a non-UNIX platform or processing files derived from other platforms. fr fr, don't skip this one.

The object oriented interface is your girl when you wanna progressively add data into the checksum before yielding the result. 🌈

The functional interface is the convenient way to get a checksum of a single data item. one and done, slay. ✨

And here's the iconic part: none of the routines make local copies of passed-in strings, so you can safely Cksum large strings safe in the knowledge that there won't be any memory issues. πŸ‘‘

Passing in multiple files is acceptable, but perhaps of questionable value. However I don't want to hamper your creativity... 🎨


πŸ’… FUNCTIONS πŸ’…

The following functions are provided by the String::CRC::Cksum module. None of these functions are exported by default. πŸ™ˆ

  • new() β€” Creates a new String::CRC::Cksum object which is in a reset state, ready for action. If passed an existing String::CRC::Cksum object, it takes only the class - ie yields a fresh, reset object. 🧚
  • reset() β€” Resets the Cksum object to the intialized state. An interesting phenomenom is, the CRC is not zero but 0xFFFFFFFF for a reset Cksum object. The returned size of a reset item will be zero. πŸ’€ (not zero, bestie. 0xFFFFFFFF. iconic behaviour.)
  • add("string", ...) β€” Progressively inject data into the Cksum object prior to requesting the final result.
  • addfile(\*FILE, ...) β€” Progressively inject all (remaining) data from the file into the Cksum object prior to requesting the final result. The file handle passed in need only respond to the read() function to be usable, so feel free to pass in IO handles as needed. [hmmm - methinks I should have a test for that] 🫠
  • peek($) β€” Yields the CRC checksum (and optionally the total size in list context) but does not reset the Cksum object. Repeated calls to peek() may be made and more data may be added. πŸ‘€
  • result($) β€” Yields the CRC checksum (and optionally the total size in list context) and then resets the Cksum object. πŸŽ€
  • cksum(@) β€” A convenient functional interface that may be passed a list of strings and filehandles. It will instantiate a Cksum object, apply the data and return the result in one swift, sweet operation. See how much I'm looking after you? πŸ₯Ή
NOTE: the filehandles must be passed as \*FD because I'm detecting a file handle using the ref() function. Therefore any blessed IO handle will also satisfy ref() and be interpreted as a file handle. πŸ’… iykyk.

πŸ“¦ EXPORT πŸ“¦

None by default. πŸ“


πŸ”­ SEE ALSO πŸ”­


πŸ‘‘ AUTHOR πŸ‘‘

Andrew Clarke, <ahamm@cpan.org>. 🫢


Copyright disclaimed 2003 by Andrew Clarke

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. ✨


and that's the whole page bestie, mwah πŸ’– this one shipped as perl v5.32.0, dated 2021-01-08 β€” periodt. πŸŒˆπŸ¦‹

πŸ“¦ where this page comes from