Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Opening multiple filehandlers

by leons (Pilgrim)
on Apr 06, 2001 at 11:24 UTC ( #70418=perlquestion: print w/replies, xml ) Need Help??

leons has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks

Can anyone tell me how I can open multiple filehandlers ?

#!/usr/bin/perl -w my @FHlist; open FH1, ">./1.txt"; open FH2, ">./2.txt"; open FH3, ">./3.txt"; push @FHlist,*{FH1},*{FH2},*{FH3}; foreach my $FH (@FHlist) { print $FH "Don't eat the yellow snow\n"; } print "@FHlist\n"; close FH3; close FH2; close FH1;

This example clearly opens 3 filehandlers ... but how can I do
this when I need to open N filehandlers ?

What I actually want to do is (see my last node) opening
a certain number of sessions to a remote host. Each session is
being handled by a new instance of the parent process ....

Anyway .... I really can't get it to work ...


Thanks, Leon

Replies are listed 'Best First'.
Re: Opening multiple filehandlers
by btrott (Parson) on Apr 06, 2001 at 11:28 UTC
    How about using Symbol:
    use Symbol; my $N = 15; my @FH; for my $i (0..$N-1) { my $fh = gensym; open $fh, ">$i.txt" or die "Can't open $i.txt: $!"; push @FH, $fh; }
    Now all of your filehandles are in @FH, and you can loop through there and do what you will with them. Will this work for you?

    Note that in Perl 5.6.0 you can just do:

    open my $fh, ">$i.txt" or die "Can't open $i.txt: $!";
    And it Just Works. You could then take out the 'use Symbol' and 'gensym' bits.
      Yep. It works perfectly ! Thanks

      Aha, I didn't know the fact that you could use:
      open my $fh, ">$i.txt" or die "Can't open $i.txt: $!";
      in Perl 5.6.0.

      I tried something similar on a system that ran a lower version
      and I couldn't get it to work. But knowing that this works in
      Perl 5.6.0, really makes life lots easier !!!

      I'm a happy man ;-)


(dkubb) Re: (2) Opening multiple filehandlers
by dkubb (Deacon) on Apr 06, 2001 at 13:22 UTC

    As an alternative to btrott's example using Symbol to create file handle references, you can also use IO::File to help you create n filehandles and push them onto an array:

    #!/usr/bin/perl -w use strict; use IO::File; my @file_handles; foreach my $number (0..100) { my $fh = IO::File->new("> $number.txt") or die "Could not open file $number.txt: $!"; push @file_handles, $fh; }
Re: Opening multiple filehandlers
by AgentM (Curate) on Apr 06, 2001 at 21:00 UTC
    Be very careful with this! While you may have a legitimate reason for doing this, you can hardly anticipate its failures as cross-platform code. The standard DOS install allowed for 20 file descriptors system-wide! That means that opening 100 would merely crash your program. You would effectively be left with 17 or less file descriptors. On modern OSs, each program may be limited to a certain no. (minimally X by POSIX (Petruchio has my book on this)) and also the system-wide total. It might simply be better to open each file in the loop, output the info, close the filehandle and reuse it.
    AgentM Systems nor Nasca Enterprises nor Bone::Easy nor Macperl is responsible for the comments made by AgentM. Remember, you can build any logical system with NOR.
      Historically, many unices are set to either 128 or 256. These days I believe Linux is most often at either 256 or 1024 depending on the distribution and sysadmin tinkering. AFAIK the *BSD family tend to 256. I am unaware of PC or Mac defaults and whether they can be changed. IRIX was 256, Sun was 256, HP was 256; last I checked. If you keep it reasonable, say less than 100 you seem to be ok just about everywhere but the pathological cases.

      $you = new YOU;
      honk() if $you->love(perl)

Re: Opening multiple filehandlers
by stefan k (Curate) on Apr 06, 2001 at 12:25 UTC
    Yeah, this is off-topic, but still:
    I just wanted to give this little advice to
    watch out where the huskies go

    *grin* sorry, couldn't resist

    Regards Stefan K

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://70418]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2023-05-30 21:37 GMT
Find Nodes?
    Voting Booth?

    No recent polls found