How do we do an array of File Handles?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Core Core
    New Member
    • Jun 2010
    • 6

    #1

    How do we do an array of File Handles?

    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!
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    #2
    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

    Comment

    • Core Core
      New Member
      • Jun 2010
      • 6

      #3
      Okay, the first link shows me this at the bottom, which i will try:
      Code:
       ## These are illegal and won't even compile:
      
      @fd = (*STDIN, *STDOUT, *STDERR);
      print $fd[1] "Type it: ";           # WRONG
      $got = <$fd[0]>                     # WRONG
      print $fd[2] "What was that: $got"; # WRONG
      
       ## With print and printf, you get around this by using a block and an expression where you would place the filehandle:
      
      print  { $fd[1] } "funny stuff\n";
      printf { $fd[1] } "Pity the poor %x.\n", 3_735_928_559;
      Pity the poor deadbeef.
      
       ## That block is a proper block, so you can put more complicated code there. This sends the message out to one of two places:
      
      $ok = -x "/bin/cat";                
      print { $ok ? $fd[1] : $fd[2] } "cat stat $ok\n";
      print { $fd[ 1 + ($ok || 0) ]  } "cat stat $ok\n";           
      
       ## 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:
      
      $got = readline($fd[0]);
      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. =)

      Comment

      • eWish
        Recognized Expert Contributor
        • Jul 2007
        • 973

        #4
        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.

        Comment

        • Core Core
          New Member
          • Jun 2010
          • 6

          #5
          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], "<$filename s[$i]",); { do some things;} close ($FHA[$i]);
          But the syntax bombs...

          Comment

          • RonB
            Recognized Expert Contributor
            • Jun 2009
            • 589

            #6
            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.

            Comment

            Working...