File Coverage

blib/lib/MP3/Tag/Inf.pm
Criterion Covered Total %
statement 46 52 88.4
branch 25 38 65.7
condition 1 3 33.3
subroutine 7 8 87.5
pod 1 3 33.3
total 80 104 76.9


line stmt bran cond sub pod time code
1             package MP3::Tag::Inf;
2              
3 6     6   20 use strict;
  6         6  
  6         159  
4 6     6   16 use vars qw /$VERSION @ISA/;
  6         5  
  6         3378  
5              
6             $VERSION="1.00";
7             @ISA = 'MP3::Tag::__hasparent';
8              
9             =pod
10              
11             =head1 NAME
12              
13             MP3::Tag::Inf - Module for parsing F<.inf> files associated with music tracks.
14              
15             =head1 SYNOPSIS
16              
17             my $mp3inf = MP3::Tag::Inf->new($filename); # Name of MP3 or .INF file
18             # or an MP3::Tag::File object
19              
20             ($title, $artist, $album, $year, $comment, $track) = $mp3inf->parse();
21              
22             see L
23              
24             =head1 DESCRIPTION
25              
26             MP3::Tag::Inf is designed to be called from the MP3::Tag module.
27              
28             It parses the content of F<.inf> file (created, e.g., by cdda2wav).
29              
30             =over 4
31              
32             =cut
33              
34              
35             # Constructor
36              
37             sub new_with_parent {
38 85     85 0 117 my ($class, $filename, $parent) = @_;
39 85         142 my $self = bless {parent => $parent}, $class;
40              
41 85 50       226 $filename = $filename->filename if ref $filename;
42 85         167 my $ext_rex = $self->get_config('extension')->[0];
43 85         557 $filename =~ s/($ext_rex)|$/.inf/; # replace extension
44 85 100       822 return unless -f $filename;
45 1         3 $self->{filename} = $filename;
46 1         2 $self;
47             }
48              
49             # Destructor
50              
51       0     sub DESTROY {}
52              
53             =item parse()
54              
55             ($title, $artist, $album, $year, $comment, $track) =
56             $mp3inf->parse($what);
57              
58             parse_filename() extracts information about artist, title, track number,
59             album and year from the F<.inf> file. $what is optional; it maybe title,
60             track, artist, album, year or comment. If $what is defined parse() will return
61             only this element.
62              
63             As a side effect of this call, $mp3inf->{info} is set to the hash reference
64             with the content of particular elements of the F<.inf> file. Typically present
65             are the following fields:
66              
67             CDINDEX_DISCID
68             CDDB_DISCID
69             MCN
70             ISRC
71             Albumperformer
72             Performer
73             Albumtitle
74             Tracktitle
75             Tracknumber
76             Trackstart
77             Tracklength
78             Pre-emphasis
79             Channels
80             Copy_permitted
81             Endianess
82             Index
83              
84             The following fields are also recognized:
85              
86             Year
87             Trackcomment
88              
89             =cut
90              
91             sub return_parsed {
92 113     113 0 89 my ($self,$what) = @_;
93 113 50       151 if (defined $what) {
94 113 100       240 return $self->{parsed}{album} if $what =~/^al/i;
95 97 100       165 return $self->{parsed}{artist} if $what =~/^a/i;
96 81 100       141 return $self->{parsed}{track} if $what =~/^tr/i;
97 65 100       149 return $self->{parsed}{year} if $what =~/^y/i;
98 49 100       98 return $self->{parsed}{genre} if $what =~/^g/i;
99 33 50       39 if ($what =~/^cddb_id/i) {
100 0         0 my $o = $self->{parsed}{Cddb_discid};
101 0 0       0 $o =~ s/^0x//i if $o;
102 0         0 return $o;
103             }
104 33 50       40 return $self->{parsed}{Cdindex_discid} if $what =~/^cdindex_id/i;
105 33 100       78 return $self->{parsed}{comment}if $what =~/^c/i;
106 15         43 return $self->{parsed}{title};
107             }
108              
109 0 0       0 return $self->{parsed} unless wantarray;
110 0         0 return map $self->{parsed}{$_} , qw(title artist album year comment track);
111             }
112              
113             sub parse {
114 57     57 1 46 my ($self,$what) = @_;
115              
116 57 100       115 $self->return_parsed($what) if exists $self->{parsed};
117 57         94 local *IN;
118 57 50       1082 open IN, "< $self->{filename}" or die "Error opening `$self->{filename}': $!";
119 57         62 my $e;
120 57 0 33     117 if ($e = $self->get_config('decode_encoding_inf') and $e->[0]) {
121 0         0 eval "binmode IN, ':encoding($e->[0])'"; # old binmode won't compile...
122             }
123 57         51 my ($line, %info);
124 57         822 for $line () {
125 1311 100       5284 $self->{info}{ucfirst lc $1} = $2
126             if $line =~ /^(\S+)\s*=\s*['"]?(.*?)['"]?\s*$/;
127             }
128 57 50       369 close IN or die "Error closing `$self->{filename}': $!";
129 57         39 my %parsed;
130             @parsed{ qw( title artist album year comment track Cddb_discid Cdindex_discid ) } =
131 57         52 @{ $self->{info} }{ qw( Tracktitle Performer Albumtitle
  57         276  
132             Year Trackcomment Tracknumber
133             Cddb_discid Cdindex_discid) };
134             $parsed{artist} = $self->{info}{Albumperformer}
135 57 50       102 unless defined $parsed{artist};
136 57         53 $self->{parsed} = \%parsed;
137 57         147 $self->return_parsed($what);
138             }
139              
140             for my $elt ( qw( title track artist album comment year genre cddb_id cdindex_id ) ) {
141 6     6   27 no strict 'refs';
  6         4  
  6         362  
142             *$elt = sub (;$) {
143 57     57   42 my $self = shift;
144 57         79 $self->parse($elt, @_);
145             }
146             }
147              
148             1;