Pipeline/Operators.pm

filter: Statementstmt 100% 103 / 103 Branchbran 100% 22 / 22 Conditioncond 100% 12 / 12 MC/DCmcdc 100% 7 / 7 Subroutinesub 100% 29 / 29 Podpod 100% 2 / 2 totaltotal 100% 175 / 175 CC 17 SCAR 15.5
CC
17
Cov
100%
chain
13.0
_check_in_force
3.0
drain
2.0
CRAP 4.7
« Pipeline/Sugar.pm ↑ Summary Pipeline/JSON.pm »
1 3 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package Ekorn::Pipeline::Operators;
4  
5 3 ▶use experimental qw(builtin);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use builtin qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Ekorn::Pipeline qw(is_sink is_stage);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Ekorn::Pipeline::Errors qw(croakf);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Ekorn::Pipeline::Gather qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Ekorn::Pipeline::Sink qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Ekorn::Pipeline::Source qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Ekorn::Pipeline::Stage qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 # Croak at the line that called Ekorn::Pipeline.
16 3 our @CARP_NOT = @Ekorn::Pipeline::Errors::INTERNAL;
17  
18 # The key in %^H marking the scopes where the operators are in force.
19 3 my $IN_FORCE = 'Ekorn::Pipeline::Operators';
20  
21 # Put the operators in force for the rest of the scope being compiled:
22 # they work on Ekorn's types only in code that says so.
23 2 ▶sub import ($class, @) {
Subroutine import: called CC=1 SCAR=0.0
24 2 $^H{$IN_FORCE} = 1; ## no critic (Variables::RequireLocalizedPunctuationVars): a pragma's import sets %^H for the scope being compiled
25 2 return;
26 }
27  
28  
29 # Take the operators out of force for the rest of the scope.
30 2 ▶sub unimport ($class, @) {
Subroutine unimport: called CC=1 SCAR=0.0
31 2 delete $^H{$IN_FORCE};
32 2 return;
33 }
34  
35  
36 # Croak unless the operators are in force where the line that used one
37 # was compiled.
38 52 ▶sub _check_in_force ($operator) {
Subroutine _check_in_force: called CC=3 SCAR=11.0
39 52 my $hints = (caller 1)[10];
40 croakf 'Expected use Ekorn::Pipeline::Operators in scope, for %s', $operator
41 52 ▶ unless $hints && $hints->{$IN_FORCE};
Branch: 1 branch
BranchTrueFalseTotal
unless $hints and $hints->{$IN_FORCE} 5 47 52
Condition100%3/3
$hints and $hints->{$IN_FORCE}
!ll&&!rl&&r
2347
MC/DC100%2/2
$hints and $hints->{$IN_FORCE}
$hints $hints->{$IN_FORCE}
Truth table100%3/3
A and B
A= $hintsB= $hints->{$IN_FORCE}
ABresult
0X0
100
111
42 47 return;
43 }
44  
45 # The | of two parts. From a source: with a scalar sink, the sink's
46 # result, at once; with a list sink, its list, as an array reference;
47 # with a stage, a chain, a source of the values through it. From a
48 # stage: a chain of stages, a pipeline with |. A class of sources of its
49 # own can overload | with this, to chain as Ekorn's do.
50 45 ▶sub chain ($before, $after, $swapped = undef, @) {
Subroutine chain: called CC=13 SCAR=25.6
Pod documented
51 45 _check_in_force('|');
52 41 ▶ ($before, $after) = ($after, $before) if $swapped;
Branch: 1 branch
BranchTrueFalseTotal
if $swapped 3 38 41
53 41 ▶ croakf 'Expected a source, or a stage made with a word, on the left of |'
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($before) and _is_source($before) || is_stage($before) 2 39 41
Condition100%6/6
builtin::blessed($before) and _is_source($before) || is_stage($before)
!ll&&!rl&&r
1139
_is_source($before) or is_stage($before)
l!l&&r!l&&!r
3631
MC/DC100%3/3
builtin::blessed($before) and _is_source($before) || is_stage($before)
builtin::blessed($before) _is_source($before) is_stage($before)
Truth table100%4/4
A and B or C
A= builtin::blessed($before)B= _is_source($before)C= is_stage($before)
ABCresult
0XX0
1000
11X1
1011
54 unless blessed($before) && (_is_source($before) || is_stage($before));
55 # A pipeline made with new has no operators, though Perl swaps the
56 # sides to call one for a typed part on its right.
57 39 ▶ croakf 'Expected a source, or a stage made with a word, on the left of |'
Branch: 1 branch
BranchTrueFalseTotal
if $before isa "Ekorn::Pipeline" and not $before isa "Ekorn::Pipeline::Operators::Stages" 1 38 39
Condition100%3/3
$before isa "Ekorn::Pipeline" and not $before isa "Ekorn::Pipeline::Operators::Stages"
!ll&&!rl&&r
3711
MC/DC100%2/2
$before isa "Ekorn::Pipeline" and not $before isa "Ekorn::Pipeline::Operators::Stages"
$before isa "Ekorn::Pipeline" not $before isa "Ekorn::Pipeline::Operators::Stages"
Truth table100%3/3
A and not(B)
A= $before isa "Ekorn::Pipeline"B= not $before isa "Ekorn::Pipeline::Operators::Stages"
ABresult
0X0
100
111
58 if $before isa 'Ekorn::Pipeline'
59 && !($before isa 'Ekorn::Pipeline::Operators::Stages');
60 38 ▶ croakf 'Expected a stage or a sink on the right of |' unless is_stage($after);
Branch: 1 branch
BranchTrueFalseTotal
unless is_stage($after) 1 37 38
61 37 ▶ unless (_is_source($before)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_is_source($before)) 2 35 37
62 2 return Ekorn::Pipeline::Operators::Stages->new($before, $after);
63 }
64 35 my($source, @stages) = _parts($before);
65 35 my $sink = is_sink($after);
66 35 ▶ return _chain($source, @stages, $after) unless $sink;
Branch: 1 branch
BranchTrueFalseTotal
unless $sink 10 25 35
67 25 my $next = Ekorn::Pipeline->new(@stages, $after)->iterator($source);
68 25 my @results;
69 25 ▶ while (my($result) = $next->()) { push @results, $result }
Branch: 1 branch
BranchTrueFalseTotal
if (my($result) = &$next()) 42 25 67
70 25 ▶ return $sink eq 'many' ? \@results : $results[0];
Branch: 1 branch
BranchTrueFalseTotal
$sink eq 'many' ? : 14 11 25
71 }
72  
73  
74 # Return a reference to an array of every value the source gives. A
75 # class of sources of its own can overload @{} with this.
76 7 ▶sub drain ($source, @) {
Subroutine drain: called CC=2 SCAR=6.9
Pod documented
77 7 _check_in_force('@{}');
78 6 my $next = Ekorn::Pipeline->new->iterator($source);
79 5 my @values;
80 5 ▶ while (my($value) = $next->()) { push @values, $value }
Branch: 1 branch
BranchTrueFalseTotal
if (my($value) = &$next()) 10 5 15
81 5 return \@values;
82 }
83  
84  
85 # Return true if the object given offers a source's role method.
86 77 ▶sub _is_source ($object) {
Subroutine _is_source: called CC=1 SCAR=0.0
87 77 return !!$object->can('EKORN_SOURCE');
88 }
89  
90  
91 # Return what a chain was made of, a source and stages; or, given a
92 # source that isn't a chain, the source.
93 35 ▶sub _parts ($source) {
Subroutine _parts: called CC=2 SCAR=6.9
94 return $source isa 'Ekorn::Pipeline::Operators::Chain'
95 35 ▶ ? @{ $source->{parts} }
Branch: 1 branch
BranchTrueFalseTotal
$source isa 'Ekorn::Pipeline::Operators::Chain' ? : 5 30 35
96 : $source;
97 }
98  
99  
100 # Return a chain: a source of the values from $source through the
101 # stages, which keeps them, so that more can be added to it.
102 10 ▶sub _chain ($source, @stages) {
Subroutine _chain: called CC=1 SCAR=0.0
103 10 my $pipeline = Ekorn::Pipeline->new(@stages);
104 5 ▶ my $chain = Ekorn::Pipeline::Operators::Chain->new(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
105 5 my $next = $pipeline->iterator($source);
106 return (
107 11 ▶ sub { $next->() }, # for each value: no signature
Subroutine __ANON__: called CC=1 SCAR=0.0
108 4 ▶ sub ($how) { $next->finish },
Subroutine __ANON__: called CC=1 SCAR=0.0
109 $next->{context},
110 4 );
111 10 });
112 10 $chain->{parts} = [$source, @stages];
113 10 return $chain;
114 }
115  
116 # A chain from a source is a source that keeps what it was made of.
117 3 package Ekorn::Pipeline::Operators::Chain { ## no critic (Modules::ProhibitMultiplePackages)
118 3 ▶ use parent -norequire, 'Ekorn::Pipeline::Source';
Subroutine BEGIN: called CC=1 SCAR=0.0
119 }
120  
121 # A chain of stages is a pipeline, of this class, which has |.
122 3 package Ekorn::Pipeline::Operators::Stages { ## no critic (Modules::ProhibitMultiplePackages)
123 3 ▶ use parent -norequire, 'Ekorn::Pipeline';
Subroutine BEGIN: called CC=1 SCAR=0.0
124 3 ▶ use overload '|' => \&Ekorn::Pipeline::Operators::chain, fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
125 }
126  
127 # Loading this module gives Ekorn's sources and iterators | and @{},
128 # and its stages, gathers and sinks |, and only then.
129 3 package Ekorn::Pipeline::Source { ## no critic (Modules::ProhibitMultiplePackages)
130 use overload
131 3 ▶ '|' => \&Ekorn::Pipeline::Operators::chain,
Subroutine BEGIN: called CC=1 SCAR=0.0
132 '@{}' => \&Ekorn::Pipeline::Operators::drain,
133 3 fallback => 1;
134 }
135  
136 3 package Ekorn::Pipeline::Iterator { ## no critic (Modules::ProhibitMultiplePackages)
137 use overload
138 3 ▶ '|' => \&Ekorn::Pipeline::Operators::chain,
Subroutine BEGIN: called CC=1 SCAR=0.0
139 '@{}' => \&Ekorn::Pipeline::Operators::drain,
140 3 fallback => 1;
141 }
142  
143 3 package Ekorn::Pipeline::Stage { ## no critic (Modules::ProhibitMultiplePackages)
144 3 ▶ use overload '|' => \&Ekorn::Pipeline::Operators::chain, fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
145 }
146  
147 3 package Ekorn::Pipeline::Gather { ## no critic (Modules::ProhibitMultiplePackages)
148 3 ▶ use overload '|' => \&Ekorn::Pipeline::Operators::chain, fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
149 }
150  
151 3 package Ekorn::Pipeline::Sink { ## no critic (Modules::ProhibitMultiplePackages)
152 3 ▶ use overload '|' => \&Ekorn::Pipeline::Operators::chain, fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
153 }
154  
155 3 1;
156  
157 __END__
« Pipeline/Sugar.pm ↑ Summary Pipeline/JSON.pm »