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