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   42 use strict;
  6         12  
  6         205  
4 6     6   31 use vars qw /$VERSION @ISA/;
  6         12  
  6         4690  
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 248 my ($class, $filename, $parent) = @_;
39 85         245 my $self = bless {parent => $parent}, $class;
40              
41 85 50       299 $filename = $filename->filename if ref $filename;
42 85         242 my $ext_rex = $self->get_config('extension')->[0];
43 85         815 $filename =~ s/($ext_rex)|$/.inf/; # replace extension
44 85 100       1647 return unless -f $filename;
45 1         6 $self->{filename} = $filename;
46 1         4 $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 189 my ($self,$what) = @_;
93 113 50       236 if (defined $what) {
94 113 100       391 return $self->{parsed}{album} if $what =~/^al/i;
95 97 100       299 return $self->{parsed}{artist} if $what =~/^a/i;
96 81 100       203 return $self->{parsed}{track} if $what =~/^tr/i;
97 65 100       181 return $self->{parsed}{year} if $what =~/^y/i;
98 49 100       155 return $self->{parsed}{genre} if $what =~/^g/i;
99 33 50       59 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       67 return $self->{parsed}{Cdindex_discid} if $what =~/^cdindex_id/i;
105 33 100       126 return $self->{parsed}{comment}if $what =~/^c/i;
106 15         72 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 114 my ($self,$what) = @_;
115              
116 57 100       189 $self->return_parsed($what) if exists $self->{parsed};
117 57         144 local *IN;
118 57 50       1875 open IN, "< $self->{filename}" or die "Error opening `$self->{filename}': $!";
119 57         231 my $e;
120 57 0 33     204 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         107 my ($line, %info);
124 57         1496 for $line () {
125 1311 100       7359 $self->{info}{ucfirst lc $1} = $2
126             if $line =~ /^(\S+)\s*=\s*['"]?(.*?)['"]?\s*$/;
127             }
128 57 50       748 close IN or die "Error closing `$self->{filename}': $!";
129 57         130 my %parsed;
130             @parsed{ qw( title artist album year comment track Cddb_discid Cdindex_discid ) } =
131 57         101 @{ $self->{info} }{ qw( Tracktitle Performer Albumtitle
  57         424  
132             Year Trackcomment Tracknumber
133             Cddb_discid Cdindex_discid) };
134             $parsed{artist} = $self->{info}{Albumperformer}
135 57 50       144 unless defined $parsed{artist};
136 57         193 $self->{parsed} = \%parsed;
137 57         167 $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   48 no strict 'refs';
  6         11  
  6         550  
142             *$elt = sub (;$) {
143 57     57   130 my $self = shift;
144 57         135 $self->parse($elt, @_);
145             }
146             }
147              
148             1;