in reply to Is there a "better" way to cycle an array?

Using closures and modulo is one way. Define a function that returns a function reference which has a copy of the array reference and knows the last position. Every time you call this unnamed function you will get the next element of the cycle. Using array references will mean that any change of the array will be reflected in your cycling. If this is undesired use arrays instead.

use strict; use warnings; my $stuff = [ 'a', 'b', 'c', 'd' ]; sub cyclist { my $array = shift; my $pos = -1; return sub { return $$array[++$pos % @$array]; } } my $cyclestuff = cyclist $stuff; print $cyclestuff->() for 1..30; print "\n"; $$stuff[0] = 'A'; print $cyclestuff->() for 1..30; print "\n";