Pipeline/Operators.pm

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