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