in reply to Avoiding vivification with array slice

You want to stop after you have

You have chosen to put the "N elements" idea in the loop control and add a test for the end of the array. How about reversing this? Looping over an array is so easy in Perl. Adding a counter to quit after N elements is not much harder.

use constant N => 3; my $counter; my @a = 'A' .. 'Z'; $counter = 1; ELEMENT: foreach my $val (@a) { last ELEMENT if $counter++ > N; print "Looking at $val\n"; }
Looking at A Looking at B Looking at C