| 1 |
#!./perl |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
|
| 6 |
use Test::More tests => 21; |
| 7 |
use Scalar::Util qw(openhandle); |
| 8 |
|
| 9 |
ok(defined &openhandle, 'defined'); |
| 10 |
|
| 11 |
{ |
| 12 |
my $fh = \*STDERR; |
| 13 |
is(openhandle($fh), $fh, 'STDERR'); |
| 14 |
|
| 15 |
is(fileno(openhandle(*STDERR)), fileno(STDERR), 'fileno(STDERR)'); |
| 16 |
} |
| 17 |
|
| 18 |
{ |
| 19 |
use vars qw(*CLOSED); |
| 20 |
is(openhandle(*CLOSED), undef, 'closed'); |
| 21 |
} |
| 22 |
|
| 23 |
SKIP: { |
| 24 |
skip "3-arg open only on 5.6 or later", 1 if $]<5.006; |
| 25 |
|
| 26 |
open my $fh, "<", $0; |
| 27 |
skip "could not open $0 for reading: $!", 2 unless $fh; |
| 28 |
is(openhandle($fh), $fh, "works with indirect filehandles"); |
| 29 |
close($fh); |
| 30 |
is(openhandle($fh), undef, "works with indirect filehandles"); |
| 31 |
} |
| 32 |
|
| 33 |
SKIP: { |
| 34 |
skip "in-memory files only on 5.8 or later", 2 if $]<5.008; |
| 35 |
|
| 36 |
open my $fh, "<", \"in-memory file"; |
| 37 |
skip "could not open in-memory file: $!", 2 unless $fh; |
| 38 |
is(openhandle($fh), $fh, "works with in-memory files"); |
| 39 |
close($fh); |
| 40 |
is(openhandle($fh), undef, "works with in-memory files"); |
| 41 |
} |
| 42 |
|
| 43 |
ok(openhandle(\*DATA), "works for \*DATA"); |
| 44 |
ok(openhandle(*DATA), "works for *DATA"); |
| 45 |
ok(openhandle(*DATA{IO}), "works for *DATA{IO}"); |
| 46 |
|
| 47 |
{ |
| 48 |
require IO::Handle; |
| 49 |
my $fh = IO::Handle->new_from_fd(fileno(*STDERR), 'w'); |
| 50 |
skip "new_from_fd(fileno(*STDERR)) failed", 2 unless $fh; |
| 51 |
ok(openhandle($fh), "works for IO::Handle objects"); |
| 52 |
|
| 53 |
ok(!openhandle(IO::Handle->new), "unopened IO::Handle"); |
| 54 |
} |
| 55 |
|
| 56 |
{ |
| 57 |
require IO::File; |
| 58 |
my $fh = IO::File->new; |
| 59 |
$fh->open("< $0") |
| 60 |
or skip "could not open $0: $!", 3; |
| 61 |
ok(openhandle($fh), "works for IO::File objects"); |
| 62 |
close($fh); |
| 63 |
ok(!openhandle($fh), "works for IO::File objects"); |
| 64 |
|
| 65 |
ok(!openhandle(IO::File->new), "unopened IO::File" ); |
| 66 |
} |
| 67 |
|
| 68 |
SKIP: { |
| 69 |
skip( "Tied handles only on 5.8 or later", 2) if $]<5.008; |
| 70 |
|
| 71 |
use vars qw(*H); |
| 72 |
|
| 73 |
package My::Tie; |
| 74 |
require Tie::Handle; |
| 75 |
@My::Tie::ISA = qw(Tie::Handle); |
| 76 |
sub TIEHANDLE { bless {} } |
| 77 |
|
| 78 |
package main; |
| 79 |
tie *H, 'My::Tie'; |
| 80 |
ok(openhandle(*H), "tied handles are always ok"); |
| 81 |
ok(openhandle(\*H), "tied handle refs are always ok"); |
| 82 |
} |
| 83 |
|
| 84 |
ok !openhandle(undef), "undef is not a filehandle"; |
| 85 |
ok !openhandle("STDIN"), "strings are not filehandles"; |
| 86 |
ok !openhandle(0), "integers are not filehandles"; |
| 87 |
|
| 88 |
|
| 89 |
__DATA__ |