File Coverage

blib/lib/Algorithm/Verhoeff.pm
Criterion Covered Total %
statement 48 48 100.0
branch 2 2 100.0
condition n/a
subroutine 6 6 100.0
pod 2 2 100.0
total 58 58 100.0


line stmt bran cond sub pod time code
1             package Algorithm::Verhoeff;
2            
3 1     1   30205 use 5.0;
  1         3  
  1         41  
4 1     1   7 use strict;
  1         2  
  1         35  
5 1     1   4 use warnings;
  1         6  
  1         446  
6             #use bignum; # Needed so large numbers don't get turned into standard form
7            
8             require Exporter;
9            
10             our @ISA = qw(Exporter);
11            
12             # Items to export into callers namespace by default. Note: do not export
13             # names by default without a very good reason. Use EXPORT_OK instead.
14             # Do not simply export all your public functions/methods/constants.
15            
16             # This allows declaration use Algorithm::Verhoeff ':all';
17             # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
18             # will save memory.
19             our %EXPORT_TAGS = ( 'all' => [ qw(
20             verhoeff_get verhoeff_check
21             ) ] );
22            
23             our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
24            
25             our @EXPORT = qw(
26             verhoeff_get verhoeff_check
27             );
28            
29             our $VERSION = '0.3';
30            
31            
32             # Preloaded methods go here.
33            
34             our $di; #Dihedral matrix
35             our @inverted = (0, 4, 3, 2, 1, 5, 6, 7, 8, 9 );
36             our $f;
37            
38             # First, build $f according to a simple(?) equation
39             BEGIN{
40 1     1   5 $f->[0] = [(0 .. 9)];
41 1         3 $f->[1] = [( 1, 5, 7, 6, 2, 8, 3, 0, 9, 4 )];
42 1         2 my $i=2;
43 1         2 my $j=0;
44 1         5 while($i < 8)
45             {
46 6         12 while($j < 10)
47             {
48 60         121 $f->[$i]->[$j] = $f->[$i - 1]->[$f->[1]->[$j]];
49 60         103 $j++;
50             }
51 6         5 $j = 0;
52 6         12 $i++;
53             }
54            
55             # This is defined
56 1         4 $di->[0] = [( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 )];
57 1         4 $di->[1] = [( 1, 2, 3, 4, 0, 6, 7, 8, 9, 5 )];
58 1         4 $di->[2] = [( 2, 3, 4, 0, 1, 7, 8, 9, 5, 6 )];
59 1         2 $di->[3] = [( 3, 4, 0, 1, 2, 8, 9, 5, 6, 7 )];
60 1         4 $di->[4] = [( 4, 0, 1, 2, 3, 9, 5, 6, 7, 8 )];
61 1         3 $di->[5] = [( 5, 9, 8, 7, 6, 0, 4, 3, 2, 1 )];
62 1         3 $di->[6] = [( 6, 5, 9, 8, 7, 1, 0, 4, 3, 2 )];
63 1         3 $di->[7] = [( 7, 6, 5, 9, 8, 2, 1, 0, 4, 3 )];
64 1         4 $di->[8] = [( 8, 7, 6, 5, 9, 3, 2, 1, 0, 4 )];
65 1         274 $di->[9] = [( 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 )];
66            
67             }
68            
69             # Now that's all set up, we can actually do stuff.
70            
71             sub verhoeff_check
72             {
73 3     3 1 5 my $input = shift;
74 3         4 my $c = 0; # initialize check at 0
75 3         3 my $digit;
76 3         4 my $i = 0;
77 3         16 foreach $digit (reverse split(//, $input))
78             {
79 76         98 $c = $di->[$c]->[$f->[$i % 8]->[$digit]]; # did you catch that?
80 76         78 $i++;
81             }
82 3 100       11 if($c)
83             {
84 1         4 return 0; # a non-zero value of $c is a check failure
85             }
86 2         12 return 1;
87             }
88            
89             sub verhoeff_get
90             {
91 2     2 1 9 my $input = shift;
92 2         3 my $c = 0; # initialize check at 0
93 2         2 my $digit = 0;
94 2         3 my $i = 0; my $r;
  2         3  
95 2         23 foreach $digit (reverse split(//, $input))
96             {
97 61         101 $c = $di->[$c]->[$f->[($i+1) % 8]->[$digit]]; # not quite the same...
98 61         64 $i++;
99             }
100 2         16 return $inverted[$c];
101             }
102             1;
103             __END__