Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

is filehandler used ?

by jeanluca (Deacon)
on Mar 01, 2006 at 14:19 UTC ( #533649=perlquestion: print w/replies, xml ) Need Help??
jeanluca has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

How can I tell if a filehandler is used, for example, the following code does not work
if ( OUT ) { }
Any suggestions ?

Thanks a lot

Replies are listed 'Best First'.
Re: is filehandler used ?
by Fletch (Chancellor) on Mar 01, 2006 at 14:29 UTC

    How (Not) To Ask A Question

    What "doesn't work"? What do you mean by "filehandler is used"? What did you expect that to do? Why would you expect it to do that? How much wood would a woodchuck chuck if a woodchuck could chuck wood?

Re: is filehandler used ?
by Tanktalus (Canon) on Mar 01, 2006 at 17:42 UTC

    First, my confusion: "filehandler" is something that handles files, and "filehandle" is a handle to a (or token for a) file. You have a filehandle.

    Second, the solution: Using fileno works fine for files. On Linux/unix, it probably works for sockets, fifos, and many other things. On no platform does it work for IO::String or handles to a string buffer (e.g., open OUT, '>', \$buffer). If you are only worried about real files, that's fine.

    Instead, you will pretty much need to keep the state seperate. For example, using a symref:

    my $fh; open $fh, "<", "/etc/shadow" or $fh = undef; if ($fh) { # do stuff }
    Now it doesn't matter what you have that you're reading from - failure results in an undef. (Not quite true - if you start using the forking version of open, such as "-|" or "|-", then things have to work a bit differently.)

    Personally, I just merge it all:

    if (open my $fh, "<", "/etc/shadow") { # do stuff. close $fh; }
    Anywhere inside the if, the filehandle is good. Anywhere outside, the filehandle doesn't even exist.

Re: is filehandler used ?
by jeanluca (Deacon) on Mar 01, 2006 at 14:47 UTC
    Ok, here is the situation, if the filehander is used, I would like to use it
    if ( OUT ) { print OUT " bla bla\n" ; }
    unfortunately, if ( OUT ) is always true!!
    Hopefully this makes sense

      if (defined fileno(OUT)) { print OUT "bla bla\n"; }


        any idea of the diff between yours and mine? I've always used *HANDLE:
        use strict; use warnings; open(OUT, ">test.txt") or die "Can't open file to write, $!."; if(defined fileno *OUT){ print "File handle is valid."; }else{ print "Nope."; } close OUT;
Re: is filehandler used ?
by ambrus (Abbot) on Mar 01, 2006 at 19:22 UTC
    if (*STDOUT{IO}) { print "it's used\n"; }

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://533649]
Approved by marto
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (11)
As of 2018-07-17 15:50 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (372 votes). Check out past polls.