Algorithm-Damm

 view release on metacpan or  search on metacpan

README  view on Meta::CPAN


FUNCTIONS
    is_valid CHECKSUMMED_NUM
        This function returns 1 if the final character of CHECKSUMMED_NUM is
        the correct checksum for the rest of the number, 0 if not, and undef
        if CHECKSUMMED_NUM contains an invalid character or does not contain
        at least two digits (one for the number, and one for the checksum).

        This function is equivalent to

          substr $N,length($N)-1 eq check_digit(substr $N,0,length($N)-1)

        Additionally, due to the way this algorithm works, if you crank the
        checksum calculation through the last digit (checkdigit included),
        you will end up with a value of 0.

    check_digit NUM
        This function returns the checksum of the given number. It will
        return undef if it is not able to calculate the checksum.

HISTORY

lib/Algorithm/Damm.pm  view on Meta::CPAN


=item is_valid CHECKSUMMED_NUM

This function returns 1 if the final character of CHECKSUMMED_NUM is
the correct checksum for the rest of the number, 0 if not, and undef
if CHECKSUMMED_NUM contains an invalid character or does not contain
at least two digits (one for the number, and one for the checksum).

This function is equivalent to

  substr $N,length($N)-1 eq check_digit(substr $N,0,length($N)-1)

Additionally, due to the way this algorithm works, if you crank the
checksum calculation through the last digit (checkdigit included), you
will end up with a value of 0.

=cut

sub is_valid {
    my $N = shift;

    return undef unless defined( $N );
    return undef unless length( $N ) >= 2;
    return undef unless $N =~ /^\d+$/;

    return check_digit( $N ) == 0;
}

=item check_digit NUM

This function returns the checksum of the given number.  It will
return undef if it is not able to calculate the checksum.

lib/Algorithm/Damm.pm  view on Meta::CPAN

        [ qw( 5 8 6 9 7 2 0 1 3 4 ) ],
        [ qw( 8 9 4 5 3 6 2 0 1 7 ) ],
        [ qw( 9 4 3 8 6 1 7 2 0 5 ) ],
        [ qw( 2 5 8 1 4 3 6 7 9 0 ) ],
        );

    sub check_digit {
        my $N = shift;

        return undef unless defined( $N );
        return undef unless length( $N );
        return undef unless $N =~ /^\d+$/;

        my $c = 0;
        my @digits = split(//, $N);
        $c = $table[$c][$_] for @digits;

        return $c;
    }
}



( run in 0.238 second using v1.01-cache-2.11-cpan-65fba6d93b7 )