File Coverage

blib/lib/Algorithm/Nhash.pm
Criterion Covered Total %
statement 30 30 100.0
branch 6 6 100.0
condition n/a
subroutine 8 8 100.0
pod 2 2 100.0
total 46 46 100.0


line stmt bran cond sub pod time code
1             package Algorithm::Nhash;
2             BEGIN {
3 1     1   60147 $Algorithm::Nhash::DIST = 'Algorithm-Nhash';
4             }
5             BEGIN {
6 1     1   21 $Algorithm::Nhash::VERSION = '0.002';
7             }
8             # ABSTRACT: Exim nhash algorithm
9 1     1   9 use warnings;
  1         2  
  1         36  
10 1     1   7 use strict;
  1         5  
  1         54  
11              
12 1     1   5 use Carp;
  1         3  
  1         121  
13              
14 1     1   6 use vars qw( @ISA @EXPORT_OK );
  1         2  
  1         410  
15             @ISA = qw( Exporter );
16             @EXPORT_OK = qw( nhash );
17              
18              
19             sub new {
20 1     1 1 3 my($class, @div) = @_;
21 1         5 return bless \@div, $class;
22             }
23              
24              
25             my @primes = qw( 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79
26             83 89 97 101 103 107 109 113);
27              
28             sub nhash {
29 6     6 1 25 my($string, @div) = @_;
30 6 100       17 if (ref $string) { # called as a method
31             # $string is actually $self
32 2         14 ($string, @div) = ($div[0], @$string);
33             }
34              
35             #warn "'$string' @div";
36              
37 6         7 my($sum, $i);
38 6         53 foreach my $val (split //, $string) {
39 204         212 $i += 28; $i %= 29;
  204         191  
40 204         258 $sum += $primes[$i] * ord($val);
41             }
42              
43 6 100       43 return $sum unless @div;
44 5         7 my @ret;
45 5         14 while (my $div = pop @div) {
46 9         17 unshift @ret, $sum % $div;
47 9         29 $sum = int($sum / $div);
48             }
49              
50 5 100       41 return wantarray ? @ret : join '/', @ret;
51             }
52              
53              
54             1;
55              
56             __END__