Pipeline/Iterator.pm

filter: Statementstmt 100% 53 / 53 Branchbran 100% 10 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 19 / 19 Podpod 100% 9 / 9 totaltotal 100% 91 / 91 CC 11 SCAR 9.5
CC
11
Cov
100%
new
6.0
all
4.0
single
2.0
CRAP 2.6
« Pipeline/Stage.pm ↑ Summary Pipeline/Sources.pm »
1 33 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package Ekorn::Pipeline::Iterator;
4  
5 33 ▶use parent 'Ekorn::Pipeline::Element';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 33 ▶use Carp qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 33 ▶use Ekorn::Pipeline::Errors qw(croakf);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 33 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # Croak at the line that called Ekorn::Pipeline.
12 33 our $VERSION = 'v0.5.0';
13  
14 33 our @CARP_NOT = @Ekorn::Pipeline::Errors::INTERNAL;
15  
16 # Return an iterator, calling $next for each value, whose run $end_run
17 # ends from outside, $context is the context its run's source gave,
18 # $ended says how its run ended, $spent whether it has handed out
19 # everything it will, and $all, if given, gives every value still to
20 # come in one call, as calling $next until it gives none would, and
21 # $original, if given, is its run's source's original part, passed on.
22 1206 ▶sub new (
Subroutine new: called CC=6 SCAR=17.9
Pod documented
23 1206 ▶ $class, $next, $end_run, $context = undef, $ended = sub () { return },
Subroutine __ANON__: called CC=1 SCAR=0.0
24 1206 ▶ $spent = sub () { return }, $all = undef, $original = undef,
Subroutine __ANON__: called CC=1 SCAR=0.0
25 ) {
26 1206 return bless {
27 call => $next,
28 end_run => $end_run,
29 context => $context,
30 ended => $ended,
31 spent => $spent,
32 all => $all,
33 original => $original,
34 }, $class;
35 }
36  
37 # Return how the run ended: "done" if its source was read to its end,
38 # "stopped" if the reading was cut short, by a stop or from outside, or
39 # values still to be handed out were dropped, and "died" if anything in
40 # it died; nothing until the iterator has given all it will.
41 2083 ▶sub ended ($self) {
Subroutine ended: called CC=1 SCAR=0.0
Pod documented
42 2083 return $self->{ended}->();
43 }
44  
45 # End the run, if it hasn't ended, as from outside: each part is told
46 # how from where it stands, and nothing more is handed out.
47 63 ▶sub finish ($self) {
Subroutine finish: called CC=1 SCAR=0.0
Pod documented
48 62 $self->{end_run}->();
49 62 return;
50 }
51  
52 # Return the next value, as calling the iterator does, in any context:
53 # one value, or an empty list once the values run out (roadmap
54 # 2026-0056). A method, only ever called as $iterator->next, so it
55 # can't be mistaken for Perl's next.
56 16 ▶sub next ($self) { ## no critic (Subroutines::ProhibitBuiltinHomonyms)
Subroutine next: called CC=1 SCAR=0.0
Pod documented
57 16 return $self->{call}->();
58 }
59  
60 # Return the values still to be handed out, reading the run to its end:
61 # as a list, or, asked for one value, an array reference of them. A
62 # method, only ever called as $iterator->all, so it can't be mistaken
63 # for Perl's all, a keyword from 5.42.
64 947 ▶sub all ($self) { ## no critic (Subroutines::ProhibitBuiltinHomonyms)
Subroutine all: called CC=4 SCAR=13.9
Pod documented
65 947 my @all;
66 947 ▶ if (my $all = $self->{all}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $all = $self->{'all'}) { } 946 1 947
67 946 @all = $all->();
68 }
69 else {
70 1 my $next = $self->{call};
71 1 ▶ while (my($value) = $next->()) { push @all, $value }
Branch: 1 branch
BranchTrueFalseTotal
if (my($value) = &$next()) 2 1 3
72 }
73 868 ▶ return wantarray ? @all : \@all;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 822 46 868
74 }
75  
76 # Return the values still to be handed out, reading the run to its end,
77 # as an array reference, whatever the context: all's, asked for one
78 # value (roadmap 2026-0055).
79 14 ▶sub all_arrayref ($self) {
Subroutine all_arrayref: called CC=1 SCAR=0.0
Pod documented
80 14 return scalar $self->all;
81 }
82  
83 # Return the next value, or undef if there's none, and end the run, so
84 # nothing more is read.
85 4 ▶sub first ($self) {
Subroutine first: called CC=1 SCAR=0.0
Pod documented
86 4 my($value) = $self->{call}->();
87 4 $self->finish;
88 4 return $value;
89 }
90  
91 # Return the next value, or undef if there's none, asserting it's the
92 # only one: if there's another, warn. The run ends either way.
93 3 ▶sub single ($self) {
Subroutine single: called CC=2 SCAR=6.9
Pod documented
94 3 my $next = $self->{call};
95 3 my($value) = $next->();
96 3 ▶ Carp::carp 'Expected only one item, but there were more'
Branch: 1 branch
BranchTrueFalseTotal
if my($more) = &$next() 1 2 3
97 if my($more) = $next->();
98 3 $self->finish;
99 3 return $value;
100 }
101  
102 # An iterator is a source: of the values its run gives, the run ended
103 # from outside when the run reading it ends, passing on its context, and
104 # its source's original part, if it had one (roadmap 2026-0052).
105 29 ▶sub EKORN_SOURCE ($self) {
Subroutine EKORN_SOURCE: called CC=2 SCAR=6.9
Pod documented
106 croakf
107 'Expected a source with values still to give: an iterator is one run, and this one is spent'
108 29 ▶ if $self->{spent}->();
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"spent"}->() 4 25 29
109 25 my $next = $self->{call};
110 return {
111 # Without a signature, as it's called for each value.
112 73 ▶ step => sub { $next->() },
Subroutine __ANON__: called CC=1 SCAR=0.0
113 # $how is how the reading run ended, which this one doesn't take
114 # on: it's ended as from outside, and each of its parts is told
115 # how from where it stands, so "stopped", not "died", if the
116 # reading run died while this one still had values to give.
117 25 ▶ end => sub ($how) { $self->finish },
Subroutine __ANON__: called CC=1 SCAR=0.0
118 context => $self->{context},
119 original => $self->{original},
120 25 };
121 }
122  
123 # An iterator let go ends its run.
124 1205 ▶sub DESTROY ($self) {
Subroutine DESTROY: called CC=1 SCAR=0.0
125 1205 local $@ = undef;
126 1205 $self->{end_run}->();
127 1205 return;
128 }
129  
130 33 1;
131  
132 __END__
« Pipeline/Stage.pm ↑ Summary Pipeline/Sources.pm »