In perl5 v12 and later, you can use the each
iterator:
while(my($index, $element) = each @array_elements) {
...;
}
However, the more portable solution is to iterate over the indices, and manually access the element, as shown by ysth.
In any case, the number of elements that were visited (including the current element) is $index + 1
.