If you’re involved in a testing task that would benefit from you running random tests from a combination of inputs or running tests against all possible input combinations, writing the code yourself can be daunting.

While browsing cpan.org today, I stumbled across the Math::Combinatorics perl module, written by Allen Day. Allen’s module does all the heavy listing for you.

Provide an array of n inputs along with the number of objects in n that you want to group together like so:

my @n = qw(a b c);
my $combinat = Math::Combinatorics->new(count => 2,
data => [@n],
);
print "combinations of 2 from: ".join(" ",@n)."\n";
print "------------------------".("--" x scalar(@n))."\n";
while(my @combo = $combinat->next_combination){
print join(' ', @combo)."\n";
}

This simple code will give you this output, showing that it quickly figured out all the 2 item combinations possible from your array of n characters.

combinations of 2 from: a b c
------------------------------
a b
a c
b c

Allen’s module also supports permutations (for times when order of the inputs matters) and, if you want to get really fancy, multisets.

Thanks Allen for your hard work and I hope this comes in handy for all you perl testers out there wanting to really hammer on your code, with all the possible combinations of inputs!