Pipeline/Iterator.pm

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