Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Answer: How can I create an array of filehandles?

( #8882=categorized answer: print w/ replies, xml ) Need Help??

Q&A > files > How can I create an array of filehandles? contributed by perlmonkey

The trick is doing someting like:
open(FILE, "myfile.txt") $file = *FILE
After that you can use $file as a normal file handle so you can do:
print $file "Hello File"; close $file;
So here is how to put it all in an array:
#!/usr/bin/perl my @filehandles; #make array of 10 file handles for($i=0; $i<10; $i++) { #localize the file glob, so FILE is unique to # the inner loop. local *FILE; open(FILE, ">file$i.txt") || die; #push the typeglobe to the end of the array push(@filehandles, *FILE); } $count=0; #loop through the file handles. # treat $file as a normal file handle foreach $file (@filehandles) { print $file "File $count"; close $file; $count++; }

Comment on Answer: How can I create an array of filehandles?
Select or Download Code
RE: Answer: I need an array of filehandles.
by Anonymous Monk on Apr 25, 2000 at 09:21 UTC
    Here's another way of doing this as well ..

    What i'm doing is actually a hash of file handles, but it works the same way .. I'll format this example to use an array. Each part is spread out in individual sections so you can open and close and print anywhere you choose:

    
    foreach $x (0.. 10) {
       $handles$x = anon_fh()
    }
    # other code here
    
    # open handles
    foreach $fh (@handles) {
       open ($fh,  "file.txt");
    }
    
    # code here??
    
    foreach $fh (@handles) {
    # stuff .. 
       print { $fh } "Text wil do into selected file";
    # stuff
    }
    
    # more code??
    
    foreach $fh (@handles) {
       close ($fh);
    }
    
    sub anon_fh {
       local *FH;
       return *FH;
    }
    
    Doing print { $handle4 } "Testing" should work to. Something I've learning is that file handles stored in a variable (hash, array, or scalar) needs to be used in a block context {}. I'm not exactly sure why, but problems happen when I don't use the blocks. So I use them. Well.. Just found out that scalars don't have to be in a block, but arrays and hashes do (just did a few perl tests while I was writting this ;). Exact reason why.. I still don't know, I'm just telling you from the errors I got ;) -- philip
      That was actually me (guice) that posted that example.. I appears that this form is dropping my handle :( BTW .. the { $handle4 } is suppose to be: { $handle[4] } .. Appears the form doesn't like [ and ] .. It made a link out of it.. oops ;)

        I glanced at the page source, and saw you used a PRE tag, maybe a CODE tag works better?
        Here, lets test:

        PRE:
        $array$i
        
        CODE:
        $array[$i]

        Hmmm, I wish I could preview this sucker before I submit...

RE: Answer: I need an array of filehandles.
by Anonymous Monk on Apr 25, 2000 at 15:43 UTC
    You don't need a function do this. You can just:
    open( FILE, "< $file" ) or die( "Bad things happened : $!" ); my $fh = \*FILE; # # Now use $fh as you see fit... # print $fh "Hello, world\n"; # Shouldn't work...FILE was opened RDONLY + :-) $line = <$fh>; do_stuff( $fh ); sub do_stuff { my $fh = shift; print $fh "Whatever...\n"; }
    Hope this helps
Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (8)
As of 2014-10-25 15:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (145 votes), past polls