| 1 | package Business::DK::CVR; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 12 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 12 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 12 | ▶ | use vars qw($VERSION @EXPORT_OK); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 12 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 12 | ▶ | use Params::Validate qw(validate_pos SCALAR OBJECT ARRAYREF ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 12 | ▶ | use Readonly; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 12 | ▶ | use 5.008; #5.8.0 | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 12 | ▶ | use base qw(Exporter); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 12 | $VERSION = '0.12'; | |||||||||||||||||||||||||||
| 14 | 12 | @EXPORT_OK = qw(validate validateCVR generate _calculate_sum); | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 12 | ▶ | use constant MODULUS_OPERAND => 11; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 12 | ▶ | use constant MAX_CVRS => 9090908; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 12 | ▶ | use constant VALID => 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 12 | ▶ | use constant INVALID => 0; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 12 | Readonly my @controlcifers => qw(2 7 6 5 4 3 2 1); | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 7 | ▶ | sub validateCVR { | ||||||||||||||||||||||||||
|
Subroutine
validateCVR: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 24 | 7 | return validate(shift); | |||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 140 | ▶ | sub validate { | ||||||||||||||||||||||||||
|
Subroutine
validate: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 28 | 140 | my ($controlnumber) = @_; | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 140 | validate_pos( @_, { type => SCALAR, regex => qr/^\d{8}$/ } ); | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 129 | my $sum = _calculate_sum( $controlnumber, \@controlcifers ); | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 129 | ▶ | if ( $sum % MODULUS_OPERAND ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | 109 | return INVALID; | |||||||||||||||||||||||||||
| 36 | } else { | ||||||||||||||||||||||||||||
| 37 | 20 | return VALID; | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 129 | ▶ | sub _calculate_sum { | ||||||||||||||||||||||||||
|
Subroutine
_calculate_sum: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 42 | 129 | my ( $number, $controlcifers ) = @_; | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 129 | validate_pos( @_, | |||||||||||||||||||||||||||
| 45 | { type => SCALAR, regex => qr/^\d+$/ }, | ||||||||||||||||||||||||||||
| 46 | { type => ARRAYREF }, | ||||||||||||||||||||||||||||
| 47 | ); | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 129 | my $sum = 0; | |||||||||||||||||||||||||||
| 50 | 129 | my @numbers = split //smx, $number; | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 129 | ▶ | for ( my $i = 0; $i < scalar @numbers; $i++ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 1032 | $sum += $numbers[$i] * $controlcifers->[$i]; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | 129 | return $sum; | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 8 | ▶ | sub generate { | ||||||||||||||||||||||||||
|
Subroutine
generate: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||||||||
| 59 | 8 | my @array = validate_pos( @_, | |||||||||||||||||||||||||||
| 60 | { type => OBJECT | SCALAR, optional => 1 }, | ||||||||||||||||||||||||||||
| 61 | { type => SCALAR, optional => 1, default => 1 }, | ||||||||||||||||||||||||||||
| 62 | { type => SCALAR, optional => 1, default => 1 }, | ||||||||||||||||||||||||||||
| 63 | ); | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 8 | my ( $self, $amount, $seed ) = @array; | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 8 | ▶ | if ( defined $self and $self =~ m/\d+/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $self and $self =~ /\d+/
MC/DC100%2/2
defined $self and $self =~ /\d+/
defined $self
$self =~ /\d+/
Truth table100%3/3
A and B
A= defined $selfB= $self =~ /\d+/
| |||||||||||||||||||||||||||||
| 68 | 2 | $seed = $amount; | |||||||||||||||||||||||||||
| 69 | 2 | $amount = $self; | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 8 | my @cvrs; | |||||||||||||||||||||||||||
| 73 | my $cvr; | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 8 | ▶ | if ( $amount > MAX_CVRS ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 1 | croak 'The amount requested exceeds the maximum possible valid CVRs (' | |||||||||||||||||||||||||||
| 77 | . MAX_CVRS . ')'; | ||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 7 | my $count = $amount; | |||||||||||||||||||||||||||
| 81 | 7 | ▶ | while ($count) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 113 | $cvr = sprintf '%08d', $seed; | |||||||||||||||||||||||||||
| 83 | 113 | ▶ | if ( validate($cvr) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 9 | push @cvrs, $cvr; | |||||||||||||||||||||||||||
| 85 | 9 | $count--; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | 113 | $seed++; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 7 | ▶ | if (wantarray) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 2 | return @cvrs; | |||||||||||||||||||||||||||
| 92 | } else { | ||||||||||||||||||||||||||||
| 93 | 5 | ▶ | if ( $amount == 1 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 3 | return $cvr; | |||||||||||||||||||||||||||
| 95 | } else { | ||||||||||||||||||||||||||||
| 96 | 2 | return \@cvrs; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 12 | 1; | |||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | __END__ | ||||||||||||||||||||||||||||