use strict; use warnings; =pod =head1 NAME array_iterator - a factory for iterators =head1 SYNOPSIS my $iter = array_iterator(@array); while (my ($i, $data) = $iter->()) { # was &$iter, see ihb's response # $i is index, $data is data } $iter->(0); # Reset to start of array $iter->(-1); # Reset to end of array, and iterate backwards $iter->(5); # Reset $i to 5 =head1 NOTES This is an accessor only. It will not create array elements. You can do that by operating directly on the array. After running out, it implicitly resets to front or back end of the array, depending on which direction it was iterating. =cut sub array_iterator (\@) { my $aref = shift; my $i = 0; sub { if (@_) { # no need for use warnings::register; $i = shift; if ($i > $#$aref) { $i = $#$aref; warnings::warnif(misc => "index beyond end of array"); } elsif ($i < -@$aref) { $i = -@$aref; warnings::warnif(misc => "array index excessively negative"); } } elsif ($i >= -@$aref and $i <= $#$aref) { my $i_was = $i; $i += $i < 0 ? -1 : 1; ($i_was, $aref->[$i_was]); } else{ $i = ($i > $#$aref) ? 0 : -1; return (); } } } my @arr = (20..30); my $I1 = array_iterator(@arr); print "Skipping every 4th...\n"; while (my ($i, $data) = &$I1) { next if $i % 4 == 3; print "$i: $data\n"; } print "Running through again, without skips:\n"; while (my ($i, $data) = $I1->()) { # was &$I1 here and below print "$i: $data\n"; } print "-- Now...backwards! --\n"; $I1->(-1); while (my ($i, $data) = $I1->()) { print "$i: $data\n"; } print "again, but only printing every 3rd:\n"; while (my ($i, $data) = $I1->()) { print "$i: $data\n" if $i % 3 == 0; } print "Boundary check:\n"; $I1->(-50); while (my ($i, $data) = $I1->()) { print "$i: $data\n"; } print "This should print nothing:\n"; my @empty = (); my $I2 = array_iterator(@empty); print join(':', $I2->()), "\n";