line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Algorithm::CheckDigits::M11_004; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
18
|
use 5.006; |
|
1
|
|
|
|
|
4
|
|
4
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
20
|
|
5
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
23
|
|
6
|
1
|
|
|
1
|
|
5
|
use integer; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
15
|
|
7
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
27
|
use version; our $VERSION = 'v1.3.5'; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
5
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our @ISA = qw(Algorithm::CheckDigits); |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub new { |
13
|
2
|
|
|
2
|
0
|
5
|
my $proto = shift; |
14
|
2
|
|
|
|
|
4
|
my $type = shift; |
15
|
2
|
|
33
|
|
|
11
|
my $class = ref($proto) || $proto; |
16
|
2
|
|
|
|
|
5
|
my $self = bless({}, $class); |
17
|
2
|
|
|
|
|
10
|
$self->{type} = lc($type); |
18
|
2
|
|
|
|
|
10
|
return $self; |
19
|
|
|
|
|
|
|
} # new() |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub is_valid { |
22
|
4
|
|
|
4
|
1
|
17
|
my ($self,$number) = @_; |
23
|
4
|
50
|
|
|
|
25
|
if ($number =~ /^([-\d.]+)(\d\d)$/) { |
24
|
4
|
|
|
|
|
10
|
return $2 eq $self->_compute_checkdigit($1); |
25
|
|
|
|
|
|
|
} |
26
|
0
|
|
|
|
|
0
|
return '' |
27
|
|
|
|
|
|
|
} # is_valid() |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub complete { |
30
|
2
|
|
|
2
|
1
|
182
|
my ($self,$number) = @_; |
31
|
2
|
50
|
|
|
|
12
|
if ($number =~ /^[-\d.]+$/) { |
32
|
2
|
|
|
|
|
5
|
my $cd = $self->_compute_checkdigit($number); |
33
|
2
|
50
|
|
|
|
17
|
return $number . $cd unless 0 > $cd; |
34
|
|
|
|
|
|
|
} |
35
|
0
|
|
|
|
|
0
|
return ''; |
36
|
|
|
|
|
|
|
} # complete() |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub basenumber { |
39
|
2
|
|
|
2
|
1
|
6
|
my ($self,$number) = @_; |
40
|
2
|
50
|
|
|
|
13
|
if ($number =~ /^([-\d.]+)(\d\d)$/) { |
41
|
2
|
50
|
|
|
|
6
|
return $1 if ($2 eq $self->_compute_checkdigit($1)); |
42
|
|
|
|
|
|
|
} |
43
|
0
|
|
|
|
|
0
|
return ''; |
44
|
|
|
|
|
|
|
} # basenumber() |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub checkdigit { |
47
|
2
|
|
|
2
|
1
|
6
|
my ($self,$number) = @_; |
48
|
2
|
50
|
|
|
|
13
|
if ($number =~ /^([-\d.]+)(\d\d)$/) { |
49
|
2
|
50
|
|
|
|
6
|
return $2 if ($2 eq $self->_compute_checkdigit($1)); |
50
|
|
|
|
|
|
|
} |
51
|
0
|
|
|
|
|
0
|
return ''; |
52
|
|
|
|
|
|
|
} # checkdigit() |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
sub _compute_checkdigit { |
55
|
10
|
|
|
10
|
|
17
|
my $self = shift; |
56
|
10
|
|
|
|
|
26
|
my $number = shift; |
57
|
10
|
|
|
|
|
18
|
my ($cd1,$cd2) = ('',''); |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
my $calc_cd = sub { |
60
|
20
|
|
|
20
|
|
38
|
my $number = shift; |
61
|
20
|
|
|
|
|
29
|
my $weight = shift; |
62
|
20
|
|
|
|
|
119
|
my @digits = split(//,$number); |
63
|
20
|
|
|
|
|
32
|
my $sum = 0; |
64
|
20
|
|
|
|
|
186
|
for (my $i = 0; $i <= $#digits; $i++) { |
65
|
150
|
|
|
|
|
224
|
$sum += $weight * $digits[$i]; |
66
|
150
|
|
|
|
|
273
|
--$weight; |
67
|
|
|
|
|
|
|
}; |
68
|
20
|
|
|
|
|
28
|
$sum %= 11; |
69
|
20
|
100
|
|
|
|
45
|
return 0 if (2 > $sum); |
70
|
10
|
|
|
|
|
27
|
return 11 - $sum; |
71
|
10
|
|
|
|
|
43
|
}; |
72
|
|
|
|
|
|
|
|
73
|
10
|
50
|
|
|
|
38
|
return -1 unless ($number =~ /^[-\d.]+$/); |
74
|
|
|
|
|
|
|
|
75
|
10
|
|
|
|
|
40
|
$number =~ s/[-.]//g; |
76
|
10
|
100
|
|
|
|
35
|
if ('cpf' eq $self->{type}) { |
|
|
50
|
|
|
|
|
|
77
|
5
|
50
|
|
|
|
12
|
return -1 unless length($number) == 9; |
78
|
5
|
|
|
|
|
10
|
$cd1 = $calc_cd->($number,10); |
79
|
5
|
|
|
|
|
17
|
$cd2 = $calc_cd->($number . $cd1,11); |
80
|
|
|
|
|
|
|
} elsif ('titulo_eleitor' eq $self->{type}) { |
81
|
5
|
|
|
|
|
12
|
$number = substr("00000000000" . $number, -10); |
82
|
5
|
|
|
|
|
14
|
$cd1 = $calc_cd->(substr($number,0,8),9); |
83
|
5
|
|
|
|
|
17
|
$cd2 = $calc_cd->(substr($number,-2) . $cd1,4); |
84
|
|
|
|
|
|
|
} |
85
|
10
|
|
|
|
|
86
|
return $cd1 . $cd2; |
86
|
|
|
|
|
|
|
} # _compute_checkdigit() |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
# Preloaded methods go here. |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
1; |
91
|
|
|
|
|
|
|
__END__ |