Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

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

by moritz (Cardinal)
on Jul 09, 2012 at 07:40 UTC ( #980667=note: print w/ replies, xml ) Need Help??

Comment on Re: Best way to check if something is a file handle?
Download Code
Re^2: Best way to check if something is a file handle?
by tobyink (Abbot) on Jul 09, 2012 at 08:30 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

        The strange thing is that although $fh->can('close') doesn't return true, it also doesn't complain about there being no can method, whereas $fh->might('close') does complain about a missing might method. So it is, in some manner being treated as an object that kinda half implements the UNIVERSAL interface.

        Writing the empty string to a handle is quite a nice solution I suppose, but there may be some tied interfaces where printing even the empty string has side-effects. :-(

        And as it happens I'm actually more interested in reading than writing.

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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://980667]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (8)
As of 2014-12-25 21:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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





    Results (163 votes), past polls