| 1 | 8 | ▶ | use v5.36; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | package Ekorn::Pipeline::Sinks; | ||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||
| 5 | 8 | ▶ | use Carp qw(); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | 8 | ▶ | use Exporter qw(import); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | 8 | ▶ | use Scalar::Util qw(openhandle); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | 8 | ▶ | use Ekorn::Pipeline::Errors qw(croakf); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | 8 | ▶ | use Ekorn::Pipeline::Sink qw(); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 10 | 8 | ▶ | use Ekorn::Pipeline::Stop qw(); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 11 | 8 | ▶ | use namespace::clean -except => 'import'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||
| 13 | 8 | our @EXPORT_OK = qw( | |||||||||||||||||||||||||||||||
| 14 | keep keep_all count_items keep_first keep_last keep_single | ||||||||||||||||||||||||||||||||
| 15 | to_fh to_file); | ||||||||||||||||||||||||||||||||
| 16 | 8 | our %EXPORT_TAGS = (all => \@EXPORT_OK); | |||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||
| 18 | # Croak at the line that called Ekorn::Pipeline. | ||||||||||||||||||||||||||||||||
| 19 | 8 | 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 | 22 | ▶ | sub count_items : prototype() () { | ||||||||||||||||||||||||||||||
|
Subroutine
count_items: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 28 | 15 | ▶ | return Ekorn::Pipeline::Sink->new_sink_one(sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 29 | 15 | my $n = 0; | |||||||||||||||||||||||||||||||
| 30 | # Without a signature, as it's called for each value. | ||||||||||||||||||||||||||||||||
| 31 | 15 | ▶ | return {step => sub { $n++ }, flush => sub () { $n }}; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 32 | 22 | }); | |||||||||||||||||||||||||||||||
| 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 | 28 | ▶ | sub keep_all : prototype() () { | ||||||||||||||||||||||||||||||
|
Subroutine
keep_all: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 42 | 29 | ▶ | return Ekorn::Pipeline::Sink->new_sink_many(sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 43 | 29 | my @all; | |||||||||||||||||||||||||||||||
| 44 | # Without a signature, as it's called for each value. | ||||||||||||||||||||||||||||||||
| 45 | 29 | ▶ | 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 | 28 | }); | |||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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 | 10 | ▶ | sub keep_first : prototype() () { | ||||||||||||||||||||||||||||||
|
Subroutine
keep_first: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 75 | 10 | ▶ | return Ekorn::Pipeline::Sink->new_sink_one(sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 76 | 10 | my @first; | |||||||||||||||||||||||||||||||
| 77 | return { | ||||||||||||||||||||||||||||||||
| 78 | # Without a signature, as it's called for each value. | ||||||||||||||||||||||||||||||||
| 79 | 6 | ▶ | step => sub { @first = ($_[0]); Ekorn::Pipeline::Stop->throw }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 80 | 10 | ▶ | flush => sub () { @first }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 81 | 10 | }; | |||||||||||||||||||||||||||||||
| 82 | 10 | }); | |||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||
| 86 | # Return a sink giving the first $count items, as a list, stopping the | ||||||||||||||||||||||||||||||||
| 87 | # run once it has them. | ||||||||||||||||||||||||||||||||
| 88 | 17 | ▶ | sub keep ($count) { | ||||||||||||||||||||||||||||||
|
Subroutine
keep: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 89 | 17 | ▶ | croakf 'Expected a count of 0 or more' | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $count and $count =~ / \A \d+ \z /ax
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
| |||||||||||||||||||||||||||||||||
| 90 | unless defined $count && $count =~ m( \A \d+ \z )xa; | ||||||||||||||||||||||||||||||||
| 91 | 9 | ▶ | return Ekorn::Pipeline::Sink->new_sink_many(sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 92 | 9 | my @kept; | |||||||||||||||||||||||||||||||
| 93 | return { | ||||||||||||||||||||||||||||||||
| 94 | # Without a signature, as it's called for each value. | ||||||||||||||||||||||||||||||||
| 95 | 15 | ▶ | step => sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||
| 96 | 15 | ▶ | Ekorn::Pipeline::Stop->throw if @kept >= $count; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 97 | 13 | push @kept, $_[0]; | |||||||||||||||||||||||||||||||
| 98 | 13 | ▶ | Ekorn::Pipeline::Stop->throw if @kept >= $count; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 99 | 8 | return; | |||||||||||||||||||||||||||||||
| 100 | }, | ||||||||||||||||||||||||||||||||
| 101 | 9 | ▶ | flush => sub () { @kept }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 102 | 9 | }; | |||||||||||||||||||||||||||||||
| 103 | 10 | }); | |||||||||||||||||||||||||||||||
| 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 | 5 | ▶ | sub to_fh ($fh) { | ||||||||||||||||||||||||||||||
|
Subroutine
to_fh: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 124 | 5 | ▶ | croakf 'Expected an open filehandle' unless defined openhandle($fh); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 125 | 4 | ▶ | return Ekorn::Pipeline::Sink->new_sink_one(sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 126 | 4 | return _writer($fh, 'the filehandle'); | |||||||||||||||||||||||||||||||
| 127 | 4 | }); | |||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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 | 12 | ▶ | sub _writer ($fh, $what) { | ||||||||||||||||||||||||||||||
|
Subroutine
_writer: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||
| 153 | 12 | ▶ | my $after = ref $/ || !defined $/ ? '' : length $/ ? $/ : "\n\n"; | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
ref $/ or not defined $/
MC/DC100%2/2
ref $/ or not defined $/
ref $/
not defined $/
Truth table100%3/3
A or not(B)
A= ref $/B= not defined $/
| |||||||||||||||||||||||||||||||||
| 154 | 12 | my $n = 0; | |||||||||||||||||||||||||||||||
| 155 | return { | ||||||||||||||||||||||||||||||||
| 156 | # Without a signature, as it's called for each value. | ||||||||||||||||||||||||||||||||
| 157 | 57 | ▶ | step => sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 158 | 57 | ▶ | print { $fh } $_[0], $after or croakf q(Can't write %s: %s), $what, $!; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 159 | 56 | $n++; | |||||||||||||||||||||||||||||||
| 160 | }, | ||||||||||||||||||||||||||||||||
| 161 | 11 | ▶ | flush => sub () { $n }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 162 | 12 | }; | |||||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||
| 165 | 8 | 1; | |||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||
| 167 | __END__ | ||||||||||||||||||||||||||||||||