File Coverage

blib/lib/Array/Iterator/Circular.pm
Criterion Covered Total %
statement 31 31 100.0
branch 2 2 100.0
condition n/a
subroutine 12 12 100.0
pod 5 8 62.5
total 50 53 94.3


line stmt bran cond sub pod time code
1             package Array::Iterator::Circular;
2              
3 1     1   120292 use strict;
  1         2  
  1         35  
4 1     1   4 use warnings;
  1         8  
  1         50  
5              
6 1     1   366 use Array::Iterator;
  1         3  
  1         288  
7              
8             # AUTHORITY
9             # DATE
10             # DIST
11              
12             =head1 VERSION
13              
14             Version 0.136
15              
16             =cut
17              
18             our $VERSION = '0.136';
19              
20             our @ISA = qw(Array::Iterator);
21              
22             =head1 SYNOPSIS
23              
24             use Array::Iterator::Circular;
25              
26             # create an instance with a
27             # small array
28             my $color_iterator = Array::Iterator::Circular->new(qw(red green blue orange));
29              
30             # this is a large list of
31             # arbitrary items
32             my @long_list_of_items = ( ... );
33              
34             # as we loop through the items ...
35             foreach my $item (@long_list_of_items) {
36             # we assign color from our color
37             # iterator, which will keep dispensing
38             # as it loops through its set
39             $item->set_color($color_iterator->next());
40             }
41              
42             # tell us how many times the set
43             # was looped through
44             print $color_iterator->get_loop_count();
45              
46             =head1 DESCRIPTION
47              
48             This iterator will loop continuosly as long as C or C is called. The C method will always return true (C<1>), since the list will always loop back. This is useful when you need a list to repeat itself, but don't want to (or care to) know that it is doing so.
49              
50             =head1 METHODS
51              
52             This is a subclass of Array::Iterator, only those methods that have been added or altered are documented here, refer to the Array::Iterator documentation for more information.
53              
54             =over 4
55              
56             =item B
57              
58             Since we endlessly loop, this will always return true (C<1>).
59              
60             =item B
61              
62             This will return the next item in the array, and when it reaches the end of the array, it will loop back to the beginning again.
63              
64             =item B
65              
66             This method is now defined in terms of C, since neither will even stop dispensing items, there is no need to differentiate.
67              
68             =item B
69              
70             If at anytime during your looping, you want to know if you have arrived back at the start of you list, you can ask this method.
71              
72             =item B
73              
74             If at anytime during your looping, you want to know if you have gotten to the end of you list, you can ask this method.
75              
76             =item B
77              
78             This method will tell you how many times the iterator has looped back to its start.
79              
80             =back
81              
82             =cut
83              
84             sub _init {
85 3     3   10 my ($self, $length, @args) = @_;
86              
87 3         12 $self->{loop_counter} = 0;
88 3         16 $self->SUPER::_init($length, @args);
89             }
90              
91             # always return true, since
92             # we just keep looping
93 5     5 1 16 sub has_next { 1 }
94              
95             sub next {
96 46     46 1 13886 my $self = $_[0];
97              
98 46 100       126 unless ($self->_current_index < $self->getLength()) {
99 18         50 $self->_current_index = 0;
100 18         32 $self->{loop_counter}++;
101             }
102 46         161 $self->_iterated = 1;
103 46         131 return $self->_getItem($self->_iteratee(), $self->_current_index++);
104             }
105              
106             # since neither of them will
107             # ever stop dispensing items
108             # they can just be aliases of
109             # one another.
110             *get_next = \&next;
111              
112             sub is_start {
113 1     1 1 3 my $self = $_[0];
114              
115 1         9 return ($self->_current_index() == 0);
116             }
117              
118 1     1 0 4 sub isStart { my $self = shift; $self->is_start(@_) }
  1         12  
119              
120             sub is_end {
121 5     5 1 11 my $self = $_[0];
122              
123 5         11 return ($self->_current_index() == $self->getLength());
124             }
125              
126 5     5 0 28 sub isEnd { my $self = shift; $self->is_end(@_) }
  5         11  
127              
128             sub get_loop_count {
129 28     28 1 50 my $self = $_[0];
130              
131 28         83 return $self->{loop_counter};
132             }
133              
134 28     28 0 96 sub getLoopCount { my $self = shift; $self->get_loop_count(@_) }
  28         62  
135              
136             1;
137              
138              
139             =head1 SEE ALSO
140              
141             This is a subclass of B, please refer to it for more documentation.
142              
143             =head1 MAINTAINER
144              
145             Nigel Horne, C<< >>
146              
147             2025-2026
148              
149             =head1 ORIGINAL AUTHOR
150              
151             stevan little, Estevan@iinteractive.comE
152              
153             =head1 ORIGINAL COPYRIGHT AND LICENSE
154              
155             Copyright 2004 by Infinity Interactive, Inc.
156              
157             L
158              
159             This library is free software; you can redistribute it and/or modify
160             it under the same terms as Perl itself.
161              
162             =cut