Pipeline.pm

filter: Statementstmt 100% 626 / 626 Branchbran 100% 348 / 348 Conditioncond 96.7% 179 / 185 MC/DCmcdc 94.3% 101 / 107 Subroutinesub 100% 77 / 77 Podpod 100% 13 / 13 totaltotal 99.1% 1344 / 1356 CC 248 SCAR 28.4
CC
248
Cov
99%
__ANON__
48.0
__ANON__
39.0
_make_run
25.0
CRAP 17.1
↑ Summary Pipeline/Sugar.pm »
1 31 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package Ekorn::Pipeline;
4  
5 31 ▶use experimental qw(builtin try);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 31 ▶use builtin qw(blessed refaddr weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 31 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 31 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 31 ▶use Ref::Util qw(is_arrayref is_coderef is_plain_hashref is_ref);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 31 ▶use parent 'Ekorn::Pipeline::Element';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 31 ▶use Ekorn::Pipeline::Errors qw(croakf);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 31 ▶use Ekorn::Pipeline::Feed qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 31 ▶use Ekorn::Pipeline::Iterator qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 31 ▶use Ekorn::Pipeline::Stage qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 31 ▶use namespace::clean -except => 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 31 our $VERSION = 'v0.3.15';
22  
23 31 our @EXPORT_OK = qw(is_sink is_stage);
24  
25 # Croak at the line that called Ekorn::Pipeline.
26 31 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 3371 ▶sub is_stage ($thing) {
Subroutine is_stage: called CC=3 SCAR=11.0
Pod documented
32 3371 ▶ 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
12781969124
Ref::Util::XS::is_coderef($thing) or $thing isa "Ekorn::Pipeline"
l!l&&r!l&&!r
10302482093
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 1733 ▶sub is_sink ($thing) {
Subroutine is_sink: called CC=5 SCAR=16.1
Pod documented
42 1733 ▶ ($thing) = ($thing->stages)[-1] if $thing isa 'Ekorn::Pipeline';
Branch: 1 branch
BranchTrueFalseTotal
if $thing isa "Ekorn::Pipeline" 4 1729 1733
43 1733 ▶ return !!0 unless blessed($thing);
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($thing) 11 1722 1733
44 1722 ▶ return 'one' if $thing->can('EKORN_SINK_ONE');
Branch: 1 branch
BranchTrueFalseTotal
if $thing->can("EKORN_SINK_ONE") 169 1553 1722
45 1553 ▶ return 'many' if $thing->can('EKORN_SINK_MANY');
Branch: 1 branch
BranchTrueFalseTotal
if $thing->can("EKORN_SINK_MANY") 118 1435 1553
46 1435 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 5573 ▶sub _stage_role ($thing) {
Subroutine _stage_role: called CC=2 SCAR=6.9
53 5573 ▶ return unless blessed($thing);
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($thing) 1079 4494 5573
54 my @roles
55 4494 = grep { $thing->can($_) } qw(EKORN_STAGE EKORN_SINK_ONE EKORN_SINK_MANY);
56 4494 return $roles[0];
57 }
58  
59  
60 # Return true if what's given offers a source's role method.
61 1606 ▶sub _is_source ($thing) {
Subroutine _is_source: called CC=2 SCAR=6.9
62 1606 ▶ return !!(blessed($thing) && $thing->can('EKORN_SOURCE'));
Condition100%3/3
builtin::blessed($thing) && $thing->can('EKORN_SOURCE')
!ll&&!rl&&r
962241403
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 3117 ▶sub _element ($stage) {
Subroutine _element: called CC=3 SCAR=11.0
69 3117 ▶ 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) 2158 959 3117
Condition100%3/3
$stage isa "Ekorn::Pipeline::Element" or _stage_role($stage)
l!l&&r!l&&!r
21508959
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 959 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 3118 ▶sub _check_stage ($stage, $n) {
Subroutine _check_stage: called CC=6 SCAR=17.9
78 my @roles
79 = blessed($stage)
80 3118 ▶ ? grep { $stage->can($_) } qw(EKORN_STAGE EKORN_SINK_ONE EKORN_SINK_MANY)
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($stage) ? : 2157 961 3118
81 : ();
82 3118 ▶ croakf
Branch: 1 branch
BranchTrueFalseTotal
if @roles > 1 1 3117 3118
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 3117 ▶ croakf 'Expected stage #%d to be a stage, not a source', $n
Branch: 1 branch
BranchTrueFalseTotal
if not @roles and _is_source($stage) 1 3116 3117
Condition100%3/3
not @roles and _is_source($stage)
!ll&&!rl&&r
191612001
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 3116 ▶ croakf
Branch: 1 branch
BranchTrueFalseTotal
unless is_stage($stage) 7 3109 3116
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 3109 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 1891 ▶sub new ($class, @given) {
Subroutine new: called CC=6 SCAR=17.9
Pod documented
103 1891 my(@stages, @names, @within);
104 1891 for my $n (1 .. @given) {
105 3118 my $stage = $given[$n - 1];
106 3118 _check_stage($stage, $n);
107 3109 ▶ if ($stage isa 'Ekorn::Pipeline') {
Branch: 1 branch
BranchTrueFalseTotal
if ($stage isa "Ekorn::Pipeline") 239 2870 3109
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 2870 push @stages, _element($stage);
121 2870 push @names, $n;
122 2870 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 1879 ▶ my $sink = @stages ? is_sink($stages[-1]) : !!0;
Branch: 1 branch
BranchTrueFalseTotal
@stages ? : 1667 212 1879
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 3110 = (grep { ref ne 'Ekorn::Pipeline::Stage' } @stages)
134 ? ()
135 1879 ▶ : map { $_->{call} } @stages;
Branch: 1 branch
BranchTrueFalseTotal
grep({ref $_ ne 'Ekorn::Pipeline::Stage';} @stages) ? : 1105 774 1879
136 # What names a step in a message, as _push_steps takes it.
137 1879 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 1879 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 678 ▶ call => sub (@values) {
Subroutine __ANON__: called CC=7 SCAR=19.5
149 678 ▶ return @values unless @stages;
Branch: 1 branch
BranchTrueFalseTotal
unless @stages 8 670 678
150 my @results
151 670 ▶ = @steps
Branch: 1 branch
BranchTrueFalseTotal
@steps ? : 260 410 670
152 ? _push_steps($self, \@steps, $labels, undef, \@values)
153 : _pushed($self, \@stages, \@names, \@within, \@values);
154 547 ▶ return @results if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 434 113 547
155 113 ▶ return $results[0] if $sink && $sink eq 'one';
Branch: 1 branch
BranchTrueFalseTotal
if $sink and $sink eq "one" 55 58 113
Condition100%3/3
$sink and $sink eq "one"
!ll&&!rl&&r
51755
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 58 ▶ return \@results if $sink;
Branch: 1 branch
BranchTrueFalseTotal
if $sink 7 51 58
157 51 return scalar @results;
158 },
159 1879 }, $class;
160 1879 weaken($self = $pipeline);
161 1879 return $pipeline;
162 }
163  
164 # The named pipelines, each by its name qualified by its package.
165 31 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 1053 ▶sub name ($self) {
Subroutine name: called CC=1 SCAR=0.0
Pod documented
242 1053 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 588 ▶sub iterator ($self, $source) {
Subroutine iterator: called CC=3 SCAR=11.0
Pod documented
269 588 ▶ 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 585 588
Condition100%3/3
Ref::Util::XS::is_coderef($source) or _is_source($source)
l!l&&r!l&&!r
1834023
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 585 $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 428 ▶sub feed ($self) {
Subroutine feed: called CC=3 SCAR=11.0
Pod documented
285 427 my($steps, $labels) = @$self{qw(steps labels)};
286 # Stages that aren't all plain steps are made for the run.
287 427 ▶ unless (@$steps) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$steps) 347 80 427
288 347 my %run = (
289 stages => [], names => [], within => [], flushes => [], ends => [],
290 sinks => [],
291 );
292 347 try {
293 347 _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 346 ▶ if grep { defined } @{ $run{flushes} }, @{ $run{ends} };
Branch: 1 branch
BranchTrueFalseTotal
if grep {defined $_;} @{$run{"flushes"};}, @{$run{"ends"};} 315 31 346
300 31 ($steps, $labels) = ($run{stages}, [@run{qw(names within)}]);
301 }
302 # How the feed's run ended, undef while it's open, and how many
303 # results its pushes have made.
304 111 my @fed = (undef, 0);
305 return Ekorn::Pipeline::Feed->new(
306 # Without a signature, as a feed may be pushed a value at a time:
307 # its values go on as they are, not copied.
308 180 ▶ push => sub { _push_steps($self, $steps, $labels, \@fed, \@_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
309 70 ▶ close => sub () { $fed[0] = 'done'; return },
Subroutine __ANON__: called CC=1 SCAR=0.0
310 1 ▶ finish => sub () { $fed[0] = 'stopped'; return },
Subroutine __ANON__: called CC=1 SCAR=0.0
311 556 ▶ ended => sub () { return $fed[0] },
Subroutine __ANON__: called CC=1 SCAR=0.0
312 # Nothing to flush or end, for a stage driving the feed.
313 111 plain => 1,
314 );
315 }
316  
317  
318 # Return a feed of a run made into %$run with a flush, an end or a sink:
319 # an iterator of it, whose source has nothing until a push, and is done
320 # once the feed is closed; its stages see the context of the code
321 # pushing.
322 315 ▶sub _full_feed ($pipeline, $run) {
Subroutine _full_feed: called CC=1 SCAR=0.0
323 315 my(@pushed, $closed);
324 315 ▶ $run->{hungry} = sub () { !$closed };
Subroutine __ANON__: called CC=1 SCAR=0.0
325 my $iterator
326 315 ▶ = _full_iterator($pipeline, $run, sub { splice @pushed }, undef, undef);
Subroutine __ANON__: called CC=1 SCAR=0.0
327 # The iterator's own code for all, called directly, as a feed may be
328 # pushed a value at a time; the iterator is kept by the closures here.
329 315 my $all = $iterator->{all};
330 return Ekorn::Pipeline::Feed->new(
331 # Without a signature, as a feed may be pushed a value at a time.
332 534 ▶ push => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
333 534 push @pushed, @_;
334 534 return $all->();
335 },
336 257 ▶ close => sub () {
Subroutine __ANON__: called CC=1 SCAR=0.0
337 257 $closed = 1;
338 257 return $iterator->all;
339 },
340 3 ▶ finish => sub () { $iterator->finish; return },
Subroutine __ANON__: called CC=1 SCAR=0.0
341 1831 ▶ ended => sub () { return $iterator->ended },
Subroutine __ANON__: called CC=1 SCAR=0.0
342 # Whether there are ends to call, for a stage driving the feed.
343 315 ends => scalar grep { defined } @{ $run->{ends} },
344 );
345 }
346  
347  
348 # Return the stages, in order, as a list, to splice into another
349 # pipeline or give to any function taking stages.
350 271 ▶sub stages ($self) {
Subroutine stages: called CC=1 SCAR=0.0
Pod documented
351 271 return @{ $self->{stages} };
352 }
353  
354 # What a pipeline is doing while its stages run.
355 my %running;
356  
357 # The @result of the run running, for Ekorn::Pipeline::Context: how
358 # many results it has made, and the original of the value going through
359 # its stages; undef outside a run, and in an end. Each loop localises it
360 # once a call, not once a value. A package variable, as a local of one
361 # costs a call about 33 ns, against 95 for a key of %running, which
362 # has none until it's localised; a lexical can't be localised, and
363 # setting one and putting it back by hand, for each value, cost as much.
364 31 our $CURRENT_RESULT; ## no critic (Variables::ProhibitPackageVars)
365  
366 # For code telling its stages about the value they're given: true while
367 # a flush runs, and while the values it gave go through the stages,
368 # rather than values from the source.
369 19 ▶sub ending () {
Subroutine ending: called CC=1 SCAR=0.0
Pod documented
370 19 return $running{ending};
371 }
372  
373  
374 # For code telling its stages about the run: the context the run's
375 # source gave, if any, such as a statement's columns. A pushed run has
376 # none; an iterator used as a source passes on its run's.
377 61 ▶sub run_context () {
Subroutine run_context: called CC=1 SCAR=0.0
Pod documented
378 61 return $running{context};
379 }
380  
381  
382 # Ekorn::Pipeline::Context's functions, defined here, as they read
383 # $CURRENT_RESULT, which is this file's to set; that module exports and
384 # documents them. Each has an empty prototype, as well as an empty
385 # signature, so a bare call is a term: result_number + 1 adds, and
386 # result_number // 'none' isn't read as a pattern. Under use v5.36, the
387 # () alone is a signature, which leaves a sub without a prototype.
388 211 ▶sub Ekorn::Pipeline::Context::result_number : prototype() () {
Subroutine result_number: called CC=2 SCAR=6.9
Pod documented
389 211 ▶ return $CURRENT_RESULT ? $CURRENT_RESULT->[0] + 1 : undef;
Branch: 1 branch
BranchTrueFalseTotal
$Ekorn::Pipeline::CURRENT_RESULT ? : 205 6 211
390 }
391  
392  
393 # The original is the source's, if it gave an original part, directly
394 # or through an iterator read as a source, called only now (2026-0052);
395 # otherwise the run's own. A flush's values have none, either way.
396 247 ▶sub Ekorn::Pipeline::Context::result_original : prototype() () {
Subroutine result_original: called CC=5 SCAR=16.1
Pod documented
397 247 ▶ my($count, $original, $part) = $CURRENT_RESULT ? @$CURRENT_RESULT : ();
Branch: 1 branch
BranchTrueFalseTotal
$Ekorn::Pipeline::CURRENT_RESULT ? : 243 4 247
398 return
399 !defined $count || $running{ending} ? undef
400 247 ▶ : $part ? $part->()
Branch: 2 branches
BranchTrueFalseTotal
!defined($count) || $running{'ending'} ? : 21 226 247
$part ? : 34 192 226
Condition100%3/3
not defined $count or $running{"ending"}
l!l&&r!l&&!r
417226
MC/DC100%2/2
not defined $count or $running{"ending"}
not defined $count $running{"ending"}
Truth table100%3/3
not(A) or B
A= not defined $countB= $running{"ending"}
ABresult
1X1
011
000
401 : $original;
402 }
403  
404  
405 # Return the step, the end, the context and the original part of a run
406 # of $source: from its EKORN_SOURCE, if it offers one; a code reference
407 # is its own step, with no end, context or original.
408 585 ▶sub _source_run ($source) {
Subroutine _source_run: called CC=6 SCAR=17.9
409 # As _is_source asks, without the call, as every run asks it.
410 585 ▶ return ($source) unless blessed($source) && $source->can('EKORN_SOURCE');
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($source) and $source->can("EKORN_SOURCE") 183 402 585
Condition66%2/3
builtin::blessed($source) and $source->can("EKORN_SOURCE")
!ll&&!rl&&r
1830402
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
411 402 my($step, $end, $context, $original)
412 = _parts([qw(step end context original)], $source->EKORN_SOURCE);
413 392 ▶ croakf
Branch: 1 branch
BranchTrueFalseTotal
unless Ref::Util::XS::is_coderef($step) and _code_or_none($end) 4 388 392
Condition100%3/3
Ref::Util::XS::is_coderef($step) and _code_or_none($end)
!ll&&!rl&&r
31388
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
414 'Expected the source to return a code reference, and optionally an end and a context'
415 unless is_coderef($step) && _code_or_none($end);
416 # A message of its own, so the one above stays as it was.
417 388 ▶ croakf 'Expected the source\'s original to be a code reference'
Branch: 1 branch
BranchTrueFalseTotal
unless _code_or_none($original) 1 387 388
418 unless _code_or_none($original);
419 387 return ($step, $end, $context, $original);
420 }
421  
422  
423 # Return the parts a role method or a set-up returned as a list, in the
424 # order of the keys in @$order: as they are, if they're a list, or, if
425 # they're one plain hash reference, its values for those keys. Other
426 # keys are ignored.
427 2894 ▶sub _parts ($order, @returned) {
Subroutine _parts: called CC=3 SCAR=11.0
428 2894 ▶ 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]) 2239 655 2894
Condition100%3/3
@returned == 1 and Ref::Util::XS::is_plain_hashref($returned[0])
!ll&&!rl&&r
11531086655
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
429 655 return @{ $returned[0] }{@$order};
430 }
431  
432  
433 # Return true if each of what's given is a code reference, or undefined.
434 3262 ▶sub _code_or_none (@things) {
Subroutine _code_or_none: called CC=2 SCAR=6.9
435 3262 ▶ return !grep { defined && !is_coderef($_) } @things;
Condition100%3/3
defined $_ && !Ref::Util::XS::is_coderef($_)
!ll&&!rl&&r
311616117
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
436 }
437  
438  
439 # Return how a stage is named in a message: by its place, $name, and, if
440 # it's within a named pipeline, the innermost, by its name and its place
441 # in it, from the pipelines in @$within it came from, and $pipeline, the
442 # one being run.
443 1029 ▶sub _label ($pipeline, $name, $within) {
Subroutine _label: called CC=3 SCAR=11.0
444 1029 for my $in (reverse [$pipeline, $name], @$within) {
445 1048 my($outer, $place) = @$in;
446 1048 my $outer_name = $outer->name;
447 1048 ▶ return "stage #$name ($outer_name#$place)" if defined $outer_name;
Branch: 1 branch
BranchTrueFalseTotal
if defined $outer_name 14 1034 1048
448 }
449 1015 return "stage #$name";
450 }
451  
452  
453 # Return the results of pushing @values through @$steps, each a plain
454 # step, as a run of them would give them, without a run's machinery:
455 # each value through every step before the next, what a step returns
456 # going on in order, a stop's values going on from the step after it,
457 # with nothing else, and a step that dies named as a run names it, by
458 # $labels, the steps' names and the pipelines each came from, as a
459 # pair. The values are given as a reference to an array, not copied.
460 # For a feed, $fed, if given, is the feed's: its first is set to how the
461 # run ended, at a stop or a death, and its second holds how many results
462 # its pushes have made; and the steps see the context of the code
463 # pushing.
464 564 ▶sub _push_steps ($pipeline, $steps, $labels, $fed, $values) {
Subroutine _push_steps: called CC=15 SCAR=27.1
465 # A pushed run has no context, and gives no values from a flush.
466 564 ▶ local $running{context} = undef unless $fed;
Branch: 1 branch
BranchTrueFalseTotal
unless $fed 384 180 564
467 564 local $running{ending} = undef;
468 # The run's results so far, and the original of the value going
469 # through the steps, for Ekorn::Pipeline::Context; a value waiting on
470 # @todo carries its original.
471 564 ▶ my @result = ($fed ? $fed->[1] : 0, undef);
Branch: 1 branch
BranchTrueFalseTotal
$fed ? : 180 384 564
472 564 local $CURRENT_RESULT = \@result;
473 564 my(@results, @todo, $n);
474 564 my($at, $more) = (0, 1);
475 # One try for as many values as go through without a stop, which is
476 # caught, and the values it carries go on from the step after it.
477 564 ▶ while ($more) {
Branch: 1 branch
BranchTrueFalseTotal
if ($more) 681 487 1168
478 681 try {
479 VALUE:
480 681 while (1) {
481 2106 my $value;
482 2106 ▶ if (@todo) { ($n, $value, $result[1]) = @{ pop @todo } }
Branch: 2 branches
BranchTrueFalseTotal
if (@todo) { } 362 1744 2106
elsif ($at < @$values) { } 1257 487 1744
483 elsif ($at < @$values) {
484 1257 $n = 0;
485 1257 $result[1] = $value = $values->[$at++];
486 }
487 487 else { $more = 0; last VALUE }
488 1619 ▶ while ($n < @$steps) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < @$steps) 1905 1217 3122
489 1905 local $_ = $value;
490 1905 my @values = $steps->[$n]->($value);
491 1711 $n++;
492 1711 ▶ next VALUE unless @values;
Branch: 1 branch
BranchTrueFalseTotal
unless @values 208 1503 1711
493 1503 $value = shift @values;
494 1503 ▶ push @todo, map { [$n, $_, $result[1]] } reverse @values if @values;
Branch: 1 branch
BranchTrueFalseTotal
if @values 246 1257 1503
495 }
496 1217 push @results, $value;
497 1217 $result[0]++;
498 }
499 }
500 catch ($e) {
501 194 ▶ if ($e isa 'Ekorn::Pipeline::Stop') {
Branch: 1 branch
BranchTrueFalseTotal
if ($e isa "Ekorn::Pipeline::Stop") 117 77 194
502 117 ▶ my(undef, @carried) = is_arrayref($e) ? @$e : ();
Branch: 1 branch
BranchTrueFalseTotal
Ref::Util::XS::is_arrayref($e) ? : 115 2 117
503 117 ($at, @todo) = (scalar @$values);
504 117 push @todo, map { [$n + 1, $_, $result[1]] } reverse @carried;
505 117 ▶ $fed->[0] = 'stopped' if $fed;
Branch: 1 branch
BranchTrueFalseTotal
if $fed 15 102 117
506 117 next;
507 }
508 77 ▶ $fed->[0] = 'died' if $fed;
Branch: 1 branch
BranchTrueFalseTotal
if $fed 11 66 77
509 77 ▶ croak $e if is_ref($e);
Branch: 1 branch
BranchTrueFalseTotal
if Ref::Util::XS::is_ref($e) 2 75 77
510 75 my $where = Carp::shortmess() =~ s( \.? \n \z )()xr;
511 75 my $message = $e =~ s( \n \z )()xr =~ s( ^ )( )xmgr;
512 75 my($names, $within) = @$labels;
513 75 my $stage = _label($pipeline, $names->[$n], $within->[$n]);
514 75 die "Pipeline $stage failed$where:\n$message\n";
515 }
516 }
517 487 ▶ $fed->[1] = $result[0] if $fed;
Branch: 1 branch
BranchTrueFalseTotal
if $fed 169 318 487
518 487 return @results;
519 }
520  
521  
522 # Return the text of an end's error: a stop's own text, "Stop used
523 # outside a pipeline", would mislead, as the stop was in a run.
524 15 ▶sub _end_error ($error) {
Subroutine _end_error: called CC=2 SCAR=6.9
525 15 ▶ return 'Expected no stop in an end, as the run has ended'
Branch: 1 branch
BranchTrueFalseTotal
if $error isa "Ekorn::Pipeline::Stop" 2 13 15
526 if $error isa 'Ekorn::Pipeline::Stop';
527 13 return "$error";
528 }
529  
530  
531 # Call each end given, as [$label, $end, $how], in order. While the run
532 # is dying, an end that dies is reported as a warning naming it, and the
533 # rest are called. Otherwise, an end that dies tells every end after it
534 # "died", and its error is thrown once they've run.
535 1044 ▶sub _end_all ($ends, $dying) {
Subroutine _end_all: called CC=9 SCAR=22.0
536 # An end isn't a stage: it has no result.
537 1044 local $CURRENT_RESULT = undef;
538 1044 my($error, $failed);
539 1044 for my $call (@$ends) {
540 1155 my($label, $end, $how) = @$call;
541 1155 try {
542 1155 local $_ = undef;
543 1155 ▶ $end->($dying || defined $error ? 'died' : $how);
Branch: 1 branch
BranchTrueFalseTotal
$dying || defined $error ? : 124 1031 1155
Condition100%3/3
$dying or defined $error
l!l&&r!l&&!r
12131031
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
544 }
545 catch ($e) {
546 16 ▶ if ($dying || defined $error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dying or defined $error) { } 2 14 16
Condition100%3/3
$dying or defined $error
l!l&&r!l&&!r
1114
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
547 2 Carp::carp sprintf 'The end of %s died while the run was already dying: %s',
548 $label, _end_error($e) =~ s( \n \z )()xr;
549 }
550 else {
551 14 ($error, $failed) = ($e, $label);
552 }
553 }
554 }
555 1044 ▶ return unless defined $error;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $error 1030 14 1044
556 # A stop from an end is an error like any other: the run has ended.
557 14 ▶ 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 13 14
Condition100%3/3
Ref::Util::XS::is_ref($error) and not $error isa "Ekorn::Pipeline::Stop"
!ll&&!rl&&r
1121
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
558 13 my $at = Carp::shortmess() =~ s( \.? \n \z )()xr;
559 13 my $message = _end_error($error) =~ s( \n \z )()xr =~ s( ^ )( )xmgr;
560 13 die "Pipeline end of $failed failed$at:\n$message\n";
561 }
562  
563  
564 # Make the parts of a run from @$stages, named by @$names, into the
565 # lists in %$run: each part's step, its flush and its end, by its role
566 # method, and whether it's a sink. A gather's block may return a
567 # pipeline as its step, whose stages are made in its place, followed by
568 # the gather's flush and end, if any, on a stage of their own that
569 # passes values on.
570 1196 ▶sub _make_run ($stages, $names, $within, $run) {
Subroutine _make_run: called CC=25 SCAR=32.2
571 1196 for my $i (0 .. $#$stages) {
572 2513 my($stage, $name) = ($stages->[$i], $names->[$i]);
573 2513 ▶ my $role = _stage_role($stage) // '';
Condition100%2/2
_stage_role($stage) // ""
l!l
25121
MC/DC100%1/1
_stage_role($stage) // ""
_stage_role($stage)
Truth table100%2/2
A
A= _stage_role($stage)
Aresult
11
00
574 2513 my($step, $flush, $end);
575 2513 ▶ if ($stage isa 'Ekorn::Pipeline::Gather') {
Branch: 3 branches
BranchTrueFalseTotal
if ($stage isa 'Ekorn::Pipeline::Gather') { } 1556 957 2513
elsif ($role eq 'EKORN_SINK_ONE' or $role eq 'EKORN_SINK_MANY') { } 262 695 957
elsif ($role eq 'EKORN_STAGE') { } 694 1 695
576 1556 my @made = _parts([qw(step flush end)], $stage->EKORN_STAGE);
577 1550 ▶ my($inner, $inner_names, $inner_within)
Branch: 1 branch
BranchTrueFalseTotal
@made && $made[0] isa 'Ekorn::Pipeline' ? : 20 1530 1550
Condition100%3/3
@made and $made[0] isa "Ekorn::Pipeline"
!ll&&!rl&&r
1152920
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
578 = @made && $made[0] isa 'Ekorn::Pipeline' ? _composed($made[0]) : ();
579 1550 ▶ 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 1538 1550
$inner ? : 19 1528 1547
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
1021538
@made == 1 || @made == 2 || @made == 3 and $inner ? $inner_names : Ref::Util::XS::is_coderef($made[0])
!ll&&!rl&&r
371540
@made == 1 or @made == 2 or @made == 3
l!l&&r!l&&!r
45610913
@made == 1 or @made == 2
l!l&&r!l&&!r
3131431094
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
580 'Expected stage #%s, made with gather, to return a code reference or a pipeline, and optionally a flush and an end',
581 $name
582 unless (@made == 1 || @made == 2 || @made == 3)
583 && ($inner ? $inner_names : is_coderef($made[0]))
584 && _code_or_none(@made[1 .. $#made]);
585 1538 ($step, $flush, $end) = @made;
586 1538 ▶ if ($inner) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inner) 17 1521 1538
587 _make_run(
588 $inner,
589 25 [map { "$name.$_" } @$inner_names],
590 [
591 17 map { [
592 25 @{ $within->[$i] }, [$made[0], $inner_names->[$_]],
593 25 @{ $inner_within->[$_] },
594 ] } 0 .. $#$inner
595 ],
596 $run,
597 );
598 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
599 5 ▶ $step = sub { $_[0] }; # for each value: no signature
Subroutine __ANON__: called CC=1 SCAR=0.0
600 }
601 }
602 ▶ 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
155107695
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
603 262 ($step, $flush, $end) = _parts([qw(step flush end)], $stage->$role);
604 258 ▶ 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 253 258
Condition100%6/6
Ref::Util::XS::is_coderef($step) and Ref::Util::XS::is_coderef($flush) and _code_or_none($end)
!ll&&!rl&&r
41253
Ref::Util::XS::is_coderef($step) and Ref::Util::XS::is_coderef($flush)
!ll&&!rl&&r
13254
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
605 'Expected sink #%s to return a code reference, a flush, and optionally an end',
606 $name
607 unless is_coderef($step)
608 && is_coderef($flush)
609 && _code_or_none($end);
610 # What a sink's step returns is ignored, by the run, which knows
611 # it's a sink; a scalar sink's flush gives one value, undef if
612 # it has none.
613 253 my $result = $flush;
614 138 ▶ $flush = sub () { my @result = $result->(); return $result[0] }
Subroutine __ANON__: called CC=1 SCAR=0.0
615 253 ▶ if $role eq 'EKORN_SINK_ONE';
Branch: 1 branch
BranchTrueFalseTotal
if $role eq "EKORN_SINK_ONE" 146 107 253
616 }
617 elsif ($role eq 'EKORN_STAGE') {
618 694 ($step, $flush, $end) = _parts([qw(step flush end)], $stage->EKORN_STAGE);
619 694 ▶ croakf
Branch: 1 branch
BranchTrueFalseTotal
unless Ref::Util::XS::is_coderef($step) and _code_or_none($flush, $end) 5 689 694
Condition100%3/3
Ref::Util::XS::is_coderef($step) and _code_or_none($flush, $end)
!ll&&!rl&&r
32689
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
620 'Expected stage #%s to return a code reference, and optionally a flush and an end',
621 $name
622 unless is_coderef($step) && _code_or_none($flush, $end);
623 }
624 else {
625 1 $step = $stage;
626 }
627 2469 push @{ $run->{stages} }, $step;
628 2469 push @{ $run->{names} }, $name;
629 2469 push @{ $run->{within} }, $within->[$i];
630 2469 push @{ $run->{flushes} }, $flush;
631 2469 push @{ $run->{ends} }, $end;
632 2469 ▶ push @{ $run->{sinks} },
Condition100%3/3
$role eq 'EKORN_SINK_ONE' || $role eq 'EKORN_SINK_MANY'
l!l&&r!l&&!r
1461072216
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
633 $role eq 'EKORN_SINK_ONE' || $role eq 'EKORN_SINK_MANY';
634 }
635 1164 return;
636 }
637  
638  
639 # Return an iterator handing out, one per call, the values from $source
640 # run through the stages in @$stages, named in messages by @$names.
641 585 ▶sub _iterator ($pipeline, $stages, $names, $within, $source) {
Subroutine _iterator: called CC=3 SCAR=11.0
642 585 my %run = (
643 stages => [], names => [], within => [], flushes => [], ends => [],
644 sinks => [],
645 );
646 585 my($pull, $source_end, $context);
647 585 try {
648 # The source's original part, if any, kept in the run.
649 585 ($pull, $source_end, $context, $run{original}) = _source_run($source);
650 570 ▶ _make_run($stages, $names, $within, \%run) if @$stages;
Branch: 1 branch
BranchTrueFalseTotal
if @$stages 422 148 570
651 }
652 catch ($e) {
653 17 _set_up_died($pipeline, \%run, $source_end, $e);
654 }
655 # A run with no flush, no end of a stage's, and no sink, as most are,
656 # takes the shorter way.
657 return _plain_iterator($pipeline, \%run, $pull, $source_end, $context)
658 568 ▶ unless grep { defined } @{ $run{flushes} }, @{ $run{ends} };
Branch: 1 branch
BranchTrueFalseTotal
unless grep {defined $_;} @{$run{"flushes"};}, @{$run{"ends"};} 309 259 568
659 259 return _full_iterator($pipeline, \%run, $pull, $source_end, $context);
660 }
661  
662  
663 # Return the results of pushing @$values through @$stages, as a run of
664 # them gives them: once the stages are made for the run, by the plain
665 # loop if none has a flush or an end, as most haven't, so a stage made
666 # with gather costs no more a value than a plain one; otherwise by an
667 # iterator of the run. Its set-up is _iterator's, with no source to set
668 # up.
669 410 ▶sub _pushed ($pipeline, $stages, $names, $within, $values) {
Subroutine _pushed: called CC=2 SCAR=6.9
670 410 my %run = (
671 stages => [], names => [], within => [], flushes => [], ends => [],
672 sinks => [],
673 );
674 410 try {
675 410 _make_run($stages, $names, $within, \%run);
676 }
677 catch ($e) {
678 29 _set_up_died($pipeline, \%run, undef, $e);
679 }
680 return _push_steps(
681 $pipeline, $run{stages}, [@run{qw(names within)}], undef,
682 $values,
683 381 ▶ ) unless grep { defined } @{ $run{flushes} }, @{ $run{ends} };
Branch: 1 branch
BranchTrueFalseTotal
unless grep {defined $_;} @{$run{"flushes"};}, @{$run{"ends"};} 124 257 381
684 434 ▶ return _full_iterator($pipeline, \%run, sub { splice @$values }, undef, undef)
Subroutine __ANON__: called CC=1 SCAR=0.0
685 257 ->all;
686 }
687  
688  
689 # Tell each end of a run made by then, the source's first, that the run
690 # died, as a set-up died with $error, and throw it on.
691 47 ▶sub _set_up_died ($pipeline, $run, $source_end, $error) {
Subroutine _set_up_died: called CC=4 SCAR=13.9
692 _end_all(
693 [
694 ($source_end ? ['the source', $source_end, 'died'] : ()),
695 3 map { [_run_label($pipeline, $run, $_), $run->{ends}[$_], 'died'] }
696 47 ▶ grep { $run->{ends}[$_] } 0 .. $#{ $run->{ends} },
Branch: 1 branch
BranchTrueFalseTotal
$source_end ? : 2 45 47
697 ],
698 1,
699 );
700 # A set-up runs before the run has values, so has nothing to stop.
701 47 ▶ 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 45 47
702 if $error isa 'Ekorn::Pipeline::Stop';
703 45 ▶ croak $error if is_ref($error);
Branch: 1 branch
BranchTrueFalseTotal
if Ref::Util::XS::is_ref($error) 1 44 45
704 44 die $error; ## no critic (ErrorHandling::RequireCarping): thrown again, as it was
705 }
706  
707  
708 # Return an iterator of a run that isn't plain, made by _iterator or
709 # _pushed, from its source's step, end and context, and the original
710 # part, if any, in %$run.
711 #
712 # The run ends as soon as nothing in it can still act on a value: the
713 # source has run out or stopped, every flush has been called, and every
714 # value still to be handed out has passed the last stage; or when it's
715 # ended from outside, or dies. Then each end is called, the source's
716 # first, told how the run ended from where it stands: "died" if the run
717 # died; else "done" if its input ran out, and "stopped" if not.
718 #
719 # A feed's run has a hungry, true while more values may be pushed: a
720 # source with nothing now isn't done, and the iterator hands out
721 # nothing more until the next push. It has no context of its own: its
722 # stages and ends see the context of the code pushing.
723 828 ▶sub _full_iterator ($pipeline, $run, $pull, $source_end, $context) {
Subroutine _full_iterator: called CC=1 SCAR=0.0
724 828 my $hungry = $run->{hungry};
725 828 my @stages = @{ $run->{stages} };
726 828 my @flushes = @{ $run->{flushes} };
727 828 my @ends = @{ $run->{ends} };
728 828 my @sinks = @{ $run->{sinks} };
729 # The places of the stages with a flush, so finding the next needn't
730 # look at every stage.
731 828 my @flush_at = grep { $flushes[$_] } 0 .. $#stages;
732 # Each value waiting to go through a stage, as the stage's index, the
733 # value, and whether it came from a flush, the next on top. A value a
734 # stage returns on its own, as most are, carries on through the stages
735 # without waiting here. Once the source is done, the flushes are called
736 # in order, from the first not called or passed over by a stop.
737 my(
738 828 @todo, $done, $source_done, %flushed, @input_done, $finished, $outside,
739 $ended, $spent, $dropped,
740 );
741 828 my $next_flush = 0;
742 # How many values wait at each stage's index, kept as @todo changes, so
743 # asking whether any wait before a stage needn't scan @todo: a flush
744 # can put many values there at once.
745 828 my @waiting_at;
746 # The run's results so far, the original of the value going through
747 # the stages, and the source's original part, if it gave one, for
748 # Ekorn::Pipeline::Context, the current result while $next runs; a
749 # value waiting on @todo carries its original, and a flush's values
750 # have none.
751 828 my @result = (0, undef, $run->{original});
752 # Values a source's step gave, still to go through the stages from the
753 # first, held as they came: @todo's values go first, being further on.
754 828 my @from_source;
755 # Put @values on @todo, to go on from stage $n, the first on top.
756 316 ▶ my $wait = sub ($n, $from_flush, @values) {
Subroutine __ANON__: called CC=1 SCAR=0.0
757 316 push @todo, map { [$n, $_, $from_flush, $result[1]] } reverse @values;
758 316 $waiting_at[$n] += @values;
759 316 return;
760 828 };
761 # Whether any value waits at a stage up to $upto. The run has a stage,
762 # so values from the source wait at the first.
763 781 ▶ my $waiting_upto = sub ($upto) {
Subroutine __ANON__: called CC=4 SCAR=13.9
764 781 ▶ return 1 if @from_source;
Branch: 1 branch
BranchTrueFalseTotal
if @from_source 10 771 781
765 771 ▶ $waiting_at[$_] && return 1 for 0 .. $upto;
Branch: 1 branch
BranchTrueFalseTotal
$waiting_at[$_] and return 1 32 1874 1906
766 739 return 0;
767 828 };
768 # How the run ended, as its caller asks: whether the source was read
769 # to its end, "stopped" if values still to be handed out were dropped,
770 # or "died" if anything died, an end included.
771 1659 ▶ my $finish = sub ($died) {
Subroutine __ANON__: called CC=9 SCAR=22.0
772 1659 ▶ return if $finished++;
Branch: 1 branch
BranchTrueFalseTotal
if $finished++ 831 828 1659
773 828 ▶ $ended
Branch: 3 branches
BranchTrueFalseTotal
$died ? : 73 755 828
$dropped ? : 10 745 755
$source_done ? : 615 130 745
774 = $died ? 'died'
775 : $dropped ? 'stopped'
776 : $source_done ? 'done'
777 : 'stopped';
778 828 my @calls;
779 828 ▶ push @calls, ['the source', $source_end, $source_done ? 'done' : 'stopped']
Branch: 2 branches
BranchTrueFalseTotal
if $source_end 100 728 828
$source_done ? : 56 44 100
780 if $source_end;
781 push @calls,
782 map { [
783 881 ▶ _run_label($pipeline, $run, $_), $ends[$_],
Branch: 1 branch
BranchTrueFalseTotal
$input_done[$_] ? : 717 164 881
784 $input_done[$_] ? 'done' : 'stopped',
785 ] }
786 828 grep { $ends[$_] } 0 .. $#ends;
787 828 ▶ local $running{context} = $context unless $hungry;
Branch: 1 branch
BranchTrueFalseTotal
unless $hungry 513 315 828
788 828 try {
789 828 _end_all(\@calls, $died);
790 }
791 catch ($e) {
792 # An end died: the run died, and hands out nothing more.
793 11 ($ended, $spent, @todo, @waiting_at, @from_source) = ('died', 1);
794 11 die $e; ## no critic (ErrorHandling::RequireCarping): thrown again, as it was
795 }
796 817 return;
797 828 };
798 # The index of the next flush to call, if any.
799 2165 ▶ my $flush_at = sub () {
Subroutine __ANON__: called CC=4 SCAR=13.9
800 2165 for my $n (@flush_at) {
801 2015 ▶ return $n if $n >= $next_flush && !$flushed{$n};
Branch: 1 branch
BranchTrueFalseTotal
if $n >= $next_flush and not $flushed{$n} 671 1344 2015
Condition66%2/3
$n >= $next_flush and not $flushed{$n}
!ll&&!rl&&r
13440671
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
802 }
803 1494 return;
804 828 };
805 # Every part from the next flush up to $upto has had all its input.
806 1376 ▶ my $input_ran_out = sub ($upto) {
Subroutine __ANON__: called CC=2 SCAR=6.9
807 1376 $input_done[$_] = 1 for $next_flush .. $upto;
808 1376 return;
809 828 };
810 # At a stop at stage $n, with nothing more to come from upstream,
811 # every stage up to it whose input had run out: none of its values
812 # still waiting, and no flush before it still to be called.
813 33 ▶ my $input_ran_out_at_stop = sub ($n) {
Subroutine __ANON__: called CC=5 SCAR=16.1
814 33 for my $stage ($next_flush .. $n) {
815 9 ▶ last if $waiting_upto->($stage);
Branch: 1 branch
BranchTrueFalseTotal
if &$waiting_upto($stage) 4 5 9
816 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
817 4 $input_done[$stage] = 1;
818 }
819 33 return;
820 828 };
821 # End the run if nothing in it can still act on a value. Called only
822 # once the source is done.
823 1293 ▶ my $end_if_ended = sub () {
Subroutine __ANON__: called CC=4 SCAR=13.9
824 1293 ▶ return if $finished || defined $flush_at->();
Branch: 1 branch
BranchTrueFalseTotal
if $finished or defined &$flush_at() 521 772 1293
Condition100%3/3
$finished or defined &$flush_at()
l!l&&r!l&&!r
49229772
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
825 772 ▶ return if $waiting_upto->($#stages);
Branch: 1 branch
BranchTrueFalseTotal
if &$waiting_upto($#stages) 38 734 772
826 734 $input_ran_out->($#stages);
827 734 $finish->(0);
828 723 return;
829 828 };
830 # Where all collects every value still to come, while it does: the
831 # loop pushes them onto it, with no return between values. Otherwise,
832 # the loop returns the next value.
833 828 my $into;
834 # Where all collects it, the values after the first that the last
835 # part returns at once, a list sink's result above all, kept to go
836 # into @$into straight after the first, in order, rather than each
837 # wrapped and waiting on @todo: past the last part, nothing more is
838 # done to them. Only while all collects, as $next gives one at a
839 # time.
840 my @straight;
841 1490 ▶ my $next = sub () {
Subroutine __ANON__: called CC=48 SCAR=38.7
842 1490 ▶ return if $outside;
Branch: 1 branch
BranchTrueFalseTotal
if $outside 1 1489 1490
843 1489 local $CURRENT_RESULT = \@result;
844 1489 while (1) {
845 5145 ▶ if ($into) {
Branch: 1 branch
BranchTrueFalseTotal
if ($into) 4571 574 5145
846 # All takes at once the values already at the end, such as a
847 # list sink's result. A run with no stages takes the shorter
848 # way, so this one has a stage.
849 4571 ▶ if (@todo && $todo[-1][0] == @stages) {
Branch: 1 branch
BranchTrueFalseTotal
if (@todo and $todo[-1][0] == @stages) 15 4556 4571
Condition100%3/3
@todo and $todo[-1][0] == @stages
!ll&&!rl&&r
428027615
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
850 15 ▶ while (@todo && $todo[-1][0] == @stages) {
Branch: 1 branch
BranchTrueFalseTotal
if (@todo and $todo[-1][0] == @stages) 17 15 32
Condition66%2/3
@todo and $todo[-1][0] == @stages
!ll&&!rl&&r
15017
MC/DC50%1/2
@todo and $todo[-1][0] == @stages
@todo $todo[-1][0] == @stages
Truth table66%2/3
A and B
A= @todoB= $todo[-1][0] == @stages
ABresult
0X0
100
111
851 17 push @$into, (pop @todo)->[1];
852 17 $waiting_at[@stages]--;
853 17 $result[0]++;
854 }
855 15 next;
856 }
857 }
858 5130 my($n, $value, $from_flush, $flush);
859 5130 ▶ if (@todo) {
Branch: 3 branches
BranchTrueFalseTotal
if (@todo) { } 360 4770 5130
elsif (@from_source) { } 912 3858 4770
elsif (not $done) { } 2494 1364 3858
860 360 ($n, $value, $from_flush, $result[1]) = @{ pop @todo };
861 360 $waiting_at[$n]--;
862 }
863 elsif (@from_source) {
864 912 $n = 0;
865 912 $result[1] = $value = shift @from_source;
866 }
867 elsif (!$done) {
868 # Straight onto @from_source, which is empty here.
869 2494 try {
870 2494 @from_source = $pull->();
871 }
872 catch ($e) {
873 24 ▶ if ($e isa 'Ekorn::Pipeline::Stop') {
Branch: 1 branch
BranchTrueFalseTotal
if ($e isa "Ekorn::Pipeline::Stop") 15 9 24
874 # A source that stops has had enough: what it
875 # carries is its last, and it's done.
876 15 ▶ my(undef, @carried) = is_arrayref($e) ? @$e : ();
Branch: 1 branch
BranchTrueFalseTotal
Ref::Util::XS::is_arrayref($e) ? : 11 4 15
877 15 ($done, $source_done) = (1, 1);
878 15 @from_source = @carried;
879 15 next;
880 }
881 9 ($done, $next_flush, @todo, @waiting_at, @from_source)
882 = (1, scalar @stages);
883 9 $finish->(1);
884 9 ▶ croak $e if is_ref($e);
Branch: 1 branch
BranchTrueFalseTotal
if Ref::Util::XS::is_ref($e) 5 4 9
885 4 die $e; ## no critic (ErrorHandling::RequireCarping): thrown again, as it was
886 }
887 2470 ▶ unless (@from_source) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@from_source) 1115 1355 2470
888 # Nothing yet: the feed's next push carries on.
889 1115 ▶ return if $hungry && $hungry->();
Branch: 1 branch
BranchTrueFalseTotal
if $hungry and &$hungry() 481 634 1115
Condition100%3/3
$hungry and &$hungry()
!ll&&!rl&&r
377257481
MC/DC100%2/2
$hungry and &$hungry()
$hungry &$hungry()
Truth table100%3/3
A and B
A= $hungryB= &$hungry()
ABresult
0X0
100
111
890 634 ($done, $source_done) = (1, 1);
891 634 next;
892 }
893 1355 $n = 0;
894 1355 $result[1] = $value = shift @from_source;
895 }
896 else {
897 1364 $n = $flush_at->();
898 1364 ▶ unless (defined $n) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $n) 722 642 1364
899 722 $end_if_ended->();
900 719 last;
901 }
902 642 $input_ran_out->($n);
903 642 $flushed{$n} = 1;
904 642 ($next_flush, $flush, $from_flush) = ($n + 1, $flushes[$n], 1);
905 642 $result[1] = undef;
906 }
907 3269 ▶ if ($n == @stages) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n == @stages) 47 3222 3269
908 # Only while values are handed out one at a time: all takes
909 # them from the top of the loop. Nothing can end while the
910 # source is still being read.
911 47 $result[0]++;
912 47 ▶ $end_if_ended->() if $done;
Branch: 1 branch
BranchTrueFalseTotal
if $done 40 7 47
913 47 return $value;
914 }
915 # Set only when it changes, as most values come from the source,
916 # and most runs, and the code running them, have no context.
917 local $running{ending} = $from_flush
918 3222 ▶ if $from_flush || $running{ending};
Branch: 1 branch
BranchTrueFalseTotal
if $from_flush or $running{"ending"} 746 2476 3222
Condition66%2/3
$from_flush or $running{"ending"}
l!l&&r!l&&!r
74602476
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
919 # A feed's run takes the context of the code pushing, so it's
920 # asked last, as most runs have none to set.
921 local $running{context} = $context
922 3222 ▶ if defined $context || (defined $running{context} && !$hungry);
Branch: 1 branch
BranchTrueFalseTotal
if defined $context or defined $running{"context"} and not $hungry 33 3189 3222
Condition100%6/6
defined $context or defined $running{"context"} and not $hungry
l!l&&r!l&&!r
3213189
defined $running{"context"} and not $hungry
!ll&&!rl&&r
3174151
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
923 3222 my $kept = 1;
924 3222 try {
925 3222 ▶ if ($flush) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flush) 642 2580 3222
926 642 local $_ = undef;
927 642 my @values = $flush->();
928 585 $n++;
929 585 $kept = @values;
930 585 $value = shift @values;
931 585 ▶ if (@values) {
Branch: 1 branch
BranchTrueFalseTotal
if (@values) 78 507 585
932 78 ▶ if ($into && $n == @stages) { @straight = @values }
Branch: 1 branch
BranchTrueFalseTotal
if ($into and $n == @stages) { } 43 35 78
Condition100%3/3
$into and $n == @stages
!ll&&!rl&&r
28743
MC/DC100%2/2
$into and $n == @stages
$into $n == @stages
Truth table100%3/3
A and B
A= $intoB= $n == @stages
ABresult
0X0
100
111
933 35 else { $wait->($n, 1, @values) }
934 }
935 }
936 3165 ▶ while ($kept && $n < @stages) {
Branch: 1 branch
BranchTrueFalseTotal
if ($kept and $n < @stages) 5132 1409 6541
Condition100%3/3
$kept and $n < @stages
!ll&&!rl&&r
12712825132
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
937 5132 local $_ = $value;
938 my @values
939 = $sinks[$n]
940 5132 ▶ ? do { $stages[$n]->($value); $result[0]++; () }
Branch: 1 branch
BranchTrueFalseTotal
$sinks[$n] ? : 552 4580 5132
941 : $stages[$n]->($value);
942 4973 ▶ last if $outside;
Branch: 1 branch
BranchTrueFalseTotal
if $outside 5 4968 4973
943 4968 $n++;
944 4968 ▶ unless (@values) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@values) 1592 3376 4968
945 1592 $kept = 0;
946 1592 last;
947 }
948 3376 $value = shift @values;
949 3376 ▶ if (@values) {
Branch: 1 branch
BranchTrueFalseTotal
if (@values) 154 3222 3376
950 154 ▶ if ($into && $n == @stages) { @straight = @values }
Branch: 1 branch
BranchTrueFalseTotal
if ($into and $n == @stages) { } 25 129 154
Condition100%3/3
$into and $n == @stages
!ll&&!rl&&r
2410525
MC/DC100%2/2
$into and $n == @stages
$into $n == @stages
Truth table100%3/3
A and B
A= $intoB= $n == @stages
ABresult
0X0
100
111
951 129 else { $wait->($n, $from_flush, @values) }
952 }
953 }
954 }
955 catch ($e) {
956 216 ▶ if ($e isa 'Ekorn::Pipeline::Stop') {
Branch: 1 branch
BranchTrueFalseTotal
if ($e isa "Ekorn::Pipeline::Stop") 152 64 216
957 # A stop made by throw is an array; one made otherwise
958 # carries nothing. A sink that stops the run still
959 # gives its result.
960 152 ▶ my(undef, @values) = is_arrayref($e) ? @$e : ();
Branch: 1 branch
BranchTrueFalseTotal
Ref::Util::XS::is_arrayref($e) ? : 147 5 152
961 152 ▶ $input_ran_out_at_stop->($n) if $done;
Branch: 1 branch
BranchTrueFalseTotal
if $done 33 119 152
962 152 $done = 1;
963 152 ▶ my $from = $sinks[$n] && !$flushed{$n} ? $n : $n + 1;
Branch: 1 branch
BranchTrueFalseTotal
$sinks[$n] && !$flushed{$n} ? : 39 113 152
Condition100%3/3
$sinks[$n] and not $flushed{$n}
!ll&&!rl&&r
112139
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
964 152 ▶ $next_flush = $from if $next_flush < $from;
Branch: 1 branch
BranchTrueFalseTotal
if $next_flush < $from 100 52 152
965 152 (@todo, @waiting_at, @from_source) = ();
966 152 $wait->($n + 1, $from_flush, @values);
967 152 next;
968 }
969 # A stage or a flush that dies ends the run: nothing waiting
970 # goes on, and every end is told it died.
971 64 ($done, $next_flush, @todo, @waiting_at, @from_source)
972 = (1, scalar @stages);
973 64 $finish->(1);
974 64 ▶ croak $e if is_ref($e);
Branch: 1 branch
BranchTrueFalseTotal
if Ref::Util::XS::is_ref($e) 6 58 64
975 58 my $at = Carp::shortmess() =~ s( \.? \n \z )()xr;
976 58 my $message = $e =~ s( \n \z )()xr =~ s( ^ )( )xmgr;
977 58 my $stage = _run_label($pipeline, $run, $n);
978 58 die "Pipeline $stage failed$at:\n$message\n";
979 }
980 3006 ▶ return if $outside;
Branch: 1 branch
BranchTrueFalseTotal
if $outside 5 3001 3006
981 3001 ▶ if ($kept) {
Branch: 1 branch
BranchTrueFalseTotal
if ($kept) 1282 1719 3001
982 1282 $result[0]++;
983 1282 ▶ $end_if_ended->() if $done;
Branch: 1 branch
BranchTrueFalseTotal
if $done 531 751 1282
984 1274 ▶ return $value unless $into;
Branch: 1 branch
BranchTrueFalseTotal
unless $into 153 1121 1274
985 1121 push @$into, $value;
986 1121 ▶ if (@straight) {
Branch: 1 branch
BranchTrueFalseTotal
if (@straight) 68 1053 1121
987 68 $result[0] += @straight;
988 68 push @$into, splice @straight;
989 }
990 }
991 }
992 # Everything has been handed out: the iterator is spent.
993 719 $spent = 1;
994 719 return;
995 828 };
996 # Every value the run has still to give, as $next would give them one
997 # at a time, but in one call of it.
998 1192 ▶ my $all = sub () {
Subroutine __ANON__: called CC=1 SCAR=0.0
999 1192 my @all;
1000 1192 $into = \@all;
1001 1192 try {
1002 1192 $next->();
1003 }
1004 catch ($e) {
1005 67 $into = undef;
1006 67 die $e; ## no critic (ErrorHandling::RequireCarping): thrown again, as it was
1007 }
1008 1125 $into = undef;
1009 1125 return @all;
1010 828 };
1011 # Ended from outside, by the iterator's finish or as it's let go, the
1012 # run stops: nothing more is pulled, and nothing waiting goes on.
1013 852 ▶ my $end_run = sub () {
Subroutine __ANON__: called CC=4 SCAR=13.9
1014 852 $outside = 1;
1015 # A run that died has nothing waiting: its values were dropped then.
1016 852 ▶ $dropped = 1 if @todo || @from_source;
Branch: 1 branch
BranchTrueFalseTotal
if @todo or @from_source 13 839 852
Condition100%3/3
@todo or @from_source
l!l&&r!l&&!r
58839
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
1017 852 ($done, $next_flush, @todo, @waiting_at, @from_source)
1018 = (1, scalar @stages);
1019 852 $finish->(0);
1020 852 ▶ $ended = 'stopped' if $dropped;
Branch: 1 branch
BranchTrueFalseTotal
if $dropped 26 826 852
1021 852 return;
1022 828 };
1023 # How the run ended, held back until the iterator has nothing more to
1024 # give, so it's said once, and doesn't change. It's the iterator's
1025 # spent too: the run has ended, and so is said to, once it has.
1026 2003 ▶ my $ended_once = sub () {
Subroutine __ANON__: called CC=5 SCAR=16.1
1027 2003 ▶ return $ended if $spent || $outside || ($ended // '') eq 'died';
Branch: 1 branch
BranchTrueFalseTotal
if $spent or $outside or ($ended // "") eq "died" 511 1492 2003
Condition100%8/8
$spent or $outside or ($ended // "") eq "died"
l!l&&r!l&&!r
464471492
$spent or $outside
l!l&&r!l&&!r
450141539
$ended // ""
l!l
501489
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
1028 1492 return;
1029 828 };
1030 return Ekorn::Pipeline::Iterator->new(
1031 $next, $end_run, $context, $ended_once, $ended_once, $all,
1032 $run->{original},
1033 828 );
1034 }
1035  
1036  
1037 # Return how stage $n of a run made into %$run, of $pipeline, is named in
1038 # a message, as _label names it.
1039 954 ▶sub _run_label ($pipeline, $run, $n) {
Subroutine _run_label: called CC=1 SCAR=0.0
1040 954 return _label($pipeline, $run->{names}[$n], $run->{within}[$n]);
1041 }
1042  
1043  
1044 # Return an iterator, as _iterator does, for a run made into %$run with
1045 # no flush, no end of a stage's, and no sink, so with none of their
1046 # bookkeeping: each value through every step before the next, what a
1047 # step returns going on in order, and a stop's values going on from the
1048 # step after it. The run ends, as _iterator's does, once the source is
1049 # done and no value waits before the last step, and then the source's
1050 # end, its only one, is called.
1051 309 ▶sub _plain_iterator ($pipeline, $run, $pull, $source_end, $context) {
Subroutine _plain_iterator: called CC=1 SCAR=0.0
1052 309 my @steps = @{ $run->{stages} };
1053 # Each value waiting to go through a step, as the step's index and the
1054 # value, the next on top, so in order of index from the bottom: the
1055 # lowest is at the bottom. Values a source's step gave, still to go
1056 # through the steps from the first, are held as they came.
1057 309 my(@todo, @from_source);
1058 309 my($done, $source_done, $finished, $outside, $ended, $spent, $dropped, $into);
1059 # Where all collects it, the values after the first that the last step
1060 # returns at once, kept to go into @$into straight after the first,
1061 # rather than each wrapped and waiting on @todo, as _full_iterator's
1062 # are.
1063 309 my @straight;
1064 # The run's results so far, the original of the value going through
1065 # the steps, and the source's original part, if it gave one, for
1066 # Ekorn::Pipeline::Context, the current result while $next runs; a
1067 # value waiting on @todo carries its original.
1068 309 my @result = (0, undef, $run->{original});
1069 655 ▶ my $finish = sub ($died) {
Subroutine __ANON__: called CC=6 SCAR=17.9
1070 655 ▶ return if $finished++;
Branch: 1 branch
BranchTrueFalseTotal
if $finished++ 346 309 655
1071 # Values dropped from outside make it stopped, as end_run says.
1072 309 ▶ $ended = $died ? 'died' : $source_done ? 'done' : 'stopped';
Branch: 2 branches
BranchTrueFalseTotal
$died ? : 31 278 309
$source_done ? : 215 63 278
1073 309 ▶ return unless $source_end;
Branch: 1 branch
BranchTrueFalseTotal
unless $source_end 140 169 309
1074 169 local $running{context} = $context;
1075 169 try {
1076 169 ▶ _end_all(
Branch: 1 branch
BranchTrueFalseTotal
$source_done ? : 124 45 169
1077 [['the source', $source_end, $source_done ? 'done' : 'stopped']],
1078 $died,
1079 );
1080 }
1081 catch ($e) {
1082 # The end died: the run died, and hands out nothing more.
1083 3 ($ended, $spent, @todo, @from_source) = ('died', 1);
1084 3 die $e; ## no critic (ErrorHandling::RequireCarping): thrown again, as it was
1085 }
1086 166 return;
1087 309 };
1088 502 ▶ my $next = sub () {
Subroutine __ANON__: called CC=39 SCAR=36.6
1089 502 ▶ return if $outside;
Branch: 1 branch
BranchTrueFalseTotal
if $outside 3 499 502
1090 499 local $CURRENT_RESULT = \@result;
1091 499 while (1) {
1092 1170 ▶ if ($into) {
Branch: 1 branch
BranchTrueFalseTotal
if ($into) 786 384 1170
1093 # All takes at once a step's values where there are no
1094 # steps, and the values already at the end.
1095 786 ▶ if (!@steps && @from_source) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @steps and @from_source) 29 757 786
Condition100%3/3
not @steps and @from_source
!ll&&!rl&&r
38537229
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
1096 29 $result[0] += @from_source;
1097 29 push @$into, splice @from_source;
1098 29 next;
1099 }
1100 757 ▶ if (@todo && $todo[-1][0] == @steps) {
Branch: 1 branch
BranchTrueFalseTotal
if (@todo and $todo[-1][0] == @steps) 16 741 757
Condition100%3/3
@todo and $todo[-1][0] == @steps
!ll&&!rl&&r
7271416
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
1101 16 ▶ while (@todo && $todo[-1][0] == @steps) {
Branch: 1 branch
BranchTrueFalseTotal
if (@todo and $todo[-1][0] == @steps) 17 16 33
Condition100%3/3
@todo and $todo[-1][0] == @steps
!ll&&!rl&&r
15117
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
1102 17 push @$into, (pop @todo)->[1];
1103 17 $result[0]++;
1104 }
1105 16 next;
1106 }
1107 }
1108 1125 my($n, $value);
1109 1125 ▶ if (@todo) {
Branch: 3 branches
BranchTrueFalseTotal
if (@todo) { } 52 1073 1125
elsif (@from_source) { } 28 1045 1073
elsif (not $done) { } 783 262 1045
1110 52 ($n, $value, $result[1]) = @{ pop @todo };
1111 }
1112 elsif (@from_source) {
1113 28 $n = 0;
1114 28 $result[1] = $value = shift @from_source;
1115 }
1116 elsif (!$done) {
1117 783 try {
1118 783 @from_source = $pull->();
1119 }
1120 catch ($e) {
1121 28 ▶ if ($e isa 'Ekorn::Pipeline::Stop') {
Branch: 1 branch
BranchTrueFalseTotal
if ($e isa "Ekorn::Pipeline::Stop") 11 17 28
1122 11 ▶ my(undef, @carried) = is_arrayref($e) ? @$e : ();
Branch: 1 branch
BranchTrueFalseTotal
Ref::Util::XS::is_arrayref($e) ? : 8 3 11
1123 11 ($done, $source_done) = (1, 1);
1124 11 @from_source = @carried;
1125 11 next;
1126 }
1127 17 ($done, @todo, @from_source) = (1);
1128 17 $finish->(1);
1129 17 ▶ croak $e if is_ref($e);
Branch: 1 branch
BranchTrueFalseTotal
if Ref::Util::XS::is_ref($e) 2 15 17
1130 15 die $e; ## no critic (ErrorHandling::RequireCarping): thrown again, as it was
1131 }
1132 755 ▶ unless (@from_source) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@from_source) 204 551 755
1133 204 ($done, $source_done) = (1, 1);
1134 204 next;
1135 }
1136 551 $n = 0;
1137 551 $result[1] = $value = shift @from_source;
1138 }
1139 else {
1140 # Nothing waits, and the source is done.
1141 262 $finish->(0);
1142 259 last;
1143 }
1144 631 ▶ if ($n < @steps) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < @steps) 391 240 631
1145 391 ▶ local $running{ending} = undef if $running{ending};
Branch: 1 branch
BranchTrueFalseTotal
if $running{"ending"} 1 390 391
1146 local $running{context} = $context
1147 391 ▶ if defined $context || defined $running{context};
Branch: 1 branch
BranchTrueFalseTotal
if defined $context or defined $running{"context"} 36 355 391
Condition66%2/3
defined $context or defined $running{"context"}
l!l&&r!l&&!r
360355
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
1148 391 my $kept = 1;
1149 391 try {
1150 391 ▶ while ($n < @steps) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < @steps) 489 313 802
1151 489 local $_ = $value;
1152 489 my @values = $steps[$n]->($value);
1153 434 ▶ last if $outside;
Branch: 1 branch
BranchTrueFalseTotal
if $outside 2 432 434
1154 432 $n++;
1155 432 ▶ unless (@values) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@values) 21 411 432
1156 21 $kept = 0;
1157 21 last;
1158 }
1159 411 $value = shift @values;
1160 411 ▶ if (@values) {
Branch: 1 branch
BranchTrueFalseTotal
if (@values) 46 365 411
1161 46 ▶ if ($into && $n == @steps) { @straight = @values }
Branch: 1 branch
BranchTrueFalseTotal
if ($into and $n == @steps) { } 21 25 46
Condition100%3/3
$into and $n == @steps
!ll&&!rl&&r
20521
MC/DC100%2/2
$into and $n == @steps
$into $n == @steps
Truth table100%3/3
A and B
A= $intoB= $n == @steps
ABresult
0X0
100
111
1162 else {
1163 25 push @todo, map { [$n, $_, $result[1]] } reverse @values;
1164 }
1165 }
1166 }
1167 }
1168 catch ($e) {
1169 55 ▶ if ($e isa 'Ekorn::Pipeline::Stop') {
Branch: 1 branch
BranchTrueFalseTotal
if ($e isa "Ekorn::Pipeline::Stop") 41 14 55
1170 41 ▶ my(undef, @carried) = is_arrayref($e) ? @$e : ();
Branch: 1 branch
BranchTrueFalseTotal
Ref::Util::XS::is_arrayref($e) ? : 39 2 41
1171 41 $done = 1;
1172 41 @from_source = ();
1173 41 @todo = map { [$n + 1, $_, $result[1]] } reverse @carried;
1174 41 next;
1175 }
1176 14 ($done, @todo, @from_source) = (1);
1177 14 $finish->(1);
1178 14 ▶ croak $e if is_ref($e);
Branch: 1 branch
BranchTrueFalseTotal
if Ref::Util::XS::is_ref($e) 2 12 14
1179 12 my $at = Carp::shortmess() =~ s( \.? \n \z )()xr;
1180 12 my $message = $e =~ s( \n \z )()xr =~ s( ^ )( )xmgr;
1181 12 my $stage = _run_label($pipeline, $run, $n);
1182 12 die "Pipeline $stage failed$at:\n$message\n";
1183 }
1184 336 ▶ return if $outside;
Branch: 1 branch
BranchTrueFalseTotal
if $outside 2 334 336
1185 334 ▶ next unless $kept;
Branch: 1 branch
BranchTrueFalseTotal
unless $kept 21 313 334
1186 }
1187 # The run ends once nothing waits before the last step: values
1188 # from the source wait before the first, if there is one.
1189 553 ▶ $finish->(0)
Branch: 1 branch
BranchTrueFalseTotal
if $done and not $finished and not @from_source && @steps and not @todo && $todo[0][0] < @steps 23 530 553
Condition100%15/15
$done and not $finished and not @from_source && @steps and not @todo && $todo[0][0] < @steps
!ll&&!rl&&r
524623
$done and not $finished and not @from_source && @steps
!ll&&!rl&&r
520429
$done and not $finished
!ll&&!rl&&r
516433
@from_source && @steps
!ll&&!rl&&r
2634
@todo && $todo[0][0] < @steps
!ll&&!rl&&r
2216
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
1190 if $done
1191 && !$finished
1192 && !(@from_source && @steps)
1193 && !(@todo && $todo[0][0] < @steps);
1194 553 $result[0]++;
1195 553 ▶ return $value unless $into;
Branch: 1 branch
BranchTrueFalseTotal
unless $into 204 349 553
1196 349 push @$into, $value;
1197 349 ▶ if (@straight) {
Branch: 1 branch
BranchTrueFalseTotal
if (@straight) 21 328 349
1198 21 $result[0] += @straight;
1199 21 push @$into, splice @straight;
1200 }
1201 }
1202 # Everything has been handed out: the iterator is spent.
1203 259 $spent = 1;
1204 259 return;
1205 309 };
1206 199 ▶ my $all = sub () {
Subroutine __ANON__: called CC=1 SCAR=0.0
1207 199 my @all;
1208 199 $into = \@all;
1209 199 try {
1210 199 $next->();
1211 }
1212 catch ($e) {
1213 17 $into = undef;
1214 17 die $e; ## no critic (ErrorHandling::RequireCarping): thrown again, as it was
1215 }
1216 182 $into = undef;
1217 182 return @all;
1218 309 };
1219 339 ▶ my $end_run = sub () {
Subroutine __ANON__: called CC=4 SCAR=13.9
1220 339 $outside = 1;
1221 339 ▶ $dropped = 1 if @todo || @from_source;
Branch: 1 branch
BranchTrueFalseTotal
if @todo or @from_source 8 331 339
Condition100%3/3
@todo or @from_source
l!l&&r!l&&!r
17331
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
1222 339 ($done, @todo, @from_source) = (1);
1223 339 $finish->(0);
1224 339 ▶ $ended = 'stopped' if $dropped;
Branch: 1 branch
BranchTrueFalseTotal
if $dropped 14 325 339
1225 339 return;
1226 309 };
1227 89 ▶ my $ended_once = sub () {
Subroutine __ANON__: called CC=5 SCAR=16.1
1228 89 ▶ return $ended if $spent || $outside || ($ended // '') eq 'died';
Branch: 1 branch
BranchTrueFalseTotal
if $spent or $outside or ($ended // "") eq "died" 77 12 89
Condition100%8/8
$spent or $outside or ($ended // "") eq "died"
l!l&&r!l&&!r
661112
$spent or $outside
l!l&&r!l&&!r
561023
$ended // ""
l!l
1112
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
1229 12 return;
1230 309 };
1231 return Ekorn::Pipeline::Iterator->new(
1232 $next, $end_run, $context, $ended_once, $ended_once, $all,
1233 $run->{original},
1234 309 );
1235 }
1236 31 1;
1237 __END__
↑ Summary Pipeline/Sugar.pm »