in reply to Best way to check if something is a file handle?

IO::Detect 0.001

Hm. Another whole module substitute for a single, built-in function?


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?

  • Comment on Re: Best way to check if something is a file handle?

Replies are listed 'Best First'.
Re^2: Best way to check if something is a file handle?
by tobyink (Canon) on Jul 09, 2012 at 19:28 UTC

    A whole module that does what I want as a substitute for a single, built-in function that does not, and actually dies in some cases I'd want it to return true.

    use 5.010; use Test::More tests => 6; use IO::Detect; use IO::All; use IO::Scalar; my $filename = '/dev/null'; open my $fh, '>', $filename; my $all = IO::All->new($filename); my $scalar = IO::Scalar->new(\(my $dummy)); diag "Testing IO::Detect"; ok is_filehandle($fh); ok is_filehandle($all); ok is_filehandle($scalar); diag "Testing fileno"; ok defined fileno($fh); ok defined fileno($all); ok defined fileno($scalar);
    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
      A whole module that does what I want ... in some cases I'd want it to return true.

      That is only the case if you define "what you want" as: "What my newly minted module does."

      But, what does a.n.other user actually need? And does your is_filehandle() actually supply it?

      That is, someone is writing a module that accepts a "file handle" as a input argument, and they want to know if what they've been given, is acceptable for their purpose. That means they can either read from it; write to it; or both. And your module fails to detect that information.

      #! perl -slw use strict; use IO::Detect qw[ is_filehandle ]; sub funcTakesIO1 { my $fh = shift; if( is_filehandle( $fh ) ) { print $fh 'Bang!'; } return; } sub funcTakesIO2 { my $fh = shift; if( is_filehandle( $fh ) ) { return <$fh>; } return; } eval { funcTakesIO1( \*STDIN ); } or warn "IO::Detect detected the wrong thing"; eval { funcTakesIO2( \*STDOUT ); } or warn "IO::Detect detected the wrong thing"; __END__ C:\test>junk5 Filehandle STDIN opened only for input at C:\test\junk5.pl line 9. IO::Detect detected the wrong thing at C:\test\junk5.pl line 24. Filehandle STDOUT opened only for output at C:\test\junk5.pl line 18. IO::Detect detected the wrong thing at C:\test\junk5.pl line 28.

      What the user actually requires in that situation is something like siphylis' Filehandle::Fmode, which has been around for a few years and effectively renders your module redundant.


      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?

        Or if the disk is full; or if it's an IO::Socket::INET handle where the network connection has dropped; etc. Detecting anything that could possibly go wrong with a filehandle is not my aim. The primary use case for IO::Detect is, as stated in the documentation, writing functions that can accept either a file name or a file handle.

        For example parse($file) - I want it to accept a filehandle, and I want it to accept a filename. If someone passes a filehandle in write-only mode then that's their own fault and I'm quite happy for it to blow up in their face.

        As far as detecting whether a file handle is read or write, I may add that functionality if I come across a situation where it would be useful.

        Re FileHandle::Fmode...

        $ perl -MFileHandle::Fmode=is_FH -MIO::All -E'say is_FH(IO::All->new(" +/dev/null"))' 0
        perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re^2: Best way to check if something is a file handle?
by Anonymous Monk on Jul 09, 2012 at 21:22 UTC
    Right, because fileno is flawless and works with everything that passes for a filehandle