Solved

Test if file handle exists

Posted on 2004-08-03
7
865 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Perl for loop for 2000 ms 7 109
remove duplicates from the csv file 13 118
Executing multiple sybase statements in perl dbi 2 100
Perl script to process a .csv file 18 65
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 …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…

733 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