File Coverage

blib/lib/App/ISBN/Format.pm
Criterion Covered Total %
statement 43 48 89.5
branch 7 10 70.0
condition 4 6 66.6
subroutine 8 8 100.0
pod 2 2 100.0
total 64 74 86.4


line stmt bran cond sub pod time code
1             package App::ISBN::Format;
2              
3 4     4   195855 use strict;
  4         36  
  4         168  
4 4     4   43 use warnings;
  4         8  
  4         234  
5              
6 4     4   2574 use Business::ISBN;
  4         430875  
  4         261  
7 4     4   2672 use Class::Utils qw(set_params);
  4         80802  
  4         105  
8 4     4   341 use Error::Pure qw(err);
  4         8  
  4         168  
9 4     4   2464 use Getopt::Std;
  4         10323  
  4         2111  
10              
11             our $VERSION = 0.03;
12              
13             # Constructor.
14             sub new {
15 6     6 1 1173617 my ($class, @params) = @_;
16              
17             # Create object.
18 6         23 my $self = bless {}, $class;
19              
20             # Process parameters.
21 6         38 set_params($self, @params);
22              
23             # Object.
24 6         55 return $self;
25             }
26              
27             # Run.
28             sub run {
29 5     5 1 11 my $self = shift;
30              
31             # Process arguments.
32 5         23 $self->{'_opts'} = {
33             'h' => 0,
34             };
35 5 100 66     17 if (! getopts('h', $self->{'_opts'}) || @ARGV < 1
      66        
36             || $self->{'_opts'}->{'h'}) {
37              
38 2         671 print STDERR "Usage: $0 [-h] [--version] isbn_string\n";
39 2         53 print STDERR "\t-h\t\tPrint help.\n";
40 2         21 print STDERR "\t--version\tPrint version.\n";
41 2         19 print STDERR "\tisbn_string\tISBN string.\n";
42 2         10 return 1;
43             }
44 3         90 $self->{'_isbn_string'} = shift @ARGV;
45              
46 3         27 my $isbn_obj = Business::ISBN->new($self->{'_isbn_string'});
47 3 50       1005 if (! $isbn_obj) {
48 0         0 err "ISBN '$self->{'_isbn_string'}' is bad.";
49 0         0 return 1;
50             }
51              
52             # Validation.
53 3 100       12 if (! $isbn_obj->is_valid) {
54 1         17 $isbn_obj->fix_checksum;
55              
56             # Check again.
57 1 50       95 if (! $isbn_obj->is_valid) {
58 0         0 err "ISBN '$self->{'_isbn_string'}' is not valid.";
59 0         0 return 1;
60             }
61             }
62              
63             # Construct output.
64 3         15 my $isbn_concrete;
65 3         7 my $isbn_without_dash = $self->{'_isbn_string'};
66 3         27 $isbn_without_dash =~ s/-//msg;
67 3 50       10 if (length $isbn_without_dash > 10) {
68 3         29 $isbn_concrete = $isbn_obj->as_isbn13;
69             } else {
70 0         0 $isbn_concrete = $isbn_obj->as_isbn10;
71             }
72              
73 3         1287 print $self->{'_isbn_string'}.' -> '.$isbn_concrete->as_string, "\n";
74            
75 3         2428 return 0;
76             }
77              
78             1;
79              
80             __END__