sub combinatoric($$); # prototype forward reference sub combinatoric( $$ ) { my ( $zero, $one ) = @_; my @ret = (); if ( $one == 0 ) { # no more ones left to select, the rest is zeroes. push @ret, scalar ( '0' x $zero ); } elsif ( $zero == 0 ) { # no more zeroes left to select, the rest is ones. push @ret, scalar ( '1' x $one ); } else { # take a zero from the pile, and compute what's left push @ret, "0$_" foreach combinatoric( $zero-1, $one ); # take a one from the pile, and compute what's left push @ret, "1$_" foreach combinatoric( $zero, $one-1 ); } return @ret; } print "$_\n" foreach combinatoric( 10, 10 );