e6183cd4673ec1696d806669d64f52da8982003c
[catagits/HTML-Zoom.git] / lib / HTML / Zoom / MappedStream.pm
1 package HTML::Zoom::MappedStream;
2
3 use strict;
4 use warnings FATAL => 'all';
5 use base qw(HTML::Zoom::StreamBase);
6
7 sub new {
8   my ($class, $args) = @_;
9   bless({
10     _source => $args->{source}, _mapper => $args->{mapper},
11     _zconfig => $args->{zconfig}
12   }, $class);
13 }
14
15 sub _next {
16   my ($self, $am_peek) = @_;
17   return unless $self->{_source};
18   # If we were aiming for a "true" perl-like map then we should
19   # elegantly handle the case where the map function returns 0 events
20   # and the case where it returns >1 - if you're reading this comment
21   # because you wanted it to do that, now would be the time to fix it :)
22   if (my ($next) = $self->{_source}->${\($am_peek ? 'peek' : 'next')}) {
23     $self->{_peeked_from} = $next if $am_peek;
24     local $_ = $next;
25     return $self->{_mapper}->($next);
26   }
27   delete $self->{_source};
28   return
29 }
30
31 1;