Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

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.

Dave


Comment on Re^3: Open multiple file handles?
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2014-07-10 02:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (198 votes), past polls