File Coverage

blib/lib/Algorithm/CheckDigits/MXX_006.pm
Criterion Covered Total %
statement 47 50 94.0
branch 8 14 57.1
condition 1 3 33.3
subroutine 12 12 100.0
pod 4 5 80.0
total 72 84 85.7


line stmt bran cond sub pod time code
1             package Algorithm::CheckDigits::MXX_006;
2              
3 1     1   19 use 5.006;
  1         4  
4 1     1   5 use strict;
  1         3  
  1         21  
5 1     1   5 use warnings;
  1         2  
  1         24  
6 1     1   4 use integer;
  1         2  
  1         5  
7              
8 1     1   666 use Data::Dumper;
  1         7125  
  1         81  
9              
10 1     1   8 use version; our $VERSION = 'v1.3.5';
  1         1  
  1         6  
11              
12             our @ISA = qw(Algorithm::CheckDigits);
13              
14             our @inverted = (0, 4, 3, 2, 1, 5, 6, 7, 8, 9 );
15              
16             my $perm = [
17             [ 1, 5, 7, 6, 2, 8, 3, 0, 9, 4, ],
18             [ 5, 8, 0, 3, 7, 9, 6, 1, 4, 2, ],
19             [ 8, 9, 1, 6, 0, 4, 3, 5, 2, 7, ],
20             [ 9, 4, 5, 3, 1, 2, 6, 8, 7, 0, ],
21             [ 4, 2, 8, 6, 5, 7, 3, 9, 0, 1, ],
22             [ 2, 7, 9, 3, 8, 0, 6, 4, 1, 5, ],
23             [ 7, 0, 4, 6, 9, 1, 3, 2, 5, 8, ],
24             [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ],
25             [ 1, 5, 7, 6, 2, 8, 3, 0, 9, 4, ],
26             [ 5, 8, 0, 3, 7, 9, 6, 1, 4, 2, ],
27             [ 8, 9, 1, 6, 0, 4, 3, 5, 2, 7, ],
28             ];
29              
30             my $dieder = [
31             [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ],
32             [ 1, 2, 3, 4, 0, 6, 7, 8, 9, 5, ],
33             [ 2, 3, 4, 0, 1, 7, 8, 9, 5, 6, ],
34             [ 3, 4, 0, 1, 2, 8, 9, 5, 6, 7, ],
35             [ 4, 0, 1, 2, 3, 9, 5, 6, 7, 8, ],
36              
37             [ 5, 9, 8, 7, 6, 0, 4, 3, 2, 1, ],
38             [ 6, 5, 9, 8, 7, 1, 0, 4, 3, 2, ],
39             [ 7, 6, 5, 6, 8, 2, 1, 0, 4, 3, ],
40             [ 8, 7, 6, 5, 9, 3, 2, 1, 0, 4, ],
41             [ 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, ],
42             ];
43              
44             sub new {
45 1     1 0 2 my $proto = shift;
46 1         3 my $type = shift;
47 1   33     6 my $class = ref($proto) || $proto;
48 1         2 my $self = bless({}, $class);
49 1         9 $self->{type} = lc($type);
50 1         7 return $self;
51             } # new()
52              
53             sub is_valid {
54 2     2 1 10 my ($self,$number) = @_;
55 2 50       14 if ($number =~ /^(\d+)(\d)$/i) {
56 2 100       10 return 1 if ($2 == $self->_compute_checkdigit(uc($1)));
57             }
58 1         6 return ''
59             } # is_valid()
60              
61             sub complete {
62 1     1 1 108 my ($self,$number) = @_;
63 1 50       8 if ($number =~ /^\d+$/i) {
64 1         4 return $number . $self->_compute_checkdigit(uc($number));
65             }
66 0         0 return '';
67             } # complete()
68              
69             sub basenumber {
70 1     1 1 3 my ($self,$number) = @_;
71 1 50       8 if ($number =~ /^(\d+)(\d)$/i) {
72 1 50       4 return $1 if ($2 == $self->_compute_checkdigit(uc($1)));
73             }
74 0         0 return '';
75             } # basenumber()
76              
77             sub checkdigit {
78 1     1 1 4 my ($self,$number) = @_;
79 1 50       9 if ($number =~ /^(\d+)(\d)$/i) {
80 1 50       5 return $2 if ($2 == $self->_compute_checkdigit(uc($1)));
81             }
82 0         0 return '';
83             } # checkdigit()
84              
85             sub _compute_checkdigit {
86 5     5   10 my $self = shift;
87 5         7 my $number = shift;
88              
89 5         9 my $input = shift;
90 5         8 my $c = 0; # initialize check at 0
91 5         6 my $digit = 0;
92 5         8 my $i = 0; my $r;
  5         8  
93 5         20 foreach $digit (reverse split(//, $number)) {
94             # This was jonathans implementation, his permutation
95             # table is offset by one compared to the one I already
96             # took in MXX_003.pm and reused here
97             # $c = $di->[$c]->[$f->[($i+1) % 8]->[$digit]];
98 35         53 $c = $dieder->[$c]->[$perm->[$i % 8]->[$digit]];
99 35         58 $i++;
100             }
101 5         38 return $inverted[$c];
102             } # _compute_checkdigit()
103              
104             # Preloaded methods go here.
105              
106             1;
107             __END__