By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,710 Members | 1,906 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,710 IT Pros & Developers. It's quick & easy.

How do we do an array of File Handles?

P: 6
I want to open and read a number of files, but this number is an unknown at compile time. So logically i want an array of File Handles to open and read them all, such as FH[0], FH[1], FH[2], ... etc.

How is this done? Are there any examples anywhere? Tia!
Jun 26 '10 #1
Share this Question
Share on Google+
5 Replies


numberwhun
Expert Mod 2.5K+
P: 3,503
Not sure if it helps you, but I found this link from the Perl Cookbook, and this from Perlmonks. Both seems to discuss what you are trying to do.

Regards,

Jeff
Jun 26 '10 #2

P: 6
Okay, the first link shows me this at the bottom, which i will try:
Expand|Select|Wrap|Line Numbers
  1.  ## These are illegal and won't even compile:
  2.  
  3. @fd = (*STDIN, *STDOUT, *STDERR);
  4. print $fd[1] "Type it: ";           # WRONG
  5. $got = <$fd[0]>                     # WRONG
  6. print $fd[2] "What was that: $got"; # WRONG
  7.  
  8.  ## With print and printf, you get around this by using a block and an expression where you would place the filehandle:
  9.  
  10. print  { $fd[1] } "funny stuff\n";
  11. printf { $fd[1] } "Pity the poor %x.\n", 3_735_928_559;
  12. Pity the poor deadbeef.
  13.  
  14.  ## That block is a proper block, so you can put more complicated code there. This sends the message out to one of two places:
  15.  
  16. $ok = -x "/bin/cat";                
  17. print { $ok ? $fd[1] : $fd[2] } "cat stat $ok\n";
  18. print { $fd[ 1 + ($ok || 0) ]  } "cat stat $ok\n";           
  19.  
  20.  ## This approach of treating print and printf like object methods calls doesn't work for the diamond operator, because it's a real operator, not just a function with a comma-less argument. Assuming you've been storing typeglobs in your structure as we did above, you can use the built-in function named readline to read a record just as <FD> does. Given the preceding initialization of @fd, this would work:
  21.  
  22. $got = readline($fd[0]);
  23.  
The next link outlines a few more ways (though convoluted and possibly deprecated code(?), i'm running 5.12.x). Though new to Perl, i'm completely struck by its bend over backwards implementation of simple FH arrays as well as Array of Arrays!

Thanks Jeff for the links for two reasons: one to find the answer(s), and two for even more sites i'll search for help as i need it. =)
Jun 26 '10 #3

eWish
Expert 100+
P: 971
Why do you need to use an array of filehandles? If you use a lexical filehandle when it goes out of scope you don't have to worry about the things you listed.
Jun 26 '10 #4

P: 6
I want a indexable random number of open files to read from in Main. e.g.
my @FHA = (); # File Handle Array.
my @filenames = (); # Actual File Names in Array.
...
open($FHA[$i], "<$filenames[$i]",); { do some things;} close ($FHA[$i]);
But the syntax bombs...
Jun 26 '10 #5

Expert Mod 100+
P: 589
It's rare to want or need to use an array of filehandles, but I do have one script where I'm storing a few in a hash. In this case I'm running perl 5.8.8.

I ran a few tests on 5.10.1 and it appears that they took out support for using an array or hash for filehandles. You need to use a scalar.
Jun 27 '10 #6

Post your reply

Sign in to post your reply or Sign up for a free account.