Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Open multiple file handles?

by davido (Archbishop)
on May 07, 2011 at 02:57 UTC ( #903482=note: print w/ replies, xml ) Need Help??


in reply to Open multiple file handles?

Perl has an operator that does what you're asking for. It's the diamond operator, and when used 'empty', it reads each item listed in @ARGV sequentially. See perlop. Here's a snippet from the documentation:

The null filehandle <> is special: it can be used to emulate the behavior of sed and awk. Input from <> comes either from standard input, or from each file listed on the command line. Here's how it works: the first time <> is evaluated, the @ARGV array is checked, and if it is empty, $ARGV[0] is set to "-", which when opened gives you standard input. The @ARGV array is then processed as a list of filenames. The loop

while (<>) { ... # code for each line }

is equivalent to the following Perl-like pseudo code:

unshift(@ARGV, '-') unless @ARGV; while ($ARGV = shift) { open(ARGV, $ARGV); while (<ARGV>) { ... # code for each line } }

Dave


Comment on Re: Open multiple file handles?
Select or Download Code
Re^2: Open multiple file handles?
by onlyIDleft (Acolyte) on May 07, 2011 at 04:05 UTC

    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?

      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

      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.

      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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://903482]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (13)
As of 2014-10-22 16:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (119 votes), past polls