Tie/Cycle.pm untested

filter: Statementstmt 0.0% 0 / 48 Branchbran 0.0% 0 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 77 CC 4 SCAR 12.3
CC
4
Cov
0%
TIESCALAR
6.0
STORE
6.0
_item
6.0
CRAP 3.4
1 package Tie::Cycle;
2 use strict;
3  
4 our $VERSION = '1.224';
5  
6 use Carp qw(carp);
7  
8 use constant CURSOR_COL => 0;
9 use constant COUNT_COL => 1;
10 use constant ITEM_COL => 2;
11  
12 sub TIESCALAR {
13 my( $class, $list_ref ) = @_;
14 my $self = bless [], $class;
15  
16 unless( $self->STORE( $list_ref ) ) {
17 carp "The argument to Tie::Cycle must be an array reference";
18 return;
19 }
20  
21 return $self;
22 }
23  
24 sub FETCH {
25 my( $self ) = @_;
26  
27 my $index = $self->[CURSOR_COL]++;
28 $self->[CURSOR_COL] %= $self->_count;
29  
30 return $self->_item( $index );
31 }
32  
33 sub STORE {
34 my( $self, $list_ref ) = @_;
35 return unless ref $list_ref eq ref [];
36 my @shallow_copy = map { $_ } @$list_ref;
37  
38 $self->[CURSOR_COL] = 0;
39 $self->[COUNT_COL] = scalar @shallow_copy;
40 $self->[ITEM_COL] = \@shallow_copy;
41 }
42  
43 sub reset { $_[0]->[CURSOR_COL] = 0 }
44  
45 sub previous {
46 my( $self ) = @_;
47  
48 my $index = $self->_cursor - 1;
49 $self->[CURSOR_COL] %= $self->_count;
50  
51 return $self->_item( $index );
52 }
53  
54 sub next {
55 my( $self ) = @_;
56  
57 my $index = $self->_cursor + 1;
58 $self->[CURSOR_COL] %= $self->_count;
59  
60 return $self->_item( $index );
61 }
62  
63 sub _cursor { $_[0]->[CURSOR_COL] }
64 sub _count { $_[0]->[COUNT_COL] }
65 sub _item {
66 my( $self, $index ) = @_;
67 $index = defined $index ? $index : $self->_cursor;
68 $self->[ITEM_COL][ $index ]
69 }
70  
71 "Tie::Cycle";
72  
73 __END__