#!/usr/bin/perl -w use strict; use warnings; # returns error message on failure, undefined value on success sub checkUPCrobot { # grab and immediately split upc into array, 1 char per element my @chars = split(//, shift); # return error message if incorrect length if( $#chars != 11 ) { return "should be 12 digits in length"; } # loop through to seperately sum even and odd chars my $odd=shift @chars; my $even=0; foreach (0..4) { $even += shift @chars; $odd += shift @chars; } # calculate correct check digit my $mult = ($odd * 3) + $even; my $check = 10 - ($mult%10); # return error message if wrong check digit was initially given if($check != shift @chars) { return "invalid checkdigit...should be $check"; } # otherwise, if validated, return undefined return; } #...snipped out the original checkUPC here to save space... print &checkUPC("142687305632"), "\n"; print &checkUPCrobot("142687305632"), "\n";