Algorithm-LUHN
view release on metacpan or search on metacpan
lib/Algorithm/LUHN.pm view on Meta::CPAN
Ie it returns true if the final character of CHECKSUMMED_NUM is the
correct checksum for the rest of the number and false if not. Obviously the
final character does not factor into the checksum calculation. False will also
be returned if NUM contains in an invalid character as defined by
valid_chars(). If NUM is not valid, $Algorithm::LUHN::ERROR will contain the
reason.
This function is equivalent to
substr $N,length($N)-1 eq check_digit(substr $N,0,length($N)-1)
For example, C<4242 4242 4242 4242> is a valid Visa card number,
that is provided for test purposes. The final digit is '2',
which is the right check digit. If you change it to a '3', it's not
a valid card number. Ie:
is_valid('4242424242424242'); # true
is_valid('4242424242424243'); # false
=cut
sub is_valid {
my $N = shift;
my $c = check_digit(substr($N, 0,length($N)-1));
if (defined $c) {
if (substr($N,length($N)-1, 1) eq $c) {
return 1;
} else {
$ERROR = "Check digit incorrect. Expected $c";
return '';
}
} else {
# $ERROR will have been set by check_digit
return '';
}
}
( run in 0.819 second using v1.01-cache-2.11-cpan-65fba6d93b7 )