use Algorithm::Loops qw( NestedLoops ); my $iter= NestedLoops( [ [1], [ 2, 3, 4 ], [5], [ 6, 7, 8 ], [9] ], ); my @perms; while( @perms = $iter->() ) { print @perms, "\n"; }