blib/lib/Math/CheckDigits.pm

filter: Statementstmt 25.3% 18 / 71 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 41.6% 5 / 12 Podpod 0.0% 0 / 6 totaltotal 17.5% 23 / 131 SCAR 57.2
CC
22
Cov
17%
_calc_check_sum
72.0
new
30.0
checkdigit
30.0
CRAP 303.6
« inc/Test/More.pm ↑ Summary
1 package Math::CheckDigits;
2  
3 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 our $VERSION = '0.01_01';
9 1 $VERSION = eval $VERSION;
10  
11 1 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 sub new {
21 0 ▶ my $cls = shift;
Subroutine new: not called CC=5 SCAR=34.0
Pod undocumented
22 0 my $self = \%DEFAULT;
23 0 ▶ if ( @_ == 2 ){
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2) { } 0 0 0
24 0 ( $self->{modulus}, $self->{weight} ) = @_
25 }
26 else{
27 0 ▶ $self = { %$self, ref $_[0] ? %{$_[0]} : @_ };
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
28 }
29 die 'not enough arguments!'
30 0 ▶ if !$self->{modulus} || !$self->{weight};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"modulus"} and $self->{"weight"} 0 0 0
Condition0%0/3
$self->{"modulus"} and $self->{"weight"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"modulus"} and $self->{"weight"}
$self->{"modulus"} $self->{"weight"}
Truth table0%0/3
A and B
A= $self->{"modulus"}B= $self->{"weight"}
ABresult
0X0
100
111
31
32 0 bless $self, $cls;
33 }
34  
35 sub checkdigit {
36 0 ▶ my $self = shift;
Subroutine checkdigit: not called CC=5 SCAR=34.0
Pod undocumented
37 0 my @digits = split //, shift;
38
39 0 ▶ @digits = reverse @digits if $self->options('start_at_right');
Branch: 1 branch
BranchTrueFalseTotal
if $self->options("start_at_right") 0 0 0
40
41 # only support non runes format at present.
42 0 my $check_sum = $self->_calc_check_sum( $self->{weight}, @digits );
43 0 my $check_digit = $check_sum % $self->{modulus};
44
45 # DSR or DR ?
46 0 ▶ $check_digit = $self->{modulus} - $check_digit if $self->options('DSR');
Branch: 1 branch
BranchTrueFalseTotal
if $self->options("DSR") 0 0 0
47
48 # see trans table if exists. ( eg. 16 => 'g' )
49 0 my %trans_table = $self->trans_table;
50 $check_digit =
51 defined $trans_table{$check_digit} ?
52 0 ▶ $trans_table{$check_digit} : $check_digit;
Branch: 1 branch
BranchTrueFalseTotal
defined $trans_table{$check_digit} ? : 0 0 0
53 0 ▶ $check_digit = 0 if length( $check_digit ) >= 2;
Branch: 1 branch
BranchTrueFalseTotal
if length $check_digit >= 2 0 0 0
54
55 0 return $check_digit;
56 }
57  
58 sub is_valid {
59 0 ▶ my ( $self, $digits ) = @_;
Subroutine is_valid: not called CC=1 SCAR=6.9
Pod undocumented
60 0 ( $digits, my $check_num )
61 = $digits =~ /^(.*)(.)$/;
62 0 return $self->checkdigit( $digits ) == $check_num;
63 }
64  
65 sub complete {
66 0 ▶ my ( $self, $digits ) = @_;
Subroutine complete: not called CC=1 SCAR=6.9
Pod undocumented
67 0 return $digits . $self->checkdigit( $digits );
68 }
69  
70 sub trans_table {
71 0 ▶ my $self = shift;
Subroutine trans_table: not called CC=3 SCAR=24.8
Pod undocumented
72 0 ▶ if ( @_ ){
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
73 0 ▶ $self->{TRANS_TABLE} = ref $_[0] ? shift : { @_ };
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
74 0 return $self;
75 }
76 0 return %{$self->{TRANS_TABLE}};
77 }
78  
79 sub options {
80 0 ▶ my $self = shift;
Subroutine options: not called CC=5 SCAR=34.0
Pod undocumented
81
82 0 ▶ return %{$self->{OPTIONS}} if @_ == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 0 0 0 0
83 0 ▶ return $self->{OPTIONS}{$_[0]} if (@_ == 1) && (!ref $_[0]);
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 and not ref $_[0] 0 0 0
Condition0%0/3
@_ == 1 and not ref $_[0]
!ll&&!rl&&r
000
MC/DC0%0/2
@_ == 1 and not ref $_[0]
@_ == 1 not ref $_[0]
Truth table0%0/3
A and not(B)
A= @_ == 1B= not ref $_[0]
ABresult
0X0
100
111
84
85 $self->{OPTIONS}
86 0 ▶ = { %{$self->{OPTIONS}}, ref $_[0] ? %{$_[0]} : @_ };
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
87
88 0 return $self;
89 }
90  
91 sub _calc_check_sum {
92 0 ▶ my $self = shift;
Subroutine _calc_check_sum: not called CC=8 SCAR=42.8
93 0 my ( $weight, @digits ) = @_;
94
95 0 my %trans_table = reverse $self->trans_table;
96 0 for ( keys %trans_table ){
97 0 ▶ delete $trans_table{$_} if /\d/;
Branch: 1 branch
BranchTrueFalseTotal
if /\d/ 0 0 0
98 }
99
100 0 my ( $i, $check_sum ) = ( 0, 0 );
101 0 for my $digit ( @digits ){
102 0 ▶ my $num = defined $trans_table{$digit} ? $trans_table{$digit} : $digit;
Branch: 1 branch
BranchTrueFalseTotal
defined $trans_table{$digit} ? : 0 0 0
103 0 ▶ die "'$num' does not map to number. Use trans_table method." if $num =~ /\D/;
Branch: 1 branch
BranchTrueFalseTotal
if $num =~ /\D/ 0 0 0
104
105 0 $num = $weight->[ $i % @$weight ] * $num;
106 0 ▶ if ( !$self->options('runes') ){
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->options('runes')) { } 0 0 0
107 0 $check_sum += $num;
108 }
109 else{
110 0 my @nums = split //, $num;
111 0 $check_sum += $_ for @nums;
112 }
113 0 $i++;
114 }
115
116 0 return $check_sum;
117 }
118  
119 1 1;
120 __END__
« inc/Test/More.pm ↑ Summary