Solved

Test if file handle exists

Posted on 2004-08-03
7
850 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
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

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.…
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…
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

773 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