I would suggest neither solution. Why not simply keep all the files open? Or if there are many, you could keep a cache of open files which closes files based upon how long ago they were used (an LRU cache) or some other kind of heuristic that can determine the likelihood the file will be used again soon (such as frequency of use). For example:
my %file_cache;
sub open_file($) {
my $filename = shift;
return $file_cache{$filename} ||
(open $file_cache{$filename}, ">>$filename" or
die "blah blah $!");
}
while (<FILE> {
foreach my $n (@natures) {
if ($_ =~ /%n/) {
my $out = open_file "$n.txt";
print $out $n;
}
}
}
If there is the potential for many files, delete an entry from the cache managed by open_file when the given $filename isn't currently open and the cache is "full".
This way you don't have to open files over and over again quite so often--we hope--and you don't have to perform reiterations.