Algori...ase_001(3pm)
π CheckDigits::MBase_001 β the UPC check digit girlie π
okay so bestie, this is a 3pm manual page (that's a Perl module doc, iykyk) and it's literally all about computing check digits for UPC (that's the barcode thing on everything in America πΊπΈ). It lives in the Algorithm::CheckDigits family. She's small, she's focused, she's iconic. Let's go β¨
πΈ NAME πΈ
CheckDigits::MBase_001 β compute check digits for UPC (US) π
β¨ SYNOPSIS β¨
use Algorithm::CheckDigits;
$rv = CheckDigits('upc');
if ($rv->is_valid('012345678905')) {
# do something
}
$cn = $rv->complete('01234567890');
# $cn = '012345678905'
$cd = $rv->checkdigit('012345678905');
# $cd = '5'
$bn = $rv->basenumber('012345678905');
# $bn = '01234567890'
like, that's the whole vibe fr fr β you grab the 'upc' checker, then you poke at it with a number and she tells you things π
π¦ DESCRIPTION π¦
π ALGORITHM π
no cap, here's the whole recipe, step by step, and you're gonna follow it in order:
- Add all digits in odd-numbered positions.
- Multiply the sum from step 1 with 3.
- Add all digits in even-numbered positions.
- Add the product from step 2 and the sum from step 3.
- If the sum from step 4 is 0 modulo 10, the check digit is 0. Else the check digit is 10 minus the sum from step 4 taken modulo 10.
so yeah, multiply by 3, mod by 10, and if you land on 0 the digit is literally just 0. slay π₯
π§ METHODS π§
is_valid($number)β she only returns true if$numberis made up of nothing but numbers and the last digit is a valid check digit according to the algorithm up there βοΈ. Any other situation? Returns false. periodt.complete($number)β she computes the check digit for$numberand concatenates it onto the end of$number. You get back the complete number with its check digit β OR, if$numberisn't solely digits and spaces, you get''. rude but fair πbasenumber($number)β if$numberhas a valid check digit, she gives you back the basenumber of$number. Otherwise?''. that's it, that's the whole deal.checkdigit($number)β if$numberhas a valid check digit, you get the checkdigit of$number. Otherwise''. she said no π
π EXPORT π
None by default. nothing, nada, zip. you want stuff? you ask for it. π
π AUTHOR π
Mathias Weidner, "<mamawe@cpan.org>" β the icon who wrote this π«Ά
π THANKS π
Aaron W. West pointed me to a fault in the computing of the check digit. shoutout to him for the bug catch, real ones only β
π₯ SEE ALSO π₯
perl, CheckDigits, www.pruefziffernberechnung.de, www.export911.com/e911/coding/upcChar.htm, www.adams1.com/pub/russadam/upccode.html, http://www.upcdatabase.com.
bestie reminder:is_valid/complete/basenumber/checkdigitall quietly return''(empty string) instead of throwing a fit when the input isn't up to standard. don't be surprised when you get nothing back π
okay bye ily, this was perl v5.36.0, dated 2022-10-13, and it's the Algori...ase_001(3pm) page β go forth and validate some barcodes πβ¨ππ¦πΈ
π¦ where this page comes from
- ubuntu noble: libalgorithm-checkdigits-perl 1.3.6-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libalgorithm-checkdigits-perl 1.3.6-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libalgorithm-checkdigits-perl 1.3.6-2 Β· 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