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

RE: Testing for filehandles

Thread Previous | Thread Next
From:
Timothy Johnson
Date:
March 27, 2002 13:40
Subject:
RE: Testing for filehandles
Message ID:
C0FD5BECE2F0C84EAA97D7300A500D50025811C8@SMILEY

You'd probably have better luck testing for the open() command's success

use Getopts::Std;
getopts("n");
if($opt_n){
   open(OPT_LOG,">/tmp/foo.txt") || die "Could not open foo.txt!\n";
}

-----Original Message-----
From: eric-perl@pretorious.net [mailto:eric-perl@pretorious.net]
Sent: Wednesday, March 27, 2002 12:18 PM
To: Beginners Perl Mailing List
Subject: Testing for filehandles


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


-- 
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org


--------------------------------------------------------------------------------
This email may contain confidential and privileged 
material for the sole use of the intended recipient. 
If you are not the intended recipient, please contact 
the sender and delete all copies.

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