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              
2             package Array::Iterator::Circular;
3              
4 1     1   121383 use strict;
  1         2  
  1         45  
5 1     1   5 use warnings;
  1         10  
  1         55  
6              
7 1     1   457 use Array::Iterator;
  1         3  
  1         347  
8              
9             # AUTHORITY
10             # DATE
11             # DIST
12              
13             =head1 VERSION
14              
15             Version 0.135
16              
17             =cut
18              
19             our $VERSION = '0.135';
20              
21             our @ISA = qw(Array::Iterator);
22              
23             sub _init {
24 1     1   4 my ($self, @args) = @_;
25 1         7 $self->{loop_counter} = 0;
26 1         9 $self->SUPER::_init(@args);
27             }
28              
29             # always return true, since
30             # we just keep looping
31 5     5 1 16 sub has_next { 1 }
32              
33             sub next {
34 26     26 1 145 my ($self) = @_;
35 26 100       66 unless ($self->_current_index < $self->getLength()) {
36 5         13 $self->_current_index = 0;
37 5         17 $self->{loop_counter}++;
38             }
39 26         237 $self->_iterated = 1;
40 26         60 return $self->_getItem($self->_iteratee(), $self->_current_index++);
41             }
42              
43             # since neither of them will
44             # ever stop dispensing items
45             # they can just be aliases of
46             # one another.
47             *get_next = \&next;
48              
49             sub is_start {
50 1     1 1 12 my ($self) = @_;
51 1         11 return ($self->_current_index() == 0);
52             }
53              
54 1     1 0 9517 sub isStart { my $self = shift; $self->is_start(@_) }
  1         5  
55              
56             sub is_end {
57 5     5 1 9 my ($self) = @_;
58 5         14 return ($self->_current_index() == $self->getLength());
59             }
60              
61 5     5 0 29 sub isEnd { my $self = shift; $self->is_end(@_) }
  5         24  
62              
63             sub get_loop_count {
64 28     28 1 57 my ($self) = @_;
65 28         67 return $self->{loop_counter};
66             }
67              
68 28     28 0 103 sub getLoopCount { my $self = shift; $self->get_loop_count(@_) }
  28         79  
69              
70             1;
71             #ABSTRACT: A subclass of Array::Iterator to allow circular iteration
72              
73             =for Pod::Coverage .+
74              
75             =head1 SYNOPSIS
76              
77             use Array::Iterator::Circular;
78              
79             # create an instance with a
80             # small array
81             my $color_iterator = Array::Iterator::Circular->new(qw(red green blue orange));
82              
83             # this is a large list of
84             # arbitrary items
85             my @long_list_of_items = ( ... );
86              
87             # as we loop through the items ...
88             foreach my $item (@long_list_of_items) {
89             # we assign color from our color
90             # iterator, which will keep dispensing
91             # as it loops through its set
92             $item->set_color($color_iterator->next());
93             }
94              
95             # tell us how many times the set
96             # was looped through
97             print $color_iterator->get_loop_count();
98              
99             =head1 DESCRIPTION
100              
101             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.
102              
103             =head1 METHODS
104              
105             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.
106              
107             =over 4
108              
109             =item B
110              
111             Since we endlessly loop, this will always return true (C<1>).
112              
113             =item B
114              
115             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.
116              
117             =item B
118              
119             This method is now defined in terms of C, since neither will even stop dispensing items, there is no need to differentiate.
120              
121             =item B
122              
123             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.
124              
125             =item B
126              
127             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.
128              
129             =item B
130              
131             This method will tell you how many times the iterator has looped back to its start.
132              
133             =back
134              
135             =head1 SEE ALSO
136              
137             This is a subclass of B, please refer to it for more documentation.
138              
139             =head1 ORIGINAL AUTHOR
140              
141             stevan little, Estevan@iinteractive.comE
142              
143             =head1 ORIGINAL COPYRIGHT AND LICENSE
144              
145             Copyright 2004 by Infinity Interactive, Inc.
146              
147             L
148              
149             This library is free software; you can redistribute it and/or modify
150             it under the same terms as Perl itself.
151              
152             =cut