| 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
| |||||||||||||||||||||||||||||
| 24 | 1 | ( $self->{modulus}, $self->{weight} ) = @_ | |||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||
| 26 | else { | ||||||||||||||||||||||||||||
| 27 | 6 | ▶ | $self = { %$self, ref $_[0] ? %{$_[0]} : @_ }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | die 'not enough arguments!' | ||||||||||||||||||||||||||||
| 30 | 7 | ▶ | if !$self->{modulus} || !$self->{weight}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->{"modulus"} and $self->{"weight"}
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"}
| |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 7 | ▶ | if (!ref $self->{weight}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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'} || {}
MC/DC0%0/1
$self->{'trans_table'} || {}
$self->{'trans_table'}
Truth table50%1/2
A
A= $self->{'trans_table'}
| |||||||||||||||||||||||||||||
| 39 | }; | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | $self->{options} = { | ||||||||||||||||||||||||||||
| 42 | 7 | %{$DEFAULT{options}}, | |||||||||||||||||||||||||||
| 43 | 7 | ▶ | %{$self->{options} || {}}, | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->{'options'} || {}
MC/DC0%0/1
$self->{'options'} || {}
$self->{'options'}
Truth table50%1/2
A
A= $self->{'options'}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 66 | 15 | ▶ | $check_digit = 0 if length( $check_digit ) >= 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 86 | 2 | ▶ | $self->{trans_table} = ref $_[0] ? shift : { @_ }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 96 | 146 | ▶ | return $self->{options}{$_[0]} if (@_ == 1) && (!ref $_[0]); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@_ == 1 and not ref $_[0]
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]
| |||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | $self->{options} | ||||||||||||||||||||||||||||
| 99 | 0 | ▶ | = { %{$self->{options}}, ref $_[0] ? %{$_[0]} : @_ }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 116 | 114 | ▶ | die "'$num' does not map to number. Use trans_table method." if $num =~ /\D/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 114 | $num = $weight->[ $i % @$weight ] * $num; | |||||||||||||||||||||||||||
| 119 | 114 | ▶ | if ( !$self->options('runes') ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||