Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

checking bareword-filehandle

by morgon (Deacon)
on Aug 06, 2012 at 13:08 UTC ( #985699=perlquestion: print w/ replies, xml ) Need Help??
morgon has asked for the wisdom of the Perl Monks concerning the following question:

Hi

I have inherited some tangled mess of code that uses barewords for filehandles like so:

open FH, ">whatever" or warn "blah"; # much much later print FH "something";
The problem is that when the open fails the code goes on (doing many other things) and when it comes to the print Perl emits a warning as FH is not an open filehandle (strict and warnings are on).

I don't want to go into the code right now, all I want is to suppress the warning, but I cannot write

if(FH) { print FH "something" }
as FH is a bareword.

So my question is: What is the idiomatic way to check a such a filehandle?

Many thanks!

Comment on checking bareword-filehandle
Select or Download Code
Re: checking bareword-filehandle
by johngg (Abbot) on Aug 06, 2012 at 13:14 UTC

    Wrap the print in an eval perhaps?

    Cheers,

    JohnGG

        seems not to work in my old Perl (5.8.8):
        use strict; use warnings; no warnings qw(unopened); open HUBBA, "/hubba"; print HUBBA "hubba";
        Produces a warning "print() on closed filehandle HUBBA at t line 6."

        Guess I have to disable all warnings...

Re: checking bareword-filehandle
by moritz (Cardinal) on Aug 06, 2012 at 13:18 UTC
      Evidently, but at the moment the open is in a different lexial scope as the print so I would need to declare a lexical $FH in the common enclosing scope - I'll do that eventually.

      But apart from your suggestions for suppressing the warning outright - is there really no easy way for checking such a handle?

Re: checking bareword-filehandle
by aitap (Deacon) on Aug 06, 2012 at 13:23 UTC
    Perhaps, autodie can help you? For example, use autodie qw(open close);.
    Sorry if my advice was wrong.
Re: checking bareword-filehandle
by ww (Bishop) on Aug 06, 2012 at 13:46 UTC
    So change to a lexical filehandle.

    Or does "I don't want to go into the code right now" mean something other than "I don't have time/inclination/skills to do it right?"

Re: checking bareword-filehandle
by BrowserUk (Pope) on Aug 06, 2012 at 14:04 UTC

    if( fileno(FH) ) { print FH "something" }

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

Re: checking bareword-filehandle
by rovf (Priest) on Aug 06, 2012 at 14:59 UTC
    How about changing the open to

    use File::Spec::Functions qw(devnull); ... if(!open(FH, ">...")) { warn " ... $!..."; open(FH,'>',devnull) or die "$!"; }
    If you can't even write to the bitbucket, something is really messed up in your system, so I think it makes sense to die in that case.

    -- 
    Ronald Fischer <ynnor@mm.st>
Re: checking bareword-filehandle
by davido (Archbishop) on Aug 06, 2012 at 16:42 UTC

    Checking a filehandle is problematic. Why not change your open to something like this:

    open FH, '<', 'filename' or do { $failed = 1; warn $! };

    Now you've got a reliable Boolean variable rather than fragile hackish testing of the filehandle.


    Dave

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://985699]
Approved by Corion
Front-paged by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (10)
As of 2014-12-18 21:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (66 votes), past polls