use IO::File; my %fhs = map { my $fh = IO::File->new($_, 'w') or die "ack [$_]: $!"; $_ => $fh; } @natures; while() { for my $match (keys %fhs) { print {$fhs{$match}} $_ if /$match/; } } close $_ for values %fhs;