Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Best way to check if something is a file handle?

by tobyink (Abbot)
on Jul 09, 2012 at 07:29 UTC ( #980665=perlquestion: print w/ replies, xml ) Need Help??
tobyink has asked for the wisdom of the Perl Monks concerning the following question:

What is the best way to check if a scalar variable is a file handle?

The na´ve way would be to use something like ref($var) eq 'IO' but that doesn't cover things like blessed IO::File, IO::Scalar, etc objects.

Perhaps it's best to use duck typing, and check for the functionality we need, e.g. $var->can('getline') && $var->can('close')?

What do you use?

Update: is_real_fh from IO::Handle::Util and openhandle from Scalar::Util each seem close, but neither seems to identify, say IO::All objects. :-(

Another update: I've tried to consolidate some of these techniques into IO::Detect 0.001 which is now on CPAN.

perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

Comment on Best way to check if something is a file handle?
Select or Download Code
Re: Best way to check if something is a file handle?
by moritz (Cardinal) on Jul 09, 2012 at 07:40 UTC

      Practical experimentation shows this doesn't work. :-(

      use 5.010; use Test::More; use IO::Handle; open my $fh, ">", "/tmp/foo" or die "argh"; ok $fh->can("close"); ok UNIVERSAL::can($fh, "close"); done_testing(); __END__ not ok 1 # Failed test at fh.pl line 7. not ok 2 # Failed test at fh.pl line 8. 1..2 # Looks like you failed 2 tests of 2.
      perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

        Not really surprising, since, according to perldoc,

        UNIVERSAL is the base class from which all blessed references inherit

        that is, the can method applies only to objects.

        Would something like this do?

        #! perl use strict; use warnings; my $in_file = 'temp1.txt'; # this must exist my $outfile = 'temp2.txt'; my $arrayref = []; open(my $in, '<', $in_file) or die "Cannot open file '$in_file' for r +eading: $!"; open(my $out, '>', $outfile) or die "Cannot open file '$outfile' for w +riting: $!"; can_write( $in_file, $in); can_write( $outfile, $out); can_write('$arrayref', $arrayref); close($in) or die "Cannot close file '$in_file': $!" +; close($out) or die "Cannot close file '$outfile': $!" +; sub can_write { my ($name, $fh) = @_; my $result = eval { no warnings; print $fh '' }; printf "$name %s write\n", ($result ? 'can' : 'cannot'); } __END__ temp1.txt cannot write temp2.txt can write $arrayref cannot write

        Update: The CPAN module FileHandle::Fmode by Sisyphus/syphilis may also be worth investigating, although it too appears not to work with IO::All objects.

        HTH,

        Athanasius <°(((><contra mundum

Re: Best way to check if something is a file handle?
by frozenwithjoy (Curate) on Jul 09, 2012 at 07:44 UTC
    There are a few more approaches discussed here.
Re: Best way to check if something is a file handle?
by BrowserUk (Pope) on Jul 09, 2012 at 18:31 UTC
    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?

      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?

      Right, because fileno is flawless and works with everything that passes for a filehandle

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (10)
As of 2014-10-01 10:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (6 votes), past polls