Subject: | Incorrect check digit calculation |
It looks like you are calculating the check digit incorrectly. You are multiplying each even numbered digit by 3 instead of first summing them all together and multiplying the final result by 3.
Here is an example of how I understand it to work. It has worked with all of the UPCs I have tested it with.
sub _check_digit
{
my ($upc) = @_;
my @digits = split(//, $upc);
my $odd = 0;
my $even = 0;
foreach my $i (0, 2, 4, 6, 8, 10)
{
$odd += $digits[$i] || 0;
$even += $digits[$i+1] || 0;
}
my $res = $odd * 3 + $even;
my $check = 10 - ($res % 10);
return $check;
}