NAME
Ekorn::Pipeline::Stages::Taps - stages run for their side effects: peek, count_into, dump_to and tee
SYNOPSIS
use Ekorn::Pipeline;
use Ekorn::Pipeline::Stages::Taps; # peek and count_into
my $read = 0;
my @names = Ekorn::Pipeline->new(
count_into(\$read), # how many values were read
peek { log_user($_) }, # each value, as it goes by
sub { $_->{name} },
)->(@users);
use Ekorn::Pipeline::Stages::Taps qw(dump_to tee);
Ekorn::Pipeline->new(dump_to)->(@users); # to STDERR
# A tap of any shape: a branch of stages, beside the main run
my @names = Ekorn::Pipeline->new(
tee(take_if { $_->{admin} }, take(1), sub { log_first_admin($_); () }),
sub { $_->{name} },
)->(@users);
DESCRIPTION
Stages that look at each value as it goes by and pass it on unchanged, for logging, counting or debugging. A stage passes on what it returns, so a stage of your own that runs for its side effects has to return its value. These do that for you. Each function returns a stage, and checks its arguments when it's called, not on each value.
peek, count_into and dump_to each do one thing. tee takes stages, so a tap can be anything a pipeline can do: look only at some values, or the first few, batch them, total them when the values run out, or write them through a sink.
Each stage is an object, an Ekorn::Pipeline::Stage or, for those that keep state, an Ekorn::Pipeline::Gather, called as a code reference, so it goes wherever a stage goes. Code testing it with ref or is_coderef sees an object: v0.2.0 gave code references.
They work wherever stages do: in any pipeline, or on an object with pipe and tap, from Ekorn::Pipeline::Pipes, where the first value is the object:
$client->tap(peek { say "connected to $_->{host}" })->run;
FUNCTIONS
peek and count_into are exported by default. dump_to and tee are exported on request, and :all gives all four.
peek
peek { ... }
Calls the block with each value, in $_ and as its argument, and passes the value on, whatever the block returns.
count_into
count_into(\$count)
Counts the values into $count. It sets $count to 0 at the start of each run, so after a run it holds how many values went by, even if none did.
dump_to
dump_to($fh)
dump_to
Writes each value to $fh, or to STDERR, with Data::Dumper, keys sorted, and passes it on.
When Text::Redact::Secrets is installed, a v1.x, each secret it finds in what's written, such as a password, a token or the password in a DSN, is written as *REDACTED*. Only the dump is redacted: the value passed on is as it came. Without the module, values are written as they are. It's looked for once, at the first dump, and tested then, by redacting a few made-up secrets. If it's there but fails to load, isn't a v1.x, or fails the test, dump_to croaks, on that dump and each after, rather than write a dump that may hold secrets.
To write secrets as they are, set $Ekorn::Pipeline::Stages::Taps::DumpSecrets true, with local, for as long as you need it. It's read as each value is dumped, and while it's true, the module is neither loaded nor tested:
{
local $Ekorn::Pipeline::Stages::Taps::DumpSecrets = 1;
$pipeline->(@rows);
}
It's for debugging. Text::Redact::Secrets finds the secrets its rules know, not every secret, so a dump can still hold some. It doesn't escape control characters in strings, so a value from outside can send escape sequences to a terminal.
tee
tee(@stages)
Runs each value through a branch of @stages, for what they do on the side, and passes the value on unchanged. What comes out of the branch is dropped. @stages are whatever "new" in Ekorn::Pipeline takes, a pipeline or a sink last included, and are made into a pipeline when tee is called, so a mistake croaks then, at your line, with new's message, a stage counted among the tee's. With no stages, it does nothing.
The branch is one run of its own, beside the main run, fed a value at a time by an Ekorn::Pipeline::Feed that the tee makes for each main run. So the branch's stages see every value the tee does, in order, and keep their state across them, and each main run gets a fresh branch:
# Note the first two big hits, and pass every track on
tee(take_if { $_->{plays} > 6000 }, take(2), sub { note_hit($_); () })
A stop in the branch, such as
take(2)'s, ends only the branch. The tee passes the values after it on, without running them through the branch.A death in the branch kills the main run, as any stage's error does, and its message names the tee's stage, then, within it, the branch's:
Pipeline stage #1 failed at app.pl line 12: Pipeline stage #2 failed at app.pl line 12: bad valueThe branch's flushes are called in the tee's flush, which closes the branch: so once the main run's values have run out, before the stages after the tee are flushed. A total, a last batch or a sink's result is given then. If a stop after the tee ends the main run first, such as
take's or an iterator'sfirst, the tee's flush isn't called, so the tee closes the branch in its end: a stop ends the branch's input as much as running out would, so a writer in the branch keeps what it holds. That's new in v0.3.10; before, the branch's flushes weren't called, and what they held was lost. A branch's flush that dies there kills the run, as an end that dies does.The branch's ends are called as its run ends, told how it ended from where each stands: in the push that met a stop or a death in it; or as the tee closes it, in its flush or its end. If the main run died, the tee's end doesn't close the branch, but finishes it, its flushes not called, and its ends told
stopped, as the branch's input was cut short, as they are for an iterator read as a source by a run that dies.The value itself goes into the branch, not a copy, as it goes to
peek's block. A branch that changes a hash changes it for the main run too.The main run's context is the branch's: in the branch, "run_context" in Ekorn::Pipeline gives what it gives in the main run, such as a statement's columns.
Each value costs a push into the branch: about 1.3 microseconds a value through a branch of one plain stage, against peek's 0.4, on Perl 5.36. A tee has a flush and an end only if its branch needs them, so a branch of plain stages leaves the main run on the plain loop. A branch with a flush, such as batch's, puts the main run on the full path, and costs about 2.2 microseconds a value. It's for logging, debugging and side jobs, not a hot loop.
DIAGNOSTICS
- Expected a code reference
-
peekwas given something other than a block or code reference. - Expected a reference to a scalar
-
count_intowas given something other than a reference to a scalar. - Expected an open filehandle
-
dump_towas given something other than an open filehandle. - Text::Redact::Secrets %s, so dump_to won't write a dump that may hold secrets; local $Ekorn::Pipeline::Stages::Taps::DumpSecrets = 1 dumps them without redacting
-
dump_tofound Text::Redact::Secrets installed, but can't redact with it: it failed to load, with the first line of its error; it isn't a v1.x, the versionsdump_totakes; it has noredact; or it failed the self-test, leaving a made-up secret in a dump. Install a v1.x that works, or set$Ekorn::Pipeline::Stages::Taps::DumpSecretstrue to write dumps without redacting them.
SEE ALSO
Ekorn::Pipeline, Ekorn::Pipeline::Gather, Ekorn::Pipeline::Feed, Data::Dumper, Text::Redact::Secrets
AUTHOR
Iain Campbell <cpanic@cpan.org>
LICENSE
Copyright (c) 2024-2026 by Iain Campbell.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.