lib/Module/Generic/Iterator.pm

filter: Statementstmt 72.2% 133 / 184 Branchbran 37.0% 20 / 54 Conditioncond 42.8% 18 / 42 MC/DCmcdc 19.0% 4 / 21 Subroutinesub 78.2% 36 / 46 Podpod 87.5% 14 / 16 totaltotal 61.9% 225 / 363 CC 47 SCAR 29.1
CC
47
Cov
61%
THAW
110.0
FREEZE
42.0
_find_pos
10.1
CRAP 18.4
« lib/Module/Generic/JSON.pm ↑ Summary lib/Module/Generic/File/IO.pm »
1 ##----------------------------------------------------------------------------
2 ## Module Generic - ~/lib/Module/Generic/Iterator.pm
3 ## Version v1.2.4
4 ## Copyright(c) 2025 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2021/03/20
7 ## Modified 2026/01/22
8 ## All rights reserved
9 ##
10 ## This program is free software; you can redistribute it and/or modify it
11 ## under the same terms as Perl itself.
12 ##----------------------------------------------------------------------------
13 package Module::Generic::Iterator;
14 BEGIN
15 2 ▶{
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶ use v5.16.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶ use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 warnings::register_categories( 'Module::Generic' );
20 2 ▶ use parent qw( Module::Generic );
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶ use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶ use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶ use Wanted;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 our( $VERSION ) = 'v1.2.4';
25 };
26  
27 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 2 require Module::Generic::Array;
29  
30 sub init
31 3 ▶{
Subroutine init: called CC=6 SCAR=19.5
Pod documented
32 3 my $self = CORE::shift( @_ );
33 3 my $init = [];
34 3 ▶ $init = CORE::shift( @_ ) if( @_ && ( ( Scalar::Util::blessed( $_[0] ) && $_[0]->isa( 'ARRAY' ) ) || ref( $_[0] ) eq 'ARRAY' ) );
Branch: 1 branch
BranchTrueFalseTotal
if @_ and builtin::blessed($_[0]) && $_[0]->isa("ARRAY") || ref $_[0] eq "ARRAY" 3 0 3
Condition33%3/9
@_ and builtin::blessed($_[0]) && $_[0]->isa("ARRAY") || ref $_[0] eq "ARRAY"
!ll&&!rl&&r
003
builtin::blessed($_[0]) and $_[0]->isa("ARRAY") or ref $_[0] eq "ARRAY"
l!l&&r!l&&!r
030
builtin::blessed($_[0]) and $_[0]->isa("ARRAY")
!ll&&!rl&&r
300
MC/DC0%0/4
@_ and builtin::blessed($_[0]) && $_[0]->isa("ARRAY") || ref $_[0] eq "ARRAY"
@_ builtin::blessed($_[0]) $_[0]->isa("ARRAY") ref $_[0] eq "ARRAY"
Truth table16%1/6
A and B and C or D
A= @_B= builtin::blessed($_[0])C= $_[0]->isa("ARRAY")D= ref $_[0] eq "ARRAY"
ABCDresult
0XXX0
10X00
11000
111X1
10X11
11011
35 3 $self->{_init_strict_use_sub} = 1;
36 3 $self->SUPER::init( @_ );
37 3 my $elems = Module::Generic::Array->new;
38 ## Wrap each element in an Iterator element to enable next, prev, etc
39 3 foreach my $this ( @$init )
40 {
41 8 CORE::push( @$elems, Module::Generic::Iterator::Element->new( $this, { parent => $self, debug => $self->debug } ) );
42 }
43 3 $self->{elements} = $elems;
44 3 $self->{pos} = 0;
45 3 $self->{_started} = 0;
46 3 return( $self );
47 }
48  
49 # This class does not convert to an HASH
50 0 ▶sub as_hash { return( $_[0] ); }
Subroutine as_hash: not called CC=1 SCAR=6.9
Pod documented
51  
52 38 ▶sub elements { return( shift->_set_get_array_as_object( 'elements', @_ ) ); }
Subroutine elements: called CC=1 SCAR=0.0
Pod documented
53  
54 sub eof
55 6 ▶{
Subroutine eof: called CC=3 SCAR=13.5
Pod documented
56 6 my $self = shift( @_ );
57 6 my $pos;
58 6 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 6 6
59 {
60 0 $pos = $self->_find_pos( @_ );
61 0 ▶ return if( !CORE::defined( $pos ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pos 0 0 0
62 }
63 else
64 {
65 6 $pos = $self->pos;
66 }
67 6 return( $pos >= ( $self->elements->length - 1 ) );
68 }
69  
70 sub find
71 2 ▶{
Subroutine find: called CC=2 SCAR=6.9
Pod documented
72 2 my $self = shift( @_ );
73 2 my $pos = $self->_find_pos( @_ );
74 2 ▶ return if( !CORE::defined( $pos ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pos 1 1 2
75 1 return( $self->elements->index( $pos ) );
76 }
77  
78 sub first
79 2 ▶{
Subroutine first: called CC=1 SCAR=0.0
Pod documented
80 2 my $self = shift( @_ );
81 2 $self->pos = 0;
82 2 return( $self->elements->index( 0 ) );
83 }
84  
85 sub has_next
86 2 ▶{
Subroutine has_next: called CC=1 SCAR=0.0
Pod documented
87 2 my $self = shift( @_ );
88 2 my $pos = $self->pos;
89 2 return( $pos < ( $self->elements->length - 1 ) );
90 }
91  
92 sub has_prev
93 2 ▶{
Subroutine has_prev: called CC=2 SCAR=7.0
Pod documented
94 2 my $self = shift( @_ );
95 2 my $pos = $self->pos;
96 2 ▶ return( $pos > 0 && $self->elements->length > 0 );
Condition66%2/3
$pos > 0 && $self->elements->length > 0
!ll&&!rl&&r
101
MC/DC50%1/2
$pos > 0 && $self->elements->length > 0
$pos > 0 $self->elements->length > 0
Truth table66%2/3
A && B
A= $pos > 0B= $self->elements->length > 0
ABresult
0X0
100
111
97 }
98  
99 sub last
100 3 ▶{
Subroutine last: called CC=1 SCAR=0.0
Pod documented
101 3 my $self = shift( @_ );
102 3 my $pos = $self->elements->length - 1;
103 3 $self->pos = $pos;
104 3 return( $self->elements->index( $pos ) );
105 }
106  
107 2 ▶sub length { return( shift->elements->length ); }
Subroutine length: called CC=1 SCAR=0.0
Pod documented
108  
109 sub next
110 4 ▶{
Subroutine next: called CC=6 SCAR=21.0
Pod documented
111 4 my $self = shift( @_ );
112 4 my $pos;
113 4 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 4 4
114 {
115 0 $pos = $self->_find_pos( @_ );
116 0 ▶ return if( !CORE::defined( $pos ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pos 0 0 0
117 0 ▶ return if( $pos >= ( $self->elements->length - 1 ) );
Branch: 1 branch
BranchTrueFalseTotal
if $pos >= $self->elements->length - 1 0 0 0
118 0 $pos++;
119 }
120 else
121 {
122 4 ▶ return if( $self->eof );
Branch: 1 branch
BranchTrueFalseTotal
if $self->eof 1 3 4
123 3 ▶ if( $self->{_started} )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_started'}) { } 2 1 3
124 {
125 2 $self->pos++;
126 }
127 else
128 {
129 1 $self->{_started} = 1;
130 }
131 3 $pos = $self->pos;
132 }
133 3 return( $self->elements->index( $pos ) );
134 }
135  
136 39 ▶sub pos : lvalue { return( shift->_set_get_scalar({
Subroutine pos: called CC=1 SCAR=0.0
Pod documented
137 field => 'pos',
138 check => sub
139 24 ▶ {
Subroutine __ANON__: called CC=2 SCAR=6.9
140 24 my( $self, $a ) = @_;
141 24 ▶ if( $a !~ /^\-?\d+$/ )
Branch: 1 branch
BranchTrueFalseTotal
unless ($a =~ /^\-?\d+$/) 1 23 24
142 {
143 1 CORE::warn( "Position provided \"$a\" is not an integer.\n" );
144 1 return(0);
145 }
146 23 return(1);
147 },
148 39 }, @_ ) ); }
149  
150 sub prev
151 3 ▶{
Subroutine prev: called CC=6 SCAR=22.9
Pod documented
152 3 my $self = shift( @_ );
153 3 my $pos;
154 3 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 3 3
155 {
156 0 $pos = $self->_find_pos( @_ );
157 0 ▶ return if( !CORE::defined( $pos ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pos 0 0 0
158 0 ▶ return if ( $pos <= 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $pos <= 0 0 0 0
159 0 $pos--;
160 }
161 else
162 {
163 3 ▶ $self->pos-- if( $self->pos >= 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $self->pos >= 0 3 0 3
164  
165 # Position of the given element is at the beginning of our array, there is nothing more
166 3 $pos = $self->pos;
167 # return if( $pos <= 0 );
168 3 ▶ return if( $pos < 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $pos < 0 1 2 3
169 # $self->pos--;
170 }
171 2 return( $self->elements->index( $pos ) );
172 }
173  
174 sub reset
175 3 ▶{
Subroutine reset: called CC=1 SCAR=0.0
Pod documented
176 3 my $self = shift( @_ );
177 3 $self->pos = 0;
178 3 $self->{_started} = 0;
179 3 return( $self );
180 }
181  
182 sub _find_pos
183 8 ▶{
Subroutine _find_pos: called CC=10 SCAR=23.1
184 8 my $self = shift( @_ );
185 8 my $this = shift( @_ );
186 8 ▶ return if( !CORE::length( $this ) );
Branch: 1 branch
BranchTrueFalseTotal
unless CORE::length $this 0 8 8
187 8 my $is_ref = ref( $this );
188 8 ▶ my $ref = $is_ref ? Scalar::Util::refaddr( $this // '' ) : $this;
Branch: 1 branch
BranchTrueFalseTotal
$is_ref ? : 6 2 8
Condition50%1/2
$this // ""
l!l
60
MC/DC0%0/1
$this // ""
$this
Truth table50%1/2
A
A= $this
Aresult
11
00
189 8 my $elems = $self->elements;
190 8 foreach my $i ( 0 .. $#$elems )
191 {
192 25 my $val = $elems->[$i]->value;
193 25 ▶ if( ( $is_ref && Scalar::Util::refaddr( $elems->[$i] // '' ) eq $ref ) ||
Branch: 1 branch
BranchTrueFalseTotal
if ($is_ref and builtin::refaddr($elems->[$i] // "") eq $ref or not $is_ref and $val eq $this) 7 18 25
Condition90%10/11
$is_ref and builtin::refaddr($elems->[$i] // "") eq $ref or not $is_ref and $val eq $this
l!l&&r!l&&!r
6118
$is_ref and builtin::refaddr($elems->[$i] // "") eq $ref
!ll&&!rl&&r
7126
$elems->[$i] // ""
l!l
180
not $is_ref and $val eq $this
!ll&&!rl&&r
1261
MC/DC50%2/4
$is_ref and builtin::refaddr($elems->[$i] // "") eq $ref or not $is_ref and $val eq $this
$is_ref $elems->[$i] not $is_ref $val eq $this
Truth table42%3/7
A and B or not(C) and D
A= $is_refB= $elems->[$i]C= not $is_refD= $val eq $this
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
194 ( !$is_ref && $val eq $this ) )
195 {
196 7 return( $i );
197 }
198 }
199 1 return;
200 }
201  
202 # NOTE: FREEZE is inherited
203  
204 0 ▶sub STORABLE_freeze { CORE::return( CORE::shift->FREEZE( @_ ) ); }
Subroutine STORABLE_freeze: not called CC=1 SCAR=6.9
Pod undocumented
205  
206 0 ▶sub STORABLE_thaw { CORE::return( CORE::shift->THAW( @_ ) ); }
Subroutine STORABLE_thaw: not called CC=1 SCAR=6.9
Pod undocumented
207  
208 # NOTE: THAW is inherited
209  
210 # NOTE: package Module::Generic::Iterator::Element
211 package Module::Generic::Iterator::Element;
212 BEGIN
213 2 ▶{
Subroutine BEGIN: called CC=1 SCAR=0.0
214 2 ▶ use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
215 2 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
216 2 ▶ use warnings::register;
Subroutine BEGIN: called CC=1 SCAR=0.0
217 2 ▶ use parent qw( Module::Generic );
Subroutine BEGIN: called CC=1 SCAR=0.0
218 2 ▶ use Wanted;
Subroutine BEGIN: called CC=1 SCAR=0.0
219 2 our( $VERSION ) = 'v0.1.0';
220 };
221  
222 sub init
223 8 ▶{
Subroutine init: called CC=1 SCAR=0.0
224 8 my $self = CORE::shift( @_ );
225 ## This could be anything
226 8 my $value = CORE::shift( @_ );
227 8 $self->{value} = '';
228 8 $self->{parent} = '';
229 8 $self->{_init_strict_use_sub} = 1;
230 8 $self->SUPER::init( @_ );
231 8 $self->{value} = $value;
232 8 return( $self );
233 }
234  
235 # This class does not convert to an HASH
236 0 ▶sub as_hash { return( $_[0] ); }
Subroutine as_hash: not called CC=1 SCAR=6.9
237  
238 sub has_next
239 2 ▶{
Subroutine has_next: called CC=1 SCAR=0.0
240 2 my $self = shift( @_ );
241 2 my $pos = $self->pos;
242 2 return( $pos < ( $self->parent->elements->length - 1 ) );
243 }
244  
245 sub has_prev
246 2 ▶{
Subroutine has_prev: called CC=2 SCAR=7.0
247 2 my $self = shift( @_ );
248 2 my $pos = $self->pos;
249 2 ▶ return( $pos > 0 && $self->parent->elements->length > 0 );
Condition66%2/3
$pos > 0 && $self->parent->elements->length > 0
!ll&&!rl&&r
101
MC/DC50%1/2
$pos > 0 && $self->parent->elements->length > 0
$pos > 0 $self->parent->elements->length > 0
Truth table66%2/3
A && B
A= $pos > 0B= $self->parent->elements->length > 0
ABresult
0X0
100
111
250 }
251  
252 sub next
253 0 ▶{
Subroutine next: not called CC=2 SCAR=17.9
254 0 my $self = shift( @_ );
255 0 my $next = $self->parent->next( $self );
256 0 ▶ if( want( 'OBJECT' ) )
Branch: 1 branch
BranchTrueFalseTotal
if (want('OBJECT')) { } 0 0 0
257 {
258 0 return( $next );
259 }
260 else
261 {
262 0 return( $next->value );
263 }
264 }
265  
266 18 ▶sub parent { return( shift->_set_get_object( 'parent', 'Module::Generic::Iterator', @_ ) ); }
Subroutine parent: called CC=1 SCAR=0.0
267  
268 6 ▶sub pos { return( $_[0]->parent->_find_pos( $_[0] ) ); }
Subroutine pos: called CC=1 SCAR=0.0
269  
270 sub prev
271 0 ▶{
Subroutine prev: not called CC=2 SCAR=17.9
272 0 my $self = shift( @_ );
273 0 my $prev = $self->parent->prev( $self );
274 0 ▶ if( want( 'OBJECT' ) )
Branch: 1 branch
BranchTrueFalseTotal
if (want('OBJECT')) { } 0 0 0
275 {
276 0 return( $prev );
277 }
278 else
279 {
280 0 return( $prev->value );
281 }
282 }
283  
284 33 ▶sub value { return( shift->{value} ); }
Subroutine value: called CC=1 SCAR=0.0
285  
286 sub FREEZE
287 0 ▶{
Subroutine FREEZE: not called CC=6 SCAR=37.4
288 0 my $self = CORE::shift( @_ );
289 0 ▶ my $serialiser = CORE::shift( @_ ) // '';
Condition0%0/2
shift @_ // ""
l!l
00
MC/DC0%0/1
shift @_ // ""
shift @_
Truth table0%0/2
A
A= shift @_
Aresult
11
00
290 0 my $class = CORE::ref( $self );
291 0 my @props = qw( elements pos _started );
292 0 my $hash = {};
293 0 foreach my $prop ( @props )
294 {
295 0 ▶ if( exists( $self->{ $prop } ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{$prop}) 0 0 0
296 {
297 0 $hash->{ $prop } = $self->{ $prop };
298 }
299 }
300 # Return an array reference rather than a list so this works with Sereal and CBOR
301 # On or before Sereal version 4.023, Sereal did not support multiple values returned
302 0 ▶ if( $serialiser eq 'Sereal' )
Branch: 1 branch
BranchTrueFalseTotal
if ($serialiser eq "Sereal") 0 0 0
303 {
304 0 require Sereal::Encoder;
305 0 require version;
306
307 0 ▶ if( version->parse( Sereal::Encoder->VERSION ) <= version->parse( '4.023' ) )
Branch: 1 branch
BranchTrueFalseTotal
if ("version"->parse("Sereal::Encoder"->VERSION) <= "version"->parse("4.023")) 0 0 0
308 {
309 0 CORE::return( [$class, $hash] );
310 }
311 }
312 # But Storable want a list with the first element being the serialised element
313 0 CORE::return( $class, $hash );
314 }
315  
316 0 ▶sub STORABLE_freeze { CORE::return( CORE::shift->FREEZE( @_ ) ); }
Subroutine STORABLE_freeze: not called CC=1 SCAR=6.9
317  
318 0 ▶sub STORABLE_thaw { CORE::return( CORE::shift->THAW( @_ ) ); }
Subroutine STORABLE_thaw: not called CC=1 SCAR=6.9
319  
320 # NOTE: CBOR will call the THAW method with the stored classname as first argument, the constant string CBOR as second argument, and all values returned by FREEZE as remaining arguments.
321 # NOTE: Storable calls it with a blessed object it created followed with $cloning and any other arguments initially provided by STORABLE_freeze
322 sub THAW
323 0 ▶{
Subroutine THAW: not called CC=10 SCAR=47.0
324 0 my( $self, undef, @args ) = @_;
325 0 ▶ my $ref = ( CORE::scalar( @args ) == 1 && CORE::ref( $args[0] ) eq 'ARRAY' ) ? CORE::shift( @args ) : \@args;
Branch: 1 branch
BranchTrueFalseTotal
scalar @args == 1 && ref $args[0] eq 'ARRAY' ? : 0 0 0
Condition0%0/3
scalar @args == 1 and ref $args[0] eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
scalar @args == 1 and ref $args[0] eq "ARRAY"
scalar @args == 1 ref $args[0] eq "ARRAY"
Truth table0%0/3
A and B
A= scalar @args == 1B= ref $args[0] eq "ARRAY"
ABresult
0X0
100
111
326 0 ▶ my $class = ( CORE::defined( $ref ) && CORE::ref( $ref ) eq 'ARRAY' && CORE::scalar( @$ref ) > 1 ) ? CORE::shift( @$ref ) : ( CORE::ref( $self ) || $self );
Branch: 1 branch
BranchTrueFalseTotal
defined $ref && ref $ref eq 'ARRAY' && scalar @$ref > 1 ? : 0 0 0
Condition0%0/9
defined $ref and ref $ref eq "ARRAY" and scalar @$ref > 1
!ll&&!rl&&r
000
defined $ref and ref $ref eq "ARRAY"
!ll&&!rl&&r
000
ref $self or $self
l!l&&r!l&&!r
000
MC/DC0%0/3
defined $ref and ref $ref eq "ARRAY" and scalar @$ref > 1
defined $ref ref $ref eq "ARRAY" scalar @$ref > 1
MC/DC0%0/2
ref $self or $self
ref $self $self
Truth table0%0/4
A and B and C
A= defined $refB= ref $ref eq "ARRAY"C= scalar @$ref > 1
ABCresult
0XX0
10X0
1100
1111
Truth table0%0/3
A or B
A= ref $selfB= $self
ABresult
1X1
011
000
327 0 ▶ my $hash = CORE::ref( $ref ) eq 'ARRAY' ? CORE::shift( @$ref ) : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $ref eq 'ARRAY' ? : 0 0 0
328 0 my $new;
329 # Storable pattern requires to modify the object it created rather than returning a new one
330 0 ▶ if( CORE::ref( $self ) )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) { } 0 0 0
331 {
332 0 foreach( CORE::keys( %$hash ) )
333 {
334 0 $self->{ $_ } = CORE::delete( $hash->{ $_ } );
335 }
336 0 $new = $self;
337 }
338 else
339 {
340 0 $new = CORE::bless( $hash => $class );
341 }
342 0 CORE::return( $new );
343 }
344  
345 2 1;
346  
347 __END__
« lib/Module/Generic/JSON.pm ↑ Summary lib/Module/Generic/File/IO.pm »