Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Answer: How can I create an array of filehandles?

by perlmonkey (Hermit)
on Apr 25, 2000 at 08:09 UTC ( #8882=categorized answer: print w/replies, xml ) Need Help??

Q&A > files > How can I create an array of filehandles? - Answer 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++; }

Replies are listed 'Best First'.
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:


        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?

What's my password?
Create A New User
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2017-04-25 01:57 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (447 votes). Check out past polls.