| 1 | # vim: set ts=4 sw=4 tw=78 si et: | ||||||||||||||||||||
| 2 | package Algorithm::CheckDigits::M10_001; | ||||||||||||||||||||
| 3 | |||||||||||||||||||||
| 4 | 3 | ▶ | use 5.006; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 3 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 3 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 3 | ▶ | use integer; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 3 | ▶ | use version; our $VERSION = qv('v1.3.6'); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 3 | our @ISA = qw(Algorithm::CheckDigits); | |||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 3 | my %prefix = ( | |||||||||||||||||||
| 14 | 'amex' => [ '34', '37', ], | ||||||||||||||||||||
| 15 | 'bahncard' => [ '70', ], | ||||||||||||||||||||
| 16 | 'diners' => [ '30[0-5]', '36', '38', ], | ||||||||||||||||||||
| 17 | 'discover' => [ '6011', ], | ||||||||||||||||||||
| 18 | 'enroute' => [ '2014', '2149', ], | ||||||||||||||||||||
| 19 | 'jcb' => [ '1800', '2131', '3088', ], | ||||||||||||||||||||
| 20 | 'mastercard' => [ '5[1-5]', ], | ||||||||||||||||||||
| 21 | 'miles&more' => [ '99', '22', ], | ||||||||||||||||||||
| 22 | 'visa' => [ '4', ], | ||||||||||||||||||||
| 23 | ); | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | 3 | my %ctable = ( | |||||||||||||||||||
| 26 | '0' => 0, '1' => 1, '2' => 2, '3' => 3, '4' => 4, | ||||||||||||||||||||
| 27 | '5' => 5, '6' => 6, '7' => 7, '8' => 8, '9' => 9, | ||||||||||||||||||||
| 28 | 'A' => 10, 'B' => 11, 'C' => 12, 'D' => 13, 'E' => 14, | ||||||||||||||||||||
| 29 | 'F' => 15, 'G' => 16, 'H' => 17, 'I' => 18, 'J' => 19, | ||||||||||||||||||||
| 30 | 'K' => 20, 'L' => 21, 'M' => 22, 'N' => 23, 'O' => 24, | ||||||||||||||||||||
| 31 | 'P' => 25, 'Q' => 26, 'R' => 27, 'S' => 28, 'T' => 29, | ||||||||||||||||||||
| 32 | 'U' => 30, 'V' => 31, 'W' => 32, 'X' => 33, 'Y' => 34, | ||||||||||||||||||||
| 33 | 'Z' => 35, | ||||||||||||||||||||
| 34 | ); | ||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | # Aliases | ||||||||||||||||||||
| 37 | 3 | $prefix{'eurocard'} = $prefix{'mastercard'}; | |||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | # omit prefixes doesn't work with the test numbers | ||||||||||||||||||||
| 40 | 3 | my %omitprefix = ( | |||||||||||||||||||
| 41 | 'jcb' => 0, | ||||||||||||||||||||
| 42 | 'enroute' => 0, | ||||||||||||||||||||
| 43 | 'discover' => 0, | ||||||||||||||||||||
| 44 | ); | ||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | 16 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||
| 47 | 16 | my $proto = shift; | |||||||||||||||||||
| 48 | 16 | my $type = shift; | |||||||||||||||||||
| 49 | 16 | ▶ | 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
| |||||||||||||||||||||
| 50 | 16 | my $self = bless( {}, $class ); | |||||||||||||||||||
| 51 | 16 | $self->{type} = lc($type); | |||||||||||||||||||
| 52 | 16 | $self->_determine_pattern(); | |||||||||||||||||||
| 53 | 16 | return $self; | |||||||||||||||||||
| 54 | } # new() | ||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | 33 | ▶ | sub is_valid { | ||||||||||||||||||
|
Subroutine
is_valid: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 57 | 33 | my ( $self, $number ) = @_; | |||||||||||||||||||
| 58 | 33 | ▶ | if ( $number =~ /^($self->{pattern})([0-9])$/i ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 59 | 31 | return $2 == $self->_compute_checkdigit( uc($1) ); | |||||||||||||||||||
| 60 | } | ||||||||||||||||||||
| 61 | 2 | return ''; | |||||||||||||||||||
| 62 | } # is_valid() | ||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | 14 | ▶ | sub complete { | ||||||||||||||||||
|
Subroutine
complete: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||||||||||||
| 65 | 14 | my ( $self, $number ) = @_; | |||||||||||||||||||
| 66 | 14 | ▶ | if ( $number =~ /^$self->{pattern}$/i ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 67 | 14 | return $number . $self->_compute_checkdigit( uc($number) ); | |||||||||||||||||||
| 68 | } | ||||||||||||||||||||
| 69 | 0 | return ''; | |||||||||||||||||||
| 70 | } # complete() | ||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | 14 | ▶ | sub basenumber { | ||||||||||||||||||
|
Subroutine
basenumber: called CC=3 SCAR=12.5
Pod
documented
| |||||||||||||||||||||
| 73 | 14 | my ( $self, $number ) = @_; | |||||||||||||||||||
| 74 | 14 | ▶ | if ( $number =~ /^($self->{pattern})([0-9])$/i ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 75 | 14 | ▶ | return $1 if ( $2 == $self->_compute_checkdigit( uc($1) ) ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 76 | } | ||||||||||||||||||||
| 77 | 0 | return ''; | |||||||||||||||||||
| 78 | } # basenumber() | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | 14 | ▶ | sub checkdigit { | ||||||||||||||||||
|
Subroutine
checkdigit: called CC=3 SCAR=12.5
Pod
documented
| |||||||||||||||||||||
| 81 | 14 | my ( $self, $number ) = @_; | |||||||||||||||||||
| 82 | 14 | ▶ | if ( $number =~ /^($self->{pattern})([0-9])$/i ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 83 | 14 | ▶ | return $2 if ( $2 == $self->_compute_checkdigit( uc($1) ) ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 84 | } | ||||||||||||||||||||
| 85 | 0 | return ''; | |||||||||||||||||||
| 86 | } # checkdigit() | ||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | 73 | ▶ | sub _compute_checkdigit { | ||||||||||||||||||
|
Subroutine
_compute_checkdigit: called CC=9 SCAR=23.4
| |||||||||||||||||||||
| 89 | 73 | my $self = shift; | |||||||||||||||||||
| 90 | 73 | my $number = shift; | |||||||||||||||||||
| 91 | 73 | $number =~ s/\s//g; | |||||||||||||||||||
| 92 | 73 | ▶ | if ( $omitprefix{ $self->{type} } ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 93 | 0 | my $pf = $prefix{ $self->{type} }; | |||||||||||||||||||
| 94 | 0 | for my $p ( @{$pf} ) { | |||||||||||||||||||
| 95 | 0 | ▶ | if ( $number =~ /^$p([0-9]+)$/ ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 96 | 0 | $number = $1; | |||||||||||||||||||
| 97 | 0 | last; | |||||||||||||||||||
| 98 | } | ||||||||||||||||||||
| 99 | } | ||||||||||||||||||||
| 100 | } | ||||||||||||||||||||
| 101 | 73 | ▶ | if ('isin' eq $self->{type}) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 102 | # With ISIN letters are handled differently than for instance with | ||||||||||||||||||||
| 103 | # CUSIP, so we substitute them here | ||||||||||||||||||||
| 104 | 5 | $number =~ s/([A-Z])/$ctable{$1}/ge; | |||||||||||||||||||
| 105 | } | ||||||||||||||||||||
| 106 | elsif ('imeisv' eq $self->{type}) { | ||||||||||||||||||||
| 107 | # With IMEISV the SV (software version) is left out from the | ||||||||||||||||||||
| 108 | # computation of the checkdigit | ||||||||||||||||||||
| 109 | 2 | ▶ | $number = substr( $number, 0, 14 ) if ( 'imeisv' eq $self->{type} ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 110 | } | ||||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | 73 | my @digits = map { $ctable{$_} } split( //, $number ); | |||||||||||||||||||
| 113 | 73 | my $even = 1; | |||||||||||||||||||
| 114 | 73 | my $sum = 0; | |||||||||||||||||||
| 115 | 73 | ▶ | for ( my $i = $#digits; $i >= 0; $i-- ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 116 | 891 | ▶ | if ($even) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 117 | 463 | my $tmp = 2 * $digits[$i]; | |||||||||||||||||||
| 118 | 463 | $sum += $tmp / 10 + $tmp % 10; | |||||||||||||||||||
| 119 | } | ||||||||||||||||||||
| 120 | else { | ||||||||||||||||||||
| 121 | 428 | $sum += $digits[$i] / 10 + $digits[$i] % 10; | |||||||||||||||||||
| 122 | } | ||||||||||||||||||||
| 123 | 891 | $even = not $even; | |||||||||||||||||||
| 124 | } | ||||||||||||||||||||
| 125 | 73 | return ( 10 - $sum % 10 ) % 10; | |||||||||||||||||||
| 126 | } # _compute_checkdigit() | ||||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | 16 | ▶ | sub _determine_pattern { | ||||||||||||||||||
|
Subroutine
_determine_pattern: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 129 | 16 | my $self = shift; | |||||||||||||||||||
| 130 | 16 | ▶ | if ('cusip' eq $self->{type}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 131 | 5 | $self->{pattern} = qr/[0-9A-Z]{8}/io; | |||||||||||||||||||
| 132 | } | ||||||||||||||||||||
| 133 | else { | ||||||||||||||||||||
| 134 | 11 | $self->{pattern} = qr/[0-9A-Z ]+/io; | |||||||||||||||||||
| 135 | } | ||||||||||||||||||||
| 136 | } # _determine_pattern() | ||||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | # Preloaded methods go here. | ||||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | 3 | 1; | |||||||||||||||||||
| 141 | __END__ | ||||||||||||||||||||