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