blib/lib/Math/CheckDigits.pm

filter: Statementstmt 97.4% 77 / 79 Branchbran 76.4% 26 / 34 Conditioncond 50.0% 5 / 10 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 100% 12 / 12 Podpod 0.0% 0 / 6 totaltotal 82.3% 121 / 147 CC 25 SCAR 17.1
CC
25
Cov
88%
new
8.3
_calc_check_sum
8.0
options
5.6
CRAP 5.6
↑ Summary
1 package Math::CheckDigits;
2  
3 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 our $VERSION = '0.05';
9 2 $VERSION = eval $VERSION; ## no critic
10  
11 2 my %DEFAULT = (
12 trans_table => {},
13 options => {
14 start_at_right => 1, # multipule
15 DSR => 1, # use DSR or DR
16 runes => 0, # use runes
17 },
18 );
19  
20 7 ▶sub new {
Subroutine new: called CC=8 SCAR=21.1
Pod undocumented
21 7 my $cls = shift;
22 7 my $self = {};
23 7 ▶ if ( @_ == 2 ){
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2) { } 1 6 7
24 1 ( $self->{modulus}, $self->{weight} ) = @_
25 }
26 else {
27 6 ▶ $self = { %$self, ref $_[0] ? %{$_[0]} : @_ };
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 2 4 6
28 }
29 die 'not enough arguments!'
30 7 ▶ if !$self->{modulus} || !$self->{weight};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"modulus"} and $self->{"weight"} 0 7 7
Condition33%1/3
$self->{"modulus"} and $self->{"weight"}
!ll&&!rl&&r
007
MC/DC0%0/2
$self->{"modulus"} and $self->{"weight"}
$self->{"modulus"} $self->{"weight"}
Truth table33%1/3
A and B
A= $self->{"modulus"}B= $self->{"weight"}
ABresult
0X0
100
111
31  
32 7 ▶ if (!ref $self->{weight}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self->{"weight"}) 1 6 7
33 1 $self->{weight} = [$self->{weight}, 1];
34 }
35  
36 $self->{trans_table} = {
37 7 %{$DEFAULT{trans_table}},
38 7 ▶ %{$self->{trans_table} || {}},
Condition50%1/2
$self->{'trans_table'} || {}
l!l
07
MC/DC0%0/1
$self->{'trans_table'} || {}
$self->{'trans_table'}
Truth table50%1/2
A
A= $self->{'trans_table'}
Aresult
11
00
39 };
40  
41 $self->{options} = {
42 7 %{$DEFAULT{options}},
43 7 ▶ %{$self->{options} || {}},
Condition50%1/2
$self->{'options'} || {}
l!l
07
MC/DC0%0/1
$self->{'options'} || {}
$self->{'options'}
Truth table50%1/2
A
A= $self->{'options'}
Aresult
11
00
44 };
45  
46 7 bless $self, $cls;
47 }
48  
49 15 ▶sub checkdigit {
Subroutine checkdigit: called CC=5 SCAR=16.2
Pod undocumented
50 15 my $self = shift;
51 15 my @digits = split //, shift;
52  
53 15 ▶ @digits = reverse @digits if $self->options('start_at_right');
Branch: 1 branch
BranchTrueFalseTotal
if $self->options("start_at_right") 14 1 15
54  
55 15 my $check_sum = $self->_calc_check_sum( $self->{weight}, @digits );
56 15 my $check_digit = $check_sum % $self->{modulus};
57  
58 # DSR or DR ?
59 15 ▶ $check_digit = $self->{modulus} - $check_digit if $self->options('DSR');
Branch: 1 branch
BranchTrueFalseTotal
if $self->options("DSR") 15 0 15
60  
61 # see trans table if exists. ( eg. 16 => 'g' )
62 15 my %trans_table = $self->trans_table;
63 $check_digit =
64 defined $trans_table{$check_digit} ?
65 15 ▶ $trans_table{$check_digit} : $check_digit;
Branch: 1 branch
BranchTrueFalseTotal
defined $trans_table{$check_digit} ? : 1 14 15
66 15 ▶ $check_digit = 0 if length( $check_digit ) >= 2;
Branch: 1 branch
BranchTrueFalseTotal
if length $check_digit >= 2 0 15 15
67  
68 15 return $check_digit;
69 }
70  
71 8 ▶sub is_valid {
Subroutine is_valid: called CC=1 SCAR=0.0
Pod undocumented
72 8 my ( $self, $digits ) = @_;
73 8 ( $digits, my $check_num )
74 = $digits =~ /^(.*)(.)$/;
75 8 return $self->checkdigit( $digits ) == $check_num;
76 }
77  
78 3 ▶sub complete {
Subroutine complete: called CC=1 SCAR=0.0
Pod undocumented
79 3 my ( $self, $digits ) = @_;
80 3 return $digits . $self->checkdigit( $digits );
81 }
82  
83 32 ▶sub trans_table {
Subroutine trans_table: called CC=3 SCAR=11.0
Pod undocumented
84 32 my $self = shift;
85 32 ▶ if ( @_ ){
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 2 30 32
86 2 ▶ $self->{trans_table} = ref $_[0] ? shift : { @_ };
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 2 2
87 2 return $self;
88 }
89 30 return %{$self->{trans_table}};
90 }
91  
92 146 ▶sub options {
Subroutine options: called CC=5 SCAR=17.3
Pod undocumented
93 146 my $self = shift;
94  
95 0 ▶ return %{$self->{options}} if @_ == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 0 0 146 146
96 146 ▶ return $self->{options}{$_[0]} if (@_ == 1) && (!ref $_[0]);
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 and not ref $_[0] 144 2 146
Condition66%2/3
@_ == 1 and not ref $_[0]
!ll&&!rl&&r
20144
MC/DC50%1/2
@_ == 1 and not ref $_[0]
@_ == 1 not ref $_[0]
Truth table66%2/3
A and not(B)
A= @_ == 1B= not ref $_[0]
ABresult
0X0
100
111
97  
98 $self->{options}
99 0 ▶ = { %{$self->{options}}, ref $_[0] ? %{$_[0]} : @_ };
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 2 2
100  
101 2 return $self;
102 }
103  
104 15 ▶sub _calc_check_sum {
Subroutine _calc_check_sum: called CC=8 SCAR=20.8
105 15 my $self = shift;
106 15 my ( $weight, @digits ) = @_;
107  
108 15 my %trans_table = reverse $self->trans_table;
109 15 for ( keys %trans_table ){
110 11 ▶ delete $trans_table{$_} if /\d/;
Branch: 1 branch
BranchTrueFalseTotal
if /\d/ 0 11 11
111 }
112  
113 15 my ( $i, $check_sum ) = ( 0, 0 );
114 15 for my $digit ( @digits ){
115 114 ▶ my $num = defined $trans_table{$digit} ? $trans_table{$digit} : $digit;
Branch: 1 branch
BranchTrueFalseTotal
defined $trans_table{$digit} ? : 3 111 114
116 114 ▶ die "'$num' does not map to number. Use trans_table method." if $num =~ /\D/;
Branch: 1 branch
BranchTrueFalseTotal
if $num =~ /\D/ 0 114 114
117  
118 114 $num = $weight->[ $i % @$weight ] * $num;
119 114 ▶ if ( !$self->options('runes') ){
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->options('runes')) { } 108 6 114
120 108 $check_sum += $num;
121 }
122 else{
123 6 my @nums = split //, $num;
124 6 $check_sum += $_ for @nums;
125 }
126 114 $i++;
127 }
128  
129 15 return $check_sum;
130 }
131  
132 2 1;
133 __END__
↑ Summary