Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Re^3: Open multiple file handles?

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

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

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.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://903497]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2017-11-22 04:38 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (315 votes). Check out past polls.