in reply to Re: how can i use one handle for multifple files
in thread how can i use one handle for multifple files

The other benefit here, is that scalars can go out of scope, while file handles can't.. which can be very advantageous in some cases, because it provides for automatic cleanup so you don't have to close your filehandles explicitly in subs and blocks.
  • Comment on Re: how can i use one handle for multiple files

Replies are listed 'Best First'.
Re: Re: how can i use one handle for multiple files
by bart (Canon) on May 15, 2003 at 13:04 UTC
    File handles can go out of scope, if you use localized typeglobs.
    { local *FH; open FH, ">output.txt" or die "Can't write to file: $!"; print FH "Anything...\n"; printf STDERR "fileno for FH: %d\n", fileno(FH); } # ... open FH2, ">output2.txt" or die "Can't write to file: $!"; printf STDERR "fileno for FH2: %d\n", fileno(FH2);
    The file handle will be closed when the block is exited. It is extremely likely that you'll see the same value for fileno() for both handles... (for this as a script, I expect to see the value 3) indicating that the first one was released, thus, the handle got closed.