File Coverage

blib/lib/List/Rotation/Cycle.pm
Criterion Covered Total %
statement 15 15 100.0
branch n/a
condition n/a
subroutine 5 5 100.0
pod 1 1 100.0
total 21 21 100.0


line stmt bran cond sub pod time code
1             package List::Rotation::Cycle;
2 2     2   23717 use vars qw( $VERSION );
  2         6  
  2         125  
3             $VERSION = 1.009;
4              
5 2     2   12 use strict;
  2         2  
  2         65  
6 2     2   10 use warnings;
  2         3  
  2         79  
7              
8 2     2   2199 use Memoize;
  2         5626  
  2         378  
9             memoize('new');
10              
11             sub new {
12             my $class = shift;
13              
14             do {
15             require Carp;
16             Carp::croak ("Incorrect number of arguments; must be >= 1.");
17             } unless @_ >= 1;
18              
19             my $self = [ @_ ];
20              
21             my $closure = sub {
22             push @$self, shift @$self;
23             return $self->[-1];
24             };
25              
26             bless $closure, $class;
27             }
28              
29             sub next {
30 10     10 1 961 my $self = shift;
31 10         11 &{ $self };
  10         24  
32             }
33              
34             "List::Rotation::Cycle";
35              
36              
37             __END__
38              
39             =head1 NAME
40              
41             List::Rotation::Cycle - Cycle through a list of values via a singleton object implemented as closure.
42              
43             =head1 SYNOPSIS
44              
45             use List::Rotation::Cycle;
46              
47             my @array = qw( A B C );
48              
49             my $first_cycle = List::Rotation::Cycle->new(@array);
50             my $second_cycle = List::Rotation::Cycle->new(@array);
51              
52             print $first_cycle->next; ## prints A
53             print $second_cycle->next; ## prints B
54             print $first_cycle->next; ## prints C
55             print $second_cycle->next; ## prints A, looping back to beginning
56              
57             =head1 DESCRIPTION
58              
59             Use C<List::Rotation::Cycle> to loop through a list of values.
60             Once you get to the end of the list, you go back to the beginning.
61              
62             C<List::Rotation::Cycle> is implemented as a Singleton Pattern. You always just
63             get 1 (the very same) Cycle object even if you use the new method several times.
64             This is done by using C<Memoize> on the C<new> method. It returns the same object
65             for every use of C<new> that comes with the same List of parameters.
66              
67             =head1 OBJECT METHODS
68              
69             =over 4
70              
71             =item new
72              
73             Create a Cycle object for the list of values in the list.
74              
75             =item next
76              
77             Return the next element. This method is implemented as a closure.
78              
79             =back
80              
81             =head1 AUTHOR
82              
83             Imre Saling, C<< <pelagicatcpandotorg> >>
84              
85             =head1 COPYRIGHT and LICENSE
86              
87             Copyright 2000-2004, Imre Saling, All rights reserved.
88              
89             This software is available under the same terms as perl.
90              
91             =cut