File Coverage

blib/lib/Algorithm/CheckDigits/M11_003.pm
Criterion Covered Total %
statement 44 49 89.8
branch 10 18 55.5
condition 1 3 33.3
subroutine 11 11 100.0
pod 4 5 80.0
total 70 86 81.4


line stmt bran cond sub pod time code
1             package Algorithm::CheckDigits::M11_003;
2              
3 1     1   18 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         4  
7              
8 1     1   33 use version; our $VERSION = 'v1.3.6';
  1         2  
  1         4  
9              
10             our @ISA = qw(Algorithm::CheckDigits);
11              
12             my @weight = ( 4,2,1,6,3,7,9,10,5,8,4,2 );
13              
14             sub new {
15 2     2 0 4 my $proto = shift;
16 2         4 my $type = shift;
17 2   33     8 my $class = ref($proto) || $proto;
18 2         6 my $self = bless({}, $class);
19 2         9 $self->{type} = lc($type);
20 2         11 return $self;
21             } # new()
22              
23             sub is_valid {
24 4     4 1 14 my ($self,$number) = @_;
25 4 50       18 if ($number =~ /^(\d{11})(\d)$/) {
26 4         11 return $2 == $self->_compute_checkdigit($1);
27             }
28 0         0 return ''
29             } # is_valid()
30              
31             sub complete {
32 2     2 1 174 my ($self,$number) = @_;
33 2 50       10 if ($number =~ /^\d{11}$/) {
34 2         8 my $cd = $self->_compute_checkdigit($number);
35 2 50       13 return 0 > $cd ? '' : $number . $cd;
36             }
37 0         0 return '';
38             } # complete()
39              
40             sub basenumber {
41 2     2 1 6 my ($self,$number) = @_;
42 2 50       10 if ($number =~ /^(\d{11})(\d)$/) {
43 2 50       6 return $1 if ($2 == $self->_compute_checkdigit($1));
44             }
45 0         0 return '';
46             } # basenumber()
47              
48             sub checkdigit {
49 2     2 1 5 my ($self,$number) = @_;
50 2 50       10 if ($number =~ /^(\d{11})(\d)$/) {
51 2 50       5 return $2 if ($2 == $self->_compute_checkdigit($1));
52             }
53 0         0 return '';
54             } # checkdigit()
55              
56             sub _compute_checkdigit {
57 10     10   16 my $self = shift;
58 10         22 my $number = shift;
59              
60 10 50       27 if ($number =~ /^\d{11}$/) {
61              
62 10         37 my @digits = split(//,$number);
63 10         16 my $sum = 0;
64 10         13 my $cf = 0;
65              
66 10         24 for (my $i = 0; $i <= $#digits; $i++) {
67              
68 110         220 $sum += $weight[$i] * $digits[$i];
69              
70             }
71 10         15 $sum %= 11;
72 10         20 for (my $i = 0; $i <= 9; $i++) {
73 65 100       182 return $i
74             if (10 == ($sum + $weight[11] * $i) % 11);
75             }
76             }
77 0           return -1;
78             } # _compute_checkdigit()
79              
80             # Preloaded methods go here.
81              
82             1;
83             __END__