Solved

Test if file handle exists

Posted on 2004-08-03
7
827 Views
Last Modified: 2012-05-05
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.
0
Comment
Question by:rtsh
  • 3
  • 2
7 Comments
 
LVL 8

Assisted Solution

by:davorg
davorg earned 100 total points
Comment Utility
I'd probably use a lexically scoped filehandle for this.

#!/usr/bin/perl
                                                                               
use strict;
use warnings;
                                                                               
my $log;
                                                                               
log_this("error 1\n");
openlog();
log_this("error 2\n");
                                                                               
{
  my $log;
                                                                               
  sub openlog {
    open $log, '>', 'log.txt' or die $!;
  }
                                                                               
  sub log_this {
    if ($log) {
      print $log @_;
    } else {
      warn @_;
    }
  }
}

Or something like that.

Dave...
0
 
LVL 1

Author Comment

by:rtsh
Comment Utility
(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)
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
You might also try something like
if( -f LOG ){ print LOG @_; }
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 1

Author Comment

by:rtsh
Comment Utility
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?
0
 
LVL 84

Accepted Solution

by:
ozo earned 250 total points
Comment Utility
eval{no warnings; print LOG @_} or warn @_;
0
 
LVL 1

Author Comment

by:rtsh
Comment Utility
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.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now