my $iter = sub { @foo ? shift @foo : () }; while (my ($elem) = $iter->()) { # $elem may be undefined. }