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