From 9923dd520381f1d901054bbce0234f8aadeff302 Mon Sep 17 00:00:00 2001 From: Alex 'AdUser' Z Date: Mon, 23 Jul 2018 19:26:20 +1000 Subject: [PATCH] * Subtitle::BASE : strip BOM, add events() --- lib/Subtitle/BASE.pm | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/lib/Subtitle/BASE.pm b/lib/Subtitle/BASE.pm index 9b7dfef..5d3a636 100644 --- a/lib/Subtitle/BASE.pm +++ b/lib/Subtitle/BASE.pm @@ -4,6 +4,8 @@ use strict; use warnings; use utf8; +use Subtitle::Utils 'strip_bom'; + sub log { my ($self, $level, $msg) = @_; @@ -25,6 +27,14 @@ sub log { sub parse { return "Unimplemented by subclass"; } sub build { return "Unimplemented by subclass"; } +sub events { + my ($self, $events) = @_; + if ($events and ref($events) eq 'ARRAY') { + $self->{events} = $events; + } + return wantarray ? @{ $self->{events} } : $self->{events}; +} + sub from_string { my ($self, $text) = @_; my @lines = split /\r?\n/o, $text; @@ -38,7 +48,7 @@ sub from_file { my @lines = <$FH>; close $FH; return unless @lines; # empty file? - $lines[0] =~ s[^\x{EF}\x{BB}\x{BF}][]; # strip BOM + strip_bom($lines[0]); return $self->parse(\@lines); } @@ -84,8 +94,16 @@ Collected messages can be accessed like this: my @log = @{ $obj->{log} }; -=head C -=head C +=head2 C + + my @events = $obj->events; # array + my $events = $obj->events; # arrayref + $obj->events([$e1, $e2, $e3]); + +Access to subtitle events + +=head2 C +=head2 C Stubs for subclasses. Should return -1 on error, >= 0 on success (parsed events count).