Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Test if file handle exists

Posted on 2004-08-03
7
Medium Priority
?
876 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 400 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 1000 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

Industry Leaders: 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

In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
Six Sigma Control Plans

604 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