my $i = 0; # ... somewhere in the code: $i = $i < 4 ? $i + 1 : 0;