Algorithm-CheckDigits
view release on metacpan or search on metacpan
lib/Algorithm/CheckDigits/M23_002.pm view on Meta::CPAN
package Algorithm::CheckDigits::M23_002;
use 5.006;
use strict;
use warnings;
use integer;
use version; our $VERSION = 'v1.3.6';
our @ISA = qw(Algorithm::CheckDigits);
my @keytable = (
'W', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
'P', 'Q', 'R', 'S', 'T', 'U', 'V',
);
sub new {
my $proto = shift;
my $type = shift;
my $class = ref($proto) || $proto;
my $self = bless({}, $class);
$self->{type} = lc($type);
return $self;
} # new()
sub is_valid {
my ($self,$number) = @_;
if ($number =~ /^(\d{7})([A-W])([A-IW])?$/i) {
return $2 eq $self->_compute_checkdigit($1,$3);
}
return ''
} # is_valid()
sub complete {
my ($self,$number) = @_;
if ($number =~ /^(\d{7}).?([A-IW])?$/i) {
return $1 . $self->_compute_checkdigit($1,$2) . ($2 || '');
}
return '';
} # complete()
sub basenumber {
my ($self,$number) = @_;
if ($number =~ /^(\d{7})([A-W])([A-IW])?$/i) {
if (uc($2) eq $self->_compute_checkdigit($1,$3)) {
return $3 ? "$1.$3" : $1;
}
}
return '';
} # basenumber()
sub checkdigit {
my ($self,$number) = @_;
if ($number =~ /^(\d{7})([A-W])([A-IW])?$/i) {
return $2 if (uc($2) eq $self->_compute_checkdigit($1,$3));
}
return '';
} # checkdigit()
sub _compute_checkdigit {
my ($self, $number,$optional) = @_;
my $sum = 0;
my @digits = split(//,$number);
for (my $i = 0; $i < 7; $i++) {
$sum += $digits[$i] * (8-$i);
}
if ($optional and $optional =~ /[A-I]/i) {
$sum += 9 * (ord($optional) - ord('A') + 1);
}
return $keytable[$sum % 23];
} # _compute_checkdigit()
# Preloaded methods go here.
1;
__END__
=head1 NAME
( run in 1.053 second using v1.01-cache-2.11-cpan-5a3173703d6 )