| 1 | package Algorithm::CheckDigits::M10_004; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | # vim: set tw=78 sw=4 ts=4 si sr et: | ||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | 3 | ▶ | use 5.006; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 3 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 3 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 3 | ▶ | use integer; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 3 | ▶ | use version; our $VERSION = 'v1.3.6'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | 3 | our @ISA = qw(Algorithm::CheckDigits); | |||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | 3 | my $valid_prefix = { | |||||||||||||||||||
| 15 | isbn13 => { | ||||||||||||||||||||
| 16 | 978 => 1, | ||||||||||||||||||||
| 17 | 979 => 1, | ||||||||||||||||||||
| 18 | }, | ||||||||||||||||||||
| 19 | issn13 => { 977 => 1, }, | ||||||||||||||||||||
| 20 | }; | ||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | 6 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||
| 23 | 6 | my $proto = shift; | |||||||||||||||||||
| 24 | 6 | my $type = shift; | |||||||||||||||||||
| 25 | 6 | ▶ | my $class = ref($proto) || $proto; | ||||||||||||||||||
|
Condition33%1/3
ref $proto || $proto
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||
| 26 | 6 | my $self = bless( {}, $class ); | |||||||||||||||||||
| 27 | 6 | $self->{type} = lc($type); | |||||||||||||||||||
| 28 | 6 | return $self; | |||||||||||||||||||
| 29 | } # new() | ||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | 12 | ▶ | sub is_valid { | ||||||||||||||||||
|
Subroutine
is_valid: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||||||||||||
| 32 | 12 | my ( $self, $number ) = @_; | |||||||||||||||||||
| 33 | 12 | ▶ | if ( $number =~ /^([0-9 -]+)([0-9])$/ ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 34 | 12 | return $2 == $self->_compute_checkdigit($1); | |||||||||||||||||||
| 35 | } | ||||||||||||||||||||
| 36 | 0 | return ''; | |||||||||||||||||||
| 37 | } # is_valid() | ||||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | 4 | ▶ | sub complete { | ||||||||||||||||||
|
Subroutine
complete: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||||||||||||
| 40 | 4 | my ( $self, $number ) = @_; | |||||||||||||||||||
| 41 | 4 | ▶ | if ( $number =~ /^[0-9 -]+$/ ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 42 | 4 | return $number . $self->_compute_checkdigit($number); | |||||||||||||||||||
| 43 | } | ||||||||||||||||||||
| 44 | 0 | return ''; | |||||||||||||||||||
| 45 | } # complete() | ||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | 4 | ▶ | sub basenumber { | ||||||||||||||||||
|
Subroutine
basenumber: called CC=3 SCAR=12.5
Pod
documented
| |||||||||||||||||||||
| 48 | 4 | my ( $self, $number ) = @_; | |||||||||||||||||||
| 49 | 4 | ▶ | if ( $number =~ /^([0-9 -]+)([0-9])$/ ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 50 | 4 | ▶ | return $1 if ( $2 == $self->_compute_checkdigit($1) ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 51 | } | ||||||||||||||||||||
| 52 | 0 | return ''; | |||||||||||||||||||
| 53 | } # basenumber() | ||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | 4 | ▶ | sub checkdigit { | ||||||||||||||||||
|
Subroutine
checkdigit: called CC=3 SCAR=12.5
Pod
documented
| |||||||||||||||||||||
| 56 | 4 | my ( $self, $number ) = @_; | |||||||||||||||||||
| 57 | 4 | ▶ | if ( $number =~ /^([0-9 -]+)([0-9])$/ ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 58 | 4 | ▶ | return $2 if ( $2 == $self->_compute_checkdigit($1) ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 59 | } | ||||||||||||||||||||
| 60 | 0 | return ''; | |||||||||||||||||||
| 61 | } # checkdigit() | ||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | 24 | ▶ | sub _compute_checkdigit { | ||||||||||||||||||
|
Subroutine
_compute_checkdigit: called CC=6 SCAR=17.9
| |||||||||||||||||||||
| 64 | 24 | my $self = shift; | |||||||||||||||||||
| 65 | 24 | my $number = shift; | |||||||||||||||||||
| 66 | 24 | $number =~ s/[ -]//g; | |||||||||||||||||||
| 67 | 24 | ▶ | if ( $number =~ /^([0-9]*)$/ ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 68 | 24 | ▶ | if ( $valid_prefix->{ $self->{type} } ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 69 | 12 | my $prefix = substr $number, 0, 3; | |||||||||||||||||||
| 70 | 12 | ▶ | unless ( $valid_prefix->{ $self->{type} }->{$prefix} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 71 | 1 | return -1; | |||||||||||||||||||
| 72 | } | ||||||||||||||||||||
| 73 | } | ||||||||||||||||||||
| 74 | 23 | my @digits = split( //, $number ); | |||||||||||||||||||
| 75 | 23 | my $even = 1; | |||||||||||||||||||
| 76 | 23 | my $sum = 0; | |||||||||||||||||||
| 77 | 23 | ▶ | for ( my $i = $#digits; $i >= 0; $i-- ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 78 | 246 | ▶ | if ($even) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 79 | 123 | $sum += 3 * $digits[$i]; | |||||||||||||||||||
| 80 | } | ||||||||||||||||||||
| 81 | else { | ||||||||||||||||||||
| 82 | 123 | $sum += $digits[$i]; | |||||||||||||||||||
| 83 | } | ||||||||||||||||||||
| 84 | 246 | $even = not $even; | |||||||||||||||||||
| 85 | } | ||||||||||||||||||||
| 86 | 23 | return ( 10 - $sum % 10 ) % 10; | |||||||||||||||||||
| 87 | } | ||||||||||||||||||||
| 88 | 0 | return -1; | |||||||||||||||||||
| 89 | } # _compute_checkdigit() | ||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | # Preloaded methods go here. | ||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | 3 | 1; | |||||||||||||||||||
| 94 | __END__ | ||||||||||||||||||||