|
|
| Welcome to the Monastery | |
| PerlMonks |
Answer: How can I create an array of filehandles? |
| ( #87404=categorized answer: print w/ replies, xml ) | Need Help?? |
|
Q&A > files > How can I create an array of filehandles? contributed by CharlesClarkson
As of Perl 5.6.0 file handles can be autovivified. So it is possible to open a file like this:
This allows us to refer to the filehandle as $fh. The following sub returns a hash keyed to the file names:
Note that files that cannot be opened are not defined in the hash. Now the files can be referred to from %handles:
or:
To return an array we need to decide what happens if a file can't be opened. This sub dies:
While this will return undef for failed opens:
|
|
||||||||||||||||||