Algori...Digits(3pm)
π Algorithm::CheckDigits β the perl module that checks your digits (literally) π
okay bestie, sit DOWN, because this is Algorithm::CheckDigits, a Perl extension that generates and tests check digits, and it is genuinely one of the most useful little modules you'll ever meet. It ships as part of the User Contributed Perl Documentation (that's the (3pm) vibe β a perl manual page, perl v5.36.0 era), and it is so much less scary than the name makes it sound. π
β¨ NAME β¨
Algorithm::CheckDigits β a Perl extension to generate and test check digits. that's it, that's the whole job, and it does it flawlessly. π
πΈ SYNOPSIS πΈ
you can literally just run this from the command line to see what's up:
perl -MAlgorithm::CheckDigits -e Algorithm::CheckDigits::print_methods
or, y'know, actually use it like a normal person:
use Algorithm::CheckDigits;
@ml = Algorithm::CheckDigits->method_list();
%md = Algorithm::CheckDigits->method_descriptions();
$isbn = CheckDigits('ISBN');
if ($isbn->is_valid('3-930673-48-7')) {
# do something
}
$cn = $isbn->complete('3-930673-48'); # $cn = '3-930673-48-7'
$cd = $isbn->checkdigit('3-930673-48-7'); # $cd = '7'
$bn = $isbn->basenumber('3-930673-48-7'); # $bn = '3-930673-48'
look at that. it validates, it completes, it extracts. main character energy fr fr. π
π¦ ABSTRACT π¦
This module provides a bunch of methods to test and generate check digits. If you want even more info, go peek at the web site www.pruefziffernberechnung.de (it's in german β ja, genau, bestie π©πͺ).
π SUBROUTINES/METHODS π
CheckDigits($method)
Returns an object of an appropriate Algorithm::CheckDigits class for the given algorithm. π«Ά
β οΈ It dies with an error message if you call it with an unknown algorithm. so don't be messy.
See below for the available algorithms. Every object understands the following methods:
is_valid($number)β Returns true or false depending on whether$numbercontains / contains no valid check digit.complete($number)β Returns a string representation of$numbercompleted with the appropriate check digit.checkdigit($number)β Extracts the check digit from$numberif$numbercontains a valid check digit.basenumber($number)β Extracts the basenumber from$numberif$numbercontains a valid check digit.
Algorithm::CheckDigits::plug_in($module, $description, $prefkey)
Register a module that provides the same four methods as above. This function returns a handle with which the registered module can be instantiated. πβ¨
- The first argument
$moduleis the module to be used for this plugin. This argument is mandatory. Do not register'Algorithm::CheckDigits'. - The second argument is a short description of the algorithm. If it is omitted, the string
"algorithm of module $module"will be taken. - The third argument is the preferred key for this algorithm. The
"plug_in()"function does not guarantee to register the algorithm with this key. Instead it returns the key under which the algorithm is registered.
See "REGISTERING PLUGINS" for more information on registering plugins.
Algorithm::CheckDigits::method_list()
Returns a list of known methods for check digit computation. π
Algorithm::CheckDigits::method_descriptions()
Returns a hash of descriptions for the known methods for check digit computations. The keys of the hash are the values returned by "method_list()".
Algorithm::CheckDigits::print_methods()
Returns a list of known methods for check digit computation. π¨οΈ
You may use the following to find out which methods your version of Algorithm::CheckDigits provides and where to look for further information.
perl -MAlgorithm::CheckDigits -e Algorithm::CheckDigits::print_methods
CHECK SUM METHODS
At the moment these methods to compute check digits are provided: (vatrn = VAT Return Number, in german ustid = UmsatzSTeuer-ID)
euronoteβ European bank notes, seeAlgorithm::CheckDigits::M09_001. πΆamex,bahncard,diners,discover,enroute,eurocard,happydigits,isin,jcb,klubkarstadt,mastercard,miles&more,visa,imei,imeisvβ SeeAlgorithm::CheckDigits::M10_001.siren,siretβ SeeAlgorithm::CheckDigits::M10_002.ismnβ SeeAlgorithm::CheckDigits::M10_003.ean,iln,isbn13,nve,2aus5β SeeAlgorithm::CheckDigits::M10_004.identcode_dp,leitcode_dpβ SeeAlgorithm::CheckDigits::M10_005.rentenversicherungβ SeeAlgorithm::CheckDigits::M10_006.sedolβ SeeAlgorithm::CheckDigits::M10_008.betriebsnummerβ SeeAlgorithm::CheckDigits::M10_009.postscheckkontiβ SeeAlgorithm::CheckDigits::M10_010.upsβ SeeAlgorithm::CheckDigits::M10_011.hkid,isbn,issn,nhs_gb,ustid_pt,vat_sl,wagonnr_brβ SeeAlgorithm::CheckDigits::M11_001.pznβ SeeAlgorithm::CheckDigits::M11_002.pkzβ SeeAlgorithm::CheckDigits::M11_003.cpf,titulo_eleitorβ SeeAlgorithm::CheckDigits::M11_004.ccc_esβ SeeAlgorithm::CheckDigits::M11_006.ustid_fi,vatrn_fiβ SeeAlgorithm::CheckDigits::M11_007.ustid_dk,vatrn_dkβ SeeAlgorithm::CheckDigits::M11_008.nric_sgβ SeeAlgorithm::CheckDigits::M11_009.ahv_chβ SeeAlgorithm::CheckDigits::M11_010.ustid_nl,vatrn_nlβ SeeAlgorithm::CheckDigits::M11_011.bwpk_deβ SeeAlgorithm::CheckDigits::M11_012.ustid_gr,vatrn_grβ SeeAlgorithm::CheckDigits::M11_013.esr5_chβ SeeAlgorithm::CheckDigits::M11_015.ustid_pl,vatrn_plβ SeeAlgorithm::CheckDigits::M11_016.ecno,ec-no,einecs,elincsβ SeeAlgorithm::CheckDigits::M11_017.isanβ SeeAlgorithm::CheckDigits::M16_001.dni_esβ SeeAlgorithm::CheckDigits::M23_001.ustid_ie,vatrn_ieβ SeeAlgorithm::CheckDigits::M23_002.code_39β SeeAlgorithm::CheckDigits::M43_001.ustid_lu,vatrn_luβ SeeAlgorithm::CheckDigits::M89_001.ustid_be,vatrn_beβ SeeAlgorithm::CheckDigits::M97_001.ibanβ SeeAlgorithm::CheckDigits::M97_002.upcβ SeeAlgorithm::CheckDigits::MBase_001.blutbeutel,bzue_de,ustid_de,vatrn_deβ SeeAlgorithm::CheckDigits::MBase_002.siciβ SeeAlgorithm::CheckDigits::MBase_003.pa_deβ SeeAlgorithm::CheckDigits::MXX_001.casβ SeeAlgorithm::CheckDigits::MXX_002.demβ Old german bank notes (DEM), seeAlgorithm::CheckDigits::MXX_003. πͺustid_at,vatrn_atβ SeeAlgorithm::CheckDigits::MXX_004.esr9_chβ SeeAlgorithm::CheckDigits::MXX_005.verhoeffβ Verhoeff scheme, seeAlgorithm::CheckDigits::MXX_006orAlgorithm::Verhoeff.
π EXPORT π
This module exports the Function "CheckDigits()" that is used to create an instance of a checker with the given algorithm.
π§ REGISTERING PLUGINS π§
Brian T. Wightman was the first to ask me to add a plugin registry to Algorithm::CheckDigits, and so I added the function "plug_in()" that does just this β registering plug in modules to be used just like the modules in the distribution of this module. shoutout Brian, you ate. π«Ά
Providing some means to add additional algorithms without needing to change the module has the benefit that the user of those additional algorithms may easily use them with the same interface as the other algorithms, without having to wait for a new version that may or may not include the wanted algorithm. iconic, honestly.
But there's a problem, bestie: the user must be able to select the new algorithms like they did with the other ones. And the catch is β since these new algorithms are developed independently, there is no guarantee that no more than one module applies for the same handle. I could have implemented some simple strategies like last one wins (the module that registers last for a given handle is the one that is chosen) or first one wins (the first registered module is chosen). Instead I went for something more complex, to assure that every module that wants to get registered will be registered, and that every registered module will be accessible by the same handle as long as the program runs. To make this work, "plug_in()" sees the third argument only as a hint for how the handle should look like when a module is registered. It returns the real handle with which the algorithm can be instantiated. That means a developer of a plugin module cannot make the handle immediately available like I did for the modules in the distribution. Instead there should be something like a public variable or function that returns the handle as it came back from the "plug_in()" function.
This could go like this in the module:
package Algorithm::XyZ;
use Algorithm::CheckDigits;
our $xyz = Algorithm::CheckDigits::plug_in('Algorithm::XyZ',
'XyZ check digits',
'xyz');
And the user of this algorithm would write something like this:
use Algorithm::CheckDigits;
use Algorithm::XyZ;
my $cd = CheckDigits($Algorithm::XyZ::xyz);
if ($cd->is_valid($some_number)) {
# do something
}
Please have a look at the plugin tests in the test directory (t/plugin*.t) and the accompanying modules (t/PluginLib*.pm) for example usage. You may also try to load an additional module with the scripts in bin and cgi-bin, and look for the additional algorithms in the output:
perl -Ilib -It -MPluginLibA bin/checkdigits.pl list
perl -Ilib -It -MPluginLibA cgi-bin/checkdigits.cgi
Namespace
I would like to ask you to use any namespace below or outside, but not direct Algorithm::CheckDigits. That means for instance, for the XyZ algorithm, Algorithm::XyZ would be fine, Algorithm::CheckDigits::Plugin::XyZ or Algorithm::CheckDigits::X::XyZ would be fine too. But Algorithm::CheckDigits::XyZ could collide with some future version of the Algorithm::CheckDigits distribution, so please avoid this namespace. π no drama, just don't do it.
π SEE ALSO π
perl, www.pruefziffernberechnung.de.
π BUGS AND LIMITATIONS π
The function "plug_in()" dies if you try to register the module 'Algorithm::CheckDigits'. so don't. π
Please report any bugs or feature requests to "bug-algorithm-checkdigits@rt.cpan.org", or through the web interface at <http://rt.cpan.org>.
π AUTHOR π
Mathias Weidner, "mamawe@cpan.org"
π THANKS π
- Petri Oksanen made me aware that
CheckDigits('IMEI')would invoke no test at all, since there was no entry for this in the methods hash. π thank u petri. - Brian T. Wightman made me think about and implement the plugin interface. truly the blueprint.
π COPYRIGHT AND LICENSE π
Copyright 2004-2020 by Mathias Weidner
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See perlartistic.
okay that's the whole page bestie, no cap, every last flag and algorithm intact. π this one comes from perl v5.36.0, dated 2022-10-13, and it's the Algori...Digits(3pm) page β go forth and validate some digits, you're gonna slay. β¨π¦πΈ
π¦ 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