Your problem is the loop counter foreach. You cannot retreat like that.
Instead, you can:
while (@array) {
print pop @array;
}
or simply:
print pop @array while (@array);
while (@array)will evaluate @arrayin a scalar context, which means that the size of the array will be checked. When the size reaches zero, the loop ends.
Since this is perl, there are, of course, a million ways to do this. Another variant:
print for reverse @array
... , unshift:
use strict;
use warnings;
my @array;
while (<>) {
unshift @array, $_;
}
print for @array;
... :
use strict;
use warnings;
print reverse <>;
Perl!