| 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
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table66%2/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 55 | 3 | $self->{ERROR} = shift; | |||||||||||||||||||||||||||
| 56 | 3 | ▶ | print "Error: $self->{ERROR}\n" if $self->verbosity; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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)
MC/DC0%0/1
shift() || (return)
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 64 | 23 | my $prefix; | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 23 | ▶ | return unless(length $isbn == 10 || length $isbn == 13); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
length $isbn == 10 or length $isbn == 13
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
| |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 22 | ▶ | if(length $isbn == 13) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 14 | ▶ | return if($isbn !~ /^(978|979)(\d{10})$/); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 12 | ($prefix,$isbn) = ($1,$2); | |||||||||||||||||||||||||||
| 71 | } else { | ||||||||||||||||||||||||||||
| 72 | 8 | ▶ | return if($isbn !~ /^(\d{10}|\d{9}X)$/); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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)
MC/DC0%0/1
shift() || (return)
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 16 | ▶ | return unless(length $ean == 10 || length $ean == 13); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
length $ean == 10 or length $ean == 13
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
| |||||||||||||||||||||||||||||
| 97 | 15 | ▶ | return if($ean !~ /^(?:978|979)?(\d{9})[\dX]$/); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 119 | 9 | ▶ | return 0 if(length $isbn == 13 && $isbn ne $ean); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
length $isbn == 13 and $isbn ne $ean
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
| |||||||||||||||||||||||||||||
| 120 | 8 | ▶ | return 0 if(length $isbn == 10 && $isbn ne $self->convert_to_isbn10($ean)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
length $isbn == 10 and $isbn ne $self->convert_to_isbn10($ean)
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)
| |||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 6 | return 1; | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 8 | 1; | |||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | __END__ | ||||||||||||||||||||||||||||