Another alternative, which I think I prefer. Best thing is you don't get "Use of uninitialized value in join or string at ..." if the array size isn't an exact multiple of the chunk size.
sub getIter (\@$;$) {
my ($ref, $size, $next) = @_;
$next ||= 0;
return sub {
$next = 0, return () unless $next <= $#$ref;
my $start = $next;
$next = $next+$size <= $#$ref ? $next+$size-1 : $#$ref;
@$ref[ $start .. $next++ ]
}
}
my $iter = getIter( @array, 2 );
while( my @chunk = $iter->() ) {
print "Chunk: @chunk";
}
Examine what is said, not who speaks.
The 7th Rule of perl club is -- pearl clubs are easily damaged. Use a diamond club instead.