I have an array of entries which will be search in a large file, each entry is then written to its own output file. the array varies based on certain conditions and therefore has to be dynamic.
Here is my example array.
use strict;
use warnings;
my @array = ("ABC",
             "DEF",
             "GHI",
             "JKL",
             "MNO",
             "PQR"
             );
So I created a loop to open create a filehandle for each of the entries. run my commands, then a loop again to close the filehandles.
foreach my $handle(@array) {
        my $output = "OUTPUT_$handle.txt";
           open ($handle, '>', $output) or die "unable to open file $!";
}
#... write to files and do some other fancy stuff ...
foreach $handle(@array) {
           close $handle;
    }
Which will create: OUTPUT_ABC.txt OUTPUT_DEF.txt.. etc.
So my question: It seems to feel very awkward for me to have to loop through opening each and loop closing each handle, is there a more elegant method to open and close file handles dynamically?