Cksum(3pm)
π 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 but0xFFFFFFFFfor 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 theread()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 topeek()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\*FDbecause I'm detecting a file handle using theref()function. Therefore any blessed IO handle will also satisfyref()and be interpreted as a file handle. π iykyk.
π¦ EXPORT π¦
None by default. π
π SEE ALSO π
- manpages:
cksum(1)orcksum(C)depending on your flavour of UNIX. http://www.opengroup.org/onlinepubs/007904975/utilities/cksum.html
π AUTHOR π
Andrew Clarke, <ahamm@cpan.org>. π«Ά
π COPYRIGHT AND LICENSE π
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
- debian bookworm: libstring-crc-cksum-perl 0.91-1.1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libstring-crc-cksum-perl 0.91-1.1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu jammy: libstring-crc-cksum-perl 0.91-1.1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libstring-crc-cksum-perl 0.91-1.1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms