Link to home
Start Free TrialLog in
Avatar of rtsh
rtsh

asked on

Test if file handle exists

In some code with exhaustive error handling, it calls a particular function to write to a log file, which does a "print LOG $message;".  However, if an error occurs before LOG is opened, then (obviously) I see a "print() on unopened filehandle LOG" error.

If I try wrapping the "print LOG $message" with "if (defined(LOG)) { ... }", I get a "Bareword "LOG" not allowed while "strict subs" in use".

So, my question is: How can I test whether LOG is an open file handle?

Fast answer appreciated.
SOLUTION
Avatar of Dave Cross
Dave Cross
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rtsh
rtsh

ASKER

(NB For future people viewing this question: I haven't actually tested this code (yet), but it gives a good enough idea as to how it could be done - ie not using LOG but $log as the file handle)
You might also try something like
if( -f LOG ){ print LOG @_; }
Avatar of rtsh

ASKER

ozo... now that looks a lot neater for what I want to do, but I still get a warning:

"-f on unopened filehandle LOG at file.pl line X"

Anyone else got any good ideas?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rtsh

ASKER

ozo. Brilliant. Worked perfectly. You can sleep easy tonight that you've helped a busy coder solve an annoying error.

If there's an admin around that can split the points I awarded to davorg between him and ozo, or let me know how I can do it, that'd be excellent.