develooper Front page | perl.beginners | Postings from March 2002

RE: Testing for filehandles

Thread Previous | Thread Next
From:
eric-perl
Date:
March 28, 2002 12:33
Subject:
RE: Testing for filehandles
Message ID:
Pine.LNX.4.33.0203281108580.20785-100000@lucy
On Thu, 28 Mar 2002, Bob Showalter wrote:
> > From: eric-perl@pretorious.net [mailto:eric-perl@pretorious.net]
> > Thanks, Bob! After reading the IO::Handle man page, I decided 
> > to distill this approach a bit further:
> > 
> >   print F if fileno(F);
> 
> Of course, that's even simpler! Just to be nitpicky, the test maybe 
> should be 
> 
>    defined(fileno(F))
> 
> Since 0 is a valid file number (STDIN).

So, technically, I should be testing for "definedness" instead of "truth". 
i.e., From `perldoc -f fileno`:

  Returns the file descriptor for a filehandle, or
  undefined if the filehandle is not open.

> If you're just going to do the test on the print statement, it's
> not accomplishing all that much, since the print() will silently
> fail if the file isn't open. But a test like this would be useful
> if you want to avoid doing a lengthy operation to generate the
> data to be printed.

Printing to a non-existant filehandle (while using the -w switch)
generates a warning message:

  Filehandle main::FOO never opened at...

-- 
Eric P.
Los Gatos, CA



Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About