Solved

Test if file handle exists

Posted on 2004-08-03
7
871 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
7 Comments
 
LVL 8

Assisted Solution

by:davorg
davorg earned 100 total points
ID: 11701783
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
ID: 11701876
(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
ID: 11711287
You might also try something like
if( -f LOG ){ print LOG @_; }
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:rtsh
ID: 11712945
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
ID: 11713213
eval{no warnings; print LOG @_} or warn @_;
0
 
LVL 1

Author Comment

by:rtsh
ID: 11714335
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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 …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Six Sigma Control Plans

724 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