| 1 | #!./perl |
| 2 | |
| 3 | BEGIN { |
| 4 | chdir 't' if -d 't'; |
| 5 | @INC = '../lib'; |
| 6 | require Config; import Config; |
| 7 | if ($Config{'extensions'} !~ /\bIO\b/ && $^O ne 'VMS') { |
| 8 | print "1..0\n"; |
| 9 | exit 0; |
| 10 | } |
| 11 | } |
| 12 | |
| 13 | use strict; |
| 14 | use FileHandle; |
| 15 | autoflush STDOUT 1; |
| 16 | use Test::More; |
| 17 | my $TB = Test::More->builder; |
| 18 | |
| 19 | my $mystdout = new_from_fd FileHandle 1,"w"; |
| 20 | $| = 1; |
| 21 | autoflush $mystdout; |
| 22 | |
| 23 | print $mystdout "ok ".fileno($mystdout), |
| 24 | " - ", "create new handle from file descriptor", "\n"; |
| 25 | $TB->current_test($TB->current_test + 1); |
| 26 | |
| 27 | my $fh = (new FileHandle "./TEST", O_RDONLY |
| 28 | or new FileHandle "TEST", O_RDONLY); |
| 29 | ok(defined($fh), "create new handle O_RDONLY"); |
| 30 | |
| 31 | my $buffer = <$fh>; |
| 32 | is($buffer, "#!./perl\n", "Got expected first line via handle"); |
| 33 | |
| 34 | ungetc $fh ord 'A'; |
| 35 | my $buf; |
| 36 | CORE::read($fh, $buf,1); |
| 37 | is($buf, 'A', "Got expected ordinal value via ungetc in handle's input stream"); |
| 38 | close $fh; |
| 39 | |
| 40 | $fh = new FileHandle; |
| 41 | ok(($fh->open("< TEST") && <$fh> eq $buffer), |
| 42 | "FileHandle open() method created handle, which got expected first line"); |
| 43 | |
| 44 | $fh->seek(0,0); |
| 45 | ok((<$fh> eq $buffer), "Averted possible mixed CRLF/LF in t/TEST"); |
| 46 | |
| 47 | $fh->seek(0,2); |
| 48 | my $line = <$fh>; |
| 49 | ok(! (defined($line) || !$fh->eof), "FileHandle seek() and eof() methods"); |
| 50 | |
| 51 | ok(($fh->open("TEST","r") && !$fh->tell && $fh->close), |
| 52 | "FileHandle open(), tell() and close() methods"); |
| 53 | |
| 54 | autoflush STDOUT 0; |
| 55 | ok(! $|, "handle not auto-flushing current output channel"); |
| 56 | |
| 57 | autoflush STDOUT 1; |
| 58 | ok($|, "handle auto-flushing current output channel"); |
| 59 | |
| 60 | { |
| 61 | my ($rd,$wr) = FileHandle::pipe; |
| 62 | my $non_forking = ( |
| 63 | $^O eq 'VMS' || $^O eq 'os2' || $^O eq 'amigaos' || |
| 64 | $^O eq 'MSWin32' || $Config{d_fork} ne 'define' |
| 65 | ); |
| 66 | my $content = "Writing to one end of a pipe, reading from the other\n"; |
| 67 | if ($non_forking) { |
| 68 | $wr->autoflush; |
| 69 | $wr->print($content); |
| 70 | is($rd->getline, $content, |
| 71 | "Read content from pipe on non-forking platform"); |
| 72 | } |
| 73 | else { |
| 74 | my $child; |
| 75 | if ($child = fork) { |
| 76 | # parent |
| 77 | $wr->close; |
| 78 | is($rd->getline, $content, |
| 79 | "Read content from pipe on forking platform"); |
| 80 | } |
| 81 | elsif (defined $child) { |
| 82 | # child |
| 83 | $rd->close; |
| 84 | $wr->print($content); |
| 85 | exit(0); |
| 86 | } |
| 87 | else { |
| 88 | die "fork failed: $!"; |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | ok(!FileHandle->new('', 'r'), "Can't open empty filename"); |
| 94 | |
| 95 | done_testing(); |