use Algorithm::Loops qw( NestedLoop ); my $iter = NestedLoop(\@subs); while ( my @items = $iter->() ) { ... }