| 1 | package Async::Stream::Iterator; | ||||||||||
| 2 | |||||||||||
| 3 | 5 | ▶ | use 5.010; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 5 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 5 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 5 | ▶ | use Carp qw(croak); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | =head1 NAME | ||||||||||
| 10 | |||||||||||
| 11 | Iterator for Async stream | ||||||||||
| 12 | |||||||||||
| 13 | =head1 VERSION | ||||||||||
| 14 | |||||||||||
| 15 | Version 0.11 | ||||||||||
| 16 | |||||||||||
| 17 | =cut | ||||||||||
| 18 | |||||||||||
| 19 | 5 | 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 | 65 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 40 | 65 | my ($class, $item) = @_; | |||||||||
| 41 | |||||||||||
| 42 | 65 | ▶ | if (!$item->isa('Async::Stream::Item')) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 43 | 1 | croak "First argument can be only instance of Async::Stream::Item or instance of derived class"; | |||||||||
| 44 | } | ||||||||||
| 45 | |||||||||||
| 46 | 250 | ▶ | return bless sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 47 | 250 | my $return_cb = shift; | |||||||||
| 48 | 250 | ▶ | return $return_cb->() if (!defined $item); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 49 | |||||||||||
| 50 | 248 | ▶ | $item->next(sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 51 | 248 | ▶ | if (defined $_[0]) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 52 | 190 | $item = shift; | |||||||||
| 53 | 190 | $return_cb->($item->val); | |||||||||
| 54 | 190 | return; | |||||||||
| 55 | } else { | ||||||||||
| 56 | 58 | $return_cb->(); | |||||||||
| 57 | 58 | undef $item; | |||||||||
| 58 | 58 | return; | |||||||||
| 59 | } | ||||||||||
| 60 | 248 | }); | |||||||||
| 61 | 64 | }, $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 | 251 | ▶ | sub next { | ||||||||
|
Subroutine
next: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 74 | 251 | my $self = shift; | |||||||||
| 75 | 251 | my $return_cb = shift; | |||||||||
| 76 | |||||||||||
| 77 | 251 | ▶ | if (ref $return_cb ne 'CODE') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 78 | 1 | croak "First argument can be only subroutine reference" | |||||||||
| 79 | } | ||||||||||
| 80 | |||||||||||
| 81 | 250 | $self->($return_cb); | |||||||||
| 82 | |||||||||||
| 83 | 250 | return; | |||||||||
| 84 | } | ||||||||||
| 85 | |||||||||||
| 86 | =head1 AUTHOR | ||||||||||
| 87 | |||||||||||
| 88 | Kirill Sysoev, C<< <k.sysoev at me.com> >> | ||||||||||
| 89 | |||||||||||
| 90 | =head1 BUGS | ||||||||||
| 91 | |||||||||||
| 92 | Please report any bugs or feature requests to L<https://github.com/pestkam/p5-Async-Stream/issues>. | ||||||||||
| 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<http://www.perlfoundation.org/artistic_license_2_0> | ||||||||||
| 110 | |||||||||||
| 111 | =cut | ||||||||||
| 112 | |||||||||||
| 113 | 5 | 1; # End of Async::Stream::Item | |||||||||