I often find the need to open a fairly large number of input files simultaneously, to run line-by-line comparisons on them. I can not read in all of the complete files before starting my analysis - due to the large size the files, that would be a very slow and RAM-intensive approach.

The way I'm doing this now is horribly clumsy. I have an array of input file names, and then I have an array of arbitrary file handles, with at least as many elements as the former array, e.g.:

my @FileHandles = ('A', 'B', 'C'...'X');

I have a loop that opens all input files. Then to start reading them:
$file1 = $InHandles[0]; while (<$file1>){ ... for ($f = 1; $f < @InHandles; $f++){ $file = $InHandles[$f]; $_ = (<$file>); ...
where "..." includes the retrieval of information from the current line of each file.

Today I found a need to read in over 100 files, and this has motivated me to find a more sensible way to deal with large numbers of concurrently used file handles. Is there a simple way to generate unique handles automatically for each element in my array of input files, without typing out a silly array like the @FileHandles shown above?

I've also found that at least when using double letter file handles from an array, I am forced to turn off 'use strict', which I otherwise keep on to catch typos.

Thanks for any help you can offer.

In reply to File handles - there must be a better way by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.