ISBN/Driver.pm

filter: Statementstmt 100% 75 / 75 Branchbran 100% 34 / 34 Conditioncond 84.2% 16 / 19 MC/DCmcdc 66.6% 8 / 12 Subroutinesub 100% 14 / 14 Podpod 100% 10 / 10 totaltotal 95.7% 157 / 164 CC 25 SCAR 16.8
CC
25
Cov
98%
convert_to_ean13
9.0
convert_to_isbn10
7.0
is_valid
7.0
CRAP 5.4
« ISBN.pm ↑ Summary ISBN/Record.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.05';
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 11 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
18 11 my $proto = shift;
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 35 ▶sub _accessor {
Subroutine _accessor: called CC=2 SCAR=6.9
38 35 my $self = shift;
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 2 ▶sub search {
Subroutine search: called CC=1 SCAR=0.0
Pod documented
45 2 croak(q{Child class must overload 'search()' method.});
46 }
47  
48 #----------------------------------------------------------------------------
49 # Internal Class methods
50  
51 # a generic method for storing the error & setting not found
52 4 ▶sub handler {
Subroutine handler: called CC=3 SCAR=11.0
Pod documented
53 4 my $self = shift;
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 23 ▶sub convert_to_ean13 {
Subroutine convert_to_ean13: called CC=9 SCAR=22.0
Pod documented
62 23 my $self = shift;
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 16 ▶sub convert_to_isbn10 {
Subroutine convert_to_isbn10: called CC=7 SCAR=19.5
Pod documented
93 16 my $self = shift;
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  
96 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
97 15 ▶ return if($ean !~ /^(?:978|979)?(\d{9})[\dX]$/);
Branch: 1 branch
BranchTrueFalseTotal
unless $ean =~ /^(?:978|979)?(\d{9})[\dX]$/ 2 13 15
98 13 my ($isbn,$isbn10) = ($1,$1);
99  
100 13 my ($csum, $pos, $digit) = (0, 0, 0);
101 13 ▶ for ($pos = 9; $pos > 0; $pos--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos > 0) 117 13 130
102 117 $digit = $isbn % 10;
103 117 $isbn /= 10; # Decimal shift ISBN for next time
104 117 $csum += ($pos * $digit);
105 }
106 13 $csum %= 11;
107 13 ▶ $csum = 'X' if ($csum == 10);
Branch: 1 branch
BranchTrueFalseTotal
if $csum == 10 1 12 13
108 13 return $isbn10 . $csum;
109 }
110  
111 11 ▶sub is_valid {
Subroutine is_valid: called CC=7 SCAR=19.5
Pod documented
112 11 my $self = shift;
113 11 ▶ my $isbn = shift or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $isbn = shift() or return 0 10 1 11
114  
115 # validate and convert into EAN13 format
116 10 my $ean = $self->convert_to_ean13($isbn);
117  
118 10 ▶ return 0 if(!$ean);
Branch: 1 branch
BranchTrueFalseTotal
unless $ean 1 9 10
119 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
120 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
121  
122 6 return 1;
123 }
124  
125 8 1;
126  
127 __END__
« ISBN.pm ↑ Summary ISBN/Record.pm »