Here's an option that uses Tie::File to bind the fastq records' file to an array for processing. The files will automatically close when my %FHs falls out of scope. My thanks to BrowserUk for the elegant file handle/hash routine.
use Modern::Perl;
use Tie::File;
{
tie my @fastqLines, 'Tie::File', 'records.fastq', recsep => "\n" o
+r die $!;
my %FHs = map {
open my $fh, '>', "$_.fastq" or die $!;
$_ => $fh
} qw[ TTGT GGTT ACCT ];
for ( my $i = 0 ; $i < scalar @fastqLines ; $i += 4 ) {
$fastqLines[ $i + 1 ] =~ /^...(.{4})/
and print { $FHs{$1} } @fastqLines[ $i .. $i + 3 ];
}
untie @fastqLines;
}
Update: Don't try this at home, as the OP's 34G file is much too large for Tie::File to efficiently handle.