Make ->add_after("foo") etc. from HTML::Zoom::FilterBuilder work action
Jakub Narebski [Wed, 12 Jan 2011 18:46:39 +0000 (19:46 +0100)]
Unfortunately fixing ->add_before("foo") is not that easy.

  sub add_before {
    my ($self, $events) = @_;
    sub { return $self->$self->_stream_from_proto($events), $_[0]) };
  }

doesn't work, as it dies with the following error message

  Can't call method "next" on unblessed reference
  at HTML-Zoom/lib/HTML/Zoom/FlattenedStream.pm line 17.

The following does work, but is overly complicated; there should be
easier way to do this:

  sub add_before {
    my ($self, $events) = @_;
    my $emit = $self->_stream_from_proto($events);
    my @evts = $self->_zconfig->stream_utils->stream_to_array($emit);
    sub { return $self->_stream_from_array(@evts, $_[0]) };
  }

lib/HTML/Zoom/FilterBuilder.pm

index 50398b3..940e736 100644 (file)
@@ -151,7 +151,7 @@ sub add_after {
   my $coll_proto = $self->collect({ passthrough => 1 });
   sub {
     my ($evt) = @_;
-    my $emit = $self->_stream_from_array(@$events);
+    my $emit = $self->_stream_from_proto($events);
     my $coll = &$coll_proto;
     return ref($coll) eq 'HASH' # single event, no collect
       ? [ $coll, $emit ]