sub getIter{ my ($start,$end, $aryRef) = @_; $end=$#$aryRef if $end==-1; return sub { return $aryRef->[$start++] if $start <= $end; (); } } my @a = 1 .. 1000000; my $iter = getIter 500000, 500005, \@a; while ( $iter->() ) { print $_, $/; }