How to check that a filehandle is open?

Discussion in 'Perl Misc' started by Ilya Zakharevich, Apr 25, 2009.

  1. Re the subject: 10 years ago I would check

    defined fileno $fh

    But nowadays, filehandle does not necessarily has an OS file
    descriptor associated to it...

    I could also check *$fh{IO} - but it would be defined if, e.g., $fh is
    \*FOO, and a bareword FOO was *mentioned* in the code at place where
    filehandle is expected (the code may not be ever executed, just
    mentioning is enough). Moreover, I expect that *$fh{IO} survives
    after close()ure of $fh.

    I could do

    defined eval { seek $fh, 0, 1 }

    but the answer is ALWAYS defined (although sometimes ''), and, to add
    insult to injury, would send a warning in my wake...

    So: what should one do to check that $fh is open()?

    Thanks,
    Ilya
    Ilya Zakharevich, Apr 25, 2009
    #1
    1. Advertising

  2. On 2009-04-25, Ilya Zakharevich <> wrote:
    > Re the subject: 10 years ago I would check
    >
    > defined fileno $fh
    >
    > But nowadays, filehandle does not necessarily has an OS file
    > descriptor associated to it...
    >
    > I could also check *$fh{IO} - but it would be defined if, e.g., $fh is
    > \*FOO, and a bareword FOO was *mentioned* in the code at place where
    > filehandle is expected (the code may not be ever executed, just
    > mentioning is enough). Moreover, I expect that *$fh{IO} survives
    > after close()ure of $fh.
    >
    > I could do
    >
    > defined eval { seek $fh, 0, 1 }
    >
    > but the answer is ALWAYS defined (although sometimes ''), and, to add
    > insult to injury, would send a warning in my wake...
    >
    > So: what should one do to check that $fh is open()?


    IO::Handle provides a bit of hope

    perl -MIO::Scalar -wle '
    $sfh = IO::Scalar->new(\$data);
    print "foo" if $sfh->opened;
    print $sfh->fileno;
    '
    Name "main::data" used only once: possible typo at -e line 2.
    foo
    Can't locate object method "FILENO" via package "IO::Scalar" at /usr/lib/perl/5.10/IO/Handle.pm line 132.

    YMMV however

    perl -MIO::Scalar -MUNIVERSAL -wle '
    print IO::Scalar->isa("IO::Handle") ? 1 : 0;
    print IO::Scalar->can("opened") ? 1 : 0;
    '
    1
    1

    but

    perl -MIO::Zlib -MUNIVERSAL -wle '
    print IO::Zlib->isa("IO::Handle") ? 1 : 0;
    print IO::Zlib->can("opened") ? 1 : 0;
    '
    0
    1

    I think that's possible to step on class that provides enough API of
    B<IO::Handle> for use and lacks B<opened> method.

    --
    Torvalds' goal for Linux is very simple: World Domination
    Stallman's goal for GNU is even simpler: Freedom
    Eric Pozharski, Apr 25, 2009
    #2
    1. Advertising

  3. Ilya Zakharevich

    Bo Lindbergh Guest

    In article <>,
    Ilya Zakharevich <> wrote:

    > Re the subject: 10 years ago I would check
    >
    > defined fileno $fh
    >
    > But nowadays, filehandle does not necessarily has an OS file
    > descriptor associated to it...


    In which case fileno returns some defined value which can't be mistaken
    for a file descriptor. -1 is the traditional choice (used by e.g.
    PerlIO::scalar).


    /Bo Lindbergh
    Bo Lindbergh, Apr 25, 2009
    #3
  4. Ilya Zakharevich

    smallpond Guest

    On Apr 25, 2:11 am, Ilya Zakharevich <> wrote:
    > Re the subject: 10 years ago I would check
    >
    >    defined fileno $fh
    >
    > But nowadays, filehandle does not necessarily has an OS file
    > descriptor associated to it...
    >
    > I could also check *$fh{IO} - but it would be defined if, e.g., $fh is
    > \*FOO, and a bareword FOO was *mentioned* in the code at place where
    > filehandle is expected (the code may not be ever executed, just
    > mentioning is enough).  Moreover, I expect that *$fh{IO} survives
    > after close()ure of $fh.
    >
    > I could do
    >
    >   defined eval { seek $fh, 0, 1 }
    >
    > but the answer is ALWAYS defined (although sometimes ''), and, to add
    > insult to injury, would send a warning in my wake...
    >
    > So: what should one do to check that $fh is open()?
    >
    > Thanks,
    > Ilya



    Scalar::Util::eek:penhandle $fh;

    returns undef unless $fh is open or tied.
    smallpond, Apr 25, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andrew Hamm

    Archive::Tar and write to an open filehandle

    Andrew Hamm, Oct 9, 2003, in forum: Perl Misc
    Replies:
    1
    Views:
    189
    Jens M. Felderhoff
    Oct 9, 2003
  2. Matthew Braid
    Replies:
    1
    Views:
    203
    Ben Morrow
    Feb 11, 2004
  3. Rex Gustavus Adolphus
    Replies:
    17
    Views:
    203
    Rex Gustavus Adolphus
    Mar 7, 2004
  4. PerlFAQ Server
    Replies:
    0
    Views:
    102
    PerlFAQ Server
    Jan 13, 2011
  5. PerlFAQ Server

    FAQ 5.13 How can I open a filehandle to a string?

    PerlFAQ Server, Apr 7, 2011, in forum: Perl Misc
    Replies:
    0
    Views:
    114
    PerlFAQ Server
    Apr 7, 2011
Loading...

Share This Page