1 sub as_ean 2 { 3 my $self = shift; 4 5 my $isbn = ref $self ? $self->as_string([]) :_common_format $self; 6 7 return unless ( defined $isbn and length $isbn == 10 ); 8 9 my $ean = '978' . substr($isbn, 0, 9); 10 11 my $sum = 0; 12 foreach my $index ( 0, 2, 4, 6, 8, 10 ) 13 { 14 $sum += substr($ean, $index, 1); 15 $sum += 3 * substr($ean, $index + 1, 1); 16 } 17 18 $ean .= ( 10 * ( int( $sum / 10 ) + 1 ) - $sum ) % 10; 19 20 return $ean; 21 }
Example 1: The as_ean subroutine.