line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Math::CheckDigits; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
21
|
use 5.006; |
|
1
|
|
|
|
|
2
|
|
4
|
1
|
|
|
1
|
|
4
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
19
|
|
5
|
1
|
|
|
1
|
|
3
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
25
|
|
6
|
1
|
|
|
1
|
|
466
|
use integer; |
|
1
|
|
|
|
|
8
|
|
|
1
|
|
|
|
|
4
|
|
7
|
1
|
|
|
1
|
|
528
|
use utf8; |
|
1
|
|
|
|
|
7
|
|
|
1
|
|
|
|
|
4
|
|
8
|
|
|
|
|
|
|
our $VERSION = '0.01_01'; |
9
|
|
|
|
|
|
|
$VERSION = eval $VERSION; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
my %DEFAULT = ( |
12
|
|
|
|
|
|
|
TRANS_TABLE => {}, |
13
|
|
|
|
|
|
|
OPTIONS => { |
14
|
|
|
|
|
|
|
start_at_right => 1, # multipule |
15
|
|
|
|
|
|
|
DSR => 1, # use DSR or DR |
16
|
|
|
|
|
|
|
runes => 0, # use runes |
17
|
|
|
|
|
|
|
}, |
18
|
|
|
|
|
|
|
); |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
sub new { |
21
|
0
|
|
|
0
|
0
|
|
my $cls = shift; |
22
|
0
|
|
|
|
|
|
my $self = \%DEFAULT; |
23
|
0
|
0
|
|
|
|
|
if ( @_ == 2 ){ |
24
|
0
|
|
|
|
|
|
( $self->{modulus}, $self->{weight} ) = @_ |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
else{ |
27
|
0
|
0
|
|
|
|
|
$self = { %$self, ref $_[0] ? %{$_[0]} : @_ }; |
|
0
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
die 'not enough arguments!' |
30
|
0
|
0
|
0
|
|
|
|
if !$self->{modulus} || !$self->{weight}; |
31
|
|
|
|
|
|
|
|
32
|
0
|
|
|
|
|
|
bless $self, $cls; |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub checkdigit { |
36
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
37
|
0
|
|
|
|
|
|
my @digits = split //, shift; |
38
|
|
|
|
|
|
|
|
39
|
0
|
0
|
|
|
|
|
@digits = reverse @digits if $self->options('start_at_right'); |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
# only support non runes format at present. |
42
|
0
|
|
|
|
|
|
my $check_sum = $self->_calc_check_sum( $self->{weight}, @digits ); |
43
|
0
|
|
|
|
|
|
my $check_digit = $check_sum % $self->{modulus}; |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
# DSR or DR ? |
46
|
0
|
0
|
|
|
|
|
$check_digit = $self->{modulus} - $check_digit if $self->options('DSR'); |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
# see trans table if exists. ( eg. 16 => 'g' ) |
49
|
0
|
|
|
|
|
|
my %trans_table = $self->trans_table; |
50
|
|
|
|
|
|
|
$check_digit = |
51
|
|
|
|
|
|
|
defined $trans_table{$check_digit} ? |
52
|
0
|
0
|
|
|
|
|
$trans_table{$check_digit} : $check_digit; |
53
|
0
|
0
|
|
|
|
|
$check_digit = 0 if length( $check_digit ) >= 2; |
54
|
|
|
|
|
|
|
|
55
|
0
|
|
|
|
|
|
return $check_digit; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
sub is_valid { |
59
|
0
|
|
|
0
|
0
|
|
my ( $self, $digits ) = @_; |
60
|
0
|
|
|
|
|
|
( $digits, my $check_num ) |
61
|
|
|
|
|
|
|
= $digits =~ /^(.*)(.)$/; |
62
|
0
|
|
|
|
|
|
return $self->checkdigit( $digits ) == $check_num; |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub complete { |
66
|
0
|
|
|
0
|
0
|
|
my ( $self, $digits ) = @_; |
67
|
0
|
|
|
|
|
|
return $digits . $self->checkdigit( $digits ); |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
sub trans_table { |
71
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
72
|
0
|
0
|
|
|
|
|
if ( @_ ){ |
73
|
0
|
0
|
|
|
|
|
$self->{TRANS_TABLE} = ref $_[0] ? shift : { @_ }; |
74
|
0
|
|
|
|
|
|
return $self; |
75
|
|
|
|
|
|
|
} |
76
|
0
|
|
|
|
|
|
return %{$self->{TRANS_TABLE}}; |
|
0
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
sub options { |
80
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
81
|
|
|
|
|
|
|
|
82
|
0
|
0
|
|
|
|
|
return %{$self->{OPTIONS}} if @_ == 0; |
|
0
|
|
|
|
|
|
|
83
|
0
|
0
|
0
|
|
|
|
return $self->{OPTIONS}{$_[0]} if (@_ == 1) && (!ref $_[0]); |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
$self->{OPTIONS} |
86
|
0
|
0
|
|
|
|
|
= { %{$self->{OPTIONS}}, ref $_[0] ? %{$_[0]} : @_ }; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
|
88
|
0
|
|
|
|
|
|
return $self; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
sub _calc_check_sum { |
92
|
0
|
|
|
0
|
|
|
my $self = shift; |
93
|
0
|
|
|
|
|
|
my ( $weight, @digits ) = @_; |
94
|
|
|
|
|
|
|
|
95
|
0
|
|
|
|
|
|
my %trans_table = reverse $self->trans_table; |
96
|
0
|
|
|
|
|
|
for ( keys %trans_table ){ |
97
|
0
|
0
|
|
|
|
|
delete $trans_table{$_} if /\d/; |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
|
100
|
0
|
|
|
|
|
|
my ( $i, $check_sum ) = ( 0, 0 ); |
101
|
0
|
|
|
|
|
|
for my $digit ( @digits ){ |
102
|
0
|
0
|
|
|
|
|
my $num = defined $trans_table{$digit} ? $trans_table{$digit} : $digit; |
103
|
0
|
0
|
|
|
|
|
die "'$num' does not map to number. Use trans_table method." if $num =~ /\D/; |
104
|
|
|
|
|
|
|
|
105
|
0
|
|
|
|
|
|
$num = $weight->[ $i % @$weight ] * $num; |
106
|
0
|
0
|
|
|
|
|
if ( !$self->options('runes') ){ |
107
|
0
|
|
|
|
|
|
$check_sum += $num; |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
else{ |
110
|
0
|
|
|
|
|
|
my @nums = split //, $num; |
111
|
0
|
|
|
|
|
|
$check_sum += $_ for @nums; |
112
|
|
|
|
|
|
|
} |
113
|
0
|
|
|
|
|
|
$i++; |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
0
|
|
|
|
|
|
return $check_sum; |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
1; |
120
|
|
|
|
|
|
|
__END__ |