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