Pipeline.pm

filter: Statementstmt 100% 621 / 621 Branchbran 100% 348 / 348 Conditioncond 97.8% 181 / 185 MC/DCmcdc 96.2% 102 / 106 Subroutinesub 100% 80 / 80 Podpod 100% 13 / 13 totaltotal 99.4% 1345 / 1353 CC 250 SCAR 28.3
CC
250
Cov
100%
__ANON__
48.0
__ANON__
39.0
_make_run
25.0
CRAP 16.9
↑ Summary Pipeline/Sugar.pm »
1 32 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package Ekorn::Pipeline;
4  
5 32 ▶use experimental qw(builtin try);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 32 ▶use builtin qw(blessed refaddr weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 32 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 32 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 32 ▶use Ref::Util qw(is_arrayref is_coderef is_plain_hashref is_ref);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 32 ▶use parent 'Ekorn::Pipeline::Element';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 32 ▶use Ekorn::Pipeline::Errors qw(croakf);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 32 ▶use Ekorn::Pipeline::Feed qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 32 ▶use Ekorn::Pipeline::Iterator qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 32 ▶use Ekorn::Pipeline::Stage qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 32 ▶use namespace::clean -except => 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 32 our $VERSION = 'v0.4.0';
22  
23 32 our @EXPORT_OK = qw(is_sink is_stage);
24  
25 # Croak at the line that called Ekorn::Pipeline.
26 32 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 261 ▶sub is_stage ($thing) {
Subroutine is_stage: called CC=3 SCAR=11.0
Pod documented
32 261 ▶ 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
8559117
Ref::Util::XS::is_coderef($thing) or $thing isa "Ekorn::Pipeline"
l!l&&r!l&&!r
769176
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 # 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 1768 ▶sub is_sink ($thing) {
Subroutine is_sink: called CC=5 SCAR=16.1
Pod documented
41 1768 ▶ ($thing) = ($thing->stages)[-1] if $thing isa 'Ekorn::Pipeline';
Branch: 1 branch
BranchTrueFalseTotal
if $thing isa "Ekorn::Pipeline" 4 1764 1768
42 1768 ▶ return !!0 unless blessed($thing);
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($thing) 11 1757 1768
43 1757 ▶ return 'one' if $thing->can('EKORN_SINK_ONE');
Branch: 1 branch
BranchTrueFalseTotal
if $thing->can("EKORN_SINK_ONE") 180 1577 1757
44 1577 ▶ return 'many' if $thing->can('EKORN_SINK_MANY');
Branch: 1 branch
BranchTrueFalseTotal
if $thing->can("EKORN_SINK_MANY") 126 1451 1577
45 1451 return !!0;
46 }
47  
48  
49 # The role methods a part can offer for a stage's place, in the order
50 # they're looked for.
51 32 my @STAGE_ROLES = qw(EKORN_STAGE EKORN_SINK_ONE EKORN_SINK_MANY);
52  
53 # Return the role methods that what's given offers for a stage's place.
54 3163 ▶sub _stage_roles ($thing) {
Subroutine _stage_roles: called CC=2 SCAR=6.9
55 3163 ▶ return unless blessed($thing);
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($thing) 986 2177 3163
56 2177 return grep { $thing->can($_) } @STAGE_ROLES;
57 }
58  
59 # Return the first role method that what's given offers for a stage's
60 # place, or nothing if it offers none. Each run asks it of each part, so
61 # it looks no further than the first.
62 3718 ▶sub _stage_role ($thing) {
Subroutine _stage_role: called CC=4 SCAR=13.9
63 3718 ▶ return unless blessed($thing);
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($thing) 1097 2621 3718
64 2621 for my $role (@STAGE_ROLES) {
65 3079 ▶ return $role if $thing->can($role);
Branch: 1 branch
BranchTrueFalseTotal
if $thing->can($role) 2616 463 3079
66 }
67 5 return;
68 }
69  
70 # Return true if what's given offers a source's role method.
71 1640 ▶sub _is_source ($thing) {
Subroutine _is_source: called CC=2 SCAR=6.9
72 1640 ▶ return !!(blessed($thing) && $thing->can('EKORN_SOURCE'));
Condition100%3/3
builtin::blessed($thing) && $thing->can('EKORN_SOURCE')
!ll&&!rl&&r
987241412
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
73 }
74  
75 # Return the stage given as an element: a part offering a role method is
76 # kept as it is, and a code reference is made a Stage.
77 3162 ▶sub _element ($stage) {
Subroutine _element: called CC=3 SCAR=11.0
78 3162 ▶ 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) 2178 984 3162
Condition100%3/3
$stage isa "Ekorn::Pipeline::Element" or _stage_role($stage)
l!l&&r!l&&!r
21708984
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
79 984 return Ekorn::Pipeline::Stage->new($stage);
80 }
81  
82 # Croak unless what's given, as stage #$n, can go where a stage goes, as
83 # one role: a source isn't a stage, and a part offering more than one of
84 # a stage's and a sink's role methods couldn't say which it is.
85 3163 ▶sub _check_stage ($stage, $n) {
Subroutine _check_stage: called CC=7 SCAR=19.5
86 3163 my @roles = _stage_roles($stage);
87 3163 ▶ croakf
Branch: 1 branch
BranchTrueFalseTotal
if @roles > 1 1 3162 3163
88 'Expected stage #%d to offer one of EKORN_STAGE, EKORN_SINK_ONE and EKORN_SINK_MANY, not %s',
89 $n,
90 join ' and ', @roles
91 if @roles > 1;
92 3162 ▶ croakf 'Expected stage #%d to be a stage, not a source', $n
Branch: 1 branch
BranchTrueFalseTotal
if not @roles and _is_source($stage) 1 3161 3162
Condition100%3/3
not @roles and _is_source($stage)
!ll&&!rl&&r
193612251
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
93 if !@roles && _is_source($stage);
94 3161 ▶ croakf
Branch: 1 branch
BranchTrueFalseTotal
unless @roles or Ref::Util::XS::is_coderef($stage) or $stage isa "Ekorn::Pipeline" 7 3154 3161
Condition100%6/6
@roles or Ref::Util::XS::is_coderef($stage) or $stage isa "Ekorn::Pipeline"
l!l&&r!l&&!r
29152397
@roles or Ref::Util::XS::is_coderef($stage)
l!l&&r!l&&!r
1936979246
MC/DC100%3/3
@roles or Ref::Util::XS::is_coderef($stage) or $stage isa "Ekorn::Pipeline"
@roles Ref::Util::XS::is_coderef($stage) $stage isa "Ekorn::Pipeline"
Truth table100%4/4
A or B or C
A= @rolesB= Ref::Util::XS::is_coderef($stage)C= $stage isa "Ekorn::Pipeline"
ABCresult
1XX1
01X1
0011
0000
95 q(Expected stage #%d to be a stage: a code reference, a pipeline, or an object offering a stage's role method),
96 $n
97 unless @roles || is_coderef($stage) || $stage isa 'Ekorn::Pipeline';
98 3154 return;
99 }
100  
101 # Return a pipeline, called with values to run each of them through the
102 # stages. A pipeline given as a stage has its stages spliced in, each
103 # named by its place in each pipeline, such as "2.3". In scalar context,
104 # a call gives how many values came out, unless the last stage is a
105 # scalar sink, when it gives that sink's result.
106 1926 ▶sub new ($class, @given) {
Subroutine new: called CC=6 SCAR=17.9
Pod documented
107 1926 my(@stages, @names, @within);
108 1926 for my $n (1 .. @given) {
109 3163 my $stage = $given[$n - 1];
110 3163 _check_stage($stage, $n);
111 3154 ▶ if ($stage isa 'Ekorn::Pipeline') {
Branch: 1 branch
BranchTrueFalseTotal
if ($stage isa "Ekorn::Pipeline") 239 2915 3154
112 239 my($inner, $inner_names, $inner_within) = _composed($stage);
113 239 ▶ croakf
Branch: 1 branch
BranchTrueFalseTotal
unless $inner_names 3 236 239
114 'Expected stage #%d, a pipeline, to give only stages from its stages method',
115 $n
116 unless $inner_names;
117 236 push @stages, map { _element($_) } @$inner;
118 236 push @names, map { "$n.$_" } @$inner_names;
119 push @within,
120 236 map { [[$stage, $inner_names->[$_]], @{ $inner_within->[$_] }] }
121 0 .. $#$inner;
122 236 next;
123 }
124 2915 push @stages, _element($stage);
125 2915 push @names, $n;
126 2915 push @within, [];
127 }
128 # In scalar context, a run gives a scalar sink's result, a list
129 # sink's list as an array reference, or how many values came out.
130 1914 ▶ my $sink = @stages ? is_sink($stages[-1]) : !!0;
Branch: 1 branch
BranchTrueFalseTotal
@stages ? : 1696 218 1914
131 # Where every stage is a plain step, a Stage of Ekorn's own, with no
132 # flush, end or sink, and nothing made for a run, a push runs the
133 # steps in a plain loop, with none of a run's machinery; otherwise it
134 # makes the stages for the run first, and takes the plain loop if
135 # what's made allows it.
136 my @steps
137 3155 = (grep { ref ne 'Ekorn::Pipeline::Stage' } @stages)
138 ? ()
139 1914 ▶ : map { $_->{call} } @stages;
Branch: 1 branch
BranchTrueFalseTotal
grep({ref $_ ne 'Ekorn::Pipeline::Stage';} @stages) ? : 1120 794 1914
140 # What names a step in a message, as _push_steps takes it.
141 1914 my $labels = [\@names, \@within];
142 # The pipeline, for its name in messages, held weakly, so the closure
143 # it's called as never keeps it.
144 1914 my $self;
145 my $pipeline = bless {
146 stages => \@stages,
147 names => \@names,
148 within => \@within,
149 # For feed, which takes the plain steps as they are.
150 steps => \@steps,
151 labels => $labels,
152 711 ▶ call => sub (@values) {
Subroutine __ANON__: called CC=7 SCAR=19.5
153 711 ▶ return @values unless @stages;
Branch: 1 branch
BranchTrueFalseTotal
unless @stages 13 698 711
154 my @results
155 698 ▶ = @steps
Branch: 1 branch
BranchTrueFalseTotal
@steps ? : 274 424 698
156 ? _push_steps($self, \@steps, $labels, undef, \@values)
157 : _pushed($self, \@stages, \@names, \@within, \@values);
158 575 ▶ return @results if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 446 129 575
159 129 ▶ return $results[0] if $sink && $sink eq 'one';
Branch: 1 branch
BranchTrueFalseTotal
if $sink and $sink eq "one" 60 69 129
Condition100%3/3
$sink and $sink eq "one"
!ll&&!rl&&r
591060
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
160 69 ▶ return \@results if $sink;
Branch: 1 branch
BranchTrueFalseTotal
if $sink 10 59 69
161 59 return scalar @results;
162 },
163 1914 }, $class;
164 1914 weaken($self = $pipeline);
165 1914 return $pipeline;
166 }
167  
168 # The named pipelines, each by its name qualified by its package.
169 32 my %named;
170  
171 # Return $name qualified by $package, unless it's qualified already, or
172 # nothing if it isn't a name: identifiers, as Perl's, joined by "::".
173 118 ▶sub _qualified ($name, $package) {
Subroutine _qualified: called CC=5 SCAR=16.1
174 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
175 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
176 100 ▶ return $name =~ m( :: )x ? $name : "${package}::$name";
Branch: 1 branch
BranchTrueFalseTotal
$name =~ / :: /ux ? : 29 71 100
177 }
178  
179 # Return the stages given, each name replaced by the pipeline it names,
180 # looked up in $package unless it's qualified.
181 110 ▶sub _resolved ($package, @given) {
Subroutine _resolved: called CC=5 SCAR=16.1
182 110 my @stages;
183 110 for my $n (1 .. @given) {
184 122 my $stage = $given[$n - 1];
185 122 ▶ if (is_stage($stage)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_stage($stage)) 56 66 122
186 56 push @stages, $stage;
187 56 next;
188 }
189 66 my $name = _qualified($stage, $package);
190 66 ▶ croakf
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 13 53 66
191 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),
192 $n
193 unless defined $name;
194 53 my $pipeline = $named{$name};
195 53 ▶ croakf 'No pipeline named %s', $name unless $pipeline;
Branch: 1 branch
BranchTrueFalseTotal
unless $pipeline 6 47 53
196 47 push @stages, $pipeline;
197 }
198 91 return @stages;
199 }
200  
201 # Return a new pipeline of the invocant's class, of the stages given, each
202 # a code reference, a pipeline, or the name of one, looked up now in
203 # $package unless it's qualified; or, given one name alone, the pipeline
204 # it names, if it's of the invocant's class, so its stages are numbered
205 # where it's given. The one method that reads names, called by the words,
206 # such as Factories' pipeline, with their caller's package.
207 84 ▶sub pipeline_in ($class, $package, @stages) {
Subroutine pipeline_in: called CC=4 SCAR=13.9
Pod documented
208 84 my @resolved = _resolved($package, @stages);
209 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
210 if @stages == 1 && !is_stage($stages[0]) && $resolved[0] isa $class;
211 37 return $class->new(@resolved);
212 }
213  
214 # Name a pipeline, in the caller's package unless the name is qualified,
215 # and return it: on the class, a new one of the stages after the name,
216 # read as pipeline_in reads them; on a pipeline, that one. The name can't
217 # be one already defined for another pipeline.
218 52 ▶sub named ($invocant, $name, @stages) {
Subroutine named: called CC=8 SCAR=20.8
Pod documented
219 52 ▶ my $class = ref $invocant ? ref $invocant : $invocant;
Branch: 1 branch
BranchTrueFalseTotal
ref $invocant ? : 21 31 52
220 52 my $package = caller;
221 52 my $qualified = _qualified($name, $package);
222 52 ▶ croakf 'Expected a name, made of words joined by ::' unless defined $qualified;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $qualified 5 47 52
223 47 my $pipeline;
224 47 ▶ if (ref $invocant) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $invocant) { } 21 26 47
225 21 ▶ croakf 'Expected only a name, naming a pipeline you have' if @stages;
Branch: 1 branch
BranchTrueFalseTotal
if @stages 1 20 21
226 20 $pipeline = $invocant;
227 }
228 else {
229 # Always a new one, even of one name, so a name never aliases.
230 26 $pipeline = $class->new(_resolved($package, @stages));
231 }
232 44 my $own = $named{$qualified};
233 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
234 if $own && refaddr($own) != refaddr($pipeline);
235 41 ▶ $pipeline->{name} = $qualified unless defined $pipeline->{name};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pipeline->{"name"} 39 2 41
236 41 return $named{$qualified} = $pipeline;
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 # Return an iterator handing out, one per call, the values from $source
265 # run through the stages: $source a code reference, or anything
266 # offering a source's role method, an iterator included.
267 597 ▶sub iterator ($self, $source) {
Subroutine iterator: called CC=3 SCAR=11.0
Pod documented
268 597 ▶ 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 594 597
Condition100%3/3
Ref::Util::XS::is_coderef($source) or _is_source($source)
l!l&&r!l&&!r
1834113
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
269 unless is_coderef($source) || _is_source($source);
270 return _iterator(
271 $self, $self->{stages}, $self->{names}, $self->{within},
272 594 $source,
273 );
274 }
275  
276 # Return a feed: one run of the stages, made now, which takes values as
277 # they're pushed, giving what comes out of each push, and ends when it's
278 # closed, giving what the flushes give. A run with no flush, no end and
279 # no sink takes the plain loop, each push a pushed run's, its stop
280 # ending the run; any other takes the full loop, as an iterator whose
281 # source has nothing until a push, and is done once the feed is closed.
282 428 ▶sub feed ($self) {
Subroutine feed: called CC=3 SCAR=11.0
Pod documented
283 427 my($steps, $labels) = @$self{qw(steps labels)};
284 # Stages that aren't all plain steps are made for the run.
285 427 ▶ unless (@$steps) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$steps) 347 80 427
286 347 my $run = _made_run($self, @$self{qw(stages names within)});
287 return _full_feed($self, $run)
288 346 ▶ if grep { defined } @{ $run->{flushes} }, @{ $run->{ends} };
Branch: 1 branch
BranchTrueFalseTotal
if grep {defined $_;} @{$$run{"flushes"};}, @{$$run{"ends"};} 315 31 346
289 31 ($steps, $labels) = ($run->{stages}, [@$run{qw(names within)}]);
290 }
291 # How the feed's run ended, undef while it's open, and how many
292 # results its pushes have made.
293 111 my @fed = (undef, 0);
294 return Ekorn::Pipeline::Feed->new(
295 # Without a signature, as a feed may be pushed a value at a time:
296 # its values go on as they are, not copied.
297 180 ▶ push => sub { _push_steps($self, $steps, $labels, \@fed, \@_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
298 70 ▶ close => sub () { $fed[0] = 'done'; return },
Subroutine __ANON__: called CC=1 SCAR=0.0
299 1 ▶ finish => sub () { $fed[0] = 'stopped'; return },
Subroutine __ANON__: called CC=1 SCAR=0.0
300 556 ▶ ended => sub () { return $fed[0] },
Subroutine __ANON__: called CC=1 SCAR=0.0
301 # Nothing to flush or end, for a stage driving the feed.
302 111 plain => 1,
303 );
304 }
305  
306 # Return a feed of a run made into %$run with a flush, an end or a sink:
307 # an iterator of it, whose source has nothing until a push, and is done
308 # once the feed is closed; its stages see the context of the code
309 # pushing.
310 315 ▶sub _full_feed ($pipeline, $run) {
Subroutine _full_feed: called CC=1 SCAR=0.0
311 315 my(@pushed, $closed);
312 315 ▶ $run->{hungry} = sub () { !$closed };
Subroutine __ANON__: called CC=1 SCAR=0.0
313 my $iterator
314 315 ▶ = _full_iterator($pipeline, $run, sub { splice @pushed }, undef, undef);
Subroutine __ANON__: called CC=1 SCAR=0.0
315 # The iterator's own code for all, called directly, as a feed may be
316 # pushed a value at a time; the iterator is kept by the closures here.
317 315 my $all = $iterator->{all};
318 return Ekorn::Pipeline::Feed->new(
319 # Without a signature, as a feed may be pushed a value at a time.
320 534 ▶ push => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
321 534 push @pushed, @_;
322 534 return $all->();
323 },
324 257 ▶ close => sub () {
Subroutine __ANON__: called CC=1 SCAR=0.0
325 257 $closed = 1;
326 257 return $iterator->all;
327 },
328 3 ▶ finish => sub () { $iterator->finish; return },
Subroutine __ANON__: called CC=1 SCAR=0.0
329 1831 ▶ ended => sub () { return $iterator->ended },
Subroutine __ANON__: called CC=1 SCAR=0.0
330 # Whether there are ends to call, for a stage driving the feed.
331 315 ends => scalar grep { defined } @{ $run->{ends} },
332 );
333 }
334  
335 # Return the stages, in order, as a list, to splice into another
336 # pipeline or give to any function taking stages.
337 271 ▶sub stages ($self) {
Subroutine stages: called CC=1 SCAR=0.0
Pod documented
338 271 return @{ $self->{stages} };
339 }
340  
341 # What a pipeline is doing while its stages run.
342 my %running;
343  
344 # The @result of the run running, for Ekorn::Pipeline::Context: how
345 # many results it has made, and the original of the value going through
346 # its stages; undef outside a run, and in an end. Each loop localises it
347 # once a call, not once a value. A package variable, as a local of one
348 # costs a call about 33 ns, against 95 for a key of %running, which
349 # has none until it's localised; a lexical can't be localised, and
350 # setting one and putting it back by hand, for each value, cost as much.
351 32 our $CURRENT_RESULT; ## no critic (Variables::ProhibitPackageVars)
352  
353 # For code telling its stages about the value they're given: true while
354 # a flush runs, and while the values it gave go through the stages,
355 # rather than values from the source.
356 19 ▶sub ending () {
Subroutine ending: called CC=1 SCAR=0.0
Pod documented
357 19 return $running{ending};
358 }
359  
360 # For code telling its stages about the run: the context the run's
361 # source gave, if any, such as a statement's columns. A pushed run has
362 # none; an iterator used as a source passes on its run's.
363 63 ▶sub run_context () {
Subroutine run_context: called CC=1 SCAR=0.0
Pod documented
364 63 return $running{context};
365 }
366  
367 # Ekorn::Pipeline::Context's functions, defined here, as they read
368 # $CURRENT_RESULT, which is this file's to set; that module exports and
369 # documents them. Each has an empty prototype, as well as an empty
370 # signature, so a bare call is a term: result_number + 1 adds, and
371 # result_number // 'none' isn't read as a pattern. Under use v5.36, the
372 # () alone is a signature, which leaves a sub without a prototype.
373 211 ▶sub Ekorn::Pipeline::Context::result_number : prototype() () {
Subroutine result_number: called CC=2 SCAR=6.9
Pod documented
374 211 ▶ return $CURRENT_RESULT ? $CURRENT_RESULT->[0] + 1 : undef;
Branch: 1 branch
BranchTrueFalseTotal
$Ekorn::Pipeline::CURRENT_RESULT ? : 205 6 211
375 }
376  
377 # The original is the source's, if it gave an original part, directly
378 # or through an iterator read as a source, called only now (2026-0052);
379 # otherwise the run's own. A flush's values have none, either way.
380 249 ▶sub Ekorn::Pipeline::Context::result_original : prototype() () {
Subroutine result_original: called CC=5 SCAR=16.1
Pod documented
381 249 ▶ my($count, $original, $part) = $CURRENT_RESULT ? @$CURRENT_RESULT : ();
Branch: 1 branch
BranchTrueFalseTotal
$Ekorn::Pipeline::CURRENT_RESULT ? : 245 4 249
382 return
383 !defined $count || $running{ending} ? undef
384 249 ▶ : $part ? $part->()
Branch: 2 branches
BranchTrueFalseTotal
!defined($count) || $running{'ending'} ? : 21 228 249
$part ? : 36 192 228
Condition100%3/3
not defined $count or $running{"ending"}
l!l&&r!l&&!r
417228
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
385 : $original;
386 }
387  
388 # Return the step, the end, the context and the original part of a run
389 # of $source: from its EKORN_SOURCE, if it offers one; a code reference
390 # is its own step, with no end, context or original.
391 594 ▶sub _source_run ($source) {
Subroutine _source_run: called CC=6 SCAR=17.9
392 # As _is_source asks, without the call, as every run asks it.
393 594 ▶ return ($source) unless blessed($source) && $source->can('EKORN_SOURCE');
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($source) and $source->can("EKORN_SOURCE") 183 411 594
Condition66%2/3
builtin::blessed($source) and $source->can("EKORN_SOURCE")
!ll&&!rl&&r
1830411
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
394 411 my($step, $end, $context, $original)
395 = _parts([qw(step end context original)], $source->EKORN_SOURCE);
396 401 ▶ croakf
Branch: 1 branch
BranchTrueFalseTotal
unless Ref::Util::XS::is_coderef($step) and _code_or_none($end) 4 397 401
Condition100%3/3
Ref::Util::XS::is_coderef($step) and _code_or_none($end)
!ll&&!rl&&r
31397
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
397 'Expected the source to return a code reference, and optionally an end and a context'
398 unless is_coderef($step) && _code_or_none($end);
399 # A message of its own, so the one above stays as it was.
400 397 ▶ croakf 'Expected the source\'s original to be a code reference'
Branch: 1 branch
BranchTrueFalseTotal
unless _code_or_none($original) 1 396 397
401 unless _code_or_none($original);
402 396 return ($step, $end, $context, $original);
403 }
404  
405 # Return the parts a role method or a set-up returned as a list, in the
406 # order of the keys in @$order: as they are, if they're a list, or, if
407 # they're one plain hash reference, its values for those keys. Other
408 # keys are ignored.
409 2940 ▶sub _parts ($order, @returned) {
Subroutine _parts: called CC=3 SCAR=11.0
410 2940 ▶ 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]) 2259 681 2940
Condition100%3/3
@returned == 1 and Ref::Util::XS::is_plain_hashref($returned[0])
!ll&&!rl&&r
11491110681
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
411 681 return @{ $returned[0] }{@$order};
412 }
413  
414 # Return true if each of what's given is a code reference, or undefined.
415 3317 ▶sub _code_or_none (@things) {
Subroutine _code_or_none: called CC=2 SCAR=6.9
416 3317 ▶ return !grep { defined && !is_coderef($_) } @things;
Condition100%3/3
defined $_ && !Ref::Util::XS::is_coderef($_)
!ll&&!rl&&r
318516167
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
417 }
418  
419 # Return how a stage is named in a message: by its place, $name, and, if
420 # it's within a named pipeline, the innermost, by its name and its place
421 # in it, from the pipelines in @$within it came from, and $pipeline, the
422 # one being run.
423 1029 ▶sub _label ($pipeline, $name, $within) {
Subroutine _label: called CC=3 SCAR=11.0
424 1029 for my $in (reverse [$pipeline, $name], @$within) {
425 1048 my($outer, $place) = @$in;
426 1048 my $outer_name = $outer->name;
427 1048 ▶ return "stage #$name ($outer_name#$place)" if defined $outer_name;
Branch: 1 branch
BranchTrueFalseTotal
if defined $outer_name 14 1034 1048
428 }
429 1015 return "stage #$name";
430 }
431  
432 # Return the results of pushing @values through @$steps, each a plain
433 # step, as a run of them would give them, without a run's machinery:
434 # each value through every step before the next, what a step returns
435 # going on in order, a stop's values going on from the step after it,
436 # with nothing else, and a step that dies named as a run names it, by
437 # $labels, the steps' names and the pipelines each came from, as a
438 # pair. The values are given as a reference to an array, not copied.
439 # For a feed, $fed, if given, is the feed's: its first is set to how the
440 # run ended, at a stop or a death, and its second holds how many results
441 # its pushes have made; and the steps see the context of the code
442 # pushing.
443 578 ▶sub _push_steps ($pipeline, $steps, $labels, $fed, $values) {
Subroutine _push_steps: called CC=15 SCAR=27.1
444 # A pushed run has no context, and gives no values from a flush.
445 578 ▶ local $running{context} = undef unless $fed;
Branch: 1 branch
BranchTrueFalseTotal
unless $fed 398 180 578
446 578 local $running{ending} = undef;
447 # The run's results so far, and the original of the value going
448 # through the steps, for Ekorn::Pipeline::Context; a value waiting on
449 # @todo carries its original.
450 578 ▶ my @result = ($fed ? $fed->[1] : 0, undef);
Branch: 1 branch
BranchTrueFalseTotal
$fed ? : 180 398 578
451 578 local $CURRENT_RESULT = \@result;
452 578 my(@results, @todo, $n);
453 578 my($at, $more) = (0, 1);
454 # One try for as many values as go through without a stop, which is
455 # caught, and the values it carries go on from the step after it.
456 578 ▶ while ($more) {
Branch: 1 branch
BranchTrueFalseTotal
if ($more) 699 501 1200
457 699 try {
458 VALUE:
459 699 while (1) {
460 2152 my $value;
461 2152 ▶ if (@todo) { ($n, $value, $result[1]) = @{ pop @todo } }
Branch: 2 branches
BranchTrueFalseTotal
if (@todo) { } 380 1772 2152
elsif ($at < @$values) { } 1271 501 1772
462 elsif ($at < @$values) {
463 1271 $n = 0;
464 1271 $result[1] = $value = $values->[$at++];
465 }
466 501 else { $more = 0; last VALUE }
467 1651 ▶ while ($n < @$steps) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < @$steps) 1927 1241 3168
468 1927 local $_ = $value;
469 1927 my @values = $steps->[$n]->($value);
470 1729 $n++;
471 1729 ▶ next VALUE unless @values;
Branch: 1 branch
BranchTrueFalseTotal
unless @values 212 1517 1729
472 1517 $value = shift @values;
473 1517 ▶ push @todo, map { [$n, $_, $result[1]] } reverse @values if @values;
Branch: 1 branch
BranchTrueFalseTotal
if @values 255 1262 1517
474 }
475 1241 push @results, $value;
476 1241 $result[0]++;
477 }
478 }
479 catch ($e) {
480 198 ▶ if ($e isa 'Ekorn::Pipeline::Stop') {
Branch: 1 branch
BranchTrueFalseTotal
if ($e isa "Ekorn::Pipeline::Stop") 121 77 198
481 121 ▶ my(undef, @carried) = is_arrayref($e) ? @$e : ();
Branch: 1 branch
BranchTrueFalseTotal
Ref::Util::XS::is_arrayref($e) ? : 119 2 121
482 121 ($at, @todo) = (scalar @$values);
483 121 push @todo, map { [$n + 1, $_, $result[1]] } reverse @carried;
484 121 ▶ $fed->[0] = 'stopped' if $fed;
Branch: 1 branch
BranchTrueFalseTotal
if $fed 15 106 121
485 121 next;
486 }
487 77 ▶ $fed->[0] = 'died' if $fed;
Branch: 1 branch
BranchTrueFalseTotal
if $fed 11 66 77
488 77 ▶ croak $e if is_ref($e);
Branch: 1 branch
BranchTrueFalseTotal
if Ref::Util::XS::is_ref($e) 2 75 77
489 75 my($names, $within) = @$labels;
490 75 _failed(_label($pipeline, $names->[$n], $within->[$n]), $e);
491 }
492 }
493 501 ▶ $fed->[1] = $result[0] if $fed;
Branch: 1 branch
BranchTrueFalseTotal
if $fed 169 332 501
494 501 return @results;
495 }
496  
497 # Return the text of an end's error: a stop's own text, "Stop used
498 # outside a pipeline", would mislead, as the stop was in a run.
499 15 ▶sub _end_error ($error) {
Subroutine _end_error: called CC=2 SCAR=6.9
500 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
501 if $error isa 'Ekorn::Pipeline::Stop';
502 13 return "$error";
503 }
504  
505 # Call each end given, as [$label, $end, $how], in order. While the run
506 # is dying, an end that dies is reported as a warning naming it, and the
507 # rest are called. Otherwise, an end that dies tells every end after it
508 # "died", and its error is thrown once they've run.
509 1065 ▶sub _end_all ($ends, $dying) {
Subroutine _end_all: called CC=9 SCAR=22.0
510 # An end isn't a stage: it has no result.
511 1065 local $CURRENT_RESULT = undef;
512 1065 my($error, $failed);
513 1065 for my $call (@$ends) {
514 1158 my($label, $end, $how) = @$call;
515 1158 try {
516 1158 local $_ = undef;
517 1158 ▶ $end->($dying || defined $error ? 'died' : $how);
Branch: 1 branch
BranchTrueFalseTotal
$dying || defined $error ? : 124 1034 1158
Condition100%3/3
$dying or defined $error
l!l&&r!l&&!r
12131034
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
518 }
519 catch ($e) {
520 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
521 2 Carp::carp sprintf 'The end of %s died while the run was already dying: %s',
522 $label, _end_error($e) =~ s( \n \z )()xr;
523 }
524 else {
525 14 ($error, $failed) = ($e, $label);
526 }
527 }
528 }
529 1065 ▶ return unless defined $error;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $error 1051 14 1065
530 # A stop from an end is an error like any other: the run has ended.
531 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
532 13 return _failed("end of $failed", _end_error($error));
533 }
534  
535 # Die with the message of a part of a run that died, a stage, a flush
536 # or an end, as a run reports it: naming $what, at the line that called
537 # the run, the message it died with below, indented.
538 158 ▶sub _failed ($what, $message) {
Subroutine _failed: called CC=1 SCAR=0.0
539 158 my $at = Carp::shortmess() =~ s( \.? \n \z )()xr;
540 158 my $indented = $message =~ s( \n \z )()xr =~ s( ^ )( )xmgr;
541 158 die "Pipeline $what failed$at:\n$indented\n"; ## no critic (ErrorHandling::RequireCarping): placed by shortmess, above
542 }
543  
544 # Make the parts of a run from @$stages, named by @$names, into the
545 # lists in %$run: each part's step, its flush and its end, by its role
546 # method, and whether it's a sink. A gather's block may return a
547 # pipeline as its step, whose stages are made in its place, followed by
548 # the gather's flush and end, if any, on a stage of their own that
549 # passes values on.
550 1367 ▶sub _make_run ($stages, $names, $within, $run) {
Subroutine _make_run: called CC=25 SCAR=32.2
551 1367 for my $i (0 .. $#$stages) {
552 2550 my($stage, $name) = ($stages->[$i], $names->[$i]);
553 2550 ▶ my $role = _stage_role($stage) // '';
Condition100%2/2
_stage_role($stage) // ""
l!l
25491
MC/DC100%1/1
_stage_role($stage) // ""
_stage_role($stage)
Truth table100%2/2
A
A= _stage_role($stage)
Aresult
11
00
554 2550 my($step, $flush, $end);
555 2550 ▶ if ($stage isa 'Ekorn::Pipeline::Gather') {
Branch: 3 branches
BranchTrueFalseTotal
if ($stage isa 'Ekorn::Pipeline::Gather') { } 1556 994 2550
elsif ($role eq 'EKORN_SINK_ONE' or $role eq 'EKORN_SINK_MANY') { } 280 714 994
elsif ($role eq 'EKORN_STAGE') { } 713 1 714
556 1556 my @made = _parts([qw(step flush end)], $stage->EKORN_STAGE);
557 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
558 = @made && $made[0] isa 'Ekorn::Pipeline' ? _composed($made[0]) : ();
559 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
560 'Expected stage #%s, made with gather, to return a code reference or a pipeline, and optionally a flush and an end',
561 $name
562 unless (@made == 1 || @made == 2 || @made == 3)
563 && ($inner ? $inner_names : is_coderef($made[0]))
564 && _code_or_none(@made[1 .. $#made]);
565 1538 ($step, $flush, $end) = @made;
566 1538 ▶ if ($inner) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inner) 17 1521 1538
567 _make_run(
568 $inner,
569 25 [map { "$name.$_" } @$inner_names],
570 [
571 17 map { [
572 25 @{ $within->[$i] }, [$made[0], $inner_names->[$_]],
573 25 @{ $inner_within->[$_] },
574 ] } 0 .. $#$inner
575 ],
576 $run,
577 );
578 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
579 5 ▶ $step = sub { $_[0] }; # for each value: no signature
Subroutine __ANON__: called CC=1 SCAR=0.0
580 }
581 }
582 ▶ 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
166114714
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
583 280 ($step, $flush, $end) = _parts([qw(step flush end)], $stage->$role);
584 276 ▶ 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 271 276
Condition100%6/6
Ref::Util::XS::is_coderef($step) and Ref::Util::XS::is_coderef($flush) and _code_or_none($end)
!ll&&!rl&&r
41271
Ref::Util::XS::is_coderef($step) and Ref::Util::XS::is_coderef($flush)
!ll&&!rl&&r
13272
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
585 'Expected sink #%s to return a code reference, a flush, and optionally an end',
586 $name
587 unless is_coderef($step)
588 && is_coderef($flush)
589 && _code_or_none($end);
590 # What a sink's step returns is ignored, by the run, which knows
591 # it's a sink; a scalar sink's flush gives one value, undef if
592 # it has none.
593 271 my $result = $flush;
594 149 ▶ $flush = sub () { my @result = $result->(); return $result[0] }
Subroutine __ANON__: called CC=1 SCAR=0.0
595 271 ▶ if $role eq 'EKORN_SINK_ONE';
Branch: 1 branch
BranchTrueFalseTotal
if $role eq "EKORN_SINK_ONE" 157 114 271
596 }
597 elsif ($role eq 'EKORN_STAGE') {
598 713 ($step, $flush, $end) = _parts([qw(step flush end)], $stage->EKORN_STAGE);
599 713 ▶ croakf
Branch: 1 branch
BranchTrueFalseTotal
unless Ref::Util::XS::is_coderef($step) and _code_or_none($flush, $end) 5 708 713
Condition100%3/3
Ref::Util::XS::is_coderef($step) and _code_or_none($flush, $end)
!ll&&!rl&&r
32708
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
600 'Expected stage #%s to return a code reference, and optionally a flush and an end',
601 $name
602 unless is_coderef($step) && _code_or_none($flush, $end);
603 }
604 else {
605 1 $step = $stage;
606 }
607 2506 push @{ $run->{stages} }, $step;
608 2506 push @{ $run->{names} }, $name;
609 2506 push @{ $run->{within} }, $within->[$i];
610 2506 push @{ $run->{flushes} }, $flush;
611 2506 push @{ $run->{ends} }, $end;
612 2506 ▶ push @{ $run->{sinks} },
Condition100%3/3
$role eq 'EKORN_SINK_ONE' || $role eq 'EKORN_SINK_MANY'
l!l&&r!l&&!r
1571142235
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
613 $role eq 'EKORN_SINK_ONE' || $role eq 'EKORN_SINK_MANY';
614 }
615 1335 return;
616 }
617  
618 # Return an iterator handing out, one per call, the values from $source
619 # run through the stages in @$stages, named in messages by @$names.
620 594 ▶sub _iterator ($pipeline, $stages, $names, $within, $source) {
Subroutine _iterator: called CC=2 SCAR=6.9
621 594 my($pull, $source_end, $context, $original);
622 594 try {
623 594 ($pull, $source_end, $context, $original) = _source_run($source);
624 }
625 catch ($e) {
626 15 _set_up_died($pipeline, {ends => []}, undef, $e);
627 }
628 579 my $run = _made_run($pipeline, $stages, $names, $within, $source_end);
629 # The source's original part, if any, kept in the run.
630 577 $run->{original} = $original;
631 # A run with no flush, no end of a stage's, and no sink, as most are,
632 # takes the shorter way.
633 return _plain_iterator($pipeline, $run, $pull, $source_end, $context)
634 577 ▶ unless grep { defined } @{ $run->{flushes} }, @{ $run->{ends} };
Branch: 1 branch
BranchTrueFalseTotal
unless grep {defined $_;} @{$$run{"flushes"};}, @{$$run{"ends"};} 314 263 577
635 263 return _full_iterator($pipeline, $run, $pull, $source_end, $context);
636 }
637  
638 # Return the results of pushing @$values through @$stages, as a run of
639 # them gives them: once the stages are made for the run, by the plain
640 # loop if none has a flush or an end, as most haven't, so a stage made
641 # with gather costs no more a value than a plain one; otherwise by an
642 # iterator of the run. Its set-up is _iterator's, with no source to set
643 # up.
644 424 ▶sub _pushed ($pipeline, $stages, $names, $within, $values) {
Subroutine _pushed: called CC=2 SCAR=6.9
645 424 my $run = _made_run($pipeline, $stages, $names, $within);
646 return _push_steps(
647 $pipeline, $run->{stages}, [@$run{qw(names within)}], undef,
648 $values,
649 395 ▶ ) unless grep { defined } @{ $run->{flushes} }, @{ $run->{ends} };
Branch: 1 branch
BranchTrueFalseTotal
unless grep {defined $_;} @{$$run{"flushes"};}, @{$$run{"ends"};} 124 271 395
650 457 ▶ return _full_iterator($pipeline, $run, sub { splice @$values }, undef, undef)
Subroutine __ANON__: called CC=1 SCAR=0.0
651 271 ->all;
652 }
653  
654 # Return a run made from @$stages, named by @$names, as _make_run makes
655 # it. If a set-up dies, every end made by then, and $source_end, if
656 # given, is told the run died, and the error is thrown on.
657 1350 ▶sub _made_run ($pipeline, $stages, $names, $within, $source_end = undef) {
Subroutine _made_run: called CC=2 SCAR=6.9
658 1350 my %run = (
659 stages => [], names => [], within => [], flushes => [], ends => [],
660 sinks => [],
661 );
662 1350 try {
663 1350 _make_run($stages, $names, $within, \%run);
664 }
665 catch ($e) {
666 32 _set_up_died($pipeline, \%run, $source_end, $e);
667 }; # the ; ends the statement for Perl::Critic, which doesn't know try
668 1318 return \%run;
669 }
670  
671 # Tell each end of a run made by then, the source's first, that the run
672 # died, as a set-up died with $error, and throw it on.
673 47 ▶sub _set_up_died ($pipeline, $run, $source_end, $error) {
Subroutine _set_up_died: called CC=4 SCAR=13.9
674 _end_all(
675 [
676 ($source_end ? ['the source', $source_end, 'died'] : ()),
677 3 map { [_run_label($pipeline, $run, $_), $run->{ends}[$_], 'died'] }
678 47 ▶ grep { $run->{ends}[$_] } 0 .. $#{ $run->{ends} },
Branch: 1 branch
BranchTrueFalseTotal
$source_end ? : 2 45 47
679 ],
680 1,
681 );
682 # A set-up runs before the run has values, so has nothing to stop.
683 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
684 if $error isa 'Ekorn::Pipeline::Stop';
685 45 ▶ croak $error if is_ref($error);
Branch: 1 branch
BranchTrueFalseTotal
if Ref::Util::XS::is_ref($error) 1 44 45
686 44 die $error; ## no critic (ErrorHandling::RequireCarping): thrown again, as it was
687 }
688  
689 # Return an iterator of a run that isn't plain, made by _iterator or
690 # _pushed, from its source's step, end and context, and the original
691 # part, if any, in %$run.
692 #
693 # The run ends as soon as nothing in it can still act on a value: the
694 # source has run out or stopped, every flush has been called, and every
695 # value still to be handed out has passed the last stage; or when it's
696 # ended from outside, or dies. Then each end is called, the source's
697 # first, told how the run ended from where it stands: "died" if the run
698 # died; else "done" if its input ran out, and "stopped" if not.
699 #
700 # A feed's run has a hungry, true while more values may be pushed: a
701 # source with nothing now isn't done, and the iterator hands out
702 # nothing more until the next push. It has no context of its own: its
703 # stages and ends see the context of the code pushing.
704 846 ▶sub _full_iterator ($pipeline, $run, $pull, $source_end, $context) {
Subroutine _full_iterator: called CC=1 SCAR=0.0
705 846 my $hungry = $run->{hungry};
706 846 my @stages = @{ $run->{stages} };
707 846 my @flushes = @{ $run->{flushes} };
708 846 my @ends = @{ $run->{ends} };
709 846 my @sinks = @{ $run->{sinks} };
710 # The places of the stages with a flush, so finding the next needn't
711 # look at every stage.
712 846 my @flush_at = grep { $flushes[$_] } 0 .. $#stages;
713 # Each value waiting to go through a stage, as the stage's index, the
714 # value, and whether it came from a flush, the next on top. A value a
715 # stage returns on its own, as most are, carries on through the stages
716 # without waiting here. Once the source is done, the flushes are called
717 # in order, from the first not called or passed over by a stop.
718 my(
719 846 @todo, $done, $source_done, %flushed, @input_done, $finished, $outside,
720 $ended, $spent, $dropped,
721 );
722 846 my $next_flush = 0;
723 # How many values wait at each stage's index, kept as @todo changes, so
724 # asking whether any wait before a stage needn't scan @todo: a flush
725 # can put many values there at once.
726 846 my @waiting_at;
727 # The run's results so far, the original of the value going through
728 # the stages, and the source's original part, if it gave one, for
729 # Ekorn::Pipeline::Context, the current result while $next runs; a
730 # value waiting on @todo carries its original, and a flush's values
731 # have none.
732 846 my @result = (0, undef, $run->{original});
733 # Values a source's step gave, still to go through the stages from the
734 # first, held as they came: @todo's values go first, being further on.
735 846 my @from_source;
736 # Put @values on @todo, to go on from stage $n, the first on top.
737 324 ▶ my $wait = sub ($n, $from_flush, @values) {
Subroutine __ANON__: called CC=1 SCAR=0.0
738 324 push @todo, map { [$n, $_, $from_flush, $result[1]] } reverse @values;
739 324 $waiting_at[$n] += @values;
740 324 return;
741 846 };
742 # Whether any value waits at a stage up to $upto. The run has a stage,
743 # so values from the source wait at the first.
744 799 ▶ my $waiting_upto = sub ($upto) {
Subroutine __ANON__: called CC=4 SCAR=13.9
745 799 ▶ return 1 if @from_source;
Branch: 1 branch
BranchTrueFalseTotal
if @from_source 10 789 799
746 789 ▶ $waiting_at[$_] && return 1 for 0 .. $upto;
Branch: 1 branch
BranchTrueFalseTotal
$waiting_at[$_] and return 1 32 1908 1940
747 757 return 0;
748 846 };
749 # How the run ended, as its caller asks: whether the source was read
750 # to its end, "stopped" if values still to be handed out were dropped,
751 # or "died" if anything died, an end included.
752 1695 ▶ my $finish = sub ($died) {
Subroutine __ANON__: called CC=9 SCAR=22.0
753 1695 ▶ return if $finished++;
Branch: 1 branch
BranchTrueFalseTotal
if $finished++ 849 846 1695
754 846 ▶ $ended
Branch: 3 branches
BranchTrueFalseTotal
$died ? : 73 773 846
$dropped ? : 10 763 773
$source_done ? : 628 135 763
755 = $died ? 'died'
756 : $dropped ? 'stopped'
757 : $source_done ? 'done'
758 : 'stopped';
759 846 my @calls;
760 846 ▶ push @calls, ['the source', $source_end, $source_done ? 'done' : 'stopped']
Branch: 2 branches
BranchTrueFalseTotal
if $source_end 100 746 846
$source_done ? : 56 44 100
761 if $source_end;
762 push @calls,
763 map { [
764 881 ▶ _run_label($pipeline, $run, $_), $ends[$_],
Branch: 1 branch
BranchTrueFalseTotal
$input_done[$_] ? : 717 164 881
765 $input_done[$_] ? 'done' : 'stopped',
766 ] }
767 846 grep { $ends[$_] } 0 .. $#ends;
768 846 ▶ local $running{context} = $context unless $hungry;
Branch: 1 branch
BranchTrueFalseTotal
unless $hungry 531 315 846
769 846 try {
770 846 _end_all(\@calls, $died);
771 }
772 catch ($e) {
773 # An end died: the run died, and hands out nothing more.
774 11 ($ended, $spent, @todo, @waiting_at, @from_source) = ('died', 1);
775 11 die $e; ## no critic (ErrorHandling::RequireCarping): thrown again, as it was
776 }
777 835 return;
778 846 };
779 # The index of the next flush to call, if any: none from $next_flush
780 # on has been called.
781 2219 ▶ my $flush_at = sub () {
Subroutine __ANON__: called CC=3 SCAR=11.0
782 2219 for my $n (@flush_at) {
783 2069 ▶ return $n if $n >= $next_flush;
Branch: 1 branch
BranchTrueFalseTotal
if $n >= $next_flush 689 1380 2069
784 }
785 1530 return;
786 846 };
787 # Every part from the next flush up to $upto has had all its input.
788 1412 ▶ my $input_ran_out = sub ($upto) {
Subroutine __ANON__: called CC=2 SCAR=6.9
789 1412 $input_done[$_] = 1 for $next_flush .. $upto;
790 1412 return;
791 846 };
792 # At a stop at stage $n, with nothing more to come from upstream,
793 # every stage up to it whose input had run out: none of its values
794 # still waiting, and no flush before it still to be called.
795 33 ▶ my $input_ran_out_at_stop = sub ($n) {
Subroutine __ANON__: called CC=4 SCAR=13.9
796 33 for my $stage ($next_flush .. $n) {
797 9 ▶ last if $waiting_upto->($stage);
Branch: 1 branch
BranchTrueFalseTotal
if &$waiting_upto($stage) 4 5 9
798 5 ▶ last if grep { $flushes[$_] } $next_flush .. $stage - 1;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$flushes[$_];} $next_flush .. $stage - 1 1 4 5
799 4 $input_done[$stage] = 1;
800 }
801 33 return;
802 846 };
803 # End the run if nothing in it can still act on a value. Called only
804 # once the source is done.
805 1309 ▶ my $end_if_ended = sub () {
Subroutine __ANON__: called CC=4 SCAR=13.9
806 1309 ▶ return if $finished || defined $flush_at->();
Branch: 1 branch
BranchTrueFalseTotal
if $finished or defined &$flush_at() 519 790 1309
Condition100%3/3
$finished or defined &$flush_at()
l!l&&r!l&&!r
49029790
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
807 790 ▶ return if $waiting_upto->($#stages);
Branch: 1 branch
BranchTrueFalseTotal
if &$waiting_upto($#stages) 38 752 790
808 752 $input_ran_out->($#stages);
809 752 $finish->(0);
810 741 return;
811 846 };
812 # Where all collects every value still to come, while it does: the
813 # loop pushes them onto it, with no return between values. Otherwise,
814 # the loop returns the next value.
815 846 my $into;
816 # Where all collects it, the values after the first that the last
817 # part returns at once, a list sink's result above all, kept to go
818 # into @$into straight after the first, in order, rather than each
819 # wrapped and waiting on @todo: past the last part, nothing more is
820 # done to them. Only while all collects, as $next gives one at a
821 # time.
822 my @straight;
823 1466 ▶ my $next = sub () {
Subroutine __ANON__: called CC=48 SCAR=38.7
824 1466 ▶ return if $outside;
Branch: 1 branch
BranchTrueFalseTotal
if $outside 1 1465 1466
825 1465 local $CURRENT_RESULT = \@result;
826 1465 while (1) {
827 5217 ▶ if ($into) {
Branch: 1 branch
BranchTrueFalseTotal
if ($into) 4821 396 5217
828 # All takes at once the values already at the end, such as a
829 # list sink's result. A run with no stages takes the shorter
830 # way, so this one has a stage.
831 4821 ▶ if (@todo && $todo[-1][0] == @stages) {
Branch: 1 branch
BranchTrueFalseTotal
if (@todo and $todo[-1][0] == @stages) 15 4806 4821
Condition100%3/3
@todo and $todo[-1][0] == @stages
!ll&&!rl&&r
451029615
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
832 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
833 17 push @$into, (pop @todo)->[1];
834 17 $waiting_at[@stages]--;
835 17 $result[0]++;
836 }
837 15 next;
838 }
839 }
840 5202 my($n, $value, $from_flush, $flush);
841 5202 ▶ if (@todo) {
Branch: 3 branches
BranchTrueFalseTotal
if (@todo) { } 359 4843 5202
elsif (@from_source) { } 912 3931 4843
elsif (not $done) { } 2531 1400 3931
842 359 ($n, $value, $from_flush, $result[1]) = @{ pop @todo };
843 359 $waiting_at[$n]--;
844 }
845 elsif (@from_source) {
846 912 $n = 0;
847 912 $result[1] = $value = shift @from_source;
848 }
849 elsif (!$done) {
850 # Straight onto @from_source, which is empty here.
851 2531 try {
852 2531 @from_source = $pull->();
853 }
854 catch ($e) {
855 24 ▶ if ($e isa 'Ekorn::Pipeline::Stop') {
Branch: 1 branch
BranchTrueFalseTotal
if ($e isa "Ekorn::Pipeline::Stop") 15 9 24
856 # A source that stops has had enough: what it
857 # carries is its last, and it's done.
858 15 ▶ my(undef, @carried) = is_arrayref($e) ? @$e : ();
Branch: 1 branch
BranchTrueFalseTotal
Ref::Util::XS::is_arrayref($e) ? : 11 4 15
859 15 ($done, $source_done) = (1, 1);
860 15 @from_source = @carried;
861 15 next;
862 }
863 9 ($done, $next_flush, @todo, @waiting_at, @from_source)
864 = (1, scalar @stages);
865 9 $finish->(1);
866 9 ▶ croak $e if is_ref($e);
Branch: 1 branch
BranchTrueFalseTotal
if Ref::Util::XS::is_ref($e) 5 4 9
867 4 die $e; ## no critic (ErrorHandling::RequireCarping): thrown again, as it was
868 }
869 2507 ▶ unless (@from_source) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@from_source) 1128 1379 2507
870 # Nothing yet: the feed's next push carries on.
871 1128 ▶ return if $hungry && $hungry->();
Branch: 1 branch
BranchTrueFalseTotal
if $hungry and &$hungry() 481 647 1128
Condition100%3/3
$hungry and &$hungry()
!ll&&!rl&&r
390257481
MC/DC100%2/2
$hungry and &$hungry()
$hungry &$hungry()
Truth table100%3/3
A and B
A= $hungryB= &$hungry()
ABresult
0X0
100
111
872 647 ($done, $source_done) = (1, 1);
873 647 next;
874 }
875 1379 $n = 0;
876 1379 $result[1] = $value = shift @from_source;
877 }
878 else {
879 1400 $n = $flush_at->();
880 1400 ▶ unless (defined $n) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $n) 740 660 1400
881 740 $end_if_ended->();
882 737 last;
883 }
884 660 $input_ran_out->($n);
885 660 $flushed{$n} = 1;
886 660 ($next_flush, $flush, $from_flush) = ($n + 1, $flushes[$n], 1);
887 660 $result[1] = undef;
888 }
889 3310 ▶ if ($n == @stages) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n == @stages) 28 3282 3310
890 # Only while values are handed out one at a time: all takes
891 # them from the top of the loop. Nothing can end while the
892 # source is still being read.
893 28 $result[0]++;
894 28 ▶ $end_if_ended->() if $done;
Branch: 1 branch
BranchTrueFalseTotal
if $done 21 7 28
895 28 return $value;
896 }
897 # Set only when it changes, as most values come from the source,
898 # and most runs, and the code running them, have no context.
899 local $running{ending} = $from_flush
900 3282 ▶ if $from_flush || $running{ending};
Branch: 1 branch
BranchTrueFalseTotal
if $from_flush or $running{"ending"} 764 2518 3282
Condition66%2/3
$from_flush or $running{"ending"}
l!l&&r!l&&!r
76402518
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
901 # A feed's run takes the context of the code pushing, so it's
902 # asked last, as most runs have none to set.
903 local $running{context} = $context
904 3282 ▶ if defined $context || (defined $running{context} && !$hungry);
Branch: 1 branch
BranchTrueFalseTotal
if defined $context or defined $running{"context"} and not $hungry 33 3249 3282
Condition100%6/6
defined $context or defined $running{"context"} and not $hungry
l!l&&r!l&&!r
3213249
defined $running{"context"} and not $hungry
!ll&&!rl&&r
3234151
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
905 3282 my $kept = 1;
906 3282 try {
907 3282 ▶ if ($flush) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flush) 660 2622 3282
908 660 local $_ = undef;
909 660 my @values = $flush->();
910 603 $n++;
911 603 $kept = @values;
912 603 $value = shift @values;
913 603 ▶ if (@values) {
Branch: 1 branch
BranchTrueFalseTotal
if (@values) 84 519 603
914 84 ▶ if ($into && $n == @stages) { @straight = @values }
Branch: 1 branch
BranchTrueFalseTotal
if ($into and $n == @stages) { } 60 24 84
Condition100%3/3
$into and $n == @stages
!ll&&!rl&&r
17760
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
915 24 else { $wait->($n, 1, @values) }
916 }
917 }
918 3225 ▶ while ($kept && $n < @stages) {
Branch: 1 branch
BranchTrueFalseTotal
if ($kept and $n < @stages) 5198 1427 6625
Condition100%3/3
$kept and $n < @stages
!ll&&!rl&&r
12812995198
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
919 5198 local $_ = $value;
920 my @values
921 = $sinks[$n]
922 5198 ▶ ? do { $stages[$n]->($value); $result[0]++; () }
Branch: 1 branch
BranchTrueFalseTotal
$sinks[$n] ? : 594 4604 5198
923 : $stages[$n]->($value);
924 5034 ▶ last if $outside;
Branch: 1 branch
BranchTrueFalseTotal
if $outside 5 5029 5034
925 5029 $n++;
926 5029 ▶ unless (@values) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@values) 1629 3400 5029
927 1629 $kept = 0;
928 1629 last;
929 }
930 3400 $value = shift @values;
931 3400 ▶ if (@values) {
Branch: 1 branch
BranchTrueFalseTotal
if (@values) 168 3232 3400
932 168 ▶ if ($into && $n == @stages) { @straight = @values }
Branch: 1 branch
BranchTrueFalseTotal
if ($into and $n == @stages) { } 25 143 168
Condition100%3/3
$into and $n == @stages
!ll&&!rl&&r
2411925
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 143 else { $wait->($n, $from_flush, @values) }
934 }
935 }
936 }
937 catch ($e) {
938 221 ▶ if ($e isa 'Ekorn::Pipeline::Stop') {
Branch: 1 branch
BranchTrueFalseTotal
if ($e isa "Ekorn::Pipeline::Stop") 157 64 221
939 # A stop made by throw is an array; one made otherwise
940 # carries nothing. A sink that stops the run still
941 # gives its result.
942 157 ▶ my(undef, @values) = is_arrayref($e) ? @$e : ();
Branch: 1 branch
BranchTrueFalseTotal
Ref::Util::XS::is_arrayref($e) ? : 152 5 157
943 157 ▶ $input_ran_out_at_stop->($n) if $done;
Branch: 1 branch
BranchTrueFalseTotal
if $done 33 124 157
944 157 $done = 1;
945 157 ▶ my $from = $sinks[$n] && !$flushed{$n} ? $n : $n + 1;
Branch: 1 branch
BranchTrueFalseTotal
$sinks[$n] && !$flushed{$n} ? : 44 113 157
Condition100%3/3
$sinks[$n] and not $flushed{$n}
!ll&&!rl&&r
112144
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
946 157 ▶ $next_flush = $from if $next_flush < $from;
Branch: 1 branch
BranchTrueFalseTotal
if $next_flush < $from 105 52 157
947 157 (@todo, @waiting_at, @from_source) = ();
948 157 $wait->($n + 1, $from_flush, @values);
949 157 next;
950 }
951 # A stage or a flush that dies ends the run: nothing waiting
952 # goes on, and every end is told it died.
953 64 ($done, $next_flush, @todo, @waiting_at, @from_source)
954 = (1, scalar @stages);
955 64 $finish->(1);
956 64 ▶ croak $e if is_ref($e);
Branch: 1 branch
BranchTrueFalseTotal
if Ref::Util::XS::is_ref($e) 6 58 64
957 58 _failed(_run_label($pipeline, $run, $n), $e);
958 }
959 3061 ▶ return if $outside;
Branch: 1 branch
BranchTrueFalseTotal
if $outside 5 3056 3061
960 3056 ▶ if ($kept) {
Branch: 1 branch
BranchTrueFalseTotal
if ($kept) 1299 1757 3056
961 1299 $result[0]++;
962 1299 ▶ $end_if_ended->() if $done;
Branch: 1 branch
BranchTrueFalseTotal
if $done 548 751 1299
963 1291 ▶ return $value unless $into;
Branch: 1 branch
BranchTrueFalseTotal
unless $into 130 1161 1291
964 1161 push @$into, $value;
965 1161 ▶ if (@straight) {
Branch: 1 branch
BranchTrueFalseTotal
if (@straight) 85 1076 1161
966 85 $result[0] += @straight;
967 85 push @$into, splice @straight;
968 }
969 }
970 }
971 # Everything has been handed out: the iterator is spent.
972 737 $spent = 1;
973 737 return;
974 846 };
975 # Every value the run has still to give, as $next would give them one
976 # at a time, but in one call of it.
977 1235 ▶ my $all = sub () {
Subroutine __ANON__: called CC=1 SCAR=0.0
978 1235 my @all;
979 1235 $into = \@all;
980 1235 try {
981 1235 $next->();
982 }
983 catch ($e) {
984 67 $into = undef;
985 67 die $e; ## no critic (ErrorHandling::RequireCarping): thrown again, as it was
986 }
987 1168 $into = undef;
988 1168 return @all;
989 846 };
990 # Ended from outside, by the iterator's finish or as it's let go, the
991 # run stops: nothing more is pulled, and nothing waiting goes on.
992 870 ▶ my $end_run = sub () {
Subroutine __ANON__: called CC=4 SCAR=13.9
993 870 $outside = 1;
994 # A run that died has nothing waiting: its values were dropped then.
995 870 ▶ $dropped = 1 if @todo || @from_source;
Branch: 1 branch
BranchTrueFalseTotal
if @todo or @from_source 13 857 870
Condition100%3/3
@todo or @from_source
l!l&&r!l&&!r
58857
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
996 870 ($done, $next_flush, @todo, @waiting_at, @from_source)
997 = (1, scalar @stages);
998 870 $finish->(0);
999 870 ▶ $ended = 'stopped' if $dropped;
Branch: 1 branch
BranchTrueFalseTotal
if $dropped 26 844 870
1000 870 return;
1001 846 };
1002 # How the run ended, held back until the iterator has nothing more to
1003 # give, so it's said once, and doesn't change. It's the iterator's
1004 # spent too: the run has ended, and so is said to, once it has.
1005 2003 ▶ my $ended_once = sub () {
Subroutine __ANON__: called CC=5 SCAR=16.1
1006 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
1007 1492 return;
1008 846 };
1009 return Ekorn::Pipeline::Iterator->new(
1010 $next, $end_run, $context, $ended_once, $ended_once, $all,
1011 $run->{original},
1012 846 );
1013 }
1014  
1015 # Return how stage $n of a run made into %$run, of $pipeline, is named in
1016 # a message, as _label names it.
1017 954 ▶sub _run_label ($pipeline, $run, $n) {
Subroutine _run_label: called CC=1 SCAR=0.0
1018 954 return _label($pipeline, $run->{names}[$n], $run->{within}[$n]);
1019 }
1020  
1021 # Return an iterator, as _iterator does, for a run made into %$run with
1022 # no flush, no end of a stage's, and no sink, so with none of their
1023 # bookkeeping: each value through every step before the next, what a
1024 # step returns going on in order, and a stop's values going on from the
1025 # step after it. The run ends, as _iterator's does, once the source is
1026 # done and no value waits before the last step, and then the source's
1027 # end, its only one, is called.
1028 314 ▶sub _plain_iterator ($pipeline, $run, $pull, $source_end, $context) {
Subroutine _plain_iterator: called CC=1 SCAR=0.0
1029 314 my @steps = @{ $run->{stages} };
1030 # Each value waiting to go through a step, as the step's index and the
1031 # value, the next on top, so in order of index from the bottom: the
1032 # lowest is at the bottom. Values a source's step gave, still to go
1033 # through the steps from the first, are held as they came.
1034 314 my(@todo, @from_source);
1035 314 my($done, $source_done, $finished, $outside, $ended, $spent, $dropped, $into);
1036 # Where all collects it, the values after the first that the last step
1037 # returns at once, kept to go into @$into straight after the first,
1038 # rather than each wrapped and waiting on @todo, as _full_iterator's
1039 # are.
1040 314 my @straight;
1041 # The run's results so far, the original of the value going through
1042 # the steps, and the source's original part, if it gave one, for
1043 # Ekorn::Pipeline::Context, the current result while $next runs; a
1044 # value waiting on @todo carries its original.
1045 314 my @result = (0, undef, $run->{original});
1046 667 ▶ my $finish = sub ($died) {
Subroutine __ANON__: called CC=6 SCAR=17.9
1047 667 ▶ return if $finished++;
Branch: 1 branch
BranchTrueFalseTotal
if $finished++ 353 314 667
1048 # Values dropped from outside make it stopped, as end_run says.
1049 314 ▶ $ended = $died ? 'died' : $source_done ? 'done' : 'stopped';
Branch: 2 branches
BranchTrueFalseTotal
$died ? : 31 283 314
$source_done ? : 220 63 283
1050 314 ▶ return unless $source_end;
Branch: 1 branch
BranchTrueFalseTotal
unless $source_end 142 172 314
1051 172 local $running{context} = $context;
1052 172 try {
1053 172 ▶ _end_all(
Branch: 1 branch
BranchTrueFalseTotal
$source_done ? : 127 45 172
1054 [['the source', $source_end, $source_done ? 'done' : 'stopped']],
1055 $died,
1056 );
1057 }
1058 catch ($e) {
1059 # The end died: the run died, and hands out nothing more.
1060 3 ($ended, $spent, @todo, @from_source) = ('died', 1);
1061 3 die $e; ## no critic (ErrorHandling::RequireCarping): thrown again, as it was
1062 }
1063 169 return;
1064 314 };
1065 504 ▶ my $next = sub () {
Subroutine __ANON__: called CC=39 SCAR=36.6
1066 504 ▶ return if $outside;
Branch: 1 branch
BranchTrueFalseTotal
if $outside 3 501 504
1067 501 local $CURRENT_RESULT = \@result;
1068 501 while (1) {
1069 1196 ▶ if ($into) {
Branch: 1 branch
BranchTrueFalseTotal
if ($into) 821 375 1196
1070 # All takes at once a step's values where there are no
1071 # steps, and the values already at the end.
1072 821 ▶ if (!@steps && @from_source) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @steps and @from_source) 29 792 821
Condition100%3/3
not @steps and @from_source
!ll&&!rl&&r
38940329
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
1073 29 $result[0] += @from_source;
1074 29 push @$into, splice @from_source;
1075 29 next;
1076 }
1077 792 ▶ if (@todo && $todo[-1][0] == @steps) {
Branch: 1 branch
BranchTrueFalseTotal
if (@todo and $todo[-1][0] == @steps) 16 776 792
Condition100%3/3
@todo and $todo[-1][0] == @steps
!ll&&!rl&&r
7621416
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
1078 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
1079 17 push @$into, (pop @todo)->[1];
1080 17 $result[0]++;
1081 }
1082 16 next;
1083 }
1084 }
1085 1151 my($n, $value);
1086 1151 ▶ if (@todo) {
Branch: 3 branches
BranchTrueFalseTotal
if (@todo) { } 52 1099 1151
elsif (@from_source) { } 28 1071 1099
elsif (not $done) { } 804 267 1071
1087 52 ($n, $value, $result[1]) = @{ pop @todo };
1088 }
1089 elsif (@from_source) {
1090 28 $n = 0;
1091 28 $result[1] = $value = shift @from_source;
1092 }
1093 elsif (!$done) {
1094 804 try {
1095 804 @from_source = $pull->();
1096 }
1097 catch ($e) {
1098 28 ▶ if ($e isa 'Ekorn::Pipeline::Stop') {
Branch: 1 branch
BranchTrueFalseTotal
if ($e isa "Ekorn::Pipeline::Stop") 11 17 28
1099 11 ▶ my(undef, @carried) = is_arrayref($e) ? @$e : ();
Branch: 1 branch
BranchTrueFalseTotal
Ref::Util::XS::is_arrayref($e) ? : 8 3 11
1100 11 ($done, $source_done) = (1, 1);
1101 11 @from_source = @carried;
1102 11 next;
1103 }
1104 17 ($done, @todo, @from_source) = (1);
1105 17 $finish->(1);
1106 17 ▶ croak $e if is_ref($e);
Branch: 1 branch
BranchTrueFalseTotal
if Ref::Util::XS::is_ref($e) 2 15 17
1107 15 die $e; ## no critic (ErrorHandling::RequireCarping): thrown again, as it was
1108 }
1109 776 ▶ unless (@from_source) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@from_source) 209 567 776
1110 209 ($done, $source_done) = (1, 1);
1111 209 next;
1112 }
1113 567 $n = 0;
1114 567 $result[1] = $value = shift @from_source;
1115 }
1116 else {
1117 # Nothing waits, and the source is done.
1118 267 $finish->(0);
1119 264 last;
1120 }
1121 647 ▶ if ($n < @steps) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < @steps) 400 247 647
1122 400 ▶ local $running{ending} = undef if $running{ending};
Branch: 1 branch
BranchTrueFalseTotal
if $running{"ending"} 1 399 400
1123 local $running{context} = $context
1124 400 ▶ if defined $context || defined $running{context};
Branch: 1 branch
BranchTrueFalseTotal
if defined $context or defined $running{"context"} 40 360 400
Condition66%2/3
defined $context or defined $running{"context"}
l!l&&r!l&&!r
400360
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
1125 400 my $kept = 1;
1126 400 try {
1127 400 ▶ while ($n < @steps) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < @steps) 498 322 820
1128 498 local $_ = $value;
1129 498 my @values = $steps[$n]->($value);
1130 443 ▶ last if $outside;
Branch: 1 branch
BranchTrueFalseTotal
if $outside 2 441 443
1131 441 $n++;
1132 441 ▶ unless (@values) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@values) 21 420 441
1133 21 $kept = 0;
1134 21 last;
1135 }
1136 420 $value = shift @values;
1137 420 ▶ if (@values) {
Branch: 1 branch
BranchTrueFalseTotal
if (@values) 46 374 420
1138 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
1139 else {
1140 25 push @todo, map { [$n, $_, $result[1]] } reverse @values;
1141 }
1142 }
1143 }
1144 }
1145 catch ($e) {
1146 55 ▶ if ($e isa 'Ekorn::Pipeline::Stop') {
Branch: 1 branch
BranchTrueFalseTotal
if ($e isa "Ekorn::Pipeline::Stop") 41 14 55
1147 41 ▶ my(undef, @carried) = is_arrayref($e) ? @$e : ();
Branch: 1 branch
BranchTrueFalseTotal
Ref::Util::XS::is_arrayref($e) ? : 39 2 41
1148 41 $done = 1;
1149 41 @from_source = ();
1150 41 @todo = map { [$n + 1, $_, $result[1]] } reverse @carried;
1151 41 next;
1152 }
1153 14 ($done, @todo, @from_source) = (1);
1154 14 $finish->(1);
1155 14 ▶ croak $e if is_ref($e);
Branch: 1 branch
BranchTrueFalseTotal
if Ref::Util::XS::is_ref($e) 2 12 14
1156 12 _failed(_run_label($pipeline, $run, $n), $e);
1157 }
1158 345 ▶ return if $outside;
Branch: 1 branch
BranchTrueFalseTotal
if $outside 2 343 345
1159 343 ▶ next unless $kept;
Branch: 1 branch
BranchTrueFalseTotal
unless $kept 21 322 343
1160 }
1161 # The run ends once nothing waits before the last step: values
1162 # from the source wait before the first, if there is one.
1163 569 ▶ $finish->(0)
Branch: 1 branch
BranchTrueFalseTotal
if $done and not $finished and not @from_source && @steps and not @todo && $todo[0][0] < @steps 23 546 569
Condition100%15/15
$done and not $finished and not @from_source && @steps and not @todo && $todo[0][0] < @steps
!ll&&!rl&&r
540623
$done and not $finished and not @from_source && @steps
!ll&&!rl&&r
536429
$done and not $finished
!ll&&!rl&&r
532433
@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
1164 if $done
1165 && !$finished
1166 && !(@from_source && @steps)
1167 && !(@todo && $todo[0][0] < @steps);
1168 569 $result[0]++;
1169 569 ▶ return $value unless $into;
Branch: 1 branch
BranchTrueFalseTotal
unless $into 201 368 569
1170 368 push @$into, $value;
1171 368 ▶ if (@straight) {
Branch: 1 branch
BranchTrueFalseTotal
if (@straight) 21 347 368
1172 21 $result[0] += @straight;
1173 21 push @$into, splice @straight;
1174 }
1175 }
1176 # Everything has been handed out: the iterator is spent.
1177 264 $spent = 1;
1178 264 return;
1179 314 };
1180 207 ▶ my $all = sub () {
Subroutine __ANON__: called CC=1 SCAR=0.0
1181 207 my @all;
1182 207 $into = \@all;
1183 207 try {
1184 207 $next->();
1185 }
1186 catch ($e) {
1187 17 $into = undef;
1188 17 die $e; ## no critic (ErrorHandling::RequireCarping): thrown again, as it was
1189 }
1190 190 $into = undef;
1191 190 return @all;
1192 314 };
1193 346 ▶ my $end_run = sub () {
Subroutine __ANON__: called CC=4 SCAR=13.9
1194 346 $outside = 1;
1195 346 ▶ $dropped = 1 if @todo || @from_source;
Branch: 1 branch
BranchTrueFalseTotal
if @todo or @from_source 8 338 346
Condition100%3/3
@todo or @from_source
l!l&&r!l&&!r
17338
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
1196 346 ($done, @todo, @from_source) = (1);
1197 346 $finish->(0);
1198 346 ▶ $ended = 'stopped' if $dropped;
Branch: 1 branch
BranchTrueFalseTotal
if $dropped 14 332 346
1199 346 return;
1200 314 };
1201 95 ▶ my $ended_once = sub () {
Subroutine __ANON__: called CC=5 SCAR=16.1
1202 95 ▶ return $ended if $spent || $outside || ($ended // '') eq 'died';
Branch: 1 branch
BranchTrueFalseTotal
if $spent or $outside or ($ended // "") eq "died" 77 18 95
Condition100%8/8
$spent or $outside or ($ended // "") eq "died"
l!l&&r!l&&!r
661118
$spent or $outside
l!l&&r!l&&!r
561029
$ended // ""
l!l
1118
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
1203 18 return;
1204 314 };
1205 return Ekorn::Pipeline::Iterator->new(
1206 $next, $end_run, $context, $ended_once, $ended_once, $all,
1207 $run->{original},
1208 314 );
1209 }
1210  
1211 32 1;
1212 __END__
↑ Summary Pipeline/Sugar.pm »