open( X, 'x.txt' ); my ( $chunk, $line ); while () { $line = $_; if ( ( length($chunk) + length($line) ) > 2048 ) { # if the next line would take us over the set size doSomethingWithChunk($chunk); $chunk = $line; } else { $chunk .= $line; # append line and keep going } } doSomethingWithChunk($chunk); # process whatever's left in $chunk at the end #### my $p = new Algorithm::Permute(['a'..'d']); while (@res = $p->next) { # do something with @res }