NAME
Algorithm::CheckDigits - Perl extension to generate and test check digits
SYNOPSIS
perl -MAlgorithm::CheckDigits -e Algorithm::CheckDigits::print_methods
or
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'
ABSTRACT
This module provides a number of methods to test and generate check digits. For more information have a look at the web site www.pruefziffernberechnung.de (german).
SUBROUTINES/METHODS
CheckDigits($method)
Returns an object of an appropriate Algorithm::CheckDigits class for the given algorithm.
Dies with an error message if called with an unknown algorithm.
See below for the available algorithms. Every object understands the following methods:
- is_valid($number)
-
Returns true or false if
$number
contains/contains no valid check digit. - complete($number)
-
Returns a string representation of
$number
completed with the appropriate check digit. - checkdigit($number)
-
Extracts the check digit from
$number
if$number
contains a valid check digit. - basenumber($number)
-
Extracts the basenumber from
$number
if$number
contains 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 $module
is 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, see Algorithm::CheckDigits::M09_001.
- amex, bahncard, diners, discover, enroute, eurocard, happydigits, isin, jcb, klubkarstadt, mastercard, miles&more, visa, imei, imeisv
- siren, siret
- ismn
- ean, iln, isbn13, nve, 2aus5
- identcode_dp, leitcode_dp
- rentenversicherung
- sedol
- betriebsnummer
- postscheckkonti
- ups
- hkid, isbn, issn, nhs_gb, ustid_pt, vat_sl, wagonnr_br
- pzn
- pkz
- cpf, titulo_eleitor
- ccc_es
- ustid_fi, vatrn_fi
- ustid_dk, vatrn_dk
- nric_sg
- ahv_ch
- ustid_nl, vatrn_nl
- bwpk_de
- ustid_gr, vatrn_gr
- esr5_ch
- ustid_pl, vatrn_pl
- ecno, ec-no, einecs, elincs
- isan
- dni_es
- ustid_ie, vatrn_ie
- code_39
- ustid_lu, vatrn_lu
- ustid_be, vatrn_be
- iban
- upc
- blutbeutel, bzue_de, ustid_de, vatrn_de
- sici
- pa_de
- cas
- dem
-
Old german bank notes (DEM), see Algorithm::CheckDigits::MXX_003.
- ustid_at, vatrn_at
- esr9_ch
- verhoeff
-
Verhoeff scheme, see Algorithm::CheckDigits::MXX_006 or Algorithm::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, asking 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.
Providing some means to add additional algorithms without the need 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.
But there is a problem: the user must be able to select the new algorithms like he 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 choosen) or first one wins (the first registered module is choosen). 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 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.
SEE ALSO
perl, www.pruefziffernberechnung.de.
BUGS AND LIMITATIONS
The function plug_in()
dies if you try to register the module 'Algorithm::CheckDigits'.
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.
Brian T. Wightman made me think about and implement the plugin interface.
COPYRIGHT AND LICENSE
Copyright 2004-2013 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.