NAME

Ekorn::Pipeline::Context - what a stage can ask about the result it's making

SYNOPSIS

use Ekorn::Pipeline;
use Ekorn::Pipeline::Context qw(result_number result_original);

my @lines = Ekorn::Pipeline->new(
    sub { $_->{plays} > 6000 ? $_ : () },
    sub { result_number . ". $_->{title}" },
)->(@tracks);    # 1. Dancing Queen, 2. Parklife, ...

Ekorn::Pipeline->new(
    sub { $_ * 2 },
    sub { say result_original, ' became ', $_; $_ },
)->(1 .. 3);     # 1 became 2, 2 became 4, 3 became 6

DESCRIPTION

Each run of a pipeline counts the results it makes, and keeps the value each is being made from, so that a stage can ask which result it's making, and from what. A result is a value that leaves the last stage, or that reaches a sink. Each run counts its own: a pipeline run inside a stage, a tee's branch, and an iterator read as a source each count theirs from 1, and leave the count of the run around them as it was.

They're for a run's stages: its steps, flushes and sinks. Outside a run, and in an end, they give undef. In a source's step, they give where the run reading it has got to, which isn't promised: ask them in a stage.

Every run counts, whether its stages ask or not. Measured on Perl 5.36 and 5.44, it costs a value about 15 to 75 nanoseconds, more on an iterator's next, which counts once a call, than on a pushed run or all, which count once a run: 1 to 12% through one stage that does nothing, and less through stages that do work. Roadmap 2026-0046's record has the measurements.

The run's context, which its source gives, is asked with "run_context" in Ekorn::Pipeline, and whether the stages are handling a flush's values with "ending" in Ekorn::Pipeline.

A stage that holds values back says where each value it gives came from, with "kept_origin", so the stages after it get the right original.

It's new in v0.3.11. Nothing is exported by default; :all gives all three, and so does Ekorn::Pipeline::Tooling, in :context and :all, from v0.3.15; kept_origin is new in v0.5.0. DBIx::Squirrel::Tooling exports the same functions under the same names, from its v3.6.1, so importing both Toolings gives one of each.

FUNCTIONS

Each takes no arguments, and has an empty prototype, so a bare call is a term, as a constant is: result_number + 1 adds one, and result_original // 'none' gives 'none' where there's none. In v0.3.11, they had no prototype, so result_number + 1 died with "Too many arguments", and // after either began a pattern.

kept_origin

my $origin = kept_origin;
my @marked = $origin->gives(@values);

For a stage that holds values back, to give them later, as take_last does: it gives values the run has gone past, so the stages after it would see the original of the value the run got to last, not of the one theirs was made from. Such a stage says so, with holds => 1 among its parts, by name ("Made for a run" in Ekorn::Pipeline); in its step, as it keeps a value, calls kept_origin; and gives what it made from that value marked, by the origin's gives:

sub one_behind () {
    return gather {
        my @held;    # [value, origin]
        my $give = sub ($held) { $held->[1]->gives($held->[0]) };
        return {
            step => sub ($value) {
                push @held, [$value, kept_origin];
                return @held > 1 ? $give->(shift @held) : ();
            },
            flush => sub () { map { $give->($_) } splice @held },
            holds => 1,
        };
    };
}

kept_origin returns the value's origin: an opaque object, an Ekorn::Pipeline::Origin, holding what "result_original" answers for the value then, as later its source may answer for another. Its method gives returns each of the values given marked as made from that value, and the run takes the mark off each before the stages after the stage get it, with that value's original, in a flush too. A value the stage gives unmarked was made from many values, so has none, as a batch has. Only what the stage gives is unmarked, not a marked value inside a structure it gives.

  • The origin is captured as the value is kept, in the step it came to: a source's original part, which answers for the value it gave last, is asked then, and only by a stage that holds values back.

  • It works only in a run with a stage that holds values back, and croaks elsewhere, a run nested in such a stage's step included. It doesn't tell one stage of the run from another, so a stage that doesn't hold values back mustn't call it; in a flush, which has no value, it gives an origin of nothing.

  • A stage that holds values back but doesn't say so is taken for one that maps, as every stage was before v0.5.0.

New in v0.5.0.

result_number

my $n = result_number;

The number of the result being made, counting from 1: the run's results so far, plus one. While a stage drops values, it stays the same, as no result has been made. Each value a stage returns in place of one is a result of its own, so they're numbered one after another. In a flush, it's the number of the first value the flush returns.

result_original

my $original = result_original;

The value the result is being made from, as the run's source gave it, or as it was pushed, before any stage changed it. Each value a stage returns in place of one, and each a stop carries, has the original of the value it was made from. In a flush, and the stages after it, the values a flush gives have none, so it's undef, unless a stage that holds values back says where they came from ("kept_origin"). For a run reading an iterator, it's the value the iterator handed out, not that iterator's own original, unless the source says otherwise, as follows.

A source can say better, by giving an original part, as Ekorn::Pipeline::Source describes: a statement's source, for instance, giving the row it fetched last, when what it hands out is made from the row. Then result_original gives what that says, calling it only when asked, and a run reading an iterator of that source's run gets it too, so the row reaches every pipeline in a chain. A flush's values have none, whatever it says, unless a stage that holds values back says where they came from. New in v0.3.13.

The value itself, not a copy: a stage that changes a hash changes the original too.

SEE ALSO

Ekorn::Pipeline, Ekorn::Pipeline::Tooling, Ekorn::Pipeline::Manual::Reference

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.