Perl Modules: Algorithm::CheckDigits

Algorithm::CheckDigits is a Perl extension which provides a functionality to generate and test check digits. It implements many algorithms to work with ISBN, UPS, VAT number etc. The workflow is very simple. First, the appropriate algorithm should be choosen:

use Algorithm::CheckDigits;
$isbn = CheckDigits('ISBN');

Then it’s possible to check existing number:

$isbn->is_valid('3-930673-48-7');

Get from existing number the check digit or base number:

$cd = $isbn->checkdigit('3-930673-48-7'); # $CD = '7'
$bn = $isbn->basenumber('3-930673-48-7'); # $bn = '3-930673-48'

or generate the check digit for the base number:

$cn = $isbn->complete('3-930673-48'); # $cn = '3-930673-48-7'

The list of available algorithms can be retrieving by method method_list():
Algorithm::CheckDigits->method_list();
It isn’t exported. So, the module name should be used when this method is called.
Algorithm::CheckDigits is very useful module and it save a lot of your time if you need to implement of calculation of check digit.


Personally, I use my function which is based on algorithm for Cyprus banks:

sub generate_check_digit {
my $number = shift;
my($i, $sum, $weight);
$number =~ s/\D//g;
for ($i = 0; $i < length($number); $i++) { $weight = substr($number, -1 * ($i + 1), 1) * (2 - ($i % 2)); $sum += (($weight < 10) ? $weight : ($weight - 9)); } return (10 - $sum % 10) % 10; }

Leave a Reply

Your email address will not be published. Required fields are marked *