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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can i check if a given FILEHANDLE is open ? 1

Status
Not open for further replies.

MoshiachNow

IS-IT--Management
Joined
Feb 6, 2002
Messages
1,851
Location
IL
HI,
I have one sub that uses the open FILEHANDLE ,and another sub that closes it, occasionaly.
I need the first sub to check if the FILEHANDLE is open,prior to using it.

Appriciate any help.

Long live king Moshiach !
 
You could use the IO::Select module and call the can_read (or can_write, depending on the type of the filehandle).
 
cpan for open() method says
open() will warn you if you don't close its filehandle explicitly before the program ends. It will also warn if you give it an already open filehandle.

Normal practice if you're writing a bunch of things to the file at once is to open the file, write to it, then close it.

For things like log files, where you'll be writing to it a little at a time for a long time, the usual practice is to open, write, and close each time.

I'm not aware of any general way to test a filehandle for openness (though IO::File may provide one... perldoc it). Normally it's not necessary since the file is only open for a short section of code. But if you needed to, you could always set a flag whenever you open the filehandle, and clear it when you close it.

so create a global $openfile = 0; then when you open the file check for (!$openfile) then set $openfile = 1; and when you close set it to zero again.




"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top