http://www.perlmonks.org?node_id=903490


in reply to Re: Open multiple file handles?
in thread Open multiple file handles?

Thanks for your reply

Yes, I am familiar with using the <> operator, but my question is more of

How do I OPEN as many file handles as the number of ARGV elements without a priori knowledge of this number?

Replies are listed 'Best First'.
Re^3: Open multiple file handles?
by davido (Cardinal) on May 07, 2011 at 05:39 UTC

    Now you're presenting an X-Y problem: You have something you would like to accomplish, and you think that you have to solve how to open an unknown number of file handles up to possibly 10,000 to accomplish your primary objective. That's not what you need to solve. X is accomplish the objective. Y is how to get there. You're focusing on the wrong 'Y'. The diamond operator will automatically open one file after the other, sequentially, as you read from it. When one file finishes, it moves on to the next.

    If your concern is knowing the filenames ahead of time, they're held in @ARGV before you start reading, and as you start reading, $ARGV will hold the name of the current file you're iterating over. $. will hold the current line of the file you're iterating over. So if you need to keep track of where you are in the process, you can rely on those special variables as hints.

    So to reiterate, assuming there are no other command line arguments aside from simple filenames:

    • @ARGV will hold the list of filenames.
    • scalar(@ARGV) will tell you how many items @ARGV holds.
    • $ARGV will tell you which file the diamond operator is reading currently.
    • $. will tell you what line number you're on in that file.
    • <> will read from the first file in @ARGV, followed by the 2nd, and so on until there's nothing more to read.
    • A simple while( <> ) {.... } loop will "do the right thing" if the right thing is reading every line of every file on the command line.

    Dave

Re^3: Open multiple file handles?
by John M. Dlugosz (Monsignor) on May 07, 2011 at 07:20 UTC
    How does that follow from your question? You have not shown that you need to open a separate file handle (at the same time) for everything in ARGV. We have pointed out that the diamond operator opens each one in turn, automatically. From the problem you presented, there is no reason to open the files all at the same time, and even if you did so, what would you do with them all?

    But a direct answer to that question is: call open for each one. What's the big deal? Call open in a loop, or get fancier with a map statement.

    Do you realize that you can use a variable as the file handle, rather than an old-fashioned GLOB thing? Maybe that's the part you are missing. I hesitate to show a complete example because you should not need to do that at all.

Re^3: Open multiple file handles?
by shaitand (Initiate) on Jan 30, 2013 at 18:35 UTC

    Just in case someone stumbles on this who actually does for whatever reason need to dynamically open x number of file handles where the programmer has no way to predict what x will be....

    It is easy. You use a scalar in your open i.e. open($fh, then just stick it onto an array or hash.