From 49e9fa01e1104ca2772b3adf8033e8ea2e9c462b Mon Sep 17 00:00:00 2001 From: Alex 'AdUser' Z Date: Wed, 6 May 2015 12:56:38 +1000 Subject: [PATCH] * use base class --- lib/Text/Playlist.pm | 35 +++++++++++++++++++++++++++++++++++ lib/Text/Playlist/M3U.pm | 28 +++++----------------------- lib/Text/Playlist/PLS.pm | 28 +++++----------------------- lib/Text/Playlist/XSPF.pm | 16 +++++++--------- 4 files changed, 52 insertions(+), 55 deletions(-) create mode 100644 lib/Text/Playlist.pm diff --git a/lib/Text/Playlist.pm b/lib/Text/Playlist.pm new file mode 100644 index 0000000..cb63054 --- /dev/null +++ b/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; diff --git a/lib/Text/Playlist/M3U.pm b/lib/Text/Playlist/M3U.pm index 818c1b8..4aae071 100644 --- a/lib/Text/Playlist/M3U.pm +++ b/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; diff --git a/lib/Text/Playlist/PLS.pm b/lib/Text/Playlist/PLS.pm index 2093ed4..c62d814 100644 --- a/lib/Text/Playlist/PLS.pm +++ b/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; diff --git a/lib/Text/Playlist/XSPF.pm b/lib/Text/Playlist/XSPF.pm index 413cf0b..5ac3aa0 100644 --- a/lib/Text/Playlist/XSPF.pm +++ b/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__