| 1 | #!/usr/bin/perl -w | ||||||||||||
| 2 | # vim: set ts=4 sw=4 tw=78 et si: | ||||||||||||
| 3 | # | ||||||||||||
| 4 | 5 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | |||||||||||||
| 6 | 5 | ▶ | use Algorithm::CheckDigits; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 5 | ▶ | use Getopt::Long; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 5 | ▶ | use Pod::Usage; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | |||||||||||||
| 10 | 5 | ▶ | use version; our $VERSION = qv('v1.3.6'); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | |||||||||||||
| 12 | 5 | my %opt; | |||||||||||
| 13 | |||||||||||||
| 14 | 5 | GetOptions(\%opt, 'algorithm=s', 'help', 'man'); | |||||||||||
| 15 | |||||||||||||
| 16 | 5 | ▶ | pod2usage(-exitstatus => 0, -input => \*DATA) if $opt{help}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 17 | 4 | ▶ | pod2usage(-exitstatus => 0, -verbose => 2, -input => \*DATA) if $opt{man}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 18 | |||||||||||||
| 19 | 3 | ▶ | my $cmd = shift || 'check'; | ||||||||||
|
Condition50%1/2
shift @ARGV || 'check'
MC/DC0%0/1
shift @ARGV || 'check'
shift @ARGV
Truth table50%1/2
A
A= shift @ARGV
| |||||||||||||
| 20 | |||||||||||||
| 21 | 3 | ▶ | pod2usage(0) unless $cmd; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 22 | |||||||||||||
| 23 | 3 | ▶ | if ($cmd =~ /^list(_alg(orithms)?)?$/i) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 24 | 0 | my %descr = Algorithm::CheckDigits::method_descriptions(); | |||||||||||
| 25 | 0 | for my $method (Algorithm::CheckDigits->method_list()) { | |||||||||||
| 26 | 0 | print $method, ': ', $descr{$method}, "\n"; | |||||||||||
| 27 | } | ||||||||||||
| 28 | 0 | exit 0; | |||||||||||
| 29 | } | ||||||||||||
| 30 | 3 | ▶ | if ($cmd =~ /^descr(ibe)?$/i) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 31 | 0 | ▶ | while (my $method = shift @ARGV) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 32 | 0 | print join(': ',Algorithm::CheckDigits->method_descriptions($method)) | |||||||||||
| 33 | , "\n"; | ||||||||||||
| 34 | } | ||||||||||||
| 35 | 0 | exit 0; | |||||||||||
| 36 | } | ||||||||||||
| 37 | |||||||||||||
| 38 | 3 | my $algorithm = determine_algorithm(\%opt); | |||||||||||
| 39 | |||||||||||||
| 40 | 3 | my $cd = CheckDigits($algorithm); | |||||||||||
| 41 | |||||||||||||
| 42 | 3 | ▶ | my $complete = sub { $cd->complete($_[0]); }; | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 43 | 3 | ▶ | my $check = sub { $cd->is_valid($_[0]) ? "valid" : "not valid"; }; | ||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
| |||||||||||||
| 44 | 3 | ▶ | my $checkdigit = sub { $cd->checkdigit($_[0]); }; | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 45 | |||||||||||||
| 46 | 3 | my %cmdtable = ( | |||||||||||
| 47 | complete => $complete, | ||||||||||||
| 48 | check => $check, | ||||||||||||
| 49 | checkdigit => $checkdigit, | ||||||||||||
| 50 | ); | ||||||||||||
| 51 | |||||||||||||
| 52 | 3 | ▶ | if (my $action = $cmdtable{$cmd}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 53 | 3 | ▶ | if (0 < scalar @ARGV) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 54 | 3 | ▶ | while (my $number = shift) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 55 | 3 | printf "%s\n", $action->($number); | |||||||||||
| 56 | } | ||||||||||||
| 57 | } | ||||||||||||
| 58 | else { | ||||||||||||
| 59 | 0 | print STDERR "Reading numbers from STDIN\n"; | |||||||||||
| 60 | 0 | ▶ | while (my $number = <>) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 61 | 0 | $number =~ s/\s+$//; | |||||||||||
| 62 | 0 | printf "%s\n", $action->($number); | |||||||||||
| 63 | } | ||||||||||||
| 64 | } | ||||||||||||
| 65 | } | ||||||||||||
| 66 | else { | ||||||||||||
| 67 | 0 | pod2usage(1); | |||||||||||
| 68 | } | ||||||||||||
| 69 | |||||||||||||
| 70 | #----- only functions following ----- | ||||||||||||
| 71 | 3 | ▶ | sub determine_algorithm { | ||||||||||
|
Subroutine
determine_algorithm: called CC=4 SCAR=23.2
| |||||||||||||
| 72 | 3 | my $opt = shift; | |||||||||||
| 73 | |||||||||||||
| 74 | 3 | ▶ | return $opt->{algorithm} if $opt->{algorithm}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 75 | |||||||||||||
| 76 | 0 | ▶ | if ($0 =~ /^checkdigit(?:s)?-?(.*)$/i) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 77 | 0 | ▶ | return $1 ? $1 : undef; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 78 | } | ||||||||||||
| 79 | 0 | return $0; | |||||||||||
| 80 | } # determine algorithm() | ||||||||||||
| 81 | |||||||||||||
| 82 | __END__ | ||||||||||||