Pipeline.pm

filter: Statementstmt 100% 581 / 581 Branchbran 100% 324 / 324 Conditioncond 97.1% 168 / 173 MC/DCmcdc 94.9% 94 / 99 Subroutinesub 100% 75 / 75 Podpod 100% 11 / 11 totaltotal 99.2% 1253 / 1263 CC 232 SCAR 27.5
CC
232
Cov
100%
__ANON__
43.0
__ANON__
36.0
_make_run
25.0
CRAP 15.6
↑ Summary Pipeline/Sugar.pm »
1 27 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package Ekorn::Pipeline;
4  
5 27 ▶use experimental qw(builtin try);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 27 ▶use builtin qw(blessed refaddr weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 27 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 27 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 27 ▶use Ref::Util qw(is_arrayref is_coderef is_plain_hashref is_ref);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 27 ▶use parent 'Ekorn::Pipeline::Element';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 27 ▶use Ekorn::Pipeline::Errors qw(croakf);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 27 ▶use Ekorn::Pipeline::Feed qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 27 ▶use Ekorn::Pipeline::Iterator qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 27 ▶use Ekorn::Pipeline::Stage qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 27 ▶use namespace::clean -except => 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 27 our $VERSION = 'v0.3.6';
22  
23 27 our @EXPORT_OK = qw(is_sink is_stage);
24  
25 # Croak at the line that called Ekorn::Pipeline.
26 27 our @CARP_NOT = @Ekorn::Pipeline::Errors::INTERNAL;
27  
28 # Return true if what's given can go where a stage goes: a code
29 # reference; a pipeline, whose stages go in its place; or anything
30 # offering a stage's or a sink's role method.
31 3131 ▶sub is_stage ($thing) {
Subroutine is_stage: called CC=3 SCAR=11.0
Pod documented
32 3131 ▶ return !!(is_coderef($thing)
Condition100%6/6
Ref::Util::XS::is_coderef($thing) || $thing isa 'Ekorn::Pipeline' || _stage_role($thing)
l!l&&r!l&&!r
11391868124
Ref::Util::XS::is_coderef($thing) or $thing isa "Ekorn::Pipeline"
l!l&&r!l&&!r
8912481992
MC/DC100%3/3
Ref::Util::XS::is_coderef($thing) || $thing isa 'Ekorn::Pipeline' || _stage_role($thing)
Ref::Util::XS::is_coderef($thing) $thing isa "Ekorn::Pipeline" _stage_role($thing)
Truth table100%4/4
A or B || C
A= Ref::Util::XS::is_coderef($thing)B= $thing isa "Ekorn::Pipeline"C= _stage_role($thing)
ABCresult
1XX1
01X1
0011
0000
33 || $thing isa 'Ekorn::Pipeline'
34 || _stage_role($thing));
35 }
36  
37  
38 # Return "one" if what's given is a sink giving one value, "many" if
39 # it's a sink giving a list, or false otherwise; a pipeline, by the
40 # sink it ends in.
41 1586 ▶sub is_sink ($thing) {
Subroutine is_sink: called CC=5 SCAR=16.1
Pod documented
42 1586 ▶ ($thing) = ($thing->stages)[-1] if $thing isa 'Ekorn::Pipeline';
Branch: 1 branch
BranchTrueFalseTotal
if $thing isa "Ekorn::Pipeline" 4 1582 1586
43 1586 ▶ return !!0 unless blessed($thing);
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($thing) 11 1575 1586
44 1575 ▶ return 'one' if $thing->can('EKORN_SINK_ONE');
Branch: 1 branch
BranchTrueFalseTotal
if $thing->can("EKORN_SINK_ONE") 148 1427 1575
45 1427 ▶ return 'many' if $thing->can('EKORN_SINK_MANY');
Branch: 1 branch
BranchTrueFalseTotal
if $thing->can("EKORN_SINK_MANY") 116 1311 1427
46 1311 return !!0;
47 }
48  
49  
50 # Return the role method that what's given offers for a stage's place,
51 # EKORN_STAGE, EKORN_SINK_ONE or EKORN_SINK_MANY, or nothing if it offers none.
52 5123 ▶sub _stage_role ($thing) {
Subroutine _stage_role: called CC=2 SCAR=6.9
53 5123 ▶ return unless blessed($thing);
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($thing) 940 4183 5123
54 my @roles
55 4183 = grep { $thing->can($_) } qw(EKORN_STAGE EKORN_SINK_ONE EKORN_SINK_MANY);
56 4183 return $roles[0];
57 }
58  
59  
60 # Return true if what's given offers a source's role method.
61 1381 ▶sub _is_source ($thing) {
Subroutine _is_source: called CC=2 SCAR=6.9
62 1381 ▶ return !!(blessed($thing) && $thing->can('EKORN_SOURCE'));
Condition100%3/3
builtin::blessed($thing) && $thing->can('EKORN_SOURCE')
!ll&&!rl&&r
823241317
MC/DC100%2/2
builtin::blessed($thing) && $thing->can('EKORN_SOURCE')
builtin::blessed($thing) $thing->can('EKORN_SOURCE')
Truth table100%3/3
A && B
A= builtin::blessed($thing)B= $thing->can('EKORN_SOURCE')
ABresult
0X0
100
111
63 }
64  
65  
66 # Return the stage given as an element: a part offering a role method is
67 # kept as it is, and a code reference is made a Stage.
68 2877 ▶sub _element ($stage) {
Subroutine _element: called CC=3 SCAR=11.0
69 2877 ▶ return $stage if $stage isa 'Ekorn::Pipeline::Element' || _stage_role($stage);
Branch: 1 branch
BranchTrueFalseTotal
if $stage isa "Ekorn::Pipeline::Element" or _stage_role($stage) 2057 820 2877
Condition100%3/3
$stage isa "Ekorn::Pipeline::Element" or _stage_role($stage)
l!l&&r!l&&!r
20498820
MC/DC100%2/2
$stage isa "Ekorn::Pipeline::Element" or _stage_role($stage)
$stage isa "Ekorn::Pipeline::Element" _stage_role($stage)
Truth table100%3/3
A or B
A= $stage isa "Ekorn::Pipeline::Element"B= _stage_role($stage)
ABresult
1X1
011
000
70 820 return Ekorn::Pipeline::Stage->new($stage);
71 }
72  
73  
74 # Croak unless what's given, as stage #$n, can go where a stage goes, as
75 # one role: a source isn't a stage, and a part offering more than one of
76 # a stage's and a sink's role methods couldn't say which it is.
77 2878 ▶sub _check_stage ($stage, $n) {
Subroutine _check_stage: called CC=6 SCAR=17.9
78 my @roles
79 = blessed($stage)
80 2878 ▶ ? grep { $stage->can($_) } qw(EKORN_STAGE EKORN_SINK_ONE EKORN_SINK_MANY)
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($stage) ? : 2056 822 2878
81 : ();
82 2878 ▶ croakf
Branch: 1 branch
BranchTrueFalseTotal
if @roles > 1 1 2877 2878
83 'Expected stage #%d to offer one of EKORN_STAGE, EKORN_SINK_ONE and EKORN_SINK_MANY, not %s',
84 $n,
85 join ' and ', @roles
86 if @roles > 1;
87 2877 ▶ croakf 'Expected stage #%d to be a stage, not a source', $n
Branch: 1 branch
BranchTrueFalseTotal
if not @roles and _is_source($stage) 1 2876 2877
Condition100%3/3
not @roles and _is_source($stage)
!ll&&!rl&&r
181510611
MC/DC100%2/2
not @roles and _is_source($stage)
not @roles _is_source($stage)
Truth table100%3/3
not(A) and B
A= not @rolesB= _is_source($stage)
ABresult
0X0
100
111
88 if !@roles && _is_source($stage);
89 2876 ▶ croakf
Branch: 1 branch
BranchTrueFalseTotal
unless is_stage($stage) 7 2869 2876
90 q(Expected stage #%d to be a stage: a code reference, a pipeline, or an object offering a stage's role method),
91 $n
92 unless is_stage($stage);
93 2869 return;
94 }
95  
96  
97 # Return a pipeline, called with values to run each of them through the
98 # stages. A pipeline given as a stage has its stages spliced in, each
99 # named by its place in each pipeline, such as "2.3". In scalar context,
100 # a call gives how many values came out, unless the last stage is a
101 # scalar sink, when it gives that sink's result.
102 1703 ▶sub new ($class, @given) {
Subroutine new: called CC=6 SCAR=17.9
Pod documented
103 1703 my(@stages, @names, @within);
104 1703 for my $n (1 .. @given) {
105 2878 my $stage = $given[$n - 1];
106 2878 _check_stage($stage, $n);
107 2869 ▶ if ($stage isa 'Ekorn::Pipeline') {
Branch: 1 branch
BranchTrueFalseTotal
if ($stage isa "Ekorn::Pipeline") 239 2630 2869
108 239 my($inner, $inner_names, $inner_within) = _composed($stage);
109 239 ▶ croakf
Branch: 1 branch
BranchTrueFalseTotal
unless $inner_names 3 236 239
110 'Expected stage #%d, a pipeline, to give only stages from its stages method',
111 $n
112 unless $inner_names;
113 236 push @stages, map { _element($_) } @$inner;
114 236 push @names, map { "$n.$_" } @$inner_names;
115 push @within,
116 236 map { [[$stage, $inner_names->[$_]], @{ $inner_within->[$_] }] }
117 0 .. $#$inner;
118 236 next;
119 }
120 2630 push @stages, _element($stage);
121 2630 push @names, $n;
122 2630 push @within, [];
123 }
124 # In scalar context, a run gives a scalar sink's result, a list
125 # sink's list as an array reference, or how many values came out.
126 1691 ▶ my $sink = @stages ? is_sink($stages[-1]) : !!0;
Branch: 1 branch
BranchTrueFalseTotal
@stages ? : 1520 171 1691
127 # Where every stage is a plain step, a Stage of Ekorn's own, with no
128 # flush, end or sink, and nothing made for a run, a push runs the
129 # steps in a plain loop, with none of a run's machinery; otherwise it
130 # makes the stages for the run first, and takes the plain loop if
131 # what's made allows it.
132 my @steps
133 2870 = (grep { ref ne 'Ekorn::Pipeline::Stage' } @stages)
134 ? ()
135 1691 ▶ : map { $_->{call} } @stages;
Branch: 1 branch
BranchTrueFalseTotal
grep({ref $_ ne 'Ekorn::Pipeline::Stage';} @stages) ? : 1028 663 1691
136 # What names a step in a message, as _push_steps takes it.
137 1691 my $labels = [\@names, \@within];
138 # The pipeline, for its name in messages, held weakly, so the closure
139 # it's called as never keeps it.
140 1691 my $self;
141 my $pipeline = bless {
142 stages => \@stages,
143 names => \@names,
144 within => \@within,
145 # For feed, which takes the plain steps as they are.
146 steps => \@steps,
147 labels => $labels,
148 607 ▶ call => sub (@values) {
Subroutine __ANON__: called CC=7 SCAR=19.5
149 607 ▶ return @values unless @stages;
Branch: 1 branch
BranchTrueFalseTotal
unless @stages 8 599 607
150 my @results
151 599 ▶ = @steps
Branch: 1 branch
BranchTrueFalseTotal
@steps ? : 230 369 599
152 ? _push_steps($self, \@steps, $labels, undef, \@values)
153 : _pushed($self, \@stages, \@names, \@within, \@values);
154 489 ▶ return @results if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 394 95 489
155 95 ▶ return $results[0] if $sink && $sink eq 'one';
Branch: 1 branch
BranchTrueFalseTotal
if $sink and $sink eq "one" 42 53 95
Condition100%3/3
$sink and $sink eq "one"
!ll&&!rl&&r
47642
MC/DC100%2/2
$sink and $sink eq "one"
$sink $sink eq "one"
Truth table100%3/3
A and B
A= $sinkB= $sink eq "one"
ABresult
0X0
100
111
156 53 ▶ return \@results if $sink;
Branch: 1 branch
BranchTrueFalseTotal
if $sink 6 47 53
157 47 return scalar @results;
158 },
159 1691 }, $class;
160 1691 weaken($self = $pipeline);
161 1691 return $pipeline;
162 }
163  
164 # The named pipelines, each by its name qualified by its package.
165 27 my %named;
166  
167 # Return $name qualified by $package, unless it's qualified already, or
168 # nothing if it isn't a name: identifiers, as Perl's, joined by "::".
169 118 ▶sub _qualified ($name, $package) {
Subroutine _qualified: called CC=5 SCAR=16.1
170 118 ▶ return if !defined $name || is_ref($name);
Branch: 1 branch
BranchTrueFalseTotal
if not defined $name or Ref::Util::XS::is_ref($name) 6 112 118
Condition100%3/3
not defined $name or Ref::Util::XS::is_ref($name)
l!l&&r!l&&!r
33112
MC/DC100%2/2
not defined $name or Ref::Util::XS::is_ref($name)
not defined $name Ref::Util::XS::is_ref($name)
Truth table100%3/3
not(A) or B
A= not defined $nameB= Ref::Util::XS::is_ref($name)
ABresult
1X1
011
000
171 112 ▶ return unless $name =~ m( \A (?: [^\W\d] \w* :: )* [^\W\d] \w* \z )x;
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ / \A (?: [^\W\d] \w* :: )* [^\W\d] \w* \z /ux 12 100 112
172 100 ▶ return $name =~ m( :: )x ? $name : "${package}::$name";
Branch: 1 branch
BranchTrueFalseTotal
$name =~ / :: /ux ? : 29 71 100
173 }
174  
175  
176 # Return the stages given, each name replaced by the pipeline it names,
177 # looked up in $package unless it's qualified.
178 110 ▶sub _resolved ($package, @given) {
Subroutine _resolved: called CC=5 SCAR=16.1
179 110 my @stages;
180 110 for my $n (1 .. @given) {
181 122 my $stage = $given[$n - 1];
182 122 ▶ if (is_stage($stage)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_stage($stage)) 56 66 122
183 56 push @stages, $stage;
184 56 next;
185 }
186 66 my $name = _qualified($stage, $package);
187 66 ▶ croakf
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 13 53 66
188 q(Expected stage #%d to be a stage: a code reference, a pipeline or its name, or an object offering a stage's role method),
189 $n
190 unless defined $name;
191 53 my $pipeline = $named{$name};
192 53 ▶ croakf 'No pipeline named %s', $name unless $pipeline;
Branch: 1 branch
BranchTrueFalseTotal
unless $pipeline 6 47 53
193 47 push @stages, $pipeline;
194 }
195 91 return @stages;
196 }
197  
198  
199 # Return a new pipeline of the invocant's class, of the stages given, each
200 # a code reference, a pipeline, or the name of one, looked up now in
201 # $package unless it's qualified; or, given one name alone, the pipeline
202 # it names, if it's of the invocant's class, so its stages are numbered
203 # where it's given. The one method that reads names, called by the words,
204 # such as Factories' pipeline, with their caller's package.
205 84 ▶sub pipeline_in ($class, $package, @stages) {
Subroutine pipeline_in: called CC=4 SCAR=13.9
Pod documented
206 84 my @resolved = _resolved($package, @stages);
207 67 ▶ return $resolved[0]
Branch: 1 branch
BranchTrueFalseTotal
if @stages == 1 and not is_stage($stages[0]) and $resolved[0] isa $class 30 37 67
Condition100%6/6
@stages == 1 and not is_stage($stages[0]) and $resolved[0] isa $class
!ll&&!rl&&r
35230
@stages == 1 and not is_stage($stages[0])
!ll&&!rl&&r
211432
MC/DC100%3/3
@stages == 1 and not is_stage($stages[0]) and $resolved[0] isa $class
@stages == 1 not is_stage($stages[0]) $resolved[0] isa $class
Truth table100%4/4
A and not(B) and C
A= @stages == 1B= not is_stage($stages[0])C= $resolved[0] isa $class
ABCresult
0XX0
10X0
1100
1111
208 if @stages == 1 && !is_stage($stages[0]) && $resolved[0] isa $class;
209 37 return $class->new(@resolved);
210 }
211  
212  
213 # Name a pipeline, in the caller's package unless the name is qualified,
214 # and return it: on the class, a new one of the stages after the name,
215 # read as pipeline_in reads them; on a pipeline, that one. The name can't
216 # be one already defined for another pipeline.
217 52 ▶sub named ($invocant, $name, @stages) {
Subroutine named: called CC=8 SCAR=20.8
Pod documented
218 52 ▶ my $class = ref $invocant ? ref $invocant : $invocant;
Branch: 1 branch
BranchTrueFalseTotal
ref $invocant ? : 21 31 52
219 52 my $package = caller;
220 52 my $qualified = _qualified($name, $package);
221 52 ▶ croakf 'Expected a name, made of words joined by ::' unless defined $qualified;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $qualified 5 47 52
222 47 my $pipeline;
223 47 ▶ if (ref $invocant) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $invocant) { } 21 26 47
224 21 ▶ croakf 'Expected only a name, naming a pipeline you have' if @stages;
Branch: 1 branch
BranchTrueFalseTotal
if @stages 1 20 21
225 20 $pipeline = $invocant;
226 }
227 else {
228 # Always a new one, even of one name, so a name never aliases.
229 26 $pipeline = $class->new(_resolved($package, @stages));
230 }
231 44 my $own = $named{$qualified};
232 44 ▶ croakf 'A pipeline named %s is already defined', $qualified
Branch: 1 branch
BranchTrueFalseTotal
if $own and builtin::refaddr($own) != builtin::refaddr($pipeline) 3 41 44
Condition100%3/3
$own and builtin::refaddr($own) != builtin::refaddr($pipeline)
!ll&&!rl&&r
4013
MC/DC100%2/2
$own and builtin::refaddr($own) != builtin::refaddr($pipeline)
$own builtin::refaddr($own) != builtin::refaddr($pipeline)
Truth table100%3/3
A and B
A= $ownB= builtin::refaddr($own) != builtin::refaddr($pipeline)
ABresult
0X0
100
111
233 if $own && refaddr($own) != refaddr($pipeline);
234 41 ▶ $pipeline->{name} = $qualified unless defined $pipeline->{name};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pipeline->{"name"} 39 2 41
235 41 return $named{$qualified} = $pipeline;
236 }
237  
238  
239 # Return the first name the pipeline was given, qualified by its
240 # package, or undef if it has none.
241 1004 ▶sub name ($self) {
Subroutine name: called CC=1 SCAR=0.0
Pod documented
242 1004 return $self->{name};
243 }
244  
245 # Return the stages of a pipeline given as a stage, from its stages
246 # method, as giving its stages would, their names, and the pipelines
247 # each came from within it: its own, if the method returns the stages it
248 # was made with, as it does unless a subclass overrides it, or else their
249 # places in what it returns. The names are undefined if what it returns
250 # isn't all code references.
251 259 ▶sub _composed ($pipeline) {
Subroutine _composed: called CC=6 SCAR=17.9
252 259 my @stages = $pipeline->stages;
253 259 my $made = $pipeline->{stages};
254 259 my $same = @stages == @$made;
255 259 for my $i (0 .. $#stages) {
256 273 ▶ last unless $same;
Branch: 1 branch
BranchTrueFalseTotal
unless $same 8 265 273
257 265 ▶ $same = (refaddr($stages[$i]) // 0) == refaddr($made->[$i]);
Condition100%2/2
builtin::refaddr($stages[$i]) // 0
l!l
2641
MC/DC100%1/1
builtin::refaddr($stages[$i]) // 0
builtin::refaddr($stages[$i])
Truth table100%2/2
A
A= builtin::refaddr($stages[$i])
Aresult
11
00
258 }
259 259 ▶ return \@stages, $pipeline->{names}, $pipeline->{within} if $same;
Branch: 1 branch
BranchTrueFalseTotal
if $same 250 9 259
260 9 ▶ return \@stages, undef if grep { !is_stage($_) } @stages;
Branch: 1 branch
BranchTrueFalseTotal
if grep {not is_stage($_);} @stages 4 5 9
261 5 return \@stages, [1 .. @stages], [map { [] } @stages];
262 }
263  
264  
265 # Return an iterator handing out, one per call, the values from $source
266 # run through the stages: $source a code reference, or anything
267 # offering a source's role method, an iterator included.
268 480 ▶sub iterator ($self, $source) {
Subroutine iterator: called CC=3 SCAR=11.0
Pod documented
269 480 ▶ croakf 'Expected a source: a code reference, or an object offering EKORN_SOURCE'
Branch: 1 branch
BranchTrueFalseTotal
unless Ref::Util::XS::is_coderef($source) or _is_source($source) 3 477 480
Condition100%3/3
Ref::Util::XS::is_coderef($source) or _is_source($source)
l!l&&r!l&&!r
1613163
MC/DC100%2/2
Ref::Util::XS::is_coderef($source) or _is_source($source)
Ref::Util::XS::is_coderef($source) _is_source($source)
Truth table100%3/3
A or B
A= Ref::Util::XS::is_coderef($source)B= _is_source($source)
ABresult
1X1
011
000
270 unless is_coderef($source) || _is_source($source);
271 return _iterator(
272 $self, $self->{stages}, $self->{names}, $self->{within},
273 477 $source,
274 );
275 }
276  
277  
278 # Return a feed: one run of the stages, made now, which takes values as
279 # they're pushed, giving what comes out of each push, and ends when it's
280 # closed, giving what the flushes give. A run with no flush, no end and
281 # no sink takes the plain loop, each push a pushed run's, its stop
282 # ending the run; any other takes the full loop, as an iterator whose
283 # source has nothing until a push, and is done once the feed is closed.
284 401 ▶sub feed ($self) {
Subroutine feed: called CC=3 SCAR=11.0
Pod documented
285 400 my($steps, $labels) = @$self{qw(steps labels)};
286 # Stages that aren't all plain steps are made for the run.
287 400 ▶ unless (@$steps) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$steps) 331 69 400
288 331 my %run = (
289 stages => [], names => [], within => [], flushes => [], ends => [],
290 sinks => [],
291 );
292 331 try {
293 331 _make_run(@$self{qw(stages names within)}, \%run);
294 }
295 catch ($e) {
296 1 _set_up_died($self, \%run, undef, $e);
297 }
298 return _full_feed($self, \%run)
299 330 ▶ if grep { defined } @{ $run{flushes} }, @{ $run{ends} };
Branch: 1 branch
BranchTrueFalseTotal
if grep {defined $_;} @{$run{"flushes"};}, @{$run{"ends"};} 299 31 330
300 31 ($steps, $labels) = ($run{stages}, [@run{qw(names within)}]);
301 }
302 100 my $ended;
303 return Ekorn::Pipeline::Feed->new(
304 # Without a signature, as a feed may be pushed a value at a time:
305 # its values go on as they are, not copied.
306 157 ▶ push => sub { _push_steps($self, $steps, $labels, \$ended, \@_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
307 63 ▶ close => sub () { $ended = 'done'; return },
Subroutine __ANON__: called CC=1 SCAR=0.0
308 1 ▶ finish => sub () { $ended = 'stopped'; return },
Subroutine __ANON__: called CC=1 SCAR=0.0
309 506 ▶ ended => sub () { return $ended },
Subroutine __ANON__: called CC=1 SCAR=0.0
310 # Nothing to flush or end, for a stage driving the feed.
311 100 plain => 1,
312 );
313 }
314  
315  
316 # Return a feed of a run made into %$run with a flush, an end or a sink:
317 # an iterator of it, whose source has nothing until a push, and is done
318 # once the feed is closed; its stages see the context of the code
319 # pushing.
320 299 ▶sub _full_feed ($pipeline, $run) {
Subroutine _full_feed: called CC=1 SCAR=0.0
321 299 my(@pushed, $closed);
322 299 ▶ $run->{hungry} = sub () { !$closed };
Subroutine __ANON__: called CC=1 SCAR=0.0
323 my $iterator
324 299 ▶ = _full_iterator($pipeline, $run, sub { splice @pushed }, undef, undef);
Subroutine __ANON__: called CC=1 SCAR=0.0
325 # The iterator's own code for all, called directly, as a feed may be
326 # pushed a value at a time; the iterator is kept by the closures here.
327 299 my $all = $iterator->{all};
328 return Ekorn::Pipeline::Feed->new(
329 # Without a signature, as a feed may be pushed a value at a time.
330 505 ▶ push => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
331 505 push @pushed, @_;
332 505 return $all->();
333 },
334 243 ▶ close => sub () {
Subroutine __ANON__: called CC=1 SCAR=0.0
335 243 $closed = 1;
336 243 return $iterator->all;
337 },
338 4 ▶ finish => sub () { $iterator->finish; return },
Subroutine __ANON__: called CC=1 SCAR=0.0
339 1770 ▶ ended => sub () { return $iterator->ended },
Subroutine __ANON__: called CC=1 SCAR=0.0
340 # Whether there are ends to call, for a stage driving the feed.
341 299 ends => scalar grep { defined } @{ $run->{ends} },
342 );
343 }
344  
345  
346 # Return the stages, in order, as a list, to splice into another
347 # pipeline or give to any function taking stages.
348 271 ▶sub stages ($self) {
Subroutine stages: called CC=1 SCAR=0.0
Pod documented
349 271 return @{ $self->{stages} };
350 }
351  
352 # What a pipeline is doing while its stages run.
353 my %running;
354  
355 # For code telling its stages about the value they're given: true while
356 # a flush runs, and while the values it gave go through the stages,
357 # rather than values from the source.
358 19 ▶sub ending () {
Subroutine ending: called CC=1 SCAR=0.0
Pod documented
359 19 return $running{ending};
360 }
361  
362  
363 # For code telling its stages about the run: the context the run's
364 # source gave, if any, such as a statement's columns. A pushed run has
365 # none; an iterator used as a source passes on its run's.
366 33 ▶sub run_context () {
Subroutine run_context: called CC=1 SCAR=0.0
Pod documented
367 33 return $running{context};
368 }
369  
370  
371 # Return the step, the end and the context of a run of $source: from
372 # its EKORN_SOURCE, if it offers one; a code reference is its own step,
373 # with no end or context.
374 477 ▶sub _source_run ($source) {
Subroutine _source_run: called CC=5 SCAR=16.1
375 # As _is_source asks, without the call, as every run asks it.
376 477 ▶ return ($source) unless blessed($source) && $source->can('EKORN_SOURCE');
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($source) and $source->can("EKORN_SOURCE") 161 316 477
Condition66%2/3
builtin::blessed($source) and $source->can("EKORN_SOURCE")
!ll&&!rl&&r
1610316
MC/DC50%1/2
builtin::blessed($source) and $source->can("EKORN_SOURCE")
builtin::blessed($source) $source->can("EKORN_SOURCE")
Truth table66%2/3
A and B
A= builtin::blessed($source)B= $source->can("EKORN_SOURCE")
ABresult
0X0
100
111
377 316 my($step, $end, $context)
378 = _parts([qw(step end context)], $source->EKORN_SOURCE);
379 309 ▶ croakf
Branch: 1 branch
BranchTrueFalseTotal
unless Ref::Util::XS::is_coderef($step) and _code_or_none($end) 4 305 309
Condition100%3/3
Ref::Util::XS::is_coderef($step) and _code_or_none($end)
!ll&&!rl&&r
31305
MC/DC100%2/2
Ref::Util::XS::is_coderef($step) and _code_or_none($end)
Ref::Util::XS::is_coderef($step) _code_or_none($end)
Truth table100%3/3
A and B
A= Ref::Util::XS::is_coderef($step)B= _code_or_none($end)
ABresult
0X0
100
111
380 'Expected the source to return a code reference, and optionally an end and a context'
381 unless is_coderef($step) && _code_or_none($end);
382 305 return ($step, $end, $context);
383 }
384  
385  
386 # Return the parts a role method or a set-up returned as a list, in the
387 # order of the keys in @$order: as they are, if they're a list, or, if
388 # they're one plain hash reference, its values for those keys. Other
389 # keys are ignored.
390 2602 ▶sub _parts ($order, @returned) {
Subroutine _parts: called CC=3 SCAR=11.0
391 2602 ▶ return @returned unless @returned == 1 && is_plain_hashref($returned[0]);
Branch: 1 branch
BranchTrueFalseTotal
unless @returned == 1 and Ref::Util::XS::is_plain_hashref($returned[0]) 2045 557 2602
Condition100%3/3
@returned == 1 and Ref::Util::XS::is_plain_hashref($returned[0])
!ll&&!rl&&r
1099946557
MC/DC100%2/2
@returned == 1 and Ref::Util::XS::is_plain_hashref($returned[0])
@returned == 1 Ref::Util::XS::is_plain_hashref($returned[0])
Truth table100%3/3
A and B
A= @returned == 1B= Ref::Util::XS::is_plain_hashref($returned[0])
ABresult
0X0
100
111
392 557 return @{ $returned[0] }{@$order};
393 }
394  
395  
396 # Return true if each of what's given is a code reference, or undefined.
397 2582 ▶sub _code_or_none (@things) {
Subroutine _code_or_none: called CC=2 SCAR=6.9
398 2582 ▶ return !grep { defined && !is_coderef($_) } @things;
Condition100%3/3
defined $_ && !Ref::Util::XS::is_coderef($_)
!ll&&!rl&&r
246414516
MC/DC100%2/2
defined $_ && !Ref::Util::XS::is_coderef($_)
defined $_ !Ref::Util::XS::is_coderef($_)
Truth table100%3/3
A && not(B)
A= defined $_B= !Ref::Util::XS::is_coderef($_)
ABresult
0X0
100
111
399 }
400  
401  
402 # Return how a stage is named in a message: by its place, $name, and, if
403 # it's within a named pipeline, the innermost, by its name and its place
404 # in it, from the pipelines in @$within it came from, and $pipeline, the
405 # one being run.
406 982 ▶sub _label ($pipeline, $name, $within) {
Subroutine _label: called CC=3 SCAR=11.0
407 982 for my $in (reverse [$pipeline, $name], @$within) {
408 999 my($outer, $place) = @$in;
409 999 my $outer_name = $outer->name;
410 999 ▶ return "stage #$name ($outer_name#$place)" if defined $outer_name;
Branch: 1 branch
BranchTrueFalseTotal
if defined $outer_name 14 985 999
411 }
412 968 return "stage #$name";
413 }
414  
415  
416 # Return the results of pushing @values through @$steps, each a plain
417 # step, as a run of them would give them, without a run's machinery:
418 # each value through every step before the next, what a step returns
419 # going on in order, a stop's values going on from the step after it,
420 # with nothing else, and a step that dies named as a run names it, by
421 # $labels, the steps' names and the pipelines each came from, as a
422 # pair. The values are given as a reference to an array, not copied.
423 # For a feed, $ended, if given, is set to how the run ended, at a stop
424 # or a death, and the steps see the context of the code pushing.
425 509 ▶sub _push_steps ($pipeline, $steps, $labels, $ended, $values) {
Subroutine _push_steps: called CC=13 SCAR=25.6
426 # A pushed run has no context, and gives no values from a flush.
427 509 ▶ local $running{context} = undef unless $ended;
Branch: 1 branch
BranchTrueFalseTotal
unless $ended 352 157 509
428 509 local $running{ending} = undef;
429 509 my(@results, @todo, $n);
430 509 my($at, $more) = (0, 1);
431 # One try for as many values as go through without a stop, which is
432 # caught, and the values it carries go on from the step after it.
433 509 ▶ while ($more) {
Branch: 1 branch
BranchTrueFalseTotal
if ($more) 621 437 1058
434 621 try {
435 VALUE:
436 621 while (1) {
437 1938 my $value;
438 1938 ▶ if (@todo) { ($n, $value) = @{ pop @todo } }
Branch: 2 branches
BranchTrueFalseTotal
if (@todo) { } 334 1604 1938
elsif ($at < @$values) { } 1167 437 1604
439 1167 elsif ($at < @$values) { ($n, $value) = (0, $values->[$at++]) }
440 437 else { $more = 0; last VALUE }
441 1501 ▶ while ($n < @$steps) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < @$steps) 1756 1118 2874
442 1756 local $_ = $value;
443 1756 my @values = $steps->[$n]->($value);
444 1572 $n++;
445 1572 ▶ next VALUE unless @values;
Branch: 1 branch
BranchTrueFalseTotal
unless @values 199 1373 1572
446 1373 $value = shift @values;
447 1373 ▶ push @todo, map { [$n, $_] } reverse @values if @values;
Branch: 1 branch
BranchTrueFalseTotal
if @values 232 1141 1373
448 }
449 1118 push @results, $value;
450 }
451 }
452 catch ($e) {
453 184 ▶ if ($e isa 'Ekorn::Pipeline::Stop') {
Branch: 1 branch
BranchTrueFalseTotal
if ($e isa "Ekorn::Pipeline::Stop") 112 72 184
454 112 ▶ my(undef, @carried) = is_arrayref($e) ? @$e : ();
Branch: 1 branch
BranchTrueFalseTotal
Ref::Util::XS::is_arrayref($e) ? : 110 2 112
455 112 ($at, @todo) = (scalar @$values);
456 112 push @todo, map { [$n + 1, $_] } reverse @carried;
457 112 ▶ $$ended = 'stopped' if $ended;
Branch: 1 branch
BranchTrueFalseTotal
if $ended 13 99 112
458 112 next;
459 }
460 72 ▶ $$ended = 'died' if $ended;
Branch: 1 branch
BranchTrueFalseTotal
if $ended 11 61 72
461 72 ▶ croak $e if is_ref($e);
Branch: 1 branch
BranchTrueFalseTotal
if Ref::Util::XS::is_ref($e) 2 70 72
462 70 my $where = Carp::shortmess() =~ s( \.? \n \z )()xr;
463 70 my $message = $e =~ s( \n \z )()xr =~ s( ^ )( )xmgr;
464 70 my($names, $within) = @$labels;
465 70 my $stage = _label($pipeline, $names->[$n], $within->[$n]);
466 70 die "Pipeline $stage failed$where:\n$message\n";
467 }
468 }
469 437 return @results;
470 }
471  
472  
473 # Return the text of an end's error: a stop's own text, "Stop used
474 # outside a pipeline", would mislead, as the stop was in a run.
475 13 ▶sub _end_error ($error) {
Subroutine _end_error: called CC=2 SCAR=6.9
476 13 ▶ return 'Expected no stop in an end, as the run has ended'
Branch: 1 branch
BranchTrueFalseTotal
if $error isa "Ekorn::Pipeline::Stop" 2 11 13
477 if $error isa 'Ekorn::Pipeline::Stop';
478 11 return "$error";
479 }
480  
481  
482 # Call each end given, as [$label, $end, $how], in order. While the run
483 # is dying, an end that dies is reported as a warning naming it, and the
484 # rest are called. Otherwise, an end that dies tells every end after it
485 # "died", and its error is thrown once they've run.
486 910 ▶sub _end_all ($ends, $dying) {
Subroutine _end_all: called CC=9 SCAR=22.0
487 910 my($error, $failed);
488 910 for my $call (@$ends) {
489 1067 my($label, $end, $how) = @$call;
490 1067 try {
491 1067 local $_ = undef;
492 1067 ▶ $end->($dying || defined $error ? 'died' : $how);
Branch: 1 branch
BranchTrueFalseTotal
$dying || defined $error ? : 117 950 1067
Condition100%3/3
$dying or defined $error
l!l&&r!l&&!r
1143950
MC/DC100%2/2
$dying or defined $error
$dying defined $error
Truth table100%3/3
A or B
A= $dyingB= defined $error
ABresult
1X1
011
000
493 }
494 catch ($e) {
495 14 ▶ if ($dying || defined $error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dying or defined $error) { } 2 12 14
Condition100%3/3
$dying or defined $error
l!l&&r!l&&!r
1112
MC/DC100%2/2
$dying or defined $error
$dying defined $error
Truth table100%3/3
A or B
A= $dyingB= defined $error
ABresult
1X1
011
000
496 2 Carp::carp sprintf 'The end of %s died while the run was already dying: %s',
497 $label, _end_error($e) =~ s( \n \z )()xr;
498 }
499 else {
500 12 ($error, $failed) = ($e, $label);
501 }
502 }
503 }
504 910 ▶ return unless defined $error;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $error 898 12 910
505 # A stop from an end is an error like any other: the run has ended.
506 12 ▶ croak $error if is_ref($error) && !($error isa 'Ekorn::Pipeline::Stop');
Branch: 1 branch
BranchTrueFalseTotal
if Ref::Util::XS::is_ref($error) and not $error isa "Ekorn::Pipeline::Stop" 1 11 12
Condition100%3/3
Ref::Util::XS::is_ref($error) and not $error isa "Ekorn::Pipeline::Stop"
!ll&&!rl&&r
921
MC/DC100%2/2
Ref::Util::XS::is_ref($error) and not $error isa "Ekorn::Pipeline::Stop"
Ref::Util::XS::is_ref($error) not $error isa "Ekorn::Pipeline::Stop"
Truth table100%3/3
A and not(B)
A= Ref::Util::XS::is_ref($error)B= not $error isa "Ekorn::Pipeline::Stop"
ABresult
0X0
100
111
507 11 my $at = Carp::shortmess() =~ s( \.? \n \z )()xr;
508 11 my $message = _end_error($error) =~ s( \n \z )()xr =~ s( ^ )( )xmgr;
509 11 die "Pipeline end of $failed failed$at:\n$message\n";
510 }
511  
512  
513 # Make the parts of a run from @$stages, named by @$names, into the
514 # lists in %$run: each part's step, its flush and its end, by its role
515 # method, and whether it's a sink. A gather's block may return a
516 # pipeline as its step, whose stages are made in its place, followed by
517 # the gather's flush and end, if any, on a stage of their own that
518 # passes values on.
519 1072 ▶sub _make_run ($stages, $names, $within, $run) {
Subroutine _make_run: called CC=25 SCAR=32.2
520 1072 for my $i (0 .. $#$stages) {
521 2303 my($stage, $name) = ($stages->[$i], $names->[$i]);
522 2303 ▶ my $role = _stage_role($stage) // '';
Condition100%2/2
_stage_role($stage) // ""
l!l
23021
MC/DC100%1/1
_stage_role($stage) // ""
_stage_role($stage)
Truth table100%2/2
A
A= _stage_role($stage)
Aresult
11
00
523 2303 my($step, $flush, $end);
524 2303 ▶ if ($stage isa 'Ekorn::Pipeline::Gather') {
Branch: 3 branches
BranchTrueFalseTotal
if ($stage isa 'Ekorn::Pipeline::Gather') { } 1490 813 2303
elsif ($role eq 'EKORN_SINK_ONE' or $role eq 'EKORN_SINK_MANY') { } 237 576 813
elsif ($role eq 'EKORN_STAGE') { } 575 1 576
525 1490 my @made = _parts([qw(step flush end)], $stage->EKORN_STAGE);
526 1484 ▶ my($inner, $inner_names, $inner_within)
Branch: 1 branch
BranchTrueFalseTotal
@made && $made[0] isa 'Ekorn::Pipeline' ? : 20 1464 1484
Condition100%3/3
@made and $made[0] isa "Ekorn::Pipeline"
!ll&&!rl&&r
1146320
MC/DC100%2/2
@made and $made[0] isa "Ekorn::Pipeline"
@made $made[0] isa "Ekorn::Pipeline"
Truth table100%3/3
A and B
A= @madeB= $made[0] isa "Ekorn::Pipeline"
ABresult
0X0
100
111
527 = @made && $made[0] isa 'Ekorn::Pipeline' ? _composed($made[0]) : ();
528 1484 ▶ croakf
Branch: 2 branches
BranchTrueFalseTotal
unless @made == 1 || @made == 2 || @made == 3 and $inner ? $inner_names : Ref::Util::XS::is_coderef($made[0]) and _code_or_none(@made[1 .. $#made]) 12 1472 1484
$inner ? : 19 1462 1481
Condition100%12/12
@made == 1 || @made == 2 || @made == 3 and $inner ? $inner_names : Ref::Util::XS::is_coderef($made[0]) and _code_or_none(@made[1 .. $#made])
!ll&&!rl&&r
1021472
@made == 1 || @made == 2 || @made == 3 and $inner ? $inner_names : Ref::Util::XS::is_coderef($made[0])
!ll&&!rl&&r
371474
@made == 1 or @made == 2 or @made == 3
l!l&&r!l&&!r
41210693
@made == 1 or @made == 2
l!l&&r!l&&!r
3051071072
MC/DC100%5/5
@made == 1 || @made == 2 || @made == 3 and $inner ? $inner_names : Ref::Util::XS::is_coderef($made[0]) and _code_or_none(@made[1 .. $#made])
@made == 1 @made == 2 @made == 3 $inner ? $inner_names : Ref::Util::XS::is_coderef($made[0]) _code_or_none(@made[1 .. $#made])
Truth table80%8/10
A or B or C and D and E
A= @made == 1B= @made == 2C= @made == 3D= $inner ? $inner_names : Ref::Util::XS::is_coderef($made[0])E= _code_or_none(@made[1 .. $#made])
ABCDEresult
000XX0
1XX0X0
01X0X0
0010X0
1XX100
01X100
001100
1XX111
01X111
001111
529 'Expected stage #%s, made with gather, to return a code reference or a pipeline, and optionally a flush and an end',
530 $name
531 unless (@made == 1 || @made == 2 || @made == 3)
532 && ($inner ? $inner_names : is_coderef($made[0]))
533 && _code_or_none(@made[1 .. $#made]);
534 1472 ($step, $flush, $end) = @made;
535 1472 ▶ if ($inner) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inner) 17 1455 1472
536 _make_run(
537 $inner,
538 25 [map { "$name.$_" } @$inner_names],
539 [
540 17 map { [
541 25 @{ $within->[$i] }, [$made[0], $inner_names->[$_]],
542 25 @{ $inner_within->[$_] },
543 ] } 0 .. $#$inner
544 ],
545 $run,
546 );
547 17 ▶ next unless $flush || $end;
Branch: 1 branch
BranchTrueFalseTotal
unless $flush or $end 12 5 17
Condition100%3/3
$flush or $end
l!l&&r!l&&!r
4112
MC/DC100%2/2
$flush or $end
$flush $end
Truth table100%3/3
A or B
A= $flushB= $end
ABresult
1X1
011
000
548 5 ▶ $step = sub { $_[0] }; # for each value: no signature
Subroutine __ANON__: called CC=1 SCAR=0.0
549 }
550 }
551 ▶ elsif ($role eq 'EKORN_SINK_ONE' || $role eq 'EKORN_SINK_MANY') {
Condition100%3/3
$role eq "EKORN_SINK_ONE" or $role eq "EKORN_SINK_MANY"
l!l&&r!l&&!r
134103576
MC/DC100%2/2
$role eq "EKORN_SINK_ONE" or $role eq "EKORN_SINK_MANY"
$role eq "EKORN_SINK_ONE" $role eq "EKORN_SINK_MANY"
Truth table100%3/3
A or B
A= $role eq "EKORN_SINK_ONE"B= $role eq "EKORN_SINK_MANY"
ABresult
1X1
011
000
552 237 ($step, $flush, $end) = _parts([qw(step flush end)], $stage->$role);
553 234 ▶ croakf
Branch: 1 branch
BranchTrueFalseTotal
unless Ref::Util::XS::is_coderef($step) and Ref::Util::XS::is_coderef($flush) and _code_or_none($end) 5 229 234
Condition100%6/6
Ref::Util::XS::is_coderef($step) and Ref::Util::XS::is_coderef($flush) and _code_or_none($end)
!ll&&!rl&&r
41229
Ref::Util::XS::is_coderef($step) and Ref::Util::XS::is_coderef($flush)
!ll&&!rl&&r
13230
MC/DC100%3/3
Ref::Util::XS::is_coderef($step) and Ref::Util::XS::is_coderef($flush) and _code_or_none($end)
Ref::Util::XS::is_coderef($step) Ref::Util::XS::is_coderef($flush) _code_or_none($end)
Truth table100%4/4
A and B and C
A= Ref::Util::XS::is_coderef($step)B= Ref::Util::XS::is_coderef($flush)C= _code_or_none($end)
ABCresult
0XX0
10X0
1100
1111
554 'Expected sink #%s to return a code reference, a flush, and optionally an end',
555 $name
556 unless is_coderef($step)
557 && is_coderef($flush)
558 && _code_or_none($end);
559 # What a sink's step returns is ignored, by the run, which knows
560 # it's a sink; a scalar sink's flush gives one value, undef if
561 # it has none.
562 229 my $result = $flush;
563 121 ▶ $flush = sub () { my @result = $result->(); return $result[0] }
Subroutine __ANON__: called CC=1 SCAR=0.0
564 229 ▶ if $role eq 'EKORN_SINK_ONE';
Branch: 1 branch
BranchTrueFalseTotal
if $role eq "EKORN_SINK_ONE" 126 103 229
565 }
566 elsif ($role eq 'EKORN_STAGE') {
567 575 ($step, $flush, $end) = _parts([qw(step flush end)], $stage->EKORN_STAGE);
568 575 ▶ croakf
Branch: 1 branch
BranchTrueFalseTotal
unless Ref::Util::XS::is_coderef($step) and _code_or_none($flush, $end) 5 570 575
Condition100%3/3
Ref::Util::XS::is_coderef($step) and _code_or_none($flush, $end)
!ll&&!rl&&r
32570
MC/DC100%2/2
Ref::Util::XS::is_coderef($step) and _code_or_none($flush, $end)
Ref::Util::XS::is_coderef($step) _code_or_none($flush, $end)
Truth table100%3/3
A and B
A= Ref::Util::XS::is_coderef($step)B= _code_or_none($flush, $end)
ABresult
0X0
100
111
569 'Expected stage #%s to return a code reference, and optionally a flush and an end',
570 $name
571 unless is_coderef($step) && _code_or_none($flush, $end);
572 }
573 else {
574 1 $step = $stage;
575 }
576 2260 push @{ $run->{stages} }, $step;
577 2260 push @{ $run->{names} }, $name;
578 2260 push @{ $run->{within} }, $within->[$i];
579 2260 push @{ $run->{flushes} }, $flush;
580 2260 push @{ $run->{ends} }, $end;
581 2260 ▶ push @{ $run->{sinks} },
Condition100%3/3
$role eq 'EKORN_SINK_ONE' || $role eq 'EKORN_SINK_MANY'
l!l&&r!l&&!r
1261032031
MC/DC100%2/2
$role eq 'EKORN_SINK_ONE' || $role eq 'EKORN_SINK_MANY'
$role eq 'EKORN_SINK_ONE' $role eq 'EKORN_SINK_MANY'
Truth table100%3/3
A || B
A= $role eq 'EKORN_SINK_ONE'B= $role eq 'EKORN_SINK_MANY'
ABresult
1X1
011
000
582 $role eq 'EKORN_SINK_ONE' || $role eq 'EKORN_SINK_MANY';
583 }
584 1041 return;
585 }
586  
587  
588 # Return an iterator handing out, one per call, the values from $source
589 # run through the stages in @$stages, named in messages by @$names.
590 477 ▶sub _iterator ($pipeline, $stages, $names, $within, $source) {
Subroutine _iterator: called CC=3 SCAR=11.0
591 477 my %run = (
592 stages => [], names => [], within => [], flushes => [], ends => [],
593 sinks => [],
594 );
595 477 my($pull, $source_end, $context);
596 477 try {
597 477 ($pull, $source_end, $context) = _source_run($source);
598 466 ▶ _make_run($stages, $names, $within, \%run) if @$stages;
Branch: 1 branch
BranchTrueFalseTotal
if @$stages 355 111 466
599 }
600 catch ($e) {
601 13 _set_up_died($pipeline, \%run, $source_end, $e);
602 }
603 # A run with no flush, no end of a stage's, and no sink, as most are,
604 # takes the shorter way.
605 return _plain_iterator($pipeline, \%run, $pull, $source_end, $context)
606 464 ▶ unless grep { defined } @{ $run{flushes} }, @{ $run{ends} };
Branch: 1 branch
BranchTrueFalseTotal
unless grep {defined $_;} @{$run{"flushes"};}, @{$run{"ends"};} 232 232 464
607 232 return _full_iterator($pipeline, \%run, $pull, $source_end, $context);
608 }
609  
610  
611 # Return the results of pushing @$values through @$stages, as a run of
612 # them gives them: once the stages are made for the run, by the plain
613 # loop if none has a flush or an end, as most haven't, so a stage made
614 # with gather costs no more a value than a plain one; otherwise by an
615 # iterator of the run. Its set-up is _iterator's, with no source to set
616 # up.
617 369 ▶sub _pushed ($pipeline, $stages, $names, $within, $values) {
Subroutine _pushed: called CC=2 SCAR=6.9
618 369 my %run = (
619 stages => [], names => [], within => [], flushes => [], ends => [],
620 sinks => [],
621 );
622 369 try {
623 369 _make_run($stages, $names, $within, \%run);
624 }
625 catch ($e) {
626 28 _set_up_died($pipeline, \%run, undef, $e);
627 }
628 return _push_steps(
629 $pipeline, $run{stages}, [@run{qw(names within)}], undef,
630 $values,
631 341 ▶ ) unless grep { defined } @{ $run{flushes} }, @{ $run{ends} };
Branch: 1 branch
BranchTrueFalseTotal
unless grep {defined $_;} @{$run{"flushes"};}, @{$run{"ends"};} 122 219 341
632 373 ▶ return _full_iterator($pipeline, \%run, sub { splice @$values }, undef, undef)
Subroutine __ANON__: called CC=1 SCAR=0.0
633 219 ->all;
634 }
635  
636  
637 # Tell each end of a run made by then, the source's first, that the run
638 # died, as a set-up died with $error, and throw it on.
639 42 ▶sub _set_up_died ($pipeline, $run, $source_end, $error) {
Subroutine _set_up_died: called CC=4 SCAR=13.9
640 _end_all(
641 [
642 ($source_end ? ['the source', $source_end, 'died'] : ()),
643 3 map { [_run_label($pipeline, $run, $_), $run->{ends}[$_], 'died'] }
644 42 ▶ grep { $run->{ends}[$_] } 0 .. $#{ $run->{ends} },
Branch: 1 branch
BranchTrueFalseTotal
$source_end ? : 2 40 42
645 ],
646 1,
647 );
648 # A set-up runs before the run has values, so has nothing to stop.
649 42 ▶ croakf 'Expected no stop in a set-up, as the run has nothing yet to stop'
Branch: 1 branch
BranchTrueFalseTotal
if $error isa "Ekorn::Pipeline::Stop" 2 40 42
650 if $error isa 'Ekorn::Pipeline::Stop';
651 40 ▶ croak $error if is_ref($error);
Branch: 1 branch
BranchTrueFalseTotal
if Ref::Util::XS::is_ref($error) 1 39 40
652 39 die $error; ## no critic (ErrorHandling::RequireCarping): thrown again, as it was
653 }
654  
655  
656 # Return an iterator of a run that isn't plain, made by _iterator or
657 # _pushed, from its source's step, end and context.
658 #
659 # The run ends as soon as nothing in it can still act on a value: the
660 # source has run out or stopped, every flush has been called, and every
661 # value still to be handed out has passed the last stage; or when it's
662 # ended from outside, or dies. Then each end is called, the source's
663 # first, told how the run ended from where it stands: "died" if the run
664 # died; else "done" if its input ran out, and "stopped" if not.
665 #
666 # A feed's run has a hungry, true while more values may be pushed: a
667 # source with nothing now isn't done, and the iterator hands out
668 # nothing more until the next push. It has no context of its own: its
669 # stages and ends see the context of the code pushing.
670 747 ▶sub _full_iterator ($pipeline, $run, $pull, $source_end, $context) {
Subroutine _full_iterator: called CC=1 SCAR=0.0
671 747 my $hungry = $run->{hungry};
672 747 my @stages = @{ $run->{stages} };
673 747 my @flushes = @{ $run->{flushes} };
674 747 my @ends = @{ $run->{ends} };
675 747 my @sinks = @{ $run->{sinks} };
676 # The places of the stages with a flush, so finding the next needn't
677 # look at every stage.
678 747 my @flush_at = grep { $flushes[$_] } 0 .. $#stages;
679 # Each value waiting to go through a stage, as the stage's index, the
680 # value, and whether it came from a flush, the next on top. A value a
681 # stage returns on its own, as most are, carries on through the stages
682 # without waiting here. Once the source is done, the flushes are called
683 # in order, from the first not called or passed over by a stop.
684 my(
685 747 @todo, $done, $source_done, %flushed, @input_done, $finished, $outside,
686 $ended, $spent, $dropped,
687 );
688 747 my $next_flush = 0;
689 # How many values wait at each stage's index, kept as @todo changes, so
690 # asking whether any wait before a stage needn't scan @todo: a flush
691 # can put many values there at once.
692 747 my @waiting_at;
693 # Values a source's step gave, still to go through the stages from the
694 # first, held as they came: @todo's values go first, being further on.
695 my @from_source;
696 # Put @values on @todo, to go on from stage $n, the first on top.
697 327 ▶ my $wait = sub ($n, $from_flush, @values) {
Subroutine __ANON__: called CC=1 SCAR=0.0
698 327 push @todo, map { [$n, $_, $from_flush] } reverse @values;
699 327 $waiting_at[$n] += @values;
700 327 return;
701 747 };
702 # Whether any value waits at a stage up to $upto. The run has a stage,
703 # so values from the source wait at the first.
704 698 ▶ my $waiting_upto = sub ($upto) {
Subroutine __ANON__: called CC=4 SCAR=13.9
705 698 ▶ return 1 if @from_source;
Branch: 1 branch
BranchTrueFalseTotal
if @from_source 10 688 698
706 688 ▶ $waiting_at[$_] && return 1 for 0 .. $upto;
Branch: 1 branch
BranchTrueFalseTotal
$waiting_at[$_] and return 1 22 1722 1744
707 666 return 0;
708 747 };
709 # How the run ended, as its caller asks: whether the source was read
710 # to its end, "stopped" if values still to be handed out were dropped,
711 # or "died" if anything died, an end included.
712 1498 ▶ my $finish = sub ($died) {
Subroutine __ANON__: called CC=9 SCAR=22.0
713 1498 ▶ return if $finished++;
Branch: 1 branch
BranchTrueFalseTotal
if $finished++ 751 747 1498
714 747 ▶ $ended
Branch: 3 branches
BranchTrueFalseTotal
$died ? : 66 681 747
$dropped ? : 10 671 681
$source_done ? : 551 120 671
715 = $died ? 'died'
716 : $dropped ? 'stopped'
717 : $source_done ? 'done'
718 : 'stopped';
719 747 my @calls;
720 747 ▶ push @calls, ['the source', $source_end, $source_done ? 'done' : 'stopped']
Branch: 2 branches
BranchTrueFalseTotal
if $source_end 95 652 747
$source_done ? : 51 44 95
721 if $source_end;
722 push @calls,
723 map { [
724 846 ▶ _run_label($pipeline, $run, $_), $ends[$_],
Branch: 1 branch
BranchTrueFalseTotal
$input_done[$_] ? : 689 157 846
725 $input_done[$_] ? 'done' : 'stopped',
726 ] }
727 747 grep { $ends[$_] } 0 .. $#ends;
728 747 ▶ local $running{context} = $context unless $hungry;
Branch: 1 branch
BranchTrueFalseTotal
unless $hungry 448 299 747
729 747 try {
730 747 _end_all(\@calls, $died);
731 }
732 catch ($e) {
733 # An end died: the run died, and hands out nothing more.
734 9 ($ended, $spent, @todo, @waiting_at, @from_source) = ('died', 1);
735 9 die $e; ## no critic (ErrorHandling::RequireCarping): thrown again, as it was
736 }
737 738 return;
738 747 };
739 # The index of the next flush to call, if any.
740 1946 ▶ my $flush_at = sub () {
Subroutine __ANON__: called CC=4 SCAR=13.9
741 1946 for my $n (@flush_at) {
742 1799 ▶ return $n if $n >= $next_flush && !$flushed{$n};
Branch: 1 branch
BranchTrueFalseTotal
if $n >= $next_flush and not $flushed{$n} 607 1192 1799
Condition66%2/3
$n >= $next_flush and not $flushed{$n}
!ll&&!rl&&r
11920607
MC/DC50%1/2
$n >= $next_flush and not $flushed{$n}
$n >= $next_flush not $flushed{$n}
Truth table66%2/3
A and not(B)
A= $n >= $next_flushB= not $flushed{$n}
ABresult
0X0
100
111
743 }
744 1339 return;
745 747 };
746 # Every part from the next flush up to $upto has had all its input.
747 1239 ▶ my $input_ran_out = sub ($upto) {
Subroutine __ANON__: called CC=2 SCAR=6.9
748 1239 $input_done[$_] = 1 for $next_flush .. $upto;
749 1239 return;
750 747 };
751 # At a stop at stage $n, with nothing more to come from upstream,
752 # every stage up to it whose input had run out: none of its values
753 # still waiting, and no flush before it still to be called.
754 33 ▶ my $input_ran_out_at_stop = sub ($n) {
Subroutine __ANON__: called CC=5 SCAR=16.1
755 33 for my $stage ($next_flush .. $n) {
756 9 ▶ last if $waiting_upto->($stage);
Branch: 1 branch
BranchTrueFalseTotal
if &$waiting_upto($stage) 4 5 9
757 5 ▶ last if grep { $flushes[$_] && !$flushed{$_} } $next_flush .. $stage - 1;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$flushes[$_] and not $flushed{$_};} $next_flush .. $stage - 1 1 4 5
Condition66%2/3
$flushes[$_] && !$flushed{$_}
!ll&&!rl&&r
101
MC/DC50%1/2
$flushes[$_] && !$flushed{$_}
$flushes[$_] !$flushed{$_}
Truth table66%2/3
A && not(B)
A= $flushes[$_]B= !$flushed{$_}
ABresult
0X0
100
111
758 4 $input_done[$stage] = 1;
759 }
760 33 return;
761 747 };
762 # End the run if nothing in it can still act on a value. Called only
763 # once the source is done.
764 1158 ▶ my $end_if_ended = sub () {
Subroutine __ANON__: called CC=4 SCAR=13.9
765 1158 ▶ return if $finished || defined $flush_at->();
Branch: 1 branch
BranchTrueFalseTotal
if $finished or defined &$flush_at() 469 689 1158
Condition100%3/3
$finished or defined &$flush_at()
l!l&&r!l&&!r
44029689
MC/DC100%2/2
$finished or defined &$flush_at()
$finished defined &$flush_at()
Truth table100%3/3
A or B
A= $finishedB= defined &$flush_at()
ABresult
1X1
011
000
766 689 ▶ return if $waiting_upto->($#stages);
Branch: 1 branch
BranchTrueFalseTotal
if &$waiting_upto($#stages) 28 661 689
767 661 $input_ran_out->($#stages);
768 661 $finish->(0);
769 652 return;
770 747 };
771 # Where all collects every value still to come, while it does: the
772 # loop pushes them onto it, with no return between values. Otherwise,
773 # the loop returns the next value.
774 747 my $into;
775 1342 ▶ my $next = sub () {
Subroutine __ANON__: called CC=43 SCAR=37.6
776 1342 ▶ return if $outside;
Branch: 1 branch
BranchTrueFalseTotal
if $outside 1 1341 1342
777 1341 while (1) {
778 4722 ▶ if ($into) {
Branch: 1 branch
BranchTrueFalseTotal
if ($into) 4231 491 4722
779 # All takes at once the values already at the end, such as a
780 # list sink's result. A run with no stages takes the shorter
781 # way, so this one has a stage.
782 4231 ▶ if (@todo && $todo[-1][0] == @stages) {
Branch: 1 branch
BranchTrueFalseTotal
if (@todo and $todo[-1][0] == @stages) 62 4169 4231
Condition100%3/3
@todo and $todo[-1][0] == @stages
!ll&&!rl&&r
393023962
MC/DC100%2/2
@todo and $todo[-1][0] == @stages
@todo $todo[-1][0] == @stages
Truth table100%3/3
A and B
A= @todoB= $todo[-1][0] == @stages
ABresult
0X0
100
111
783 62 ▶ while (@todo && $todo[-1][0] == @stages) {
Branch: 1 branch
BranchTrueFalseTotal
if (@todo and $todo[-1][0] == @stages) 135 62 197
Condition100%3/3
@todo and $todo[-1][0] == @stages
!ll&&!rl&&r
611135
MC/DC100%2/2
@todo and $todo[-1][0] == @stages
@todo $todo[-1][0] == @stages
Truth table100%3/3
A and B
A= @todoB= $todo[-1][0] == @stages
ABresult
0X0
100
111
784 135 push @$into, (pop @todo)->[1];
785 135 $waiting_at[@stages]--;
786 }
787 62 next;
788 }
789 }
790 4660 my($n, $value, $from_flush, $flush);
791 4660 ▶ if (@todo) {
Branch: 3 branches
BranchTrueFalseTotal
if (@todo) { } 293 4367 4660
elsif (@from_source) { } 865 3502 4367
elsif (not $done) { } 2274 1228 3502
792 293 ($n, $value, $from_flush) = @{ pop @todo };
793 293 $waiting_at[$n]--;
794 }
795 elsif (@from_source) {
796 865 ($n, $value) = (0, shift @from_source);
797 }
798 elsif (!$done) {
799 # Straight onto @from_source, which is empty here.
800 2274 try {
801 2274 @from_source = $pull->();
802 }
803 catch ($e) {
804 24 ▶ if ($e isa 'Ekorn::Pipeline::Stop') {
Branch: 1 branch
BranchTrueFalseTotal
if ($e isa "Ekorn::Pipeline::Stop") 15 9 24
805 # A source that stops has had enough: what it
806 # carries is its last, and it's done.
807 15 ▶ my(undef, @carried) = is_arrayref($e) ? @$e : ();
Branch: 1 branch
BranchTrueFalseTotal
Ref::Util::XS::is_arrayref($e) ? : 11 4 15
808 15 ($done, $source_done) = (1, 1);
809 15 @from_source = @carried;
810 15 next;
811 }
812 9 ($done, $next_flush, @todo, @waiting_at, @from_source)
813 = (1, scalar @stages);
814 9 $finish->(1);
815 9 ▶ croak $e if is_ref($e);
Branch: 1 branch
BranchTrueFalseTotal
if Ref::Util::XS::is_ref($e) 5 4 9
816 4 die $e; ## no critic (ErrorHandling::RequireCarping): thrown again, as it was
817 }
818 2250 ▶ unless (@from_source) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@from_source) 1023 1227 2250
819 # Nothing yet: the feed's next push carries on.
820 1023 ▶ return if $hungry && $hungry->();
Branch: 1 branch
BranchTrueFalseTotal
if $hungry and &$hungry() 455 568 1023
Condition100%3/3
$hungry and &$hungry()
!ll&&!rl&&r
325243455
MC/DC100%2/2
$hungry and &$hungry()
$hungry &$hungry()
Truth table100%3/3
A and B
A= $hungryB= &$hungry()
ABresult
0X0
100
111
821 568 ($done, $source_done) = (1, 1);
822 568 next;
823 }
824 1227 ($n, $value) = (0, shift @from_source);
825 }
826 else {
827 1228 $n = $flush_at->();
828 1228 ▶ unless (defined $n) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $n) 650 578 1228
829 650 $end_if_ended->();
830 648 last;
831 }
832 578 $input_ran_out->($n);
833 578 $flushed{$n} = 1;
834 578 ($next_flush, $flush, $from_flush) = ($n + 1, $flushes[$n], 1);
835 }
836 2963 ▶ if ($n == @stages) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n == @stages) 29 2934 2963
837 # Only while values are handed out one at a time: all takes
838 # them from the top of the loop. Nothing can end while the
839 # source is still being read.
840 29 ▶ $end_if_ended->() if $done;
Branch: 1 branch
BranchTrueFalseTotal
if $done 28 1 29
841 29 return $value;
842 }
843 # Set only when it changes, as most values come from the source,
844 # and most runs, and the code running them, have no context.
845 local $running{ending} = $from_flush
846 2934 ▶ if $from_flush || $running{ending};
Branch: 1 branch
BranchTrueFalseTotal
if $from_flush or $running{"ending"} 677 2257 2934
Condition66%2/3
$from_flush or $running{"ending"}
l!l&&r!l&&!r
67702257
MC/DC50%1/2
$from_flush or $running{"ending"}
$from_flush $running{"ending"}
Truth table66%2/3
A or B
A= $from_flushB= $running{"ending"}
ABresult
1X1
011
000
847 # A feed's run takes the context of the code pushing, so it's
848 # asked last, as most runs have none to set.
849 local $running{context} = $context
850 2934 ▶ if defined $context || (defined $running{context} && !$hungry);
Branch: 1 branch
BranchTrueFalseTotal
if defined $context or defined $running{"context"} and not $hungry 17 2917 2934
Condition100%6/6
defined $context or defined $running{"context"} and not $hungry
l!l&&r!l&&!r
1612917
defined $running{"context"} and not $hungry
!ll&&!rl&&r
2902151
MC/DC100%3/3
defined $context or defined $running{"context"} and not $hungry
defined $context defined $running{"context"} not $hungry
Truth table100%4/4
A or B and not(C)
A= defined $contextB= defined $running{"context"}C= not $hungry
ABCresult
1XX1
0111
00X0
0100
851 2934 my $kept = 1;
852 2934 try {
853 2934 ▶ if ($flush) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flush) 578 2356 2934
854 578 local $_ = undef;
855 578 my @values = $flush->();
856 523 $n++;
857 523 $kept = @values;
858 523 $value = shift @values;
859 523 ▶ $wait->($n, 1, @values) if @values;
Branch: 1 branch
BranchTrueFalseTotal
if @values 63 460 523
860 }
861 2879 ▶ while ($kept && $n < @stages) {
Branch: 1 branch
BranchTrueFalseTotal
if ($kept and $n < @stages) 4742 1247 5989
Condition100%3/3
$kept and $n < @stages
!ll&&!rl&&r
10311444742
MC/DC100%2/2
$kept and $n < @stages
$kept $n < @stages
Truth table100%3/3
A and B
A= $keptB= $n < @stages
ABresult
0X0
100
111
862 4742 local $_ = $value;
863 my @values
864 = $sinks[$n]
865 4742 ▶ ? do { $stages[$n]->($value); () }
Branch: 1 branch
BranchTrueFalseTotal
$sinks[$n] ? : 510 4232 4742
866 : $stages[$n]->($value);
867 4597 ▶ last if $outside;
Branch: 1 branch
BranchTrueFalseTotal
if $outside 5 4592 4597
868 4592 $n++;
869 4592 ▶ unless (@values) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@values) 1482 3110 4592
870 1482 $kept = 0;
871 1482 last;
872 }
873 3110 $value = shift @values;
874 3110 ▶ $wait->($n, $from_flush, @values) if @values;
Branch: 1 branch
BranchTrueFalseTotal
if @values 121 2989 3110
875 }
876 }
877 catch ($e) {
878 200 ▶ if ($e isa 'Ekorn::Pipeline::Stop') {
Branch: 1 branch
BranchTrueFalseTotal
if ($e isa "Ekorn::Pipeline::Stop") 143 57 200
879 # A stop made by throw is an array; one made otherwise
880 # carries nothing. A sink that stops the run still
881 # gives its result.
882 143 ▶ my(undef, @values) = is_arrayref($e) ? @$e : ();
Branch: 1 branch
BranchTrueFalseTotal
Ref::Util::XS::is_arrayref($e) ? : 138 5 143
883 143 ▶ $input_ran_out_at_stop->($n) if $done;
Branch: 1 branch
BranchTrueFalseTotal
if $done 33 110 143
884 143 $done = 1;
885 143 ▶ my $from = $sinks[$n] && !$flushed{$n} ? $n : $n + 1;
Branch: 1 branch
BranchTrueFalseTotal
$sinks[$n] && !$flushed{$n} ? : 39 104 143
Condition100%3/3
$sinks[$n] and not $flushed{$n}
!ll&&!rl&&r
103139
MC/DC100%2/2
$sinks[$n] and not $flushed{$n}
$sinks[$n] not $flushed{$n}
Truth table100%3/3
A and not(B)
A= $sinks[$n]B= not $flushed{$n}
ABresult
0X0
100
111
886 143 ▶ $next_flush = $from if $next_flush < $from;
Branch: 1 branch
BranchTrueFalseTotal
if $next_flush < $from 91 52 143
887 143 (@todo, @waiting_at, @from_source) = ();
888 143 $wait->($n + 1, $from_flush, @values);
889 143 next;
890 }
891 # A stage or a flush that dies ends the run: nothing waiting
892 # goes on, and every end is told it died.
893 57 ($done, $next_flush, @todo, @waiting_at, @from_source)
894 = (1, scalar @stages);
895 57 $finish->(1);
896 57 ▶ croak $e if is_ref($e);
Branch: 1 branch
BranchTrueFalseTotal
if Ref::Util::XS::is_ref($e) 6 51 57
897 51 my $at = Carp::shortmess() =~ s( \.? \n \z )()xr;
898 51 my $message = $e =~ s( \n \z )()xr =~ s( ^ )( )xmgr;
899 51 my $stage = _run_label($pipeline, $run, $n);
900 51 die "Pipeline $stage failed$at:\n$message\n";
901 }
902 2734 ▶ return if $outside;
Branch: 1 branch
BranchTrueFalseTotal
if $outside 5 2729 2734
903 2729 ▶ if ($kept) {
Branch: 1 branch
BranchTrueFalseTotal
if ($kept) 1144 1585 2729
904 1144 ▶ $end_if_ended->() if $done;
Branch: 1 branch
BranchTrueFalseTotal
if $done 480 664 1144
905 1137 ▶ return $value unless $into;
Branch: 1 branch
BranchTrueFalseTotal
unless $into 129 1008 1137
906 1008 push @$into, $value;
907 }
908 }
909 # Everything has been handed out: the iterator is spent.
910 648 $spent = 1;
911 648 return;
912 747 };
913 # Every value the run has still to give, as $next would give them one
914 # at a time, but in one call of it.
915 1095 ▶ my $all = sub () {
Subroutine __ANON__: called CC=1 SCAR=0.0
916 1095 my @all;
917 1095 $into = \@all;
918 1095 try {
919 1095 $next->();
920 }
921 catch ($e) {
922 58 $into = undef;
923 58 die $e; ## no critic (ErrorHandling::RequireCarping): thrown again, as it was
924 }
925 1037 $into = undef;
926 1037 return @all;
927 747 };
928 # Ended from outside, by the iterator's finish or as it's let go, the
929 # run stops: nothing more is pulled, and nothing waiting goes on.
930 771 ▶ my $end_run = sub () {
Subroutine __ANON__: called CC=4 SCAR=13.9
931 771 $outside = 1;
932 # A run that died has nothing waiting: its values were dropped then.
933 771 ▶ $dropped = 1 if @todo || @from_source;
Branch: 1 branch
BranchTrueFalseTotal
if @todo or @from_source 13 758 771
Condition100%3/3
@todo or @from_source
l!l&&r!l&&!r
58758
MC/DC100%2/2
@todo or @from_source
@todo @from_source
Truth table100%3/3
A or B
A= @todoB= @from_source
ABresult
1X1
011
000
934 771 ($done, $next_flush, @todo, @waiting_at, @from_source)
935 = (1, scalar @stages);
936 771 $finish->(0);
937 771 ▶ $ended = 'stopped' if $dropped;
Branch: 1 branch
BranchTrueFalseTotal
if $dropped 26 745 771
938 771 return;
939 747 };
940 # How the run ended, held back until the iterator has nothing more to
941 # give, so it's said once, and doesn't change. It's the iterator's
942 # spent too: the run has ended, and so is said to, once it has.
943 1942 ▶ my $ended_once = sub () {
Subroutine __ANON__: called CC=5 SCAR=16.1
944 1942 ▶ return $ended if $spent || $outside || ($ended // '') eq 'died';
Branch: 1 branch
BranchTrueFalseTotal
if $spent or $outside or ($ended // "") eq "died" 512 1430 1942
Condition100%8/8
$spent or $outside or ($ended // "") eq "died"
l!l&&r!l&&!r
466461430
$spent or $outside
l!l&&r!l&&!r
452141476
$ended // ""
l!l
491427
MC/DC100%3/3
$spent or $outside or ($ended // "") eq "died"
$spent $outside $ended
Truth table100%4/4
A or B or C
A= $spentB= $outsideC= $ended
ABCresult
1XX1
01X1
0011
0000
945 1430 return;
946 747 };
947 747 return Ekorn::Pipeline::Iterator->new(
948 $next, $end_run, $context, $ended_once, $ended_once, $all,
949 );
950 }
951  
952  
953 # Return how stage $n of a run made into %$run, of $pipeline, is named in
954 # a message, as _label names it.
955 912 ▶sub _run_label ($pipeline, $run, $n) {
Subroutine _run_label: called CC=1 SCAR=0.0
956 912 return _label($pipeline, $run->{names}[$n], $run->{within}[$n]);
957 }
958  
959  
960 # Return an iterator, as _iterator does, for a run made into %$run with
961 # no flush, no end of a stage's, and no sink, so with none of their
962 # bookkeeping: each value through every step before the next, what a
963 # step returns going on in order, and a stop's values going on from the
964 # step after it. The run ends, as _iterator's does, once the source is
965 # done and no value waits before the last step, and then the source's
966 # end, its only one, is called.
967 232 ▶sub _plain_iterator ($pipeline, $run, $pull, $source_end, $context) {
Subroutine _plain_iterator: called CC=1 SCAR=0.0
968 232 my @steps = @{ $run->{stages} };
969 # Each value waiting to go through a step, as the step's index and the
970 # value, the next on top, so in order of index from the bottom: the
971 # lowest is at the bottom. Values a source's step gave, still to go
972 # through the steps from the first, are held as they came.
973 232 my(@todo, @from_source);
974 232 my($done, $source_done, $finished, $outside, $ended, $spent, $dropped, $into);
975 494 ▶ my $finish = sub ($died) {
Subroutine __ANON__: called CC=6 SCAR=17.9
976 494 ▶ return if $finished++;
Branch: 1 branch
BranchTrueFalseTotal
if $finished++ 262 232 494
977 # Values dropped from outside make it stopped, as end_run says.
978 232 ▶ $ended = $died ? 'died' : $source_done ? 'done' : 'stopped';
Branch: 2 branches
BranchTrueFalseTotal
$died ? : 27 205 232
$source_done ? : 147 58 205
979 232 ▶ return unless $source_end;
Branch: 1 branch
BranchTrueFalseTotal
unless $source_end 111 121 232
980 121 local $running{context} = $context;
981 121 try {
982 121 ▶ _end_all(
Branch: 1 branch
BranchTrueFalseTotal
$source_done ? : 80 41 121
983 [['the source', $source_end, $source_done ? 'done' : 'stopped']],
984 $died,
985 );
986 }
987 catch ($e) {
988 # The end died: the run died, and hands out nothing more.
989 3 ($ended, $spent, @todo, @from_source) = ('died', 1);
990 3 die $e; ## no critic (ErrorHandling::RequireCarping): thrown again, as it was
991 }
992 118 return;
993 232 };
994 385 ▶ my $next = sub () {
Subroutine __ANON__: called CC=36 SCAR=35.8
995 385 ▶ return if $outside;
Branch: 1 branch
BranchTrueFalseTotal
if $outside 3 382 385
996 382 while (1) {
997 855 ▶ if ($into) {
Branch: 1 branch
BranchTrueFalseTotal
if ($into) 535 320 855
998 # All takes at once a step's values where there are no
999 # steps, and the values already at the end.
1000 535 ▶ if (!@steps && @from_source) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @steps and @from_source) 29 506 535
Condition100%3/3
not @steps and @from_source
!ll&&!rl&&r
26024629
MC/DC100%2/2
not @steps and @from_source
not @steps @from_source
Truth table100%3/3
not(A) and B
A= not @stepsB= @from_source
ABresult
0X0
100
111
1001 29 push @$into, splice @from_source;
1002 29 next;
1003 }
1004 506 ▶ if (@todo && $todo[-1][0] == @steps) {
Branch: 1 branch
BranchTrueFalseTotal
if (@todo and $todo[-1][0] == @steps) 30 476 506
Condition100%3/3
@todo and $todo[-1][0] == @steps
!ll&&!rl&&r
467930
MC/DC100%2/2
@todo and $todo[-1][0] == @steps
@todo $todo[-1][0] == @steps
Truth table100%3/3
A and B
A= @todoB= $todo[-1][0] == @steps
ABresult
0X0
100
111
1005 30 ▶ push @$into, (pop @todo)->[1]
Branch: 1 branch
BranchTrueFalseTotal
@todo and $todo[-1][0] == @steps and do { push @$into, pop(@todo)->[1] } 31 30 61
Condition100%3/3
@todo and $todo[-1][0] == @steps
!ll&&!rl&&r
29131
MC/DC100%2/2
@todo and $todo[-1][0] == @steps
@todo $todo[-1][0] == @steps
Truth table100%3/3
A and B
A= @todoB= $todo[-1][0] == @steps
ABresult
0X0
100
111
1006 while @todo && $todo[-1][0] == @steps;
1007 30 next;
1008 }
1009 }
1010 796 my($n, $value);
1011 796 ▶ if (@todo) {
Branch: 3 branches
BranchTrueFalseTotal
if (@todo) { } 34 762 796
elsif (@from_source) { } 28 734 762
elsif (not $done) { } 545 189 734
1012 34 ($n, $value) = @{ pop @todo };
1013 }
1014 elsif (@from_source) {
1015 28 ($n, $value) = (0, shift @from_source);
1016 }
1017 elsif (!$done) {
1018 545 try {
1019 545 @from_source = $pull->();
1020 }
1021 catch ($e) {
1022 24 ▶ if ($e isa 'Ekorn::Pipeline::Stop') {
Branch: 1 branch
BranchTrueFalseTotal
if ($e isa "Ekorn::Pipeline::Stop") 11 13 24
1023 11 ▶ my(undef, @carried) = is_arrayref($e) ? @$e : ();
Branch: 1 branch
BranchTrueFalseTotal
Ref::Util::XS::is_arrayref($e) ? : 8 3 11
1024 11 ($done, $source_done) = (1, 1);
1025 11 @from_source = @carried;
1026 11 next;
1027 }
1028 13 ($done, @todo, @from_source) = (1);
1029 13 $finish->(1);
1030 13 ▶ croak $e if is_ref($e);
Branch: 1 branch
BranchTrueFalseTotal
if Ref::Util::XS::is_ref($e) 2 11 13
1031 11 die $e; ## no critic (ErrorHandling::RequireCarping): thrown again, as it was
1032 }
1033 521 ▶ unless (@from_source) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@from_source) 136 385 521
1034 136 ($done, $source_done) = (1, 1);
1035 136 next;
1036 }
1037 385 ($n, $value) = (0, shift @from_source);
1038 }
1039 else {
1040 # Nothing waits, and the source is done.
1041 189 $finish->(0);
1042 186 last;
1043 }
1044 447 ▶ if ($n < @steps) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < @steps) 271 176 447
1045 271 ▶ local $running{ending} = undef if $running{ending};
Branch: 1 branch
BranchTrueFalseTotal
if $running{"ending"} 1 270 271
1046 local $running{context} = $context
1047 271 ▶ if defined $context || defined $running{context};
Branch: 1 branch
BranchTrueFalseTotal
if defined $context or defined $running{"context"} 22 249 271
Condition66%2/3
defined $context or defined $running{"context"}
l!l&&r!l&&!r
220249
MC/DC50%1/2
defined $context or defined $running{"context"}
defined $context defined $running{"context"}
Truth table66%2/3
A or B
A= defined $contextB= defined $running{"context"}
ABresult
1X1
011
000
1048 271 my $kept = 1;
1049 271 try {
1050 271 ▶ while ($n < @steps) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < @steps) 322 203 525
1051 322 local $_ = $value;
1052 322 my @values = $steps[$n]->($value);
1053 272 ▶ last if $outside;
Branch: 1 branch
BranchTrueFalseTotal
if $outside 2 270 272
1054 270 $n++;
1055 270 ▶ unless (@values) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@values) 16 254 270
1056 16 $kept = 0;
1057 16 last;
1058 }
1059 254 $value = shift @values;
1060 254 ▶ push @todo, map { [$n, $_] } reverse @values if @values;
Branch: 1 branch
BranchTrueFalseTotal
if @values 32 222 254
1061 }
1062 }
1063 catch ($e) {
1064 50 ▶ if ($e isa 'Ekorn::Pipeline::Stop') {
Branch: 1 branch
BranchTrueFalseTotal
if ($e isa "Ekorn::Pipeline::Stop") 36 14 50
1065 36 ▶ my(undef, @carried) = is_arrayref($e) ? @$e : ();
Branch: 1 branch
BranchTrueFalseTotal
Ref::Util::XS::is_arrayref($e) ? : 34 2 36
1066 36 $done = 1;
1067 36 @from_source = ();
1068 36 @todo = map { [$n + 1, $_] } reverse @carried;
1069 36 next;
1070 }
1071 14 ($done, @todo, @from_source) = (1);
1072 14 $finish->(1);
1073 14 ▶ croak $e if is_ref($e);
Branch: 1 branch
BranchTrueFalseTotal
if Ref::Util::XS::is_ref($e) 2 12 14
1074 12 my $at = Carp::shortmess() =~ s( \.? \n \z )()xr;
1075 12 my $message = $e =~ s( \n \z )()xr =~ s( ^ )( )xmgr;
1076 12 my $stage = _run_label($pipeline, $run, $n);
1077 12 die "Pipeline $stage failed$at:\n$message\n";
1078 }
1079 221 ▶ return if $outside;
Branch: 1 branch
BranchTrueFalseTotal
if $outside 2 219 221
1080 219 ▶ next unless $kept;
Branch: 1 branch
BranchTrueFalseTotal
unless $kept 16 203 219
1081 }
1082 # The run ends once nothing waits before the last step: values
1083 # from the source wait before the first, if there is one.
1084 379 ▶ $finish->(0)
Branch: 1 branch
BranchTrueFalseTotal
if $done and not $finished and not @from_source && @steps and not @todo && $todo[0][0] < @steps 21 358 379
Condition100%15/15
$done and not $finished and not @from_source && @steps and not @todo && $todo[0][0] < @steps
!ll&&!rl&&r
354421
$done and not $finished and not @from_source && @steps
!ll&&!rl&&r
350425
$done and not $finished
!ll&&!rl&&r
346429
@from_source && @steps
!ll&&!rl&&r
2234
@todo && $todo[0][0] < @steps
!ll&&!rl&&r
2014
MC/DC100%6/6
$done and not $finished and not @from_source && @steps and not @todo && $todo[0][0] < @steps
$done not $finished @from_source @steps @todo $todo[0][0] < @steps
Truth table77%7/9
A and not(B) and not(C && D) and not(E && F)
A= $doneB= not $finishedC= @from_sourceD= @stepsE= @todoF= $todo[0][0] < @steps
ABCDEFresult
0XXXXX0
10XXXX0
1111XX0
110X110
1110110
110X0X1
110X101
11100X1
1110101
1085 if $done
1086 && !$finished
1087 && !(@from_source && @steps)
1088 && !(@todo && $todo[0][0] < @steps);
1089 379 ▶ return $value unless $into;
Branch: 1 branch
BranchTrueFalseTotal
unless $into 164 215 379
1090 215 push @$into, $value;
1091 }
1092 # Everything has been handed out: the iterator is spent.
1093 186 $spent = 1;
1094 186 return;
1095 232 };
1096 133 ▶ my $all = sub () {
Subroutine __ANON__: called CC=1 SCAR=0.0
1097 133 my @all;
1098 133 $into = \@all;
1099 133 try {
1100 133 $next->();
1101 }
1102 catch ($e) {
1103 13 $into = undef;
1104 13 die $e; ## no critic (ErrorHandling::RequireCarping): thrown again, as it was
1105 }
1106 120 $into = undef;
1107 120 return @all;
1108 232 };
1109 257 ▶ my $end_run = sub () {
Subroutine __ANON__: called CC=4 SCAR=13.9
1110 257 $outside = 1;
1111 257 ▶ $dropped = 1 if @todo || @from_source;
Branch: 1 branch
BranchTrueFalseTotal
if @todo or @from_source 8 249 257
Condition100%3/3
@todo or @from_source
l!l&&r!l&&!r
17249
MC/DC100%2/2
@todo or @from_source
@todo @from_source
Truth table100%3/3
A or B
A= @todoB= @from_source
ABresult
1X1
011
000
1112 257 ($done, @todo, @from_source) = (1);
1113 257 $finish->(0);
1114 257 ▶ $ended = 'stopped' if $dropped;
Branch: 1 branch
BranchTrueFalseTotal
if $dropped 14 243 257
1115 257 return;
1116 232 };
1117 84 ▶ my $ended_once = sub () {
Subroutine __ANON__: called CC=5 SCAR=16.1
1118 84 ▶ return $ended if $spent || $outside || ($ended // '') eq 'died';
Branch: 1 branch
BranchTrueFalseTotal
if $spent or $outside or ($ended // "") eq "died" 77 7 84
Condition100%8/8
$spent or $outside or ($ended // "") eq "died"
l!l&&r!l&&!r
66117
$spent or $outside
l!l&&r!l&&!r
561018
$ended // ""
l!l
117
MC/DC100%3/3
$spent or $outside or ($ended // "") eq "died"
$spent $outside $ended
Truth table100%4/4
A or B or C
A= $spentB= $outsideC= $ended
ABCresult
1XX1
01X1
0011
0000
1119 7 return;
1120 232 };
1121 232 return Ekorn::Pipeline::Iterator->new(
1122 $next, $end_run, $context, $ended_once, $ended_once, $all,
1123 );
1124 }
1125 27 1;
1126 __END__
↑ Summary Pipeline/Sugar.pm »