Commit | Line | Data |
456a815d |
1 | package HTML::Zoom::CodeStream; |
2 | |
3 | use strict; |
4 | use warnings FATAL => 'all'; |
5 | |
6 | sub from_array { |
7 | my ($class, @array) = @_; |
8 | $class->new({ code => sub { |
9 | return unless @array; |
10 | return shift @array; |
11 | }}); |
12 | } |
13 | |
14 | sub new { |
15 | my ($class, $args) = @_; |
16 | bless({ _code => $args->{code} }, $class); |
17 | } |
18 | |
8f962884 |
19 | sub peek { |
20 | my ($self) = @_; |
21 | if (exists $self->{_peeked}) { |
22 | return ($self->{_peeked}); |
23 | } |
24 | if (my ($peeked) = $self->next) { |
25 | return ($self->{_peeked} = $peeked); |
26 | } |
27 | return; |
28 | } |
29 | |
456a815d |
30 | sub next { |
8f962884 |
31 | my ($self) = @_; |
32 | |
33 | # peeked entry so return that |
34 | |
35 | if (exists $self->{_peeked}) { |
36 | return (delete $self->{_peeked}); |
37 | } |
38 | |
39 | $self->{_code}->(); |
456a815d |
40 | } |
41 | |
42 | 1; |
43 | |