You can also take a look at this module I wrote some time ago. It allows to access a fasta file via a Perl array, so the task could be solved with something like:
use strict;
use warnings;
use Tie::File::AnyData::Bio::Fasta;
use Fcntl;
my $fastafile = 'j:\summer\begomo_genomes2.fasta';
tie my @fastaFile, 'Tie::File::AnyData::Bio::Fasta', $fastafile or die
+ $!;
for my $fastaRec (@fastafile) {
my ($header, $rest) = split /\n/,$fastaRec, 2;
my ($accession) = $header =~ /gi\|(\d+)/;
tie my @outFasta, 'Tie::File::AnyData::Bio::Fasta', "${accession}
+.fasta", mode => O_RDWR | O_CREAT or die $!;
@outFasta = ($fastaRec);
untie @outFasta;
}
untie @fastaFile;