File Coverage

blib/lib/App/ISBN/Check.pm
Criterion Covered Total %
statement 54 54 100.0
branch 12 12 100.0
condition 4 6 66.6
subroutine 9 9 100.0
pod 2 2 100.0
total 81 83 97.5


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