That made me want to implement it. Ironically, I don't have 5.10 installed yet, and there are a few features that would be appropriate here. Note that I reversed the result list, so that the order is key/value, like hash-each is.
use strict;
use warnings;
BEGIN {
my %state;
sub a_each(\@) {
my $aref = shift;
exists $state{$aref} or $state{$aref} = -1;
if (++$state{$aref} > $#$aref) {
delete $state{$aref};
return ();
}
else {
return ($state{$aref}, $aref->[$state{$aref}]);
}
}
}
my @array = qw( foo bar rat );
while (my ($idx,$val) = a_each @array) {
print "$val at $idx\n";
}
Caution: Contents may have been coded under pressure.