The solution is to simply use iterators
.
By assigning the results of variations_with_repetition
to a scalar, it generates an iterator that you can interrogate each time to get the next element. By doing so, you don't keep the entire list in memory and you have access to the first elements immediately. This is a lovely concept called lazy evaluation.
Here is the code for your case:
use strict;
use warnings;
use Algorithm::Combinatorics 'variations_with_repetition';
my @let = qw / A G C T/;
my $cad = variations_with_repetition(\@let,24);
while(my $c = $cad->next)
{
print "@$c\n";
}
Just notice that the iterator actually returns a reference to an array that you have to dereference first and then join or do whatever operation you like on it.
Testing Results: I couldn't run the initial code on my machine (memory usage grows indefinitely as expected), but using iterators, I immediately started getting the output rows, with perl hardly consuming any memory.