in reply to Variable Filehandles

Seems to me that FileHandle is a module that might help you out here. Roughly speaking, it lets you treat filehandles as regular scalars. That's very rough (see the module documentation for more info. It's a standard module)

Philosophy can be made out of anything. Or less -- Jerry A. Fodor

Replies are listed 'Best First'.
RE (tilly) 2: Variable Filehandles
by tilly (Archbishop) on Nov 02, 2000 at 23:12 UTC
    It is also an enormous speed hit, and you can already do that, either by using Symbol and then calling gensym, or with the following:
    my $fh = do {local *FH}; my $file = "foo.txt"; open ($fh, "> $file") or die "Cannot write '$file': $!"; print $fh "Hello world\n";
    And yes, you can keep filehandles in arrays, etc.

    But note that if you try to abuse this, you could run out of filehandles in your program. If lots of programs all open lots of filehandles, you could run out of filehandles on your machine. So it is best to not leave a million of these open...

RE: Re: Variable Filehandles
by Adam (Vicar) on Nov 02, 2000 at 23:04 UTC
    Its also out-dated. I beleive the standards now are the IO::blah modules (IO::File, IO::Handle, IO::Pipe, etc) which are also part of the standard, and that FileHandle.pm is only included to support old scripts. I've heard people complain about these as well, but if you try to use STDOUT as an object, perl demands IO::Handle
    prompt>perl -we "STDOUT->print('test')" Can't locate object method "print" via package "IO::Handle" at -e line + 1. prompt>perl -MIO::Handle -we "STDOUT->print('test')" test
    That's using 5.6 (ActiveState 613) on NT4, so I'm not sure how far back this goes. (And yes, that example also works under strict)
      Those are also slower.

      With Perl 5.6 you don't need to generate symbols. Just use a scalar as a filehandle and it Just Works.