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

Re: Testing for filehandles

Thread Previous
From:
Chas Owens
Date:
March 27, 2002 13:41
Subject:
Re: Testing for filehandles
Message ID:
1017265089.29566.210.camel@tert.icallinc.com
On Wed, 2002-03-27 at 15:18, eric-perl@pretorious.net wrote:
> Hello, All:
> 
> I've looked around for an answer to this (The Camel Book, The Ram Book, 
> perldoc, google.com, etc.) but can't find a thing: Is it possible to test 
> for the existence of a filehandle?
> 
> I've got a small script that <optionally> opens a filehandle. If that 
> filehandle exists, I'd like to print to it. Otherwise, don't. e.g.,
> 
> use Getopts::Std;
> getopts("n");
> open(OPT_LOG,">/tmp/foo.txt") if ($opt_n);
> while (<>) {
> 	print OPT_LOG $_ if OPT_LOG;
> }
> close(OPT_LOG) if OPT_LOG;
> 
> -- 
> Eric P.
> Los Gatos, CA
> 

What you want is select (assuming you want the output to go to the
screen if they don't specify a file).  See "perldoc -f select".

-- 
Today is Sweetmorn the 13rd day of Discord in the YOLD 3168
Kallisti!

Missile Address: 33:48:3.521N  84:23:34.786W


Thread Previous


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