#!/usr/bin/perl -l use strict; use Algorithm::Loops 'NestedLoops'; sub pick { my( $k, @chars ) = @_; my $idx = NestedLoops( [ ( sub { [ ( @_ ? 1+$_ : 0 ) .. @chars-$k+@_ ] } ) x $k ] ); return sub { @chars[ $idx->() ] }; } @ARGV = ( 3, 'a'..'e' ) if ! @ARGV; my $iter = pick( @ARGV ); while( my @subset = $iter->() ) { print "@subset"; }