Pipeline.pm

filter: Statementstmt 100% 537 / 537 Branchbran 100% 310 / 310 Conditioncond 96.4% 161 / 167 MC/DCmcdc 93.7% 90 / 96 Subroutinesub 100% 62 / 62 Podpod 100% 10 / 10 totaltotal 98.9% 1170 / 1182 CC 223 SCAR 27.4
CC
223
Cov
99%
__ANON__
40.0
__ANON__
36.0
_make_run
25.0
CRAP 15.5
↑ Summary Pipeline/Sugar.pm »
1 25 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package Ekorn::Pipeline;
4  
5 25 ▶use experimental qw(builtin try);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 25 ▶use builtin qw(blessed refaddr weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 25 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 25 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 25 ▶use Ref::Util qw(is_arrayref is_coderef is_plain_hashref is_ref);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 25 ▶use parent 'Ekorn::Pipeline::Element';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 25 ▶use Ekorn::Pipeline::Errors qw(croakf);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 25 ▶use Ekorn::Pipeline::Iterator qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 25 ▶use Ekorn::Pipeline::Stage qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 25 ▶use namespace::clean -except => 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 25 our $VERSION = 'v0.3.5';
21  
22 25 our @EXPORT_OK = qw(is_sink is_stage);
23  
24 # Croak at the line that called Ekorn::Pipeline.
25 25 our @CARP_NOT = @Ekorn::Pipeline::Errors::INTERNAL;
26  
27 # Return true if what's given can go where a stage goes: a code
28 # reference; a pipeline, whose stages go in its place; or anything
29 # offering a stage's or a sink's role method.
30 1670 ▶sub is_stage ($thing) {
Subroutine is_stage: called CC=3 SCAR=11.0
Pod documented
31 1670 ▶ 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
860687123
Ref::Util::XS::is_coderef($thing) or $thing isa "Ekorn::Pipeline"
l!l&&r!l&&!r
615245810
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
32 || $thing isa 'Ekorn::Pipeline'
33 || _stage_role($thing));
34 }
35  
36  
37 # Return "one" if what's given is a sink giving one value, "many" if
38 # it's a sink giving a list, or false otherwise; a pipeline, by the
39 # sink it ends in.
40 1024 ▶sub is_sink ($thing) {
Subroutine is_sink: called CC=5 SCAR=16.1
Pod documented
41 1024 ▶ ($thing) = ($thing->stages)[-1] if $thing isa 'Ekorn::Pipeline';
Branch: 1 branch
BranchTrueFalseTotal
if $thing isa "Ekorn::Pipeline" 4 1020 1024
42 1024 ▶ return !!0 unless blessed($thing);
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($thing) 5 1019 1024
43 1019 ▶ return 'one' if $thing->can('EKORN_SINK_ONE');
Branch: 1 branch
BranchTrueFalseTotal
if $thing->can("EKORN_SINK_ONE") 96 923 1019
44 923 ▶ return 'many' if $thing->can('EKORN_SINK_MANY');
Branch: 1 branch
BranchTrueFalseTotal
if $thing->can("EKORN_SINK_MANY") 65 858 923
45 858 return !!0;
46 }
47  
48  
49 # Return the role method that what's given offers for a stage's place,
50 # EKORN_STAGE, EKORN_SINK_ONE or EKORN_SINK_MANY, or nothing if it offers none.
51 2282 ▶sub _stage_role ($thing) {
Subroutine _stage_role: called CC=2 SCAR=6.9
52 2282 ▶ return unless blessed($thing);
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($thing) 665 1617 2282
53 my @roles
54 1617 = grep { $thing->can($_) } qw(EKORN_STAGE EKORN_SINK_ONE EKORN_SINK_MANY);
55 1617 return $roles[0];
56 }
57  
58  
59 # Return true if what's given offers a source's role method.
60 1097 ▶sub _is_source ($thing) {
Subroutine _is_source: called CC=2 SCAR=6.9
61 1097 ▶ return !!(blessed($thing) && $thing->can('EKORN_SOURCE'));
Condition100%3/3
builtin::blessed($thing) && $thing->can('EKORN_SOURCE')
!ll&&!rl&&r
548238311
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
62 }
63  
64  
65 # Return the stage given as an element: a part offering a role method is
66 # kept as it is, and a code reference is made a Stage.
67 1416 ▶sub _element ($stage) {
Subroutine _element: called CC=3 SCAR=11.0
68 1416 ▶ 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) 870 546 1416
Condition100%3/3
$stage isa "Ekorn::Pipeline::Element" or _stage_role($stage)
l!l&&r!l&&!r
8628546
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
69 546 return Ekorn::Pipeline::Stage->new($stage);
70 }
71  
72  
73 # Croak unless what's given, as stage #$n, can go where a stage goes, as
74 # one role: a source isn't a stage, and a part offering more than one of
75 # a stage's and a sink's role methods couldn't say which it is.
76 1419 ▶sub _check_stage ($stage, $n) {
Subroutine _check_stage: called CC=6 SCAR=17.9
77 my @roles
78 = blessed($stage)
79 1419 ▶ ? grep { $stage->can($_) } qw(EKORN_STAGE EKORN_SINK_ONE EKORN_SINK_MANY)
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($stage) ? : 872 547 1419
80 : ();
81 1419 ▶ croakf
Branch: 1 branch
BranchTrueFalseTotal
if @roles > 1 1 1418 1419
82 'Expected stage #%d to offer one of EKORN_STAGE, EKORN_SINK_ONE and EKORN_SINK_MANY, not %s',
83 $n,
84 join ' and ', @roles
85 if @roles > 1;
86 1418 ▶ croakf 'Expected stage #%d to be a stage, not a source', $n
Branch: 1 branch
BranchTrueFalseTotal
if not @roles and _is_source($stage) 1 1417 1418
Condition100%3/3
not @roles and _is_source($stage)
!ll&&!rl&&r
6347831
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
87 if !@roles && _is_source($stage);
88 1417 ▶ croakf
Branch: 1 branch
BranchTrueFalseTotal
unless is_stage($stage) 6 1411 1417
89 q(Expected stage #%d to be a stage: a code reference, a pipeline, or an object offering a stage's role method),
90 $n
91 unless is_stage($stage);
92 1411 return;
93 }
94  
95  
96 # Return a pipeline, called with values to run each of them through the
97 # stages. A pipeline given as a stage has its stages spliced in, each
98 # named by its place in each pipeline, such as "2.3". In scalar context,
99 # a call gives how many values came out, unless the last stage is a
100 # scalar sink, when it gives that sink's result.
101 1155 ▶sub new ($class, @given) {
Subroutine new: called CC=6 SCAR=17.9
Pod documented
102 1155 my(@stages, @names, @within);
103 1155 for my $n (1 .. @given) {
104 1419 my $stage = $given[$n - 1];
105 1419 _check_stage($stage, $n);
106 1411 ▶ if ($stage isa 'Ekorn::Pipeline') {
Branch: 1 branch
BranchTrueFalseTotal
if ($stage isa "Ekorn::Pipeline") 236 1175 1411
107 236 my($inner, $inner_names, $inner_within) = _composed($stage);
108 236 ▶ croakf
Branch: 1 branch
BranchTrueFalseTotal
unless $inner_names 3 233 236
109 'Expected stage #%d, a pipeline, to give only stages from its stages method',
110 $n
111 unless $inner_names;
112 233 push @stages, map { _element($_) } @$inner;
113 233 push @names, map { "$n.$_" } @$inner_names;
114 push @within,
115 233 map { [[$stage, $inner_names->[$_]], @{ $inner_within->[$_] }] }
116 0 .. $#$inner;
117 233 next;
118 }
119 1175 push @stages, _element($stage);
120 1175 push @names, $n;
121 1175 push @within, [];
122 }
123 # In scalar context, a run gives a scalar sink's result, a list
124 # sink's list as an array reference, or how many values came out.
125 1144 ▶ my $sink = @stages ? is_sink($stages[-1]) : !!0;
Branch: 1 branch
BranchTrueFalseTotal
@stages ? : 977 167 1144
126 # Where every stage is a plain step, a Stage of Ekorn's own, with no
127 # flush, end or sink, and nothing made for a run, a push runs the
128 # steps in a plain loop, with none of a run's machinery; otherwise it
129 # makes the stages for the run first, and takes the plain loop if
130 # what's made allows it.
131 my @steps
132 1410 = (grep { ref ne 'Ekorn::Pipeline::Stage' } @stages)
133 ? ()
134 1144 ▶ : map { $_->{call} } @stages;
Branch: 1 branch
BranchTrueFalseTotal
grep({ref $_ ne 'Ekorn::Pipeline::Stage';} @stages) ? : 576 568 1144
135 # The pipeline, for its name in messages, held weakly, so the closure
136 # it's called as never keeps it.
137 1144 my $self;
138 my $pipeline = bless {
139 stages => \@stages,
140 names => \@names,
141 within => \@within,
142 582 ▶ call => sub (@values) {
Subroutine __ANON__: called CC=7 SCAR=19.5
143 582 ▶ return @values unless @stages;
Branch: 1 branch
BranchTrueFalseTotal
unless @stages 8 574 582
144 my @results
145 574 ▶ = @steps
Branch: 1 branch
BranchTrueFalseTotal
@steps ? : 229 345 574
146 ? _push_steps($self, \@steps, \@names, \@within, @values)
147 : _pushed($self, \@stages, \@names, \@within, \@values);
148 471 ▶ return @results if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 387 84 471
149 84 ▶ return $results[0] if $sink && $sink eq 'one';
Branch: 1 branch
BranchTrueFalseTotal
if $sink and $sink eq "one" 42 42 84
Condition100%3/3
$sink and $sink eq "one"
!ll&&!rl&&r
36642
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
150 42 ▶ return \@results if $sink;
Branch: 1 branch
BranchTrueFalseTotal
if $sink 6 36 42
151 36 return scalar @results;
152 },
153 1144 }, $class;
154 1144 weaken($self = $pipeline);
155 1144 return $pipeline;
156 }
157  
158 # The named pipelines, each by its name qualified by its package.
159 25 my %named;
160  
161 # Return $name qualified by $package, unless it's qualified already, or
162 # nothing if it isn't a name: identifiers, as Perl's, joined by "::".
163 117 ▶sub _qualified ($name, $package) {
Subroutine _qualified: called CC=5 SCAR=16.1
164 117 ▶ return if !defined $name || is_ref($name);
Branch: 1 branch
BranchTrueFalseTotal
if not defined $name or Ref::Util::XS::is_ref($name) 6 111 117
Condition100%3/3
not defined $name or Ref::Util::XS::is_ref($name)
l!l&&r!l&&!r
33111
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
165 111 ▶ 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 99 111
166 99 ▶ return $name =~ m( :: )x ? $name : "${package}::$name";
Branch: 1 branch
BranchTrueFalseTotal
$name =~ / :: /ux ? : 28 71 99
167 }
168  
169  
170 # Return the stages given, each name replaced by the pipeline it names,
171 # looked up in $package unless it's qualified.
172 109 ▶sub _resolved ($package, @given) {
Subroutine _resolved: called CC=5 SCAR=16.1
173 109 my @stages;
174 109 for my $n (1 .. @given) {
175 120 my $stage = $given[$n - 1];
176 120 ▶ if (is_stage($stage)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_stage($stage)) 54 66 120
177 54 push @stages, $stage;
178 54 next;
179 }
180 66 my $name = _qualified($stage, $package);
181 66 ▶ croakf
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 13 53 66
182 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),
183 $n
184 unless defined $name;
185 53 my $pipeline = $named{$name};
186 53 ▶ croakf 'No pipeline named %s', $name unless $pipeline;
Branch: 1 branch
BranchTrueFalseTotal
unless $pipeline 6 47 53
187 47 push @stages, $pipeline;
188 }
189 90 return @stages;
190 }
191  
192  
193 # Return a new pipeline of the invocant's class, of the stages given, each
194 # a code reference, a pipeline, or the name of one, looked up now in
195 # $package unless it's qualified; or, given one name alone, the pipeline
196 # it names, if it's of the invocant's class, so its stages are numbered
197 # where it's given. The one method that reads names, called by the words,
198 # such as Factories' pipeline, with their caller's package.
199 84 ▶sub pipeline_in ($class, $package, @stages) {
Subroutine pipeline_in: called CC=4 SCAR=13.9
Pod documented
200 84 my @resolved = _resolved($package, @stages);
201 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
202 if @stages == 1 && !is_stage($stages[0]) && $resolved[0] isa $class;
203 37 return $class->new(@resolved);
204 }
205  
206  
207 # Name a pipeline, in the caller's package unless the name is qualified,
208 # and return it: on the class, a new one of the stages after the name,
209 # read as pipeline_in reads them; on a pipeline, that one. The name can't
210 # be one already defined for another pipeline.
211 51 ▶sub named ($invocant, $name, @stages) {
Subroutine named: called CC=8 SCAR=20.8
Pod documented
212 51 ▶ my $class = ref $invocant ? ref $invocant : $invocant;
Branch: 1 branch
BranchTrueFalseTotal
ref $invocant ? : 21 30 51
213 51 my $package = caller;
214 51 my $qualified = _qualified($name, $package);
215 51 ▶ croakf 'Expected a name, made of words joined by ::' unless defined $qualified;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $qualified 5 46 51
216 46 my $pipeline;
217 46 ▶ if (ref $invocant) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $invocant) { } 21 25 46
218 21 ▶ croakf 'Expected only a name, naming a pipeline you have' if @stages;
Branch: 1 branch
BranchTrueFalseTotal
if @stages 1 20 21
219 20 $pipeline = $invocant;
220 }
221 else {
222 # Always a new one, even of one name, so a name never aliases.
223 25 $pipeline = $class->new(_resolved($package, @stages));
224 }
225 43 my $own = $named{$qualified};
226 43 ▶ croakf 'A pipeline named %s is already defined', $qualified
Branch: 1 branch
BranchTrueFalseTotal
if $own and builtin::refaddr($own) != builtin::refaddr($pipeline) 3 40 43
Condition100%3/3
$own and builtin::refaddr($own) != builtin::refaddr($pipeline)
!ll&&!rl&&r
3913
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
227 if $own && refaddr($own) != refaddr($pipeline);
228 40 ▶ $pipeline->{name} = $qualified unless defined $pipeline->{name};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pipeline->{"name"} 38 2 40
229 40 return $named{$qualified} = $pipeline;
230 }
231  
232  
233 # Return the first name the pipeline was given, qualified by its
234 # package, or undef if it has none.
235 232 ▶sub name ($self) {
Subroutine name: called CC=1 SCAR=0.0
Pod documented
236 232 return $self->{name};
237 }
238  
239 # Return the stages of a pipeline given as a stage, from its stages
240 # method, as giving its stages would, their names, and the pipelines
241 # each came from within it: its own, if the method returns the stages it
242 # was made with, as it does unless a subclass overrides it, or else their
243 # places in what it returns. The names are undefined if what it returns
244 # isn't all code references.
245 253 ▶sub _composed ($pipeline) {
Subroutine _composed: called CC=6 SCAR=17.9
246 253 my @stages = $pipeline->stages;
247 253 my $made = $pipeline->{stages};
248 253 my $same = @stages == @$made;
249 253 for my $i (0 .. $#stages) {
250 259 ▶ last unless $same;
Branch: 1 branch
BranchTrueFalseTotal
unless $same 8 251 259
251 251 ▶ $same = (refaddr($stages[$i]) // 0) == refaddr($made->[$i]);
Condition100%2/2
builtin::refaddr($stages[$i]) // 0
l!l
2501
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
252 }
253 253 ▶ return \@stages, $pipeline->{names}, $pipeline->{within} if $same;
Branch: 1 branch
BranchTrueFalseTotal
if $same 244 9 253
254 9 ▶ return \@stages, undef if grep { !is_stage($_) } @stages;
Branch: 1 branch
BranchTrueFalseTotal
if grep {not is_stage($_);} @stages 4 5 9
255 5 return \@stages, [1 .. @stages], [map { [] } @stages];
256 }
257  
258  
259 # Return an iterator handing out, one per call, the values from $source
260 # run through the stages: $source a code reference, or anything
261 # offering a source's role method, an iterator included.
262 357 ▶sub iterator ($self, $source) {
Subroutine iterator: called CC=3 SCAR=11.0
Pod documented
263 357 ▶ 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 354 357
Condition100%3/3
Ref::Util::XS::is_coderef($source) or _is_source($source)
l!l&&r!l&&!r
443103
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
264 unless is_coderef($source) || _is_source($source);
265 return _iterator(
266 $self, $self->{stages}, $self->{names}, $self->{within},
267 354 $source,
268 );
269 }
270  
271  
272 # Return the stages, in order, as a list, to splice into another
273 # pipeline or give to any function taking stages.
274 265 ▶sub stages ($self) {
Subroutine stages: called CC=1 SCAR=0.0
Pod documented
275 265 return @{ $self->{stages} };
276 }
277  
278 # What a pipeline is doing while its stages run.
279 my %running;
280  
281 # For code telling its stages about the value they're given: true while
282 # a flush runs, and while the values it gave go through the stages,
283 # rather than values from the source.
284 15 ▶sub ending () {
Subroutine ending: called CC=1 SCAR=0.0
Pod documented
285 15 return $running{ending};
286 }
287  
288  
289 # For code telling its stages about the run: the context the run's
290 # source gave, if any, such as a statement's columns. A pushed run has
291 # none; an iterator used as a source passes on its run's.
292 20 ▶sub run_context () {
Subroutine run_context: called CC=1 SCAR=0.0
Pod documented
293 20 return $running{context};
294 }
295  
296  
297 # Return the step, the end and the context of a run of $source: from
298 # its EKORN_SOURCE, if it offers one; a code reference is its own step,
299 # with no end or context.
300 354 ▶sub _source_run ($source) {
Subroutine _source_run: called CC=5 SCAR=16.1
301 # As _is_source asks, without the call, as every run asks it.
302 354 ▶ return ($source) unless blessed($source) && $source->can('EKORN_SOURCE');
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($source) and $source->can("EKORN_SOURCE") 44 310 354
Condition66%2/3
builtin::blessed($source) and $source->can("EKORN_SOURCE")
!ll&&!rl&&r
440310
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
303 310 my($step, $end, $context)
304 = _parts([qw(step end context)], $source->EKORN_SOURCE);
305 303 ▶ croakf
Branch: 1 branch
BranchTrueFalseTotal
unless Ref::Util::XS::is_coderef($step) and _code_or_none($end) 4 299 303
Condition100%3/3
Ref::Util::XS::is_coderef($step) and _code_or_none($end)
!ll&&!rl&&r
31299
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
306 'Expected the source to return a code reference, and optionally an end and a context'
307 unless is_coderef($step) && _code_or_none($end);
308 299 return ($step, $end, $context);
309 }
310  
311  
312 # Return the parts a role method or a set-up returned as a list, in the
313 # order of the keys in @$order: as they are, if they're a list, or, if
314 # they're one plain hash reference, its values for those keys. Other
315 # keys are ignored.
316 1212 ▶sub _parts ($order, @returned) {
Subroutine _parts: called CC=3 SCAR=11.0
317 1212 ▶ 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]) 909 303 1212
Condition100%3/3
@returned == 1 and Ref::Util::XS::is_plain_hashref($returned[0])
!ll&&!rl&&r
316593303
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
318 303 return @{ $returned[0] }{@$order};
319 }
320  
321  
322 # Return true if each of what's given is a code reference, or undefined.
323 1192 ▶sub _code_or_none (@things) {
Subroutine _code_or_none: called CC=2 SCAR=6.9
324 1192 ▶ return !grep { defined && !is_coderef($_) } @things;
Condition100%3/3
defined $_ && !Ref::Util::XS::is_coderef($_)
!ll&&!rl&&r
10445106
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
325 }
326  
327  
328 # Return how a stage is named in a message: by its place, $name, and, if
329 # it's within a named pipeline, the innermost, by its name and its place
330 # in it, from the pipelines in @$within it came from, and $pipeline, the
331 # one being run.
332 213 ▶sub _label ($pipeline, $name, $within) {
Subroutine _label: called CC=3 SCAR=11.0
333 213 for my $in (reverse [$pipeline, $name], @$within) {
334 227 my($outer, $place) = @$in;
335 227 my $outer_name = $outer->name;
336 227 ▶ return "stage #$name ($outer_name#$place)" if defined $outer_name;
Branch: 1 branch
BranchTrueFalseTotal
if defined $outer_name 10 217 227
337 }
338 203 return "stage #$name";
339 }
340  
341  
342 # Return the results of pushing @values through @$steps, each a plain
343 # step, as a run of them would give them, without a run's machinery: each
344 # value through every step before the next, what a step returns going on
345 # in order, a stop's values going on from the step after it, with
346 # nothing else, and a step that dies named as a run names it.
347 341 ▶sub _push_steps ($pipeline, $steps, $names, $within, @values) {
Subroutine _push_steps: called CC=10 SCAR=23.0
348 # A pushed run has no context, and gives no values from a flush.
349 341 local $running{context} = undef;
350 341 local $running{ending} = undef;
351 341 my(@results, @todo, $n);
352 341 my($at, $more) = (0, 1);
353 # One try for as many values as go through without a stop, which is
354 # caught, and the values it carries go on from the step after it.
355 341 ▶ while ($more) {
Branch: 1 branch
BranchTrueFalseTotal
if ($more) 440 283 723
356 440 try {
357 VALUE:
358 440 while (1) {
359 1476 my $value;
360 1476 ▶ if (@todo) { ($n, $value) = @{ pop @todo } }
Branch: 2 branches
BranchTrueFalseTotal
if (@todo) { } 288 1188 1476
elsif ($at < @values) { } 905 283 1188
361 905 elsif ($at < @values) { ($n, $value) = (0, $values[$at++]) }
362 283 else { $more = 0; last VALUE }
363 1193 ▶ while ($n < @$steps) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < @$steps) 1448 885 2333
364 1448 local $_ = $value;
365 1448 my @values = $steps->[$n]->($value);
366 1291 $n++;
367 1291 ▶ next VALUE unless @values;
Branch: 1 branch
BranchTrueFalseTotal
unless @values 151 1140 1291
368 1140 $value = shift @values;
369 1140 ▶ push @todo, map { [$n, $_] } reverse @values if @values;
Branch: 1 branch
BranchTrueFalseTotal
if @values 199 941 1140
370 }
371 885 push @results, $value;
372 }
373 }
374 catch ($e) {
375 157 ▶ if ($e isa 'Ekorn::Pipeline::Stop') {
Branch: 1 branch
BranchTrueFalseTotal
if ($e isa "Ekorn::Pipeline::Stop") 99 58 157
376 99 ▶ my(undef, @carried) = is_arrayref($e) ? @$e : ();
Branch: 1 branch
BranchTrueFalseTotal
Ref::Util::XS::is_arrayref($e) ? : 97 2 99
377 99 ($at, @todo) = (scalar @values);
378 99 push @todo, map { [$n + 1, $_] } reverse @carried;
379 99 next;
380 }
381 58 ▶ croak $e if is_ref($e);
Branch: 1 branch
BranchTrueFalseTotal
if Ref::Util::XS::is_ref($e) 2 56 58
382 56 my $where = Carp::shortmess() =~ s( \.? \n \z )()xr;
383 56 my $message = $e =~ s( \n \z )()xr =~ s( ^ )( )xmgr;
384 56 my $stage = _label($pipeline, $names->[$n], $within->[$n]);
385 56 die "Pipeline $stage failed$where:\n$message\n";
386 }
387 }
388 283 return @results;
389 }
390  
391  
392 # Return the text of an end's error: a stop's own text, "Stop used
393 # outside a pipeline", would mislead, as the stop was in a run.
394 13 ▶sub _end_error ($error) {
Subroutine _end_error: called CC=2 SCAR=6.9
395 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
396 if $error isa 'Ekorn::Pipeline::Stop';
397 11 return "$error";
398 }
399  
400  
401 # Call each end given, as [$label, $end, $how], in order. While the run
402 # is dying, an end that dies is reported as a warning naming it, and the
403 # rest are called. Otherwise, an end that dies tells every end after it
404 # "died", and its error is thrown once they've run.
405 502 ▶sub _end_all ($ends, $dying) {
Subroutine _end_all: called CC=9 SCAR=22.0
406 502 my($error, $failed);
407 502 for my $call (@$ends) {
408 349 my($label, $end, $how) = @$call;
409 349 try {
410 349 local $_ = undef;
411 349 ▶ $end->($dying || defined $error ? 'died' : $how);
Branch: 1 branch
BranchTrueFalseTotal
$dying || defined $error ? : 61 288 349
Condition100%3/3
$dying or defined $error
l!l&&r!l&&!r
583288
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
412 }
413 catch ($e) {
414 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
415 2 Carp::carp sprintf 'The end of %s died while the run was already dying: %s',
416 $label, _end_error($e) =~ s( \n \z )()xr;
417 }
418 else {
419 12 ($error, $failed) = ($e, $label);
420 }
421 }
422 }
423 502 ▶ return unless defined $error;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $error 490 12 502
424 # A stop from an end is an error like any other: the run has ended.
425 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
426 11 my $at = Carp::shortmess() =~ s( \.? \n \z )()xr;
427 11 my $message = _end_error($error) =~ s( \n \z )()xr =~ s( ^ )( )xmgr;
428 11 die "Pipeline end of $failed failed$at:\n$message\n";
429 }
430  
431  
432 # Make the parts of a run from @$stages, named by @$names, into the
433 # lists in %$run: each part's step, its flush and its end, by its role
434 # method, and whether it's a sink. A gather's block may return a
435 # pipeline as its step, whose stages are made in its place, followed by
436 # the gather's flush and end, if any, on a stage of their own that
437 # passes values on.
438 591 ▶sub _make_run ($stages, $names, $within, $run) {
Subroutine _make_run: called CC=25 SCAR=32.2
439 591 for my $i (0 .. $#$stages) {
440 918 my($stage, $name) = ($stages->[$i], $names->[$i]);
441 918 ▶ my $role = _stage_role($stage) // '';
Condition100%2/2
_stage_role($stage) // ""
l!l
9171
MC/DC100%1/1
_stage_role($stage) // ""
_stage_role($stage)
Truth table100%2/2
A
A= _stage_role($stage)
Aresult
11
00
442 918 my($step, $flush, $end);
443 918 ▶ if ($stage isa 'Ekorn::Pipeline::Gather') {
Branch: 3 branches
BranchTrueFalseTotal
if ($stage isa 'Ekorn::Pipeline::Gather') { } 431 487 918
elsif ($role eq 'EKORN_SINK_ONE' or $role eq 'EKORN_SINK_MANY') { } 137 350 487
elsif ($role eq 'EKORN_STAGE') { } 349 1 350
444 431 my @made = _parts([qw(step flush end)], $stage->EKORN_STAGE);
445 426 ▶ my($inner, $inner_names, $inner_within)
Branch: 1 branch
BranchTrueFalseTotal
@made && $made[0] isa 'Ekorn::Pipeline' ? : 17 409 426
Condition100%3/3
@made and $made[0] isa "Ekorn::Pipeline"
!ll&&!rl&&r
140817
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
446 = @made && $made[0] isa 'Ekorn::Pipeline' ? _composed($made[0]) : ();
447 426 ▶ 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 414 426
$inner ? : 16 407 423
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
102414
@made == 1 || @made == 2 || @made == 3 and $inner ? $inner_names : Ref::Util::XS::is_coderef($made[0])
!ll&&!rl&&r
37416
@made == 1 or @made == 2 or @made == 3
l!l&&r!l&&!r
2261973
@made == 1 or @made == 2
l!l&&r!l&&!r
17848200
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
448 'Expected stage #%s, made with gather, to return a code reference or a pipeline, and optionally a flush and an end',
449 $name
450 unless (@made == 1 || @made == 2 || @made == 3)
451 && ($inner ? $inner_names : is_coderef($made[0]))
452 && _code_or_none(@made[1 .. $#made]);
453 414 ($step, $flush, $end) = @made;
454 414 ▶ if ($inner) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inner) 14 400 414
455 _make_run(
456 $inner,
457 17 [map { "$name.$_" } @$inner_names],
458 [
459 14 map { [
460 17 @{ $within->[$i] }, [$made[0], $inner_names->[$_]],
461 17 @{ $inner_within->[$_] },
462 ] } 0 .. $#$inner
463 ],
464 $run,
465 );
466 14 ▶ next unless $flush || $end;
Branch: 1 branch
BranchTrueFalseTotal
unless $flush or $end 9 5 14
Condition100%3/3
$flush or $end
l!l&&r!l&&!r
419
MC/DC100%2/2
$flush or $end
$flush $end
Truth table100%3/3
A or B
A= $flushB= $end
ABresult
1X1
011
000
467 5 ▶ $step = sub { $_[0] }; # for each value: no signature
Subroutine __ANON__: called CC=1 SCAR=0.0
468 }
469 }
470 ▶ 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
8354350
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
471 137 ($step, $flush, $end) = _parts([qw(step flush end)], $stage->$role);
472 134 ▶ 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 129 134
Condition100%6/6
Ref::Util::XS::is_coderef($step) and Ref::Util::XS::is_coderef($flush) and _code_or_none($end)
!ll&&!rl&&r
41129
Ref::Util::XS::is_coderef($step) and Ref::Util::XS::is_coderef($flush)
!ll&&!rl&&r
13130
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
473 'Expected sink #%s to return a code reference, a flush, and optionally an end',
474 $name
475 unless is_coderef($step)
476 && is_coderef($flush)
477 && _code_or_none($end);
478 # What a sink's step returns is ignored, by the run, which knows
479 # it's a sink; a scalar sink's flush gives one value, undef if
480 # it has none.
481 129 my $result = $flush;
482 70 ▶ $flush = sub () { my @result = $result->(); return $result[0] }
Subroutine __ANON__: called CC=1 SCAR=0.0
483 129 ▶ if $role eq 'EKORN_SINK_ONE';
Branch: 1 branch
BranchTrueFalseTotal
if $role eq "EKORN_SINK_ONE" 75 54 129
484 }
485 elsif ($role eq 'EKORN_STAGE') {
486 349 ($step, $flush, $end) = _parts([qw(step flush end)], $stage->EKORN_STAGE);
487 349 ▶ croakf
Branch: 1 branch
BranchTrueFalseTotal
unless Ref::Util::XS::is_coderef($step) and _code_or_none($flush, $end) 5 344 349
Condition100%3/3
Ref::Util::XS::is_coderef($step) and _code_or_none($flush, $end)
!ll&&!rl&&r
32344
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
488 'Expected stage #%s to return a code reference, and optionally a flush and an end',
489 $name
490 unless is_coderef($step) && _code_or_none($flush, $end);
491 }
492 else {
493 1 $step = $stage;
494 }
495 879 push @{ $run->{stages} }, $step;
496 879 push @{ $run->{names} }, $name;
497 879 push @{ $run->{within} }, $within->[$i];
498 879 push @{ $run->{flushes} }, $flush;
499 879 push @{ $run->{ends} }, $end;
500 879 ▶ push @{ $run->{sinks} },
Condition100%3/3
$role eq 'EKORN_SINK_ONE' || $role eq 'EKORN_SINK_MANY'
l!l&&r!l&&!r
7554750
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
501 $role eq 'EKORN_SINK_ONE' || $role eq 'EKORN_SINK_MANY';
502 }
503 561 return;
504 }
505  
506  
507 # Return an iterator handing out, one per call, the values from $source
508 # run through the stages in @$stages, named in messages by @$names.
509 354 ▶sub _iterator ($pipeline, $stages, $names, $within, $source) {
Subroutine _iterator: called CC=3 SCAR=11.0
510 354 my %run = (
511 stages => [], names => [], within => [], flushes => [], ends => [],
512 sinks => [],
513 );
514 354 my($pull, $source_end, $context);
515 354 try {
516 354 ($pull, $source_end, $context) = _source_run($source);
517 343 ▶ _make_run($stages, $names, $within, \%run) if @$stages;
Branch: 1 branch
BranchTrueFalseTotal
if @$stages 232 111 343
518 }
519 catch ($e) {
520 13 _set_up_died($pipeline, \%run, $source_end, $e);
521 }
522 # A run with no flush, no end of a stage's, and no sink, as most are,
523 # takes the shorter way.
524 return _plain_iterator($pipeline, \%run, $pull, $source_end, $context)
525 341 ▶ unless grep { defined } @{ $run{flushes} }, @{ $run{ends} };
Branch: 1 branch
BranchTrueFalseTotal
unless grep {defined $_;} @{$run{"flushes"};}, @{$run{"ends"};} 203 138 341
526 138 return _full_iterator($pipeline, \%run, $pull, $source_end, $context);
527 }
528  
529  
530 # Return the results of pushing @$values through @$stages, as a run of
531 # them gives them: once the stages are made for the run, by the plain
532 # loop if none has a flush or an end, as most haven't, so a stage made
533 # with gather costs no more a value than a plain one; otherwise by an
534 # iterator of the run. Its set-up is _iterator's, with no source to set
535 # up.
536 345 ▶sub _pushed ($pipeline, $stages, $names, $within, $values) {
Subroutine _pushed: called CC=2 SCAR=6.9
537 345 my %run = (
538 stages => [], names => [], within => [], flushes => [], ends => [],
539 sinks => [],
540 );
541 345 try {
542 345 _make_run($stages, $names, $within, \%run);
543 }
544 catch ($e) {
545 28 _set_up_died($pipeline, \%run, undef, $e);
546 }
547 return _push_steps($pipeline, @run{qw(stages names within)}, @$values)
548 317 ▶ unless grep { defined } @{ $run{flushes} }, @{ $run{ends} };
Branch: 1 branch
BranchTrueFalseTotal
unless grep {defined $_;} @{$run{"flushes"};}, @{$run{"ends"};} 112 205 317
549 349 ▶ return _full_iterator($pipeline, \%run, sub { splice @$values }, undef, undef)
Subroutine __ANON__: called CC=1 SCAR=0.0
550 205 ->all;
551 }
552  
553  
554 # Tell each end of a run made by then, the source's first, that the run
555 # died, as a set-up died with $error, and throw it on.
556 41 ▶sub _set_up_died ($pipeline, $run, $source_end, $error) {
Subroutine _set_up_died: called CC=4 SCAR=13.9
557 _end_all(
558 [
559 ($source_end ? ['the source', $source_end, 'died'] : ()),
560 2 map { [_run_label($pipeline, $run, $_), $run->{ends}[$_], 'died'] }
561 41 ▶ grep { $run->{ends}[$_] } 0 .. $#{ $run->{ends} },
Branch: 1 branch
BranchTrueFalseTotal
$source_end ? : 2 39 41
562 ],
563 1,
564 );
565 # A set-up runs before the run has values, so has nothing to stop.
566 41 ▶ 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 39 41
567 if $error isa 'Ekorn::Pipeline::Stop';
568 39 ▶ croak $error if is_ref($error);
Branch: 1 branch
BranchTrueFalseTotal
if Ref::Util::XS::is_ref($error) 1 38 39
569 38 die $error; ## no critic (ErrorHandling::RequireCarping): thrown again, as it was
570 }
571  
572  
573 # Return an iterator of a run that isn't plain, made by _iterator or
574 # _pushed, from its source's step, end and context.
575 #
576 # The run ends as soon as nothing in it can still act on a value: the
577 # source has run out or stopped, every flush has been called, and every
578 # value still to be handed out has passed the last stage; or when it's
579 # ended from outside, or dies. Then each end is called, the source's
580 # first, told how the run ended from where it stands: "died" if the run
581 # died; else "done" if its input ran out, and "stopped" if not.
582 340 ▶sub _full_iterator ($pipeline, $run, $pull, $source_end, $context) {
Subroutine _full_iterator: called CC=1 SCAR=0.0
583 340 my @stages = @{ $run->{stages} };
584 340 my @flushes = @{ $run->{flushes} };
585 340 my @ends = @{ $run->{ends} };
586 340 my @sinks = @{ $run->{sinks} };
587 # The places of the stages with a flush, so finding the next needn't
588 # look at every stage.
589 340 my @flush_at = grep { $flushes[$_] } 0 .. $#stages;
590 # Each value waiting to go through a stage, as the stage's index, the
591 # value, and whether it came from a flush, the next on top. A value a
592 # stage returns on its own, as most are, carries on through the stages
593 # without waiting here. Once the source is done, the flushes are called
594 # in order, from the first not called or passed over by a stop.
595 my(
596 340 @todo, $done, $source_done, %flushed, @input_done, $finished, $outside,
597 $ended, $spent, $dropped,
598 );
599 340 my $next_flush = 0;
600 # How many values wait at each stage's index, kept as @todo changes, so
601 # asking whether any wait before a stage needn't scan @todo: a flush
602 # can put many values there at once.
603 340 my @waiting_at;
604 # Values a source's step gave, still to go through the stages from the
605 # first, held as they came: @todo's values go first, being further on.
606 my @from_source;
607 # Put @values on @todo, to go on from stage $n, the first on top.
608 180 ▶ my $wait = sub ($n, $from_flush, @values) {
Subroutine __ANON__: called CC=1 SCAR=0.0
609 180 push @todo, map { [$n, $_, $from_flush] } reverse @values;
610 180 $waiting_at[$n] += @values;
611 180 return;
612 340 };
613 # Whether any value waits at a stage up to $upto. The run has a stage,
614 # so values from the source wait at the first.
615 330 ▶ my $waiting_upto = sub ($upto) {
Subroutine __ANON__: called CC=4 SCAR=13.9
616 330 ▶ return 1 if @from_source;
Branch: 1 branch
BranchTrueFalseTotal
if @from_source 10 320 330
617 320 ▶ $waiting_at[$_] && return 1 for 0 .. $upto;
Branch: 1 branch
BranchTrueFalseTotal
$waiting_at[$_] and return 1 18 520 538
618 302 return 0;
619 340 };
620 # How the run ended, as its caller asks: whether the source was read
621 # to its end, "stopped" if values still to be handed out were dropped,
622 # or "died" if anything died, an end included.
623 686 ▶ my $finish = sub ($died) {
Subroutine __ANON__: called CC=8 SCAR=20.8
624 686 ▶ return if $finished++;
Branch: 1 branch
BranchTrueFalseTotal
if $finished++ 346 340 686
625 340 ▶ $ended
Branch: 3 branches
BranchTrueFalseTotal
$died ? : 30 310 340
$dropped ? : 10 300 310
$source_done ? : 245 55 300
626 = $died ? 'died'
627 : $dropped ? 'stopped'
628 : $source_done ? 'done'
629 : 'stopped';
630 340 my @calls;
631 340 ▶ push @calls, ['the source', $source_end, $source_done ? 'done' : 'stopped']
Branch: 2 branches
BranchTrueFalseTotal
if $source_end 95 245 340
$source_done ? : 51 44 95
632 if $source_end;
633 push @calls,
634 map { [
635 129 ▶ _run_label($pipeline, $run, $_), $ends[$_],
Branch: 1 branch
BranchTrueFalseTotal
$input_done[$_] ? : 93 36 129
636 $input_done[$_] ? 'done' : 'stopped',
637 ] }
638 340 grep { $ends[$_] } 0 .. $#ends;
639 340 local $running{context} = $context;
640 340 try {
641 340 _end_all(\@calls, $died);
642 }
643 catch ($e) {
644 # An end died: the run died, and hands out nothing more.
645 9 ($ended, $spent, @todo, @waiting_at, @from_source) = ('died', 1);
646 9 die $e; ## no critic (ErrorHandling::RequireCarping): thrown again, as it was
647 }
648 331 return;
649 340 };
650 # The index of the next flush to call, if any.
651 888 ▶ my $flush_at = sub () {
Subroutine __ANON__: called CC=4 SCAR=13.9
652 888 for my $n (@flush_at) {
653 844 ▶ return $n if $n >= $next_flush && !$flushed{$n};
Branch: 1 branch
BranchTrueFalseTotal
if $n >= $next_flush and not $flushed{$n} 281 563 844
Condition66%2/3
$n >= $next_flush and not $flushed{$n}
!ll&&!rl&&r
5630281
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
654 }
655 607 return;
656 340 };
657 # Every part from the next flush up to $upto has had all its input.
658 565 ▶ my $input_ran_out = sub ($upto) {
Subroutine __ANON__: called CC=2 SCAR=6.9
659 565 $input_done[$_] = 1 for $next_flush .. $upto;
660 565 return;
661 340 };
662 # At a stop at stage $n, with nothing more to come from upstream,
663 # every stage up to it whose input had run out: none of its values
664 # still waiting, and no flush before it still to be called.
665 9 ▶ my $input_ran_out_at_stop = sub ($n) {
Subroutine __ANON__: called CC=5 SCAR=16.1
666 9 for my $stage ($next_flush .. $n) {
667 9 ▶ last if $waiting_upto->($stage);
Branch: 1 branch
BranchTrueFalseTotal
if &$waiting_upto($stage) 4 5 9
668 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
669 4 $input_done[$stage] = 1;
670 }
671 9 return;
672 340 };
673 # End the run if nothing in it can still act on a value. Called only
674 # once the source is done.
675 579 ▶ my $end_if_ended = sub () {
Subroutine __ANON__: called CC=4 SCAR=13.9
676 579 ▶ return if $finished || defined $flush_at->();
Branch: 1 branch
BranchTrueFalseTotal
if $finished or defined &$flush_at() 258 321 579
Condition100%3/3
$finished or defined &$flush_at()
l!l&&r!l&&!r
24513321
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
677 321 ▶ return if $waiting_upto->($#stages);
Branch: 1 branch
BranchTrueFalseTotal
if &$waiting_upto($#stages) 24 297 321
678 297 $input_ran_out->($#stages);
679 297 $finish->(0);
680 288 return;
681 340 };
682 # Where all collects every value still to come, while it does: the
683 # loop pushes them onto it, with no return between values. Otherwise,
684 # the loop returns the next value.
685 340 my $into;
686 486 ▶ my $next = sub () {
Subroutine __ANON__: called CC=40 SCAR=36.9
687 486 ▶ return if $outside;
Branch: 1 branch
BranchTrueFalseTotal
if $outside 1 485 486
688 485 while (1) {
689 1981 ▶ if ($into) {
Branch: 1 branch
BranchTrueFalseTotal
if ($into) 1491 490 1981
690 # All takes at once the values already at the end, such as a
691 # list sink's result. A run with no stages takes the shorter
692 # way, so this one has a stage.
693 1491 ▶ if (@todo && $todo[-1][0] == @stages) {
Branch: 1 branch
BranchTrueFalseTotal
if (@todo and $todo[-1][0] == @stages) 39 1452 1491
Condition100%3/3
@todo and $todo[-1][0] == @stages
!ll&&!rl&&r
13777539
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
694 39 ▶ while (@todo && $todo[-1][0] == @stages) {
Branch: 1 branch
BranchTrueFalseTotal
if (@todo and $todo[-1][0] == @stages) 46 39 85
Condition100%3/3
@todo and $todo[-1][0] == @stages
!ll&&!rl&&r
38146
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
695 46 push @$into, (pop @todo)->[1];
696 46 $waiting_at[@stages]--;
697 }
698 39 next;
699 }
700 }
701 1942 my($n, $value, $from_flush, $flush);
702 1942 ▶ if (@todo) {
Branch: 3 branches
BranchTrueFalseTotal
if (@todo) { } 129 1813 1942
elsif (@from_source) { } 552 1261 1813
elsif (not $done) { } 707 554 1261
703 129 ($n, $value, $from_flush) = @{ pop @todo };
704 129 $waiting_at[$n]--;
705 }
706 elsif (@from_source) {
707 552 ($n, $value) = (0, shift @from_source);
708 }
709 elsif (!$done) {
710 # Straight onto @from_source, which is empty here.
711 707 try {
712 707 @from_source = $pull->();
713 }
714 catch ($e) {
715 24 ▶ if ($e isa 'Ekorn::Pipeline::Stop') {
Branch: 1 branch
BranchTrueFalseTotal
if ($e isa "Ekorn::Pipeline::Stop") 15 9 24
716 # A source that stops has had enough: what it
717 # carries is its last, and it's done.
718 15 ▶ my(undef, @carried) = is_arrayref($e) ? @$e : ();
Branch: 1 branch
BranchTrueFalseTotal
Ref::Util::XS::is_arrayref($e) ? : 11 4 15
719 15 ($done, $source_done) = (1, 1);
720 15 @from_source = @carried;
721 15 next;
722 }
723 9 ($done, $next_flush, @todo, @waiting_at, @from_source)
724 = (1, scalar @stages);
725 9 $finish->(1);
726 9 ▶ croak $e if is_ref($e);
Branch: 1 branch
BranchTrueFalseTotal
if Ref::Util::XS::is_ref($e) 5 4 9
727 4 die $e; ## no critic (ErrorHandling::RequireCarping): thrown again, as it was
728 }
729 683 ▶ unless (@from_source) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@from_source) 236 447 683
730 236 ($done, $source_done) = (1, 1);
731 236 next;
732 }
733 447 ($n, $value) = (0, shift @from_source);
734 }
735 else {
736 554 $n = $flush_at->();
737 554 ▶ unless (defined $n) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $n) 286 268 554
738 286 $end_if_ended->();
739 284 last;
740 }
741 268 $input_ran_out->($n);
742 268 $flushed{$n} = 1;
743 268 ($next_flush, $flush, $from_flush) = ($n + 1, $flushes[$n], 1);
744 }
745 1396 ▶ if ($n == @stages) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n == @stages) 29 1367 1396
746 # Only while values are handed out one at a time: all takes
747 # them from the top of the loop. Nothing can end while the
748 # source is still being read.
749 29 ▶ $end_if_ended->() if $done;
Branch: 1 branch
BranchTrueFalseTotal
if $done 28 1 29
750 29 return $value;
751 }
752 # Set only when it changes, as most values come from the source,
753 # and most runs, and the code running them, have no context.
754 local $running{ending} = $from_flush
755 1367 ▶ if $from_flush || $running{ending};
Branch: 1 branch
BranchTrueFalseTotal
if $from_flush or $running{"ending"} 279 1088 1367
Condition66%2/3
$from_flush or $running{"ending"}
l!l&&r!l&&!r
27901088
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
756 local $running{context} = $context
757 1367 ▶ if defined $context || defined $running{context};
Branch: 1 branch
BranchTrueFalseTotal
if defined $context or defined $running{"context"} 6 1361 1367
Condition66%2/3
defined $context or defined $running{"context"}
l!l&&r!l&&!r
601361
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
758 1367 my $kept = 1;
759 1367 try {
760 1367 ▶ if ($flush) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flush) 268 1099 1367
761 268 local $_ = undef;
762 268 my @values = $flush->();
763 263 $n++;
764 263 $kept = @values;
765 263 $value = shift @values;
766 263 ▶ $wait->($n, 1, @values) if @values;
Branch: 1 branch
BranchTrueFalseTotal
if @values 46 217 263
767 }
768 1362 ▶ while ($kept && $n < @stages) {
Branch: 1 branch
BranchTrueFalseTotal
if ($kept and $n < @stages) 1564 532 2096
Condition100%3/3
$kept and $n < @stages
!ll&&!rl&&r
305021564
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
769 1564 local $_ = $value;
770 my @values
771 = $sinks[$n]
772 1564 ▶ ? do { $stages[$n]->($value); () }
Branch: 1 branch
BranchTrueFalseTotal
$sinks[$n] ? : 282 1282 1564
773 : $stages[$n]->($value);
774 1487 ▶ last if $outside;
Branch: 1 branch
BranchTrueFalseTotal
if $outside 5 1482 1487
775 1482 $n++;
776 1482 ▶ unless (@values) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@values) 748 734 1482
777 748 $kept = 0;
778 748 last;
779 }
780 734 $value = shift @values;
781 734 ▶ $wait->($n, $from_flush, @values) if @values;
Branch: 1 branch
BranchTrueFalseTotal
if @values 73 661 734
782 }
783 }
784 catch ($e) {
785 82 ▶ if ($e isa 'Ekorn::Pipeline::Stop') {
Branch: 1 branch
BranchTrueFalseTotal
if ($e isa "Ekorn::Pipeline::Stop") 61 21 82
786 # A stop made by throw is an array; one made otherwise
787 # carries nothing. A sink that stops the run still
788 # gives its result.
789 61 ▶ my(undef, @values) = is_arrayref($e) ? @$e : ();
Branch: 1 branch
BranchTrueFalseTotal
Ref::Util::XS::is_arrayref($e) ? : 56 5 61
790 61 ▶ $input_ran_out_at_stop->($n) if $done;
Branch: 1 branch
BranchTrueFalseTotal
if $done 9 52 61
791 61 $done = 1;
792 61 ▶ my $from = $sinks[$n] && !$flushed{$n} ? $n : $n + 1;
Branch: 1 branch
BranchTrueFalseTotal
$sinks[$n] && !$flushed{$n} ? : 15 46 61
Condition100%3/3
$sinks[$n] and not $flushed{$n}
!ll&&!rl&&r
45115
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
793 61 ▶ $next_flush = $from if $next_flush < $from;
Branch: 1 branch
BranchTrueFalseTotal
if $next_flush < $from 45 16 61
794 61 (@todo, @waiting_at, @from_source) = ();
795 61 $wait->($n + 1, $from_flush, @values);
796 61 next;
797 }
798 # A stage or a flush that dies ends the run: nothing waiting
799 # goes on, and every end is told it died.
800 21 ($done, $next_flush, @todo, @waiting_at, @from_source)
801 = (1, scalar @stages);
802 21 $finish->(1);
803 21 ▶ croak $e if is_ref($e);
Branch: 1 branch
BranchTrueFalseTotal
if Ref::Util::XS::is_ref($e) 6 15 21
804 15 my $at = Carp::shortmess() =~ s( \.? \n \z )()xr;
805 15 my $message = $e =~ s( \n \z )()xr =~ s( ^ )( )xmgr;
806 15 my $stage = _run_label($pipeline, $run, $n);
807 15 die "Pipeline $stage failed$at:\n$message\n";
808 }
809 1285 ▶ return if $outside;
Branch: 1 branch
BranchTrueFalseTotal
if $outside 5 1280 1285
810 1280 ▶ if ($kept) {
Branch: 1 branch
BranchTrueFalseTotal
if ($kept) 502 778 1280
811 502 ▶ $end_if_ended->() if $done;
Branch: 1 branch
BranchTrueFalseTotal
if $done 265 237 502
812 495 ▶ return $value unless $into;
Branch: 1 branch
BranchTrueFalseTotal
unless $into 128 367 495
813 367 push @$into, $value;
814 }
815 }
816 # Everything has been handed out: the iterator is spent.
817 284 $spent = 1;
818 284 return;
819 340 };
820 # Every value the run has still to give, as $next would give them one
821 # at a time, but in one call of it.
822 240 ▶ my $all = sub () {
Subroutine __ANON__: called CC=1 SCAR=0.0
823 240 my @all;
824 240 $into = \@all;
825 240 try {
826 240 $next->();
827 }
828 catch ($e) {
829 22 $into = undef;
830 22 die $e; ## no critic (ErrorHandling::RequireCarping): thrown again, as it was
831 }
832 218 $into = undef;
833 218 return @all;
834 340 };
835 # Ended from outside, by the iterator's finish or as it's let go, the
836 # run stops: nothing more is pulled, and nothing waiting goes on.
837 359 ▶ my $end_run = sub () {
Subroutine __ANON__: called CC=4 SCAR=13.9
838 359 $outside = 1;
839 # A run that died has nothing waiting: its values were dropped then.
840 359 ▶ $dropped = 1 if @todo || @from_source;
Branch: 1 branch
BranchTrueFalseTotal
if @todo or @from_source 13 346 359
Condition100%3/3
@todo or @from_source
l!l&&r!l&&!r
58346
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
841 359 ($done, $next_flush, @todo, @waiting_at, @from_source)
842 = (1, scalar @stages);
843 359 $finish->(0);
844 359 ▶ $ended = 'stopped' if $dropped;
Branch: 1 branch
BranchTrueFalseTotal
if $dropped 26 333 359
845 359 return;
846 340 };
847 # How the run ended, held back until the iterator has nothing more to
848 # give, so it's said once, and doesn't change. It's the iterator's
849 # spent too: the run has ended, and so is said to, once it has.
850 81 ▶ my $ended_once = sub () {
Subroutine __ANON__: called CC=5 SCAR=16.1
851 81 ▶ return $ended if $spent || $outside || ($ended // '') eq 'died';
Branch: 1 branch
BranchTrueFalseTotal
if $spent or $outside or ($ended // "") eq "died" 75 6 81
Condition100%8/8
$spent or $outside or ($ended // "") eq "died"
l!l&&r!l&&!r
59166
$spent or $outside
l!l&&r!l&&!r
461322
$ended // ""
l!l
193
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
852 6 return;
853 340 };
854 340 return Ekorn::Pipeline::Iterator->new(
855 $next, $end_run, $context, $ended_once, $ended_once, $all,
856 );
857 }
858  
859  
860 # Return how stage $n of a run made into %$run, of $pipeline, is named in
861 # a message, as _label names it.
862 157 ▶sub _run_label ($pipeline, $run, $n) {
Subroutine _run_label: called CC=1 SCAR=0.0
863 157 return _label($pipeline, $run->{names}[$n], $run->{within}[$n]);
864 }
865  
866  
867 # Return an iterator, as _iterator does, for a run made into %$run with
868 # no flush, no end of a stage's, and no sink, so with none of their
869 # bookkeeping: each value through every step before the next, what a
870 # step returns going on in order, and a stop's values going on from the
871 # step after it. The run ends, as _iterator's does, once the source is
872 # done and no value waits before the last step, and then the source's
873 # end, its only one, is called.
874 203 ▶sub _plain_iterator ($pipeline, $run, $pull, $source_end, $context) {
Subroutine _plain_iterator: called CC=1 SCAR=0.0
875 203 my @steps = @{ $run->{stages} };
876 # Each value waiting to go through a step, as the step's index and the
877 # value, the next on top, so in order of index from the bottom: the
878 # lowest is at the bottom. Values a source's step gave, still to go
879 # through the steps from the first, are held as they came.
880 203 my(@todo, @from_source);
881 203 my($done, $source_done, $finished, $outside, $ended, $spent, $dropped, $into);
882 435 ▶ my $finish = sub ($died) {
Subroutine __ANON__: called CC=6 SCAR=17.9
883 435 ▶ return if $finished++;
Branch: 1 branch
BranchTrueFalseTotal
if $finished++ 232 203 435
884 # Values dropped from outside make it stopped, as end_run says.
885 203 ▶ $ended = $died ? 'died' : $source_done ? 'done' : 'stopped';
Branch: 2 branches
BranchTrueFalseTotal
$died ? : 26 177 203
$source_done ? : 122 55 177
886 203 ▶ return unless $source_end;
Branch: 1 branch
BranchTrueFalseTotal
unless $source_end 82 121 203
887 121 local $running{context} = $context;
888 121 try {
889 121 ▶ _end_all(
Branch: 1 branch
BranchTrueFalseTotal
$source_done ? : 80 41 121
890 [['the source', $source_end, $source_done ? 'done' : 'stopped']],
891 $died,
892 );
893 }
894 catch ($e) {
895 # The end died: the run died, and hands out nothing more.
896 3 ($ended, $spent, @todo, @from_source) = ('died', 1);
897 3 die $e; ## no critic (ErrorHandling::RequireCarping): thrown again, as it was
898 }
899 118 return;
900 203 };
901 355 ▶ my $next = sub () {
Subroutine __ANON__: called CC=36 SCAR=35.8
902 355 ▶ return if $outside;
Branch: 1 branch
BranchTrueFalseTotal
if $outside 3 352 355
903 352 while (1) {
904 714 ▶ if ($into) {
Branch: 1 branch
BranchTrueFalseTotal
if ($into) 395 319 714
905 # All takes at once a step's values where there are no
906 # steps, and the values already at the end.
907 395 ▶ if (!@steps && @from_source) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @steps and @from_source) 29 366 395
Condition100%3/3
not @steps and @from_source
!ll&&!rl&&r
12024629
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
908 29 push @$into, splice @from_source;
909 29 next;
910 }
911 366 ▶ if (@todo && $todo[-1][0] == @steps) {
Branch: 1 branch
BranchTrueFalseTotal
if (@todo and $todo[-1][0] == @steps) 18 348 366
Condition100%3/3
@todo and $todo[-1][0] == @steps
!ll&&!rl&&r
341718
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
912 18 ▶ push @$into, (pop @todo)->[1]
Branch: 1 branch
BranchTrueFalseTotal
@todo and $todo[-1][0] == @steps and do { push @$into, pop(@todo)->[1] } 19 18 37
Condition100%3/3
@todo and $todo[-1][0] == @steps
!ll&&!rl&&r
17119
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
913 while @todo && $todo[-1][0] == @steps;
914 18 next;
915 }
916 }
917 667 my($n, $value);
918 667 ▶ if (@todo) {
Branch: 3 branches
BranchTrueFalseTotal
if (@todo) { } 32 635 667
elsif (@from_source) { } 28 607 635
elsif (not $done) { } 446 161 607
919 32 ($n, $value) = @{ pop @todo };
920 }
921 elsif (@from_source) {
922 28 ($n, $value) = (0, shift @from_source);
923 }
924 elsif (!$done) {
925 446 try {
926 446 @from_source = $pull->();
927 }
928 catch ($e) {
929 24 ▶ if ($e isa 'Ekorn::Pipeline::Stop') {
Branch: 1 branch
BranchTrueFalseTotal
if ($e isa "Ekorn::Pipeline::Stop") 11 13 24
930 11 ▶ my(undef, @carried) = is_arrayref($e) ? @$e : ();
Branch: 1 branch
BranchTrueFalseTotal
Ref::Util::XS::is_arrayref($e) ? : 8 3 11
931 11 ($done, $source_done) = (1, 1);
932 11 @from_source = @carried;
933 11 next;
934 }
935 13 ($done, @todo, @from_source) = (1);
936 13 $finish->(1);
937 13 ▶ croak $e if is_ref($e);
Branch: 1 branch
BranchTrueFalseTotal
if Ref::Util::XS::is_ref($e) 2 11 13
938 11 die $e; ## no critic (ErrorHandling::RequireCarping): thrown again, as it was
939 }
940 422 ▶ unless (@from_source) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@from_source) 111 311 422
941 111 ($done, $source_done) = (1, 1);
942 111 next;
943 }
944 311 ($n, $value) = (0, shift @from_source);
945 }
946 else {
947 # Nothing waits, and the source is done.
948 161 $finish->(0);
949 158 last;
950 }
951 371 ▶ if ($n < @steps) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < @steps) 195 176 371
952 195 ▶ local $running{ending} = undef if $running{ending};
Branch: 1 branch
BranchTrueFalseTotal
if $running{"ending"} 1 194 195
953 local $running{context} = $context
954 195 ▶ if defined $context || defined $running{context};
Branch: 1 branch
BranchTrueFalseTotal
if defined $context or defined $running{"context"} 15 180 195
Condition66%2/3
defined $context or defined $running{"context"}
l!l&&r!l&&!r
150180
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
955 195 my $kept = 1;
956 195 try {
957 195 ▶ while ($n < @steps) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < @steps) 238 139 377
958 238 local $_ = $value;
959 238 my @values = $steps[$n]->($value);
960 192 ▶ last if $outside;
Branch: 1 branch
BranchTrueFalseTotal
if $outside 2 190 192
961 190 $n++;
962 190 ▶ unless (@values) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@values) 8 182 190
963 8 $kept = 0;
964 8 last;
965 }
966 182 $value = shift @values;
967 182 ▶ push @todo, map { [$n, $_] } reverse @values if @values;
Branch: 1 branch
BranchTrueFalseTotal
if @values 21 161 182
968 }
969 }
970 catch ($e) {
971 46 ▶ if ($e isa 'Ekorn::Pipeline::Stop') {
Branch: 1 branch
BranchTrueFalseTotal
if ($e isa "Ekorn::Pipeline::Stop") 33 13 46
972 33 ▶ my(undef, @carried) = is_arrayref($e) ? @$e : ();
Branch: 1 branch
BranchTrueFalseTotal
Ref::Util::XS::is_arrayref($e) ? : 31 2 33
973 33 $done = 1;
974 33 @from_source = ();
975 33 @todo = map { [$n + 1, $_] } reverse @carried;
976 33 next;
977 }
978 13 ($done, @todo, @from_source) = (1);
979 13 $finish->(1);
980 13 ▶ croak $e if is_ref($e);
Branch: 1 branch
BranchTrueFalseTotal
if Ref::Util::XS::is_ref($e) 2 11 13
981 11 my $at = Carp::shortmess() =~ s( \.? \n \z )()xr;
982 11 my $message = $e =~ s( \n \z )()xr =~ s( ^ )( )xmgr;
983 11 my $stage = _run_label($pipeline, $run, $n);
984 11 die "Pipeline $stage failed$at:\n$message\n";
985 }
986 149 ▶ return if $outside;
Branch: 1 branch
BranchTrueFalseTotal
if $outside 2 147 149
987 147 ▶ next unless $kept;
Branch: 1 branch
BranchTrueFalseTotal
unless $kept 8 139 147
988 }
989 # The run ends once nothing waits before the last step: values
990 # from the source wait before the first, if there is one.
991 315 ▶ $finish->(0)
Branch: 1 branch
BranchTrueFalseTotal
if $done and not $finished and not @from_source && @steps and not @todo && $todo[0][0] < @steps 20 295 315
Condition100%15/15
$done and not $finished and not @from_source && @steps and not @todo && $todo[0][0] < @steps
!ll&&!rl&&r
292320
$done and not $finished and not @from_source && @steps
!ll&&!rl&&r
288423
$done and not $finished
!ll&&!rl&&r
284427
@from_source && @steps
!ll&&!rl&&r
2034
@todo && $todo[0][0] < @steps
!ll&&!rl&&r
1913
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
992 if $done
993 && !$finished
994 && !(@from_source && @steps)
995 && !(@todo && $todo[0][0] < @steps);
996 315 ▶ return $value unless $into;
Branch: 1 branch
BranchTrueFalseTotal
unless $into 163 152 315
997 152 push @$into, $value;
998 }
999 # Everything has been handed out: the iterator is spent.
1000 158 $spent = 1;
1001 158 return;
1002 203 };
1003 104 ▶ my $all = sub () {
Subroutine __ANON__: called CC=1 SCAR=0.0
1004 104 my @all;
1005 104 $into = \@all;
1006 104 try {
1007 104 $next->();
1008 }
1009 catch ($e) {
1010 12 $into = undef;
1011 12 die $e; ## no critic (ErrorHandling::RequireCarping): thrown again, as it was
1012 }
1013 92 $into = undef;
1014 92 return @all;
1015 203 };
1016 228 ▶ my $end_run = sub () {
Subroutine __ANON__: called CC=4 SCAR=13.9
1017 228 $outside = 1;
1018 228 ▶ $dropped = 1 if @todo || @from_source;
Branch: 1 branch
BranchTrueFalseTotal
if @todo or @from_source 8 220 228
Condition100%3/3
@todo or @from_source
l!l&&r!l&&!r
17220
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
1019 228 ($done, @todo, @from_source) = (1);
1020 228 $finish->(0);
1021 228 ▶ $ended = 'stopped' if $dropped;
Branch: 1 branch
BranchTrueFalseTotal
if $dropped 14 214 228
1022 228 return;
1023 203 };
1024 60 ▶ my $ended_once = sub () {
Subroutine __ANON__: called CC=5 SCAR=16.1
1025 60 ▶ return $ended if $spent || $outside || ($ended // '') eq 'died';
Branch: 1 branch
BranchTrueFalseTotal
if $spent or $outside or ($ended // "") eq "died" 53 7 60
Condition100%8/8
$spent or $outside or ($ended // "") eq "died"
l!l&&r!l&&!r
43107
$spent or $outside
l!l&&r!l&&!r
331017
$ended // ""
l!l
107
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
1026 7 return;
1027 203 };
1028 203 return Ekorn::Pipeline::Iterator->new(
1029 $next, $end_run, $context, $ended_once, $ended_once, $all,
1030 );
1031 }
1032 25 1;
1033 __END__
↑ Summary Pipeline/Sugar.pm »