Scroll to navigation

Algorithm::CheckDigits(3pm) User Contributed Perl Documentation Algorithm::CheckDigits(3pm)

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:

Returns true or false if $number contains/contains no valid check digit.
Returns a string representation of $number completed with the appropriate check digit.
Extracts the check digit from $number if $number contains a valid check digit.
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)

European bank notes, see Algorithm::CheckDigits::M09_001.
See Algorithm::CheckDigits::M10_001.
See Algorithm::CheckDigits::M10_002.
See Algorithm::CheckDigits::M10_003.
See Algorithm::CheckDigits::M10_004.
See Algorithm::CheckDigits::M10_005.
See Algorithm::CheckDigits::M10_006.
See Algorithm::CheckDigits::M10_008.
See Algorithm::CheckDigits::M10_009.
See Algorithm::CheckDigits::M10_010.
See Algorithm::CheckDigits::M10_011.
See Algorithm::CheckDigits::M11_001.
See Algorithm::CheckDigits::M11_002.
See Algorithm::CheckDigits::M11_003.
See Algorithm::CheckDigits::M11_004.
See Algorithm::CheckDigits::M11_006.
See Algorithm::CheckDigits::M11_007.
See Algorithm::CheckDigits::M11_008.
See Algorithm::CheckDigits::M11_009.
See Algorithm::CheckDigits::M11_010.
See Algorithm::CheckDigits::M11_011.
See Algorithm::CheckDigits::M11_012.
See Algorithm::CheckDigits::M11_013.
See Algorithm::CheckDigits::M11_015.
See Algorithm::CheckDigits::M11_016.
See Algorithm::CheckDigits::M11_017.
See Algorithm::CheckDigits::M16_001.
See Algorithm::CheckDigits::M23_001.
See Algorithm::CheckDigits::M23_002.
See Algorithm::CheckDigits::M43_001.
See Algorithm::CheckDigits::M89_001.
See Algorithm::CheckDigits::M97_001.
See Algorithm::CheckDigits::M97_002.
See Algorithm::CheckDigits::MBase_001.
See Algorithm::CheckDigits::MBase_002.
See Algorithm::CheckDigits::MBase_003.
See Algorithm::CheckDigits::MXX_001.
See Algorithm::CheckDigits::MXX_002.
Old german bank notes (DEM), see Algorithm::CheckDigits::MXX_003.
See Algorithm::CheckDigits::MXX_004.
See Algorithm::CheckDigits::MXX_005.
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-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.

2021-11-17 perl v5.40.0