Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

checking a file handle to see if it is open

by powerhouse (Friar)
on Apr 05, 2006 at 07:15 UTC ( [id://541296]=perlquestion: print w/replies, xml ) Need Help??

powerhouse has asked for the wisdom of the Perl Monks concerning the following question:

Is there a way to test if a file handle like MAIL is open before I use close(MAIL)?

I have someone elses program I bought, and they have tons of stuff going on, then out of the blue they are running the command close(MAIL), and I am getting a recipient error, but the mail is getting sent out. So I think that close(MAIL) is called earlier, and when it is called again, since there is no MAIL open now, it is generating that error, so is there a way to do it like this or something: close(MAIL) if open(MAIL)?

or just close(MAIL) if MAIL;?

Thank you in advance for your wisdom.

thx,
Richard
  • Comment on checking a file handle to see if it is open

Replies are listed 'Best First'.
Re: checking a file handle to see if it is open
by Zaxo (Archbishop) on Apr 05, 2006 at 07:37 UTC

    You can test if the handle is writable with -w HANDLE. That will return false if the handle has been closed.

    I suspect, though, that the errors are coming from the mail application instead of a double close. Here's what happens with a double close:

    $ perl -w -e'open MAIL, ">", "/dev/null"; close MAIL or warn 1, $!; cl +ose MAIL or warn 2, $!' 2Bad file descriptor at -e line 1. $
    The second close would not pick up mail system errors, since the mail system is already dissociated from the dead handle.

    I think your problem is with the mail app, and you don't give much to go on for that. Lots of scripts people sell are not particularly well-written. Does yours use one of the standard CPAN Mail modules?

    After Compline,
    Zaxo

Re: checking a file handle to see if it is open
by borisz (Canon) on Apr 05, 2006 at 07:31 UTC
    my $open = fileno($fh); # if open is NOT undef the file is open
    Boris
Re: checking a file handle to see if it is open
by mickeyn (Priest) on Apr 05, 2006 at 07:29 UTC
    Hi Richard,

    I guess what you asked for is something like:

    close FH unless eof FH;

    Enjoy,
    Mickey

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://541296]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2024-04-24 23:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found