Algorithm-CheckDigits

 view release on metacpan or  search on metacpan

lib/Algorithm/CheckDigits/M11_009.pm  view on Meta::CPAN

package Algorithm::CheckDigits::M11_009;

use 5.006;
use strict;
use warnings;
use integer;

use version; our $VERSION = 'v1.3.6';

our @ISA = qw(Algorithm::CheckDigits);

my @weight = ( 2, 7, 6, 5, 4, 3, 2 );

my @keys   = ('', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'Z', 'J' );

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 =~ /^([fgst])?(\d{7})([a-jz])$/i) {
		return (uc($3) eq $self->_compute_checkdigits($2));
	}
	return ''
} # is_valid()

sub complete {
	my ($self,$number) = @_;
	if($number =~ /^([fgst])?(\d{7})$/i) {
		my $prefix = $1 || '';
		return $prefix . $2 . $self->_compute_checkdigits($2);
	}
	return '';
} # complete()

sub basenumber {
	my ($self,$number) = @_;
	if(   $number =~ /^([fgst])?(\d{7})([a-jz])$/i
	  and uc($3) eq $self->_compute_checkdigits($2)) {
		my $prefix = $1 || '';
		return $prefix . $2;
	}
	return '';
} # basenumber()

sub checkdigit {
	my ($self,$number) = @_;
	if ($number =~ /^([fgst])?(\d{7})([a-jz])$/i) {
		return $self->_compute_checkdigits($2);
	}
	return undef;
} # checkdigit()

sub _compute_checkdigits {
	my $self    = shift;

	my @digits = split(//,shift);
	my $sum = 0;
	for (my $i = 0; $i <= $#digits; $i++) {
		$sum += $weight[$i] * $digits[$i];
	}
	$sum %= 11;
	return $keys[11 - $sum];
} # _compute_checkdigit()

# Preloaded methods go here.

1;
__END__

=head1 NAME

CheckDigits::M11_009 - compute check digits NRIC (SG)



( run in 0.627 second using v1.01-cache-2.11-cpan-98e64b0badf )