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.

It's new in v0.3.11. Nothing is exported by default; :all gives both. Ekorn::Pipeline::Tooling doesn't export them in v0.3.x, as DBIx::Squirrel::Tooling exports functions of the same names, built on these, and a program importing both would get one or the other: import them from here.

FUNCTIONS

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. For a run reading an iterator, it's the value the iterator handed out, not that iterator's own original.

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.