Pipeline/Sinks.pm

filter: Statementstmt 100% 83 / 83 Branchbran 100% 20 / 20 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 4 / 4 Subroutinesub 100% 40 / 40 Podpod 100% 8 / 8 totaltotal 100% 161 / 161 CC 14 SCAR 5.2
CC
14
Cov
100%
_writer
4.0
keep
3.0
__ANON__
3.0
CRAP 1.7
« Pipeline/Stages/Flow.pm ↑ Summary Pipeline/Pipes.pm »
1 10 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package Ekorn::Pipeline::Sinks;
4  
5 10 ▶use Carp qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 10 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use Scalar::Util qw(openhandle);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶use Ekorn::Pipeline::Errors qw(croakf);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 ▶use Ekorn::Pipeline::Sink qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶use Ekorn::Pipeline::Stop qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 10 ▶use namespace::clean -except => 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 10 our @EXPORT_OK = qw(
14 keep keep_all count_items keep_first keep_last keep_single
15 to_fh to_file);
16 10 our %EXPORT_TAGS = (all => \@EXPORT_OK);
17  
18 # Croak at the line that called Ekorn::Pipeline.
19 10 our @CARP_NOT = @Ekorn::Pipeline::Errors::INTERNAL;
20  
21 # Each sink taking no arguments has an empty prototype, so that
22 # `count_items + 0` reads as `count_items() + 0`, not `count_items(+0)`.
23 # A chain inside a larger expression still needs parentheses, as |
24 # binds loosest.
25  
26 # Return a sink giving how many values there were.
27 25 ▶sub count_items : prototype() () {
Subroutine count_items: called CC=1 SCAR=0.0
Pod documented
28 41 ▶ return Ekorn::Pipeline::Sink->new_sink_one(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
29 41 my $n = 0;
30 # Without a signature, as it's called for each value.
31 41 ▶ return {step => sub { $n++ }, flush => sub () { $n }};
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
32 25 });
33 }
34  
35  
36 # The sinks named for what they give: keep_all, keep(N), keep_first,
37 # keep_last, keep_single and count_items. The container sinks, named for
38 # where the items go, are to_fh and to_file, below.
39  
40 # Return a sink giving every item, as a list.
41 30 ▶sub keep_all : prototype() () {
Subroutine keep_all: called CC=1 SCAR=0.0
Pod documented
42 54 ▶ return Ekorn::Pipeline::Sink->new_sink_many(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
43 54 my @all;
44 # Without a signature, as it's called for each value.
45 54 ▶ return {step => sub { push @all, $_[0] }, flush => sub () { @all }};
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
46 30 });
47 }
48  
49  
50 # Return a sink giving the one item, asserting it's the only one: at a
51 # second, it warns and stops the run, giving the first; undef if there
52 # was none.
53 7 ▶sub keep_single : prototype() () {
Subroutine keep_single: called CC=1 SCAR=0.0
Pod documented
54 7 ▶ return Ekorn::Pipeline::Sink->new_sink_one(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
55 7 my @only;
56 return {
57 # Without a signature, as it's called for each value.
58 7 ▶ step => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
59 7 ▶ if (@only) {
Branch: 1 branch
BranchTrueFalseTotal
if (@only) 2 5 7
60 2 Carp::carp 'Expected only one item, but there were more';
61 2 Ekorn::Pipeline::Stop->throw;
62 }
63 5 @only = ($_[0]);
64 5 return;
65 },
66 7 ▶ flush => sub () { @only },
Subroutine __ANON__: called CC=1 SCAR=0.0
67 7 };
68 7 });
69 }
70  
71  
72 # Return a sink giving the first item, stopping the run there; undef if
73 # there was none.
74 11 ▶sub keep_first : prototype() () {
Subroutine keep_first: called CC=1 SCAR=0.0
Pod documented
75 34 ▶ return Ekorn::Pipeline::Sink->new_sink_one(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
76 34 my @first;
77 return {
78 # Without a signature, as it's called for each value.
79 22 ▶ step => sub { @first = ($_[0]); Ekorn::Pipeline::Stop->throw },
Subroutine __ANON__: called CC=1 SCAR=0.0
80 34 ▶ flush => sub () { @first },
Subroutine __ANON__: called CC=1 SCAR=0.0
81 34 };
82 11 });
83 }
84  
85  
86 # Return a sink giving the first $count items, as a list, stopping the
87 # run once it has them.
88 18 ▶sub keep ($count) {
Subroutine keep: called CC=3 SCAR=11.0
Pod documented
89 18 ▶ croakf 'Expected a count of 0 or more'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $count and $count =~ / \A \d+ \z /ax 7 11 18
Condition100%3/3
defined $count and $count =~ / \A \d+ \z /ax
!ll&&!rl&&r
2511
MC/DC100%2/2
defined $count and $count =~ / \A \d+ \z /ax
defined $count $count =~ / \A \d+ \z /ax
Truth table100%3/3
A and B
A= defined $countB= $count =~ / \A \d+ \z /ax
ABresult
0X0
100
111
90 unless defined $count && $count =~ m( \A \d+ \z )xa;
91 33 ▶ return Ekorn::Pipeline::Sink->new_sink_many(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
92 33 my @kept;
93 return {
94 # Without a signature, as it's called for each value.
95 39 ▶ step => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
96 39 ▶ Ekorn::Pipeline::Stop->throw if @kept >= $count;
Branch: 1 branch
BranchTrueFalseTotal
if @kept >= $count 2 37 39
97 37 push @kept, $_[0];
98 37 ▶ Ekorn::Pipeline::Stop->throw if @kept >= $count;
Branch: 1 branch
BranchTrueFalseTotal
if @kept >= $count 13 24 37
99 24 return;
100 },
101 33 ▶ flush => sub () { @kept },
Subroutine __ANON__: called CC=1 SCAR=0.0
102 33 };
103 11 });
104 }
105  
106  
107 # Return a sink giving the last item, reading every one to find it;
108 # undef if there was none.
109 7 ▶sub keep_last : prototype() () {
Subroutine keep_last: called CC=1 SCAR=0.0
Pod documented
110 6 ▶ return Ekorn::Pipeline::Sink->new_sink_one(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
111 6 my @kept;
112 return {
113 # Without a signature, as it's called for each value.
114 11 ▶ step => sub { @kept = ($_[0]); return },
Subroutine __ANON__: called CC=1 SCAR=0.0
115 6 ▶ flush => sub () { @kept },
Subroutine __ANON__: called CC=1 SCAR=0.0
116 6 };
117 7 });
118 }
119  
120  
121 # Return a sink printing each value to the filehandle given, followed by
122 # $/, giving how many, and leaving it open, for its opener to close.
123 6 ▶sub to_fh ($fh) {
Subroutine to_fh: called CC=2 SCAR=6.9
Pod documented
124 6 ▶ croakf 'Expected an open filehandle' unless defined openhandle($fh);
Branch: 1 branch
BranchTrueFalseTotal
unless defined &openhandle($fh) 1 5 6
125 5 ▶ return Ekorn::Pipeline::Sink->new_sink_one(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
126 5 return _writer($fh, 'the filehandle');
127 5 });
128 }
129  
130  
131 # Return a sink writing each value to the file, followed by $/, giving
132 # how many; the file is opened as the run starts, with the I/O layers
133 # given, if any, such as ':raw', and closed as it ends.
134 10 ▶sub to_file ($path, $layers = '') {
Subroutine to_file: called CC=2 SCAR=6.9
Pod documented
135 10 ▶ return Ekorn::Pipeline::Sink->new_sink_one(sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
136 # Open for the run, and closed by its end.
137 10 ▶ open my $fh, ">$layers", $path ## no critic (InputOutput::RequireBriefOpen)
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">$layers", $path or croakf("Can't write %s: %s", $path, $!) 8 2 10
138 or croakf q(Can't write %s: %s), $path, $!;
139 return {
140 8 %{ _writer($fh, $path) },
141 8 ▶ end => sub ($how) { close $fh or croakf q(Can't write %s: %s), $path, $! },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
close $fh or croakf("Can't write %s: %s", $path, $!) 7 1 8
142 8 };
143 10 });
144 }
145  
146  
147 # Return, by name, the step and the flush of a sink printing each value to $fh,
148 # named $what in its message, followed by $/ as it is now, as from_fh and
149 # from_file take it off: nothing if it's undefined or a record's size,
150 # and a blank line, for paragraph mode, if it's empty. The flush gives
151 # how many.
152 13 ▶sub _writer ($fh, $what) {
Subroutine _writer: called CC=4 SCAR=13.9
153 13 ▶ my $after = ref $/ || !defined $/ ? '' : length $/ ? $/ : "\n\n";
Branch: 2 branches
BranchTrueFalseTotal
ref $/ || !defined($/) ? : 2 11 13
length $/ ? : 10 1 11
Condition100%3/3
ref $/ or not defined $/
l!l&&r!l&&!r
1111
MC/DC100%2/2
ref $/ or not defined $/
ref $/ not defined $/
Truth table100%3/3
A or not(B)
A= ref $/B= not defined $/
ABresult
1X1
011
000
154 13 my $n = 0;
155 return {
156 # Without a signature, as it's called for each value.
157 59 ▶ step => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
158 59 ▶ print { $fh } $_[0], $after or croakf q(Can't write %s: %s), $what, $!;
Branch: 1 branch
BranchTrueFalseTotal
print {$fh;} $_[0], $after or croakf("Can't write %s: %s", $what, $!) 58 1 59
159 58 $n++;
160 },
161 12 ▶ flush => sub () { $n },
Subroutine __ANON__: called CC=1 SCAR=0.0
162 13 };
163 }
164  
165 10 1;
166  
167 __END__
« Pipeline/Stages/Flow.pm ↑ Summary Pipeline/Pipes.pm »