line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Data::Pipeline::Iterator; |
2
|
|
|
|
|
|
|
|
3
|
5
|
|
|
5
|
|
132101
|
use Moose; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
use Data::Pipeline::Types qw( IteratorSource ); |
6
|
|
|
|
|
|
|
use MooseX::Types::Moose qw( Object ); |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
has coded_source => ( |
9
|
|
|
|
|
|
|
isa => 'CodeRef', |
10
|
|
|
|
|
|
|
is => 'rw', |
11
|
|
|
|
|
|
|
); |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
has source => ( |
14
|
|
|
|
|
|
|
isa => IteratorSource, |
15
|
|
|
|
|
|
|
is => 'rw', |
16
|
|
|
|
|
|
|
coerce => 1, |
17
|
|
|
|
|
|
|
trigger => sub { |
18
|
|
|
|
|
|
|
my $self = shift; |
19
|
|
|
|
|
|
|
#print STDERR "registering($self) with ", $self -> source, "\n"; |
20
|
|
|
|
|
|
|
$self -> source -> register($self); |
21
|
|
|
|
|
|
|
}, |
22
|
|
|
|
|
|
|
default => sub { |
23
|
|
|
|
|
|
|
#print STDERR "getting coded source for $_[0]\n"; |
24
|
|
|
|
|
|
|
$_[0] -> coded_source -> (); |
25
|
|
|
|
|
|
|
}, |
26
|
|
|
|
|
|
|
lazy => 1, |
27
|
|
|
|
|
|
|
predicate => 'has_source' |
28
|
|
|
|
|
|
|
); |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
has _can_duplicate => ( |
31
|
|
|
|
|
|
|
isa => 'Bool', |
32
|
|
|
|
|
|
|
is => 'rw', |
33
|
|
|
|
|
|
|
default => '1' |
34
|
|
|
|
|
|
|
); |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
#sub BUILD { |
37
|
|
|
|
|
|
|
# my($self) = @_; |
38
|
|
|
|
|
|
|
# |
39
|
|
|
|
|
|
|
# # do it ourselves for a while |
40
|
|
|
|
|
|
|
# #$self -> source -> register($self) if defined $self -> source; |
41
|
|
|
|
|
|
|
#} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub DEMOLISH { |
44
|
|
|
|
|
|
|
my($self) = @_; |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
$self -> source -> unregister($self) if $self -> has_source && $self -> source; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub iterator { $_[0] } |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
sub finished { |
52
|
|
|
|
|
|
|
my $self = shift; |
53
|
|
|
|
|
|
|
$self -> source -> finished($self); |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
sub next { |
57
|
|
|
|
|
|
|
my $self = shift; |
58
|
|
|
|
|
|
|
$self -> _can_duplicate(0); # if we try to act as a source |
59
|
|
|
|
|
|
|
$self -> source -> next($self); |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub duplicate { |
63
|
|
|
|
|
|
|
my($self) = @_; |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
my $source = eval { $self -> source -> duplicate } || ($self -> _can_duplicate ? $self -> as_source : $self -> source ); |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
return __PACKAGE__ -> new( source => $source ); |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
# |
71
|
|
|
|
|
|
|
# making an iterate the source of another: |
72
|
|
|
|
|
|
|
# Iterator -> new (source => $iterator -> as_source ) |
73
|
|
|
|
|
|
|
# |
74
|
|
|
|
|
|
|
# this decouples the stream |
75
|
|
|
|
|
|
|
# |
76
|
|
|
|
|
|
|
sub as_source { |
77
|
|
|
|
|
|
|
my $self = shift; |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
my $new_self = __PACKAGE__ -> new( source => $self -> source ); |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
return Data::Pipeline::Iterator::Source -> new( |
82
|
|
|
|
|
|
|
has_next => sub { !$new_self -> finished }, |
83
|
|
|
|
|
|
|
get_next => sub { $new_self -> next } |
84
|
|
|
|
|
|
|
); |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
no Moose; |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
1; |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
__END__ |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
we need a way to have observers of an iterator -- basically, anyone using the |
94
|
|
|
|
|
|
|
iterator is an observer. This allows multiple pipelines to share a common |
95
|
|
|
|
|
|
|
pipeline (one splits into multiple) while only running the common pipeline once |