File Coverage

blib/lib/App/ISBN/Format.pm
Criterion Covered Total %
statement 22 48 45.8
branch 0 10 0.0
condition 0 6 0.0
subroutine 7 8 87.5
pod 2 2 100.0
total 31 74 41.8


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