Article 9228 of comp.lang.perl:
Xref: feenix.metronet.com comp.lang.perl:9228
Newsgroups: comp.lang.perl
Path: feenix.metronet.com!news.utdallas.edu!corpgate!bnrgate!bnr.co.uk!uknet!pipex!uunet!boulder!wraeththu.cs.colorado.edu!tchrist
From: Tom Christiansen <
[email protected]>
Subject: Re: filehandles and defined
Message-ID: <
[email protected]>
Originator:
[email protected]
Sender:
[email protected] (USENET News System)
Reply-To:
[email protected] (Tom Christiansen)
Organization: University of Colorado, Boulder
References: <
[email protected]>
Date: Wed, 29 Dec 1993 21:04:45 GMT
Lines: 123
:-> In comp.lang.perl,
[email protected] writes:
:
:In perl 4, I'd like to check if a filehandle has been "defined", in other words
:is open. I would use this in a cleanup routine that can be called from any point
:in my program. It would be nice to be able to close all open filehandles, but if
:a particular one is not open, you get a nasty error message.
:
: if (defined(FH)) { close(FH); }
:
:isn't legal syntax.
:
: if (defined(fileno(FH))) { close(FH); }
:
:does not work if FH has never been opened. This is another case where
:filehandle != variable is confusing.
I notice that you said perl4. What you have there works in perl5.
Meanwhile, if you do this:
if (defined($fd = fileno(FH))) { close(FH); }
Then it will work. It's still not goin to take care of filehandles you
don't know about. You might experiment with this little program and its
functions.
#!/usr/bin/perl
$PTRLEN = length ( $_main{'_main'} );
<DATA>;
open(TMPR, "< .") || die;
open(TMPW, "> /dev/null") || die;
opendir(TMPD, ".") || die;
@dirlist = readdir(TMPD);
fileno STDERR;
fileno STDIN;
fileno STDOUT;
fileno DATA;
fileno TMPR;
fileno TMPW;
# fileno TMPD;
printf NOWHERE "fileno never is %d\n", defined($t = fileno(NEVEROPEN));
printf NOWHERE "fileno bogus is %d\n", defined($t = fileno(BOGUS));
printf NOWHERE "fileno tmpr is %d\n", defined($t = fileno(TMPR));
close(NEVEROPEN);
$this_is_a_scalar = ();
@this_is_a_list = ();
%this_is_a_table = ();
&dumpfh('main');
format THIS_IS_A_FORMAT =
@<<<<
$x