| 1 | 31 | ▶ | use v5.36; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 2 | |||||||||||
| 3 | package Ekorn::Pipeline::Iterator; | ||||||||||
| 4 | |||||||||||
| 5 | 31 | ▶ | use parent 'Ekorn::Pipeline::Element'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 31 | ▶ | use Carp qw(); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 31 | ▶ | use Ekorn::Pipeline::Errors qw(croakf); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 31 | ▶ | use namespace::clean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | # Croak at the line that called Ekorn::Pipeline. | ||||||||||
| 12 | 31 | our @CARP_NOT = @Ekorn::Pipeline::Errors::INTERNAL; | |||||||||
| 13 | |||||||||||
| 14 | # Return an iterator, calling $next for each value, whose run $end_run | ||||||||||
| 15 | # ends from outside, $context is the context its run's source gave, | ||||||||||
| 16 | # $ended says how its run ended, $spent whether it has handed out | ||||||||||
| 17 | # everything it will, and $all, if given, gives every value still to | ||||||||||
| 18 | # come in one call, as calling $next until it gives none would, and | ||||||||||
| 19 | # $original, if given, is its run's source's original part, passed on. | ||||||||||
| 20 | 1138 | ▶ | sub new ( | ||||||||
|
Subroutine
new: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||
| 21 | 1138 | ▶ | $class, $next, $end_run, $context = undef, $ended = sub () { return }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 22 | 1138 | ▶ | $spent = sub () { return }, $all = undef, $original = undef, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 23 | ) { | ||||||||||
| 24 | 1138 | return bless { | |||||||||
| 25 | call => $next, | ||||||||||
| 26 | end_run => $end_run, | ||||||||||
| 27 | context => $context, | ||||||||||
| 28 | ended => $ended, | ||||||||||
| 29 | spent => $spent, | ||||||||||
| 30 | all => $all, | ||||||||||
| 31 | original => $original, | ||||||||||
| 32 | }, $class; | ||||||||||
| 33 | } | ||||||||||
| 34 | |||||||||||
| 35 | |||||||||||
| 36 | # Return how the run ended: "done" if its source was read to its end, | ||||||||||
| 37 | # "stopped" if the reading was cut short, by a stop or from outside, or | ||||||||||
| 38 | # values still to be handed out were dropped, and "died" if anything in | ||||||||||
| 39 | # it died; nothing until the iterator has given all it will. | ||||||||||
| 40 | 2074 | ▶ | sub ended ($self) { | ||||||||
|
Subroutine
ended: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 41 | 2074 | return $self->{ended}->(); | |||||||||
| 42 | } | ||||||||||
| 43 | |||||||||||
| 44 | |||||||||||
| 45 | # End the run, if it hasn't ended, as from outside: each part is told | ||||||||||
| 46 | # how from where it stands, and nothing more is handed out. | ||||||||||
| 47 | 56 | ▶ | sub finish ($self) { | ||||||||
|
Subroutine
finish: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 48 | 55 | $self->{end_run}->(); | |||||||||
| 49 | 55 | return; | |||||||||
| 50 | } | ||||||||||
| 51 | |||||||||||
| 52 | |||||||||||
| 53 | # Return the values still to be handed out, reading the run to its end: | ||||||||||
| 54 | # as a list, or, asked for one value, an array reference of them. A | ||||||||||
| 55 | # method, only ever called as $iterator->all, so it can't be mistaken | ||||||||||
| 56 | # for Perl's all, a keyword from 5.42. | ||||||||||
| 57 | 858 | ▶ | sub all ($self) { ## no critic (Subroutines::ProhibitBuiltinHomonyms) | ||||||||
|
Subroutine
all: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||
| 58 | 858 | my @all; | |||||||||
| 59 | 858 | ▶ | if (my $all = $self->{all}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 60 | 857 | @all = $all->(); | |||||||||
| 61 | } | ||||||||||
| 62 | else { | ||||||||||
| 63 | 1 | my $next = $self->{call}; | |||||||||
| 64 | 1 | ▶ | while (my($value) = $next->()) { push @all, $value } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 65 | } | ||||||||||
| 66 | 780 | ▶ | return wantarray ? @all : \@all; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 67 | } | ||||||||||
| 68 | |||||||||||
| 69 | |||||||||||
| 70 | # Return the next value, or undef if there's none, and end the run, so | ||||||||||
| 71 | # nothing more is read. | ||||||||||
| 72 | 4 | ▶ | sub first ($self) { | ||||||||
|
Subroutine
first: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 73 | 4 | my($value) = $self->{call}->(); | |||||||||
| 74 | 4 | $self->finish; | |||||||||
| 75 | 4 | return $value; | |||||||||
| 76 | } | ||||||||||
| 77 | |||||||||||
| 78 | |||||||||||
| 79 | # Return the next value, or undef if there's none, asserting it's the | ||||||||||
| 80 | # only one: if there's another, warn. The run ends either way. | ||||||||||
| 81 | 3 | ▶ | sub single ($self) { | ||||||||
|
Subroutine
single: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 82 | 3 | my $next = $self->{call}; | |||||||||
| 83 | 3 | my($value) = $next->(); | |||||||||
| 84 | 3 | ▶ | Carp::carp 'Expected only one item, but there were more' | ||||||||
|
Branch: 1 branch
| |||||||||||
| 85 | if my($more) = $next->(); | ||||||||||
| 86 | 3 | $self->finish; | |||||||||
| 87 | 3 | return $value; | |||||||||
| 88 | } | ||||||||||
| 89 | |||||||||||
| 90 | |||||||||||
| 91 | # An iterator is a source: of the values its run gives, the run ended | ||||||||||
| 92 | # from outside when the run reading it ends, passing on its context, and | ||||||||||
| 93 | # its source's original part, if it had one (roadmap 2026-0052). | ||||||||||
| 94 | 20 | ▶ | sub EKORN_SOURCE ($self) { | ||||||||
|
Subroutine
EKORN_SOURCE: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 95 | croakf | ||||||||||
| 96 | 'Expected a source with values still to give: an iterator is one run, and this one is spent' | ||||||||||
| 97 | 20 | ▶ | if $self->{spent}->(); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 98 | 16 | my $next = $self->{call}; | |||||||||
| 99 | return { | ||||||||||
| 100 | # Without a signature, as it's called for each value. | ||||||||||
| 101 | 42 | ▶ | step => sub { $next->() }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 102 | # $how is how the reading run ended, which this one doesn't take | ||||||||||
| 103 | # on: it's ended as from outside, and each of its parts is told | ||||||||||
| 104 | # how from where it stands, so "stopped", not "died", if the | ||||||||||
| 105 | # reading run died while this one still had values to give. | ||||||||||
| 106 | 16 | ▶ | end => sub ($how) { $self->finish }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 107 | context => $self->{context}, | ||||||||||
| 108 | original => $self->{original}, | ||||||||||
| 109 | 16 | }; | |||||||||
| 110 | } | ||||||||||
| 111 | |||||||||||
| 112 | |||||||||||
| 113 | # An iterator let go ends its run. | ||||||||||
| 114 | 1138 | ▶ | sub DESTROY ($self) { | ||||||||
|
Subroutine
DESTROY: called CC=1 SCAR=0.0
| |||||||||||
| 115 | 1138 | local $@ = undef; | |||||||||
| 116 | 1138 | $self->{end_run}->(); | |||||||||
| 117 | 1138 | return; | |||||||||
| 118 | } | ||||||||||
| 119 | |||||||||||
| 120 | 31 | 1; | |||||||||
| 121 | |||||||||||
| 122 | __END__ | ||||||||||