use Algorithm::Loops qw( NestedLoops ); my $iter = NestedLoops([ map [ 1, 2 ], 1..$n ]); while ( my @combo = $iter->() ) { say "@combo"; }