| 1 | package App::ISBN::Check; | ||
| 2 | |||
| 3 | 4 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 4 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | 0 | ▶ | use Business::ISBN; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 7 | use Class::Utils qw(set_params); | ||
| 8 | use Error::Pure qw(err); | ||
| 9 | use Getopt::Std; | ||
| 10 | use Perl6::Slurp qw(slurp); | ||
| 11 | |||
| 12 | our $VERSION = 0.01; | ||
| 13 | |||
| 14 | # Constructor. | ||
| 15 | sub new { | ||
| 16 | my ($class, @params) = @_; | ||
| 17 | |||
| 18 | # Create object. | ||
| 19 | my $self = bless {}, $class; | ||
| 20 | |||
| 21 | # Process parameters. | ||
| 22 | set_params($self, @params); | ||
| 23 | |||
| 24 | # Object. | ||
| 25 | return $self; | ||
| 26 | } | ||
| 27 | |||
| 28 | # Run. | ||
| 29 | sub run { | ||
| 30 | my $self = shift; | ||
| 31 | |||
| 32 | # Process arguments. | ||
| 33 | $self->{'_opts'} = { | ||
| 34 | 'h' => 0, | ||
| 35 | }; | ||
| 36 | if (! getopts('h', $self->{'_opts'}) || @ARGV < 1 | ||
| 37 | || $self->{'_opts'}->{'h'}) { | ||
| 38 | |||
| 39 | print STDERR "Usage: $0 [-h] [--version] file_with_isbns\n"; | ||
| 40 | print STDERR "\t-h\t\tPrint help.\n"; | ||
| 41 | print STDERR "\t--version\tPrint version.\n"; | ||
| 42 | print STDERR "\tfile_with_isbns\tFile with ISBN strings, one per line.\n"; | ||
| 43 | return 1; | ||
| 44 | } | ||
| 45 | $self->{'_file'} = shift @ARGV; | ||
| 46 | |||
| 47 | my @isbns = slurp($self->{'_file'}, { chomp => 1 }); | ||
| 48 | |||
| 49 | foreach my $isbn (@isbns) { | ||
| 50 | my $isbn_obj = Business::ISBN->new($isbn); | ||
| 51 | if (! $isbn_obj) { | ||
| 52 | print STDERR $isbn.": Cannot parse.\n"; | ||
| 53 | next; | ||
| 54 | } | ||
| 55 | if (! $isbn_obj->is_valid) { | ||
| 56 | $isbn_obj->fix_checksum; | ||
| 57 | } | ||
| 58 | if (! $isbn_obj->is_valid) { | ||
| 59 | print STDERR $isbn.": Not valid.\n"; | ||
| 60 | next; | ||
| 61 | } | ||
| 62 | |||
| 63 | my $isbn_concrete; | ||
| 64 | my $isbn_without_dash = $isbn; | ||
| 65 | $isbn_without_dash =~ s/-//msg; | ||
| 66 | if (length $isbn_without_dash > 10) { | ||
| 67 | $isbn_concrete = $isbn_obj->as_isbn13; | ||
| 68 | } else { | ||
| 69 | $isbn_concrete = $isbn_obj->as_isbn10; | ||
| 70 | } | ||
| 71 | if ($isbn ne $isbn_concrete->as_string) { | ||
| 72 | print STDERR $isbn.": Different after format (".$isbn_concrete->as_string.").\n"; | ||
| 73 | } | ||
| 74 | } | ||
| 75 | |||
| 76 | return 0; | ||
| 77 | } | ||
| 78 | |||
| 79 | 1; | ||
| 80 | |||
| 81 | __END__ | ||