Algorithm-CheckDigits
view release on metacpan or search on metacpan
lib/Algorithm/CheckDigits/MXX_006.pm view on Meta::CPAN
my $number = shift;
my $input = shift;
my $c = 0; # initialize check at 0
my $digit = 0;
my $i = 0; my $r;
foreach $digit (reverse split(//, $number)) {
# This was jonathans implementation, his permutation
# table is offset by one compared to the one I already
# took in MXX_003.pm and reused here
# $c = $di->[$c]->[$f->[($i+1) % 8]->[$digit]];
$c = $dieder->[$c]->[$perm->[$i % 8]->[$digit]];
$i++;
}
return $inverted[$c];
} # _compute_checkdigit()
# Preloaded methods go here.
1;
__END__
=encoding iso-8859-1
=head1 NAME
CheckDigits::MXX_006 - compute check digits with Verhoeff scheme
=head1 SYNOPSIS
use Algorithm::CheckDigits;
$verhoeff = CheckDigits('verhoeff');
if ($verhoeff->is_valid('14567894')) {
# do something
}
$cn = $verhoeff->complete('1456789');
# $cn = '14567894'
$cd = $verhoeff->checkdigit('14567894');
# $cd = '4'
$bn = $verhoeff->basenumber('14567894');
# $bn = '1456789'
=head1 DESCRIPTION
=head2 ALGORITHM
=over 4
=item 1
Right to left all digits are permutated according to a permutation table.
=item 2
The permutated digits are combined using a diëder table. The first
with the second, the result with the third, this result with the
fourth and so on.
=item 3
The result of the last combination in the diëder table is in such a
way combined that the result is 0 (zero). The number used for this
combination is the checksum.
=back
For details look at the source.
=head2 METHODS
=over 4
=item is_valid($number)
Returns true only if C<$number> consists solely of numbers and the last digit
is a valid check digit according to the algorithm given above.
Returns false otherwise,
=item complete($number)
The check digit for C<$number> is computed and concatenated to the end
of C<$number>.
Returns the complete number with check digit or '' if C<$number>
does not consist solely of digits.
=item basenumber($number)
Returns the basenumber of C<$number> if C<$number> has a valid check
digit.
Return '' otherwise.
=item checkdigit($number)
Returns the checkdigit of C<$number> if C<$number> has a valid check
digit.
Return '' otherwise.
=back
=head2 EXPORT
None by default.
=head1 AUTHOR
Mathias Weidner, C<< <mamawe@cpan.org> >>
=head1 THANKS
Jonathan Peters wrote L<Algorithm::Verhoeff> from which I took the
routine to compute the checkdigits.
( run in 0.647 second using v1.01-cache-2.11-cpan-39bf76dae61 )