Commit | Line | Data |
3fea05b9 |
1 | #!/usr/bin/perl |
2 | |
3 | package Data::Stream::Bulk::Cat; |
4 | use Moose; |
5 | |
6 | use namespace::clean -except => 'meta'; |
7 | |
8 | with qw(Data::Stream::Bulk) => { excludes => 'list_cat' }; |
9 | |
10 | has streams => ( |
11 | isa => "ArrayRef[Data::Stream::Bulk]", |
12 | is => "ro", |
13 | required => 1, |
14 | ); |
15 | |
16 | sub is_done { |
17 | my $self = shift; |
18 | @{ $self->streams } == 0; |
19 | } |
20 | |
21 | sub next { |
22 | my $self = shift; |
23 | |
24 | my $s = $self->streams; |
25 | |
26 | return unless @$s; |
27 | |
28 | my $next; |
29 | |
30 | until ( $next = @$s && $s->[0]->next ) { |
31 | shift @$s; |
32 | return unless @$s; |
33 | } |
34 | |
35 | return $next; |
36 | } |
37 | |
38 | sub list_cat { |
39 | my ( $self, @rest ) = @_; |
40 | my ( $head, @tail ) = ( @{ $self->streams }, @rest ); |
41 | return () unless $head; |
42 | return $head->list_cat(@tail); |
43 | } |
44 | |
45 | __PACKAGE__->meta->make_immutable; |
46 | |
47 | __PACKAGE__ |
48 | |
49 | __END__ |
50 | |
51 | =pod |
52 | |
53 | =head1 NAME |
54 | |
55 | Data::Stream::Bulk::Cat - Concatenated streams |
56 | |
57 | =head1 SYNOPSIS |
58 | |
59 | use Data::Stream::Bulk::Cat; |
60 | |
61 | Data::Stream::Bulk::Cat->new( |
62 | streams => [ $s1, $s2, $s3 ], |
63 | ); |
64 | |
65 | =head1 DESCRIPTION |
66 | |
67 | This stream is a concatenation of several other streams. |
68 | |
69 | =head1 METHODS |
70 | |
71 | =over 4 |
72 | |
73 | =item is_done |
74 | |
75 | Returns true if the list of streams is empty. |
76 | |
77 | =item next |
78 | |
79 | Returns the next block from the next ready stream. |
80 | |
81 | =item list_cat |
82 | |
83 | Breaks down the internal list of streams, and delegates C<list_cat> to the |
84 | first one. |
85 | |
86 | Has the effect of inlining the nested streams into the total concatenation, |
87 | allowing L<Data::Stream::Bulk::Array/list_cat> to work better. |
88 | |
89 | =back |
90 | |
91 | =cut |
92 | |
93 | |