ISBN/Driver.pm

filter: Statementstmt 100% 79 / 79 Branchbran 100% 38 / 38 Conditioncond 84.2% 16 / 19 MC/DCmcdc 66.6% 8 / 12 Subroutinesub 100% 14 / 14 Podpod 100% 10 / 10 totaltotal 95.9% 165 / 172 SCAR 33.0
CC
27
Cov
98%
convert_to_isbn10
9.0
convert_to_ean13
9.0
is_valid
7.0
CRAP 27.0
↑ Summary ISBN.pm »
1 package WWW::Scraper::ISBN::Driver;
2  
3 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 8 our $VERSION = '1.04';
7  
8 #----------------------------------------------------------------------------
9 # Library Modules
10  
11 8 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 #----------------------------------------------------------------------------
14 # Public API
15  
16 # Preloaded methods go here.
17 sub new {
18 11 ▶ my $proto = shift;
Subroutine new: called CC=2 SCAR=7.0
Pod documented
19 11 ▶ my $class = ref($proto) || $proto;
Condition66%2/3
ref $proto || $proto
l!l&&r!l&&!r
1100
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table66%2/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
20  
21 11 my $self = {
22 FOUND => 0,
23 VERBOSITY => 0,
24 BOOK => undef,
25 ERROR => ''
26 };
27
28 11 bless ($self, $class);
29 11 return $self;
30 }
31  
32 15 ▶sub found { my $self = shift; return $self->_accessor('FOUND',@_) }
Subroutine found: called CC=1 SCAR=0.0
Pod documented
33 6 ▶sub verbosity { my $self = shift; return $self->_accessor('VERBOSITY',@_) }
Subroutine verbosity: called CC=1 SCAR=0.0
Pod documented
34 6 ▶sub book { my $self = shift; return $self->_accessor('BOOK',@_) }
Subroutine book: called CC=1 SCAR=0.0
Pod documented
35 8 ▶sub error { my $self = shift; return $self->_accessor('ERROR',@_) }
Subroutine error: called CC=1 SCAR=0.0
Pod documented
36  
37 sub _accessor {
38 35 ▶ my $self = shift;
Subroutine _accessor: called CC=2 SCAR=6.9
39 35 my $accessor = shift;
40 35 ▶ if (@_) { $self->{$accessor} = shift };
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 14 21 35
41 35 return $self->{$accessor};
42 }
43  
44 sub search {
45 2 ▶ croak(q{Child class must overload 'search()' method.});
Subroutine search: called CC=1 SCAR=0.0
Pod documented
46 }
47  
48 #----------------------------------------------------------------------------
49 # Internal Class methods
50  
51 # a generic method for storing the error & setting not found
52 sub handler {
53 4 ▶ my $self = shift;
Subroutine handler: called CC=3 SCAR=11.0
Pod documented
54 4 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 3 1 4
55 3 $self->{ERROR} = shift;
56 3 ▶ print "Error: $self->{ERROR}\n" if $self->verbosity;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbosity 1 2 3
57 };
58 4 return $self->found(0);
59 }
60  
61 sub convert_to_ean13 {
62 23 ▶ my $self = shift;
Subroutine convert_to_ean13: called CC=9 SCAR=22.0
Pod documented
63 23 ▶ my $isbn = shift || return;
Condition50%1/2
shift() || (return)
l!l
230
MC/DC0%0/1
shift() || (return)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
64 23 my $prefix;
65  
66 23 ▶ return unless(length $isbn == 10 || length $isbn == 13);
Branch: 1 branch
BranchTrueFalseTotal
unless length $isbn == 10 or length $isbn == 13 1 22 23
Condition100%3/3
length $isbn == 10 or length $isbn == 13
l!l&&r!l&&!r
8141
MC/DC100%2/2
length $isbn == 10 or length $isbn == 13
length $isbn == 10 length $isbn == 13
Truth table100%3/3
A or B
A= length $isbn == 10B= length $isbn == 13
ABresult
1X1
011
000
67  
68 22 ▶ if(length $isbn == 13) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $isbn == 13) { } 14 8 22
69 14 ▶ return if($isbn !~ /^(978|979)(\d{10})$/);
Branch: 1 branch
BranchTrueFalseTotal
unless $isbn =~ /^(978|979)(\d{10})$/ 2 12 14
70 12 ($prefix,$isbn) = ($1,$2);
71 } else {
72 8 ▶ return if($isbn !~ /^(\d{10}|\d{9}X)$/);
Branch: 1 branch
BranchTrueFalseTotal
unless $isbn =~ /^(\d{10}|\d{9}X)$/ 1 7 8
73 7 $prefix = '978';
74 }
75  
76 19 my $isbn13 = $prefix . $isbn;
77 19 chop($isbn13);
78 19 my @isbn = split(//,$isbn13);
79 19 my ($lsum,$hsum) = (0,0);
80 19 ▶ while(@isbn) {
Branch: 1 branch
BranchTrueFalseTotal
if (@isbn) 114 19 133
81 114 $hsum += shift @isbn;
82 114 $lsum += shift @isbn;
83 }
84  
85 19 my $csum = ($lsum * 3) + $hsum;
86 19 $csum %= 10;
87 19 ▶ $csum = 10 - $csum if($csum != 0);
Branch: 1 branch
BranchTrueFalseTotal
if $csum != 0 17 2 19
88  
89 19 return $isbn13 . $csum;
90 }
91  
92 sub convert_to_isbn10 {
93 16 ▶ my $self = shift;
Subroutine convert_to_isbn10: called CC=9 SCAR=22.0
Pod documented
94 16 ▶ my $ean = shift || return;
Condition50%1/2
shift() || (return)
l!l
160
MC/DC0%0/1
shift() || (return)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
95 16 my ($isbn,$isbn10);
96  
97 16 ▶ return unless(length $ean == 10 || length $ean == 13);
Branch: 1 branch
BranchTrueFalseTotal
unless length $ean == 10 or length $ean == 13 1 15 16
Condition100%3/3
length $ean == 10 or length $ean == 13
l!l&&r!l&&!r
5101
MC/DC100%2/2
length $ean == 10 or length $ean == 13
length $ean == 10 length $ean == 13
Truth table100%3/3
A or B
A= length $ean == 10B= length $ean == 13
ABresult
1X1
011
000
98  
99 15 ▶ if(length $ean == 13) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $ean == 13) { } 10 5 15
100 10 ▶ return if($ean !~ /^(?:978|979)(\d{9})\d$/);
Branch: 1 branch
BranchTrueFalseTotal
unless $ean =~ /^(?:978|979)(\d{9})\d$/ 1 9 10
101 9 ($isbn,$isbn10) = ($1,$1);
102 } else {
103 5 ▶ return if($ean !~ /^(\d{9})[\dX]$/);
Branch: 1 branch
BranchTrueFalseTotal
unless $ean =~ /^(\d{9})[\dX]$/ 1 4 5
104 4 ($isbn,$isbn10) = ($1,$1);
105 }
106  
107 13 my ($csum, $pos, $digit) = (0, 0, 0);
108 13 ▶ for ($pos = 9; $pos > 0; $pos--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos > 0) 117 13 130
109 117 $digit = $isbn % 10;
110 117 $isbn /= 10; # Decimal shift ISBN for next time
111 117 $csum += ($pos * $digit);
112 }
113 13 $csum %= 11;
114 13 ▶ $csum = 'X' if ($csum == 10);
Branch: 1 branch
BranchTrueFalseTotal
if $csum == 10 1 12 13
115 13 return $isbn10 . $csum;
116 }
117  
118 sub is_valid {
119 11 ▶ my $self = shift;
Subroutine is_valid: called CC=7 SCAR=19.5
Pod documented
120 11 ▶ my $isbn = shift or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $isbn = shift() or return 0 1 10 11
121  
122 # validate and convert into EAN13 format
123 10 my $ean = $self->convert_to_ean13($isbn);
124  
125 10 ▶ return 0 if(!$ean);
Branch: 1 branch
BranchTrueFalseTotal
unless $ean 1 9 10
126 9 ▶ return 0 if(length $isbn == 13 && $isbn ne $ean);
Branch: 1 branch
BranchTrueFalseTotal
if length $isbn == 13 and $isbn ne $ean 1 8 9
Condition100%3/3
length $isbn == 13 and $isbn ne $ean
!ll&&!rl&&r
351
MC/DC100%2/2
length $isbn == 13 and $isbn ne $ean
length $isbn == 13 $isbn ne $ean
Truth table100%3/3
A and B
A= length $isbn == 13B= $isbn ne $ean
ABresult
0X0
100
111
127 8 ▶ return 0 if(length $isbn == 10 && $isbn ne $self->convert_to_isbn10($ean));
Branch: 1 branch
BranchTrueFalseTotal
if length $isbn == 10 and $isbn ne $self->convert_to_isbn10($ean) 2 6 8
Condition100%3/3
length $isbn == 10 and $isbn ne $self->convert_to_isbn10($ean)
!ll&&!rl&&r
512
MC/DC100%2/2
length $isbn == 10 and $isbn ne $self->convert_to_isbn10($ean)
length $isbn == 10 $isbn ne $self->convert_to_isbn10($ean)
Truth table100%3/3
A and B
A= length $isbn == 10B= $isbn ne $self->convert_to_isbn10($ean)
ABresult
0X0
100
111
128  
129 6 return 1;
130 }
131  
132 8 1;
133  
134 __END__
↑ Summary ISBN.pm »