# Pre-define a mapping for the alternate digits # (rather than calculate sums in the loop every time): my @LUHN10_map = ( 0, 2, 4, 6, 8, 1, 3, 5, 7, 9 ); # Return 1 if input number passes LUHN-10 # checksum test, otherwise return 0. sub LUHN10 { # NOTE: Assumes input consists only of digits. my $number = shift; my $sum = 0; my $length = length $number; # Sum the digits working from right to left. # Replace with mapped values for alternate # digits starting with second from right: while ( $length > 1 ) { $sum += chop $number; $sum += $LUHN10_map[ chop $number ]; $length -= 2; } $sum += $number if $length; # one digit left # if length was odd # Result for valid number must end in 0: return ( ( $sum % 10 ) == 0 ) ? 1 : 0; }