my @a = qw( a b c d ); my $i = sub { @a ? splice(@a, 0, 2) : (undef,undef) }; while (my ($x,$y) = $i->()) { print("$x:$y\n"); }