Browse Source

* use base class

master
Alex 'AdUser' Z 9 years ago
parent
commit
49e9fa01e1
  1. 35
      lib/Text/Playlist.pm
  2. 28
      lib/Text/Playlist/M3U.pm
  3. 28
      lib/Text/Playlist/PLS.pm
  4. 16
      lib/Text/Playlist/XSPF.pm

35
lib/Text/Playlist.pm

@ -0,0 +1,35 @@
package Text::Playlist;
use strict;
use warnings;
our $VERSION = 0.1;
sub items {
my ($self) = @_;
return wantarray ? @{$self->{items}} : $self->{items};
}
sub load {
my ($self, $file) = @_;
open(my $FH, "<", $file) or return $!;
local $/ = undef;
my $content = <$FH>;
close($FH);
return $self->parse($content);
}
sub save {
my ($self, $file) = @_;
open(my $FH, ">", $file) or die $!;
print $FH $self->dump();
close($FH);
return 1;
}
1;

28
lib/Text/Playlist/M3U.pm

@ -3,6 +3,8 @@ package Text::Playlist::M3U;
use strict;
use warnings;
use base 'Text::Playlist';
our $VERSION = 0.1;
sub new {
@ -11,22 +13,12 @@ sub new {
return bless({ items => [], attrs => {}, }, $class);
}
sub items {
my ($self) = @_;
return wantarray ? @{$self->{items}} : $self->{items};
}
sub load {
my ($self, $file) = @_;
sub parse {
my ($self, $text) = @_;
my @lines = split /\r?\n/, $text;
$self->{items} = [];
open(my $FH, "<", $file) or return $!;
my @lines = <$FH>;
close($FH);
chomp for @lines;
# safeguard
return "Not looks like playlist"
unless grep { $_ =~ m/^#EXTM3U/o } @lines;
@ -105,14 +97,4 @@ sub dump {
return join("\n", @lines);
}
sub save {
my ($self, $file) = @_;
open(my $FH, ">", $file) or die $!;
print $FH $self->dump();
close($FH);
return 1;
}
1;

28
lib/Text/Playlist/PLS.pm

@ -3,6 +3,8 @@ package Text::Playlist::PLS;
use strict;
use warnings;
use base 'Text::Playlist';
our $VERSION = 0.1;
sub new {
@ -11,12 +13,6 @@ sub new {
return bless({ items => [] }, $class);
}
sub items {
my ($self) = @_;
return wantarray ? @{$self->{items}} : $self->{items};
}
sub add {
my ($self, %args) = @_;
@ -35,16 +31,12 @@ sub add {
return;
}
sub load {
my ($self, $file) = @_;
sub parse {
my ($self, $text) = @_;
my @lines = split /\r?\n/, $text;
$self->{items} = [];
open(my $FH, "<", $file) or die $!;
my @lines = <$FH>;
close($FH);
chomp for @lines;
# safeguard
return "Not looks like playlist"
unless grep { $_ eq "[playlist]" } @lines;
@ -87,14 +79,4 @@ sub dump {
return join("\n", @lines);
}
sub save {
my ($self, $file) = @_;
open(my $FH, ">", $file) or die $!;
print $FH $self->dump();
close($FH);
return 1;
}
1;

16
lib/Text/Playlist/XSPF.pm

@ -5,6 +5,8 @@ use warnings;
use feature qw(switch);
use utf8;
use base 'Text::Playlist';
use XML::XPath;
our $VERSION = 0.1;
@ -15,12 +17,6 @@ sub new {
return bless({ items => [], version => 0 }, $class);
}
sub items {
my ($self) = @_;
return wantarray ? @{$self->{items}} : $self->{items};
}
sub add {
my ($self, %args) = @_;
@ -38,10 +34,10 @@ sub add {
return;
}
sub load {
my ($self, $file) = @_;
sub parse {
my ($self, $text) = @_;
my $xp = XML::XPath->new(filename => $file);
my $xp = XML::XPath->new(xml => $text);
if (my $vers = $xp->find('/playlist/@version')) {
$self->{version} = $vers;
} else {
@ -70,6 +66,8 @@ sub load {
return $self->items;
}
sub dump { die("dump() unimplemented for XSPF format\n"); }
1;
__END__

Loading…
Cancel
Save