blib/lib/Net/FreeDB.pm

filter: Statementstmt 25.2% 43 / 170 Branchbran 0.0% 0 / 64 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 26.8% 11 / 41 Podpod 75.0% 6 / 8 totaltotal 19.8% 60 / 303 CC 39 SCAR 36.6
CC
39
Cov
17%
new
156.0
read
56.0
AUTOLOAD
43.8
CRAP 38.8
↑ Summary FreeDB.xs »
1 package Net::FreeDB;
2  
3 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use IO::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Net::Cmd;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use CDDB::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 require Exporter;
14 1 require DynaLoader;
15 1 ▶use AutoLoader;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 our @ISA = qw(Exporter DynaLoader Net::Cmd IO::Socket::INET);
18  
19 # Items to export into callers namespace by default. Note: do not export
20 # names by default without a very good reason. Use EXPORT_OK instead.
21 # Do not simply export all your public functions/methods/constants.
22  
23 # This allows declaration use Net::FreeDB ':all';
24 # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
25 # will save memory.
26 1 our %EXPORT_TAGS = ( 'all' => [ qw() ] );
27  
28 1 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
29  
30 1 our @EXPORT = qw();
31  
32 1 our $VERSION = '0.09';
33  
34 1 our $ERROR;
35 sub AUTOLOAD {
36 # This AUTOLOAD is used to 'autoload' constants from the constant()
37 # XS function. If a constant is not found then control is passed
38 # to the AUTOLOAD in AutoLoader.
39  
40 0 ▶ my $constname;
Subroutine AUTOLOAD: not called CC=7 SCAR=37.8
41 0 our $AUTOLOAD;
42 0 ($constname = $AUTOLOAD) =~ s/.*:://;
43 0 ▶ croak "& not defined" if $constname eq 'constant';
Branch: 1 branch
BranchTrueFalseTotal
if $constname eq "constant" 0 0 0
44 0 ▶ my $val = constant($constname, @_ ? $_[0] : 0);
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
45 0 ▶ if ($! != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($! != 0) 0 0 0
46 0 ▶ if ($! =~ /Invalid/ || $!{EINVAL}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($! =~ /Invalid/ or $!{'EINVAL'}) { } 0 0 0
Condition0%0/3
$! =~ /Invalid/ or $!{"EINVAL"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$! =~ /Invalid/ or $!{"EINVAL"}
$! =~ /Invalid/ $!{"EINVAL"}
Truth table0%0/3
A or B
A= $! =~ /Invalid/B= $!{"EINVAL"}
ABresult
1X1
011
000
47 0 $AutoLoader::AUTOLOAD = $AUTOLOAD;
48 0 goto &AutoLoader::AUTOLOAD;
49 }
50 else {
51 0 croak "Your vendor has not defined Net::FreeDB macro $constname";
52 }
53 }
54 {
55 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.2
56 # Fixed between 5.005_53 and 5.005_61
57 0 ▶ if ($] >= 5.00561) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] >= 5.00561) { } 0 0 0
58 0 ▶ *$AUTOLOAD = sub () { $val };
Subroutine __ANON__: not called CC=1 SCAR=6.9
59 }
60 else {
61 0 ▶ *$AUTOLOAD = sub { $val };
Subroutine __ANON__: not called CC=1 SCAR=6.9
62 }
63 }
64 0 goto &$AUTOLOAD;
65 }
66  
67 1 bootstrap Net::FreeDB $VERSION;
68  
69 # Preloaded methods go here.
70 sub new {
71 0 ▶ my $class = shift;
Subroutine new: not called CC=12 SCAR=50.5
Pod documented
72 0 my $self = {};
73 0 $self = {@_};
74 0 bless($self, $class);
75  
76 0 ▶ $self->{HOST} = 'freedb.freedb.org' unless defined($self->{HOST});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"HOST"} 0 0 0
77 0 ▶ $self->{PORT} = '8880' unless defined($self->{PORT});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"PORT"} 0 0 0
78  
79 0 ▶ if (!defined($self->{USER})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"USER"}) 0 0 0
80 0 ▶ $self->{USER} = defined($ENV{USER}) ? $ENV{USER} : 'unknown';
Branch: 1 branch
BranchTrueFalseTotal
defined $ENV{'USER'} ? : 0 0 0
81 }
82  
83 0 ▶ if (!defined($self->{HOSTNAME})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"HOSTNAME"}) 0 0 0
84 0 ▶ $self->{HOSTNAME} = defined($ENV{HOSTNAME}) ? $ENV{HOSTNAME} : 'unknown';
Branch: 1 branch
BranchTrueFalseTotal
defined $ENV{'HOSTNAME'} ? : 0 0 0
85 }
86  
87 my $obj = $self->SUPER::new(PeerAddr => $self->{HOST},
88 PeerPort => $self->{PORT},
89 Proto => 'tcp',
90 Timeout =>
91 0 ▶ defined($self->{TIMEOUT}) ? $self->{TIMEOUT} : 120
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'TIMEOUT'} ? : 0 0 0
92 );
93  
94 return undef
95 0 ▶ unless defined $obj;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $obj 0 0 0
96  
97 0 $obj->autoflush(1);
98 0 ▶ $obj->debug(exists $self->{DEBUG} ? $self->{DEBUG} : undef);
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'DEBUG'} ? : 0 0 0
99  
100 0 ▶ unless ($obj->response() == CMD_OK) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($obj->response == 2) 0 0 0
101 0 $obj->close;
102 0 return undef;
103 }
104  
105 $obj->command(
106 "cddb hello",
107 $self->{USER},
108 $self->{HOSTNAME},
109 0 ref($self),
110 $VERSION
111 );
112  
113 0 ▶ unless ($obj->response() == CMD_OK) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($obj->response == 2) 0 0 0
114 0 $obj->close;
115 0 return undef;
116 }
117  
118 0 $obj;
119 }
120  
121 sub read {
122 0 ▶ my $self = shift;
Subroutine read: not called CC=7 SCAR=40.3
Pod documented
123 0 my ($cat, $id);
124  
125 0 ▶ if (scalar(@_) == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ == 2) { } 0 0 0
126 0 ($cat, $id) = @_;
127 } else {
128 0 ▶ if ((scalar(@_) % 2) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar(@_) % 2 == 0) { } 0 0 0
129 0 ▶ if ($_[0] =~ /^CATEGORY$/i || $_[0] =~ /^ID$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /^CATEGORY$/i or $_[0] =~ /^ID$/i) { } 0 0 0
Condition0%0/3
$_[0] =~ /^CATEGORY$/i or $_[0] =~ /^ID$/i
l!l&&r!l&&!r
000
MC/DC0%0/2
$_[0] =~ /^CATEGORY$/i or $_[0] =~ /^ID$/i
$_[0] =~ /^CATEGORY$/i $_[0] =~ /^ID$/i
Truth table0%0/3
A or B
A= $_[0] =~ /^CATEGORY$/iB= $_[0] =~ /^ID$/i
ABresult
1X1
011
000
130 0 my %input = @_;
131 0 ($cat, $id) = ($input{CATEGORY}, $input{ID});
132 } else {
133 0 print "Error: Unknown input!\n";
134 0 return undef;
135 }
136 } else {
137 0 print "Error: Unknown input!\n";
138 0 return undef;
139 }
140 }
141  
142 # First, fetch the data, before creating any temporary files
143 0 ▶ my $data = $self->_READ($cat, $id)? $self->_read(): undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->_READ($cat, $id) ? : 0 0 0
144 0 ▶ return undef unless defined $data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 0 0 0
145
146 # Create a file for CDDB::File to use...
147 0 my $fh = new File::Temp;
148 0 print $fh join '', @$data;
149 0 seek $fh, 0, 0;
150  
151 # ...and use it.
152 0 my $cddb_file = new CDDB::File($fh->filename());
153 0 return $cddb_file;
154 }
155  
156 sub query {
157 0 ▶ my $self = shift;
Subroutine query: not called CC=2 SCAR=17.9
Pod documented
158 0 ▶ $self->_QUERY(@_) ? $self->_query : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->_QUERY(@_) ? : 0 0 0
159 }
160  
161 sub sites {
162 0 ▶ my $self = shift;
Subroutine sites: not called CC=2 SCAR=17.9
Pod documented
163 0 ▶ $self->_SITES ? $self->_sites : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->_SITES ? : 0 0 0
164 }
165  
166 sub getdiscid {
167 0 ▶ my $self = shift;
Subroutine getdiscid: not called CC=4 SCAR=30.0
Pod documented
168 0 my ($driveNo, $id);
169 0 ▶ if (ref($self) ne 'Net::FreeDB') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self ne 'Net::FreeDB') { } 0 0 0
170 0 $driveNo = $self;
171 } else {
172 0 $driveNo = shift;
173 }
174 0 $id = discid($driveNo);
175 0 ▶ if ($id eq "UNDEF" || $id eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($id eq "UNDEF" or $id eq "") 0 0 0
Condition0%0/3
$id eq "UNDEF" or $id eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
$id eq "UNDEF" or $id eq ""
$id eq "UNDEF" $id eq ""
Truth table0%0/3
A or B
A= $id eq "UNDEF"B= $id eq ""
ABresult
1X1
011
000
176 0 $ERROR = "Drive Error: no disc found\n";
177 0 return undef;
178 }
179 0 return $id;
180 }
181  
182 sub getdiscdata {
183 0 ▶ my $self = shift;
Subroutine getdiscdata: not called CC=3 SCAR=24.8
Pod documented
184 0 my ($driveNo, $data);
185 0 ▶ if (ref($self) ne 'Net::FreeDB') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self ne 'Net::FreeDB') { } 0 0 0
186 0 $driveNo = $self;
187 } else {
188 0 $driveNo = shift;
189 }
190 0 $data = discinfo($driveNo);
191 0 ▶ if (!$data) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data) 0 0 0
192 0 $ERROR = "Drive Error: no disc found\n";
193 0 return undef;
194 }
195 0 return $data;
196 }
197  
198 sub lscat {
199 0 ▶ my $self = shift;
Subroutine lscat: not called CC=1 SCAR=6.9
Pod undocumented
200 0 $self->_LSCAT();
201 }
202 sub quit {
203 0 ▶ my $self = shift;
Subroutine quit: not called CC=1 SCAR=6.9
Pod undocumented
204 0 $self->_QUIT();
205 }
206  
207 sub DESTROY {
208 0 ▶ my $self = shift;
Subroutine DESTROY: not called CC=1 SCAR=6.9
209 0 $self = {};
210 }
211  
212 sub _read {
213 0 ▶ my $self = shift;
Subroutine _read: not called CC=2 SCAR=17.9
214 0 ▶ my $data = $self->read_until_dot or
Branch: 1 branch
BranchTrueFalseTotal
my $data = $self->read_until_dot or return undef 0 0 0
215 return undef;
216 0 return $data;
217 }
218  
219 sub _query {
220 0 ▶ my $self = shift;
Subroutine _query: not called CC=6 SCAR=37.4
221 0 my $data = $self->message();
222 0 my $code = $self->code();
223 0 my @returns;
224 0 ▶ if ($code == 210 || $code == 211) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code == 210 or $code == 211) { } 0 0 0
Condition0%0/3
$code == 210 or $code == 211
l!l&&r!l&&!r
000
MC/DC0%0/2
$code == 210 or $code == 211
$code == 210 $code == 211
Truth table0%0/3
A or B
A= $code == 210B= $code == 211
ABresult
1X1
011
000
225  
226  
227 0 ▶ my $data = $self->read_until_dot
Branch: 1 branch
BranchTrueFalseTotal
my $data = $self->read_until_dot or return undef 0 0 0
228 or return undef;
229 0 foreach my $i (@{$data}) {
230 0 ▶ next if $i =~ /^\.$/;
Branch: 1 branch
BranchTrueFalseTotal
if $i =~ /^\.$/ 0 0 0
231 0 $i =~
232 /([^\s]+)\s([^\s]+)\s([^\/|\:|\-]+)\s[\/|\|:|\-]\s?(.*)\s?/;
233 0 push @returns, {GENRE =>$1,DISCID =>$2,ARTIST=>$3,ALBUM=>$4};
234 }
235 } else {
236 #we got a single; parse it, hash it and return it
237 0 $data =~ /([^\s]+)\s([^\s]+)\s([^\/|\:|\-]+)\s[\/|\:|\-]\s?(.*)\s?/;
238 0 push @returns, {GENRE=>$1,DISCID=>$2,ARTIST=>$3,ALBUM=>$4};
239 }
240 0 return @returns;
241 }
242  
243 sub _sites {
244 0 ▶ my $self = shift;
Subroutine _sites: not called CC=3 SCAR=24.8
245 0 ▶ my $data = $self->read_until_dot
Branch: 1 branch
BranchTrueFalseTotal
my $data = $self->read_until_dot or return undef 0 0 0
246 or return undef;
247 0 my @sites;
248 0 foreach (@$data) {
249 0 s/([^\s]+)\s([^\s]+).*/$1 $2/;
250 0 push(@sites, $_);
251 }
252 0 return \@sites;
253 }
254  
255 0 ▶sub _READ { shift->command('CDDB READ',@_)->response == CMD_OK }
Subroutine _READ: not called CC=1 SCAR=6.9
256 0 ▶sub _SITES { shift->command('SITES',@_)->response == CMD_OK }
Subroutine _SITES: not called CC=1 SCAR=6.9
257 0 ▶sub _LSCAT { shift->command('CDDB LSCAT')->response == CMD_OK }
Subroutine _LSCAT: not called CC=1 SCAR=6.9
258 0 ▶sub _QUERY { shift->command('CDDB QUERY',@_)->response == CMD_OK }
Subroutine _QUERY: not called CC=1 SCAR=6.9
259 0 ▶sub _QUIT { shift->command('QUIT')->response == CMD_OK }
Subroutine _QUIT: not called CC=1 SCAR=6.9
260  
261 0 ▶sub _WRITE { shift->command('CDDB WRITE',@_)->response == CMD_OK }
Subroutine _WRITE: not called CC=1 SCAR=6.9
262 0 ▶sub _WHOM { shift->command('CDDB WHOM')->response == CMD_OK }
Subroutine _WHOM: not called CC=1 SCAR=6.9
263 0 ▶sub _UPDATE { shift->command('CDDB UPDATE')->response == CMD_OK }
Subroutine _UPDATE: not called CC=1 SCAR=6.9
264 0 ▶sub _VER { shift->command('CDDB VER')->response == CMD_OK }
Subroutine _VER: not called CC=1 SCAR=6.9
265 0 ▶sub _STAT { shift->command('CDDB STAT')->response == CMD_OK }
Subroutine _STAT: not called CC=1 SCAR=6.9
266 0 ▶sub _PROTO { shift->command('CDDB PROTO')->response == CMD_OK }
Subroutine _PROTO: not called CC=1 SCAR=6.9
267 0 ▶sub _MOTD { shift->command('CDDB MOTD')->response == CMD_OK }
Subroutine _MOTD: not called CC=1 SCAR=6.9
268 0 ▶sub _LOG { shift->command('CDDB LOG',@_)->response == CMD_OK }
Subroutine _LOG: not called CC=1 SCAR=6.9
269 0 ▶sub _HELP { shift->command('CDDB HELP')->response == CMD_OK }
Subroutine _HELP: not called CC=1 SCAR=6.9
270 0 ▶sub _DISCID { shift->command('DISCID',@_)->response == CMD_OK }
Subroutine _DISCID: not called CC=1 SCAR=6.9
271  
272  
273 1 1;
274 __END__
↑ Summary FreeDB.xs »