File Coverage

blib/lib/Async/Stream/Iterator.pm
Criterion Covered Total %
statement 31 31 100.0
branch 8 8 100.0
condition n/a
subroutine 7 7 100.0
pod 2 2 100.0
total 48 48 100.0


line stmt bran cond sub pod time code
1             package Async::Stream::Iterator;
2              
3 5     5   83 use 5.010;
  5         18  
4 5     5   26 use strict;
  5         9  
  5         105  
5 5     5   31 use warnings;
  5         9  
  5         165  
6              
7 5     5   28 use Carp qw(croak);
  5         11  
  5         1148  
8              
9             =head1 NAME
10              
11             Iterator for Async stream
12              
13             =head1 VERSION
14              
15             Version 0.11
16              
17             =cut
18              
19             our $VERSION = '0.12';
20              
21              
22             =head1 SYNOPSIS
23              
24             Creating and managing item for Async::Stream
25              
26             use Async::Stream::Iterator;
27              
28             my $iterator = Async::Stream::Iterator->new($stream);
29              
30             =head1 SUBROUTINES/METHODS
31              
32             =head2 new($stream)
33              
34             Constructor creates instance of class.
35             Class method gets 1 arguments stream from which will be created iterator.
36              
37             my $iterator = Async::Stream::Iterator->new($stream);
38             =cut
39             sub new {
40 65     65 1 755 my ($class, $item) = @_;
41              
42 65 100       222 if (!$item->isa('Async::Stream::Item')) {
43 1         225 croak "First argument can be only instance of Async::Stream::Item or instance of derived class";
44             }
45              
46             return bless sub {
47 250     250   312 my $return_cb = shift;
48 250 100       399 return $return_cb->() if (!defined $item);
49              
50             $item->next(sub {
51 248 100       411 if (defined $_[0]) {
52 190         259 $item = shift;
53 190         398 $return_cb->($item->val);
54 190         2048 return;
55             } else {
56 58         151 $return_cb->();
57 58         22403 undef $item;
58 58         176 return;
59             }
60 248         847 });
61 64         266 }, $class;
62             }
63              
64             =head2 next($returning_cb)
65              
66             Method gets returning callback and call that when iterator ready to return next value.
67              
68             $iterator->(sub {
69             my $item_value = shift;
70             });
71             =cut
72              
73             sub next {
74 251     251 1 796 my $self = shift;
75 251         339 my $return_cb = shift;
76              
77 251 100       513 if (ref $return_cb ne 'CODE') {
78 1         130 croak "First argument can be only subroutine reference"
79             }
80              
81 250         649 $self->($return_cb);
82              
83 250         532 return;
84             }
85              
86             =head1 AUTHOR
87              
88             Kirill Sysoev, C<< >>
89              
90             =head1 BUGS
91              
92             Please report any bugs or feature requests to L.
93              
94             =head1 SUPPORT
95              
96             You can find documentation for this module with the perldoc command.
97              
98             perldoc Async::Stream::Item
99              
100              
101             =head1 LICENSE AND COPYRIGHT
102              
103             Copyright 2017 Kirill Sysoev.
104              
105             This program is free software; you can redistribute it and/or modify it
106             under the terms of the the Artistic License (2.0). You may obtain a
107             copy of the full license at:
108              
109             L
110              
111             =cut
112              
113             1; # End of Async::Stream::Item