Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checking filehandles

Status
Not open for further replies.

redgenie

Programmer
Aug 10, 2000
36
GB
Hey everyone,

Does anyone know how to check if a filehandle is currently open for writing (or open for that matter) see below:

Code:
$SIG{'INT'} = 'SIGNALHANDLER';

open FILE, ">/somefile";
# Some one presses Ctrl C here!
close FILE;
# Some one may press Ctrl C here!


sub SIGNAL HANDLER {

        if (FILE) {    <-- This bit wrong
                close FILE;
        } else {
                 print "Filehandle wasn't open.";
        }

        exit;
}


Any help would be greatly appreciated
 
Obvious wrong bit in your code is that:

sub SIGNAL HANDLER {

should be:

sub SIGNALHANDLER {

That may just be a typo here though.

I would try and use the filehandle, wrapping that bit of code in an eval(), if I wanted to test its state.

Mike

"Deliver me from the bane of civilised life; teddy bear envy."

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

 
This isn't entirely bulletproof, but it should work for what you're trying to do.

Code:
open FH, "< somefile.txt" or die;

if (fileno(FH)) {print "Filehandle is open.\n";}
else {print "Filehandle is closed.\n";}

close FH;

if (fileno(FH)) {print "Filehandle is open.\n";}
else {print "Filehandle is closed.\n";}
 
Oops, sorry for that yes it was just a typo in here. I'll give your eval() suggestions a try.
 
Thanks, rharsh. Not sure if that works when writing I thought I had looked at that but I'll give it another try.
 
rharsh's fileno() solution looks better to me.

Mike

"Deliver me from the bane of civilised life; teddy bear envy."

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

 
Many thanks for that rharsh, it does work with files open for writing.

Thank you very much for your help! :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top