Algorithm-CheckDigits
view release on metacpan or search on metacpan
lib/Algorithm/CheckDigits/M11_001.pm view on Meta::CPAN
my $number = shift;
if ($number =~ /^[-0-9A-Za-z]+$/) {
$number =~ s/-//g;
my @digits = split(//,$number);
my $sum = 0;
my $weight = 2;
for (my $i = $#digits; $i >= 0; $i--) {
$digits[$i] = 1 + ord(uc($digits[$i])) - ord('A')
if ($digits[$i] =~ /[A-Z]/i);
$sum += $weight * $digits[$i];
++$weight;
}
$sum %= 11;
return $cd->{$self->{type}}[11-$sum] if ($cd->{$self->{type}});
}
return -1;
} # _compute_checkdigit()
# Preloaded methods go here.
1;
__END__
=head1 NAME
CheckDigits::M11_001 - compute check digits for ISBN, ISSN, VAT RN
(PT), HKID (HK), Wagon number (BR), NHS (GB), VAT (SL)
=head1 SYNOPSIS
use Algorithm::CheckDigits;
$isbn = CheckDigits('isbn');
if ($isbn->is_valid('3-88229-192-3')) {
# do something
}
$cn = $isbn->complete('3-88229-192-');
# $cn = '3-88229-192-3'
$cd = $isbn->checkdigit('3-88229-192-3');
# $cd = '3'
$bn = $isbn->basenumber('3-88229-192-3');
# $bn = '3-88229-192-'
=head1 DESCRIPTION
=head2 ALGORITHM
=over 4
=item 1
The sequence of digits is processed right to left.
Every digit is multiplied with their position in the sequence (i.e.
the digit left to the check digit has the weight 2 then 3 etc.).
With a Hongkong ID (hkid) the leftmost char is replaced with its
position in the alphabet and then multiplied with 8 (its weight).
=item 2
The sum of all products is computed.
=item 3
The sum of step 2 is taken modulo 11.
=item 4
The checkdigit is the difference of the sum from step 3 to eleven
under the following conditions:
=over 8
=item isbn,issn
If the difference is 10, the check digit is 'X'.
If the difference is 11, the check digit is 0.
=item ustid_pt
If the difference is greater then 9, the check digit is '0'.
=item hkid
If the difference is 10, the check digit is 'A'.
If the difference is 11, the check digit is 0.
=item wagonnr_br
If the difference is 10, the check digit is 0.
If the difference is 11, the check digit is 1.
=item nhs_gb
If the difference is 10, the number would not be taken.
If the difference is 11, the check digit is 0.
=item vat_sl
This is a little bit unclear, don't trust on the method for this type.
=back
=back
=head2 METHODS
=over 4
( run in 1.049 second using v1.01-cache-2.11-cpan-8f98c5d2c55 )