This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Bump the perl version in various places for 5.37.8
[perl5.git] / lib / FileHandle.t
CommitLineData
c07a80fd 1#!./perl
2
3BEGIN {
4 chdir 't' if -d 't';
20822f61 5 @INC = '../lib';
c07a80fd 6 require Config; import Config;
36477c24 7 if ($Config{'extensions'} !~ /\bIO\b/ && $^O ne 'VMS') {
c07a80fd 8 print "1..0\n";
9 exit 0;
10 }
11}
12
9902086f 13use strict;
c07a80fd 14use FileHandle;
36477c24 15autoflush STDOUT 1;
70cbce25 16use Test::More;
9902086f 17my $TB = Test::More->builder;
36477c24 18
9902086f 19my $mystdout = new_from_fd FileHandle 1,"w";
5ef887f5 20$| = 1;
c07a80fd 21autoflush $mystdout;
c07a80fd 22
9902086f
JK
23print $mystdout "ok ".fileno($mystdout),
24 " - ", "create new handle from file descriptor", "\n";
25$TB->current_test($TB->current_test + 1);
36477c24 26
9902086f
JK
27my $fh = (new FileHandle "./TEST", O_RDONLY
28 or new FileHandle "TEST", O_RDONLY);
29ok(defined($fh), "create new handle O_RDONLY");
36477c24 30
9902086f 31my $buffer = <$fh>;
63217fa2 32is($buffer, "#!./perl\n", "Got expected first line via handle");
36477c24 33
9d116dd7 34ungetc $fh ord 'A';
9902086f 35my $buf;
5ef887f5 36CORE::read($fh, $buf,1);
9902086f 37is($buf, 'A', "Got expected ordinal value via ungetc in handle's input stream");
36477c24 38close $fh;
39
40$fh = new FileHandle;
9902086f
JK
41ok(($fh->open("< TEST") && <$fh> eq $buffer),
42 "FileHandle open() method created handle, which got expected first line");
36477c24 43
44$fh->seek(0,0);
9902086f 45ok((<$fh> eq $buffer), "Averted possible mixed CRLF/LF in t/TEST");
36477c24 46
47$fh->seek(0,2);
9902086f
JK
48my $line = <$fh>;
49ok(! (defined($line) || !$fh->eof), "FileHandle seek() and eof() methods");
36477c24 50
9902086f
JK
51ok(($fh->open("TEST","r") && !$fh->tell && $fh->close),
52 "FileHandle open(), tell() and close() methods");
36477c24 53
54autoflush STDOUT 0;
9902086f 55ok(! $|, "handle not auto-flushing current output channel");
36477c24 56
57autoflush STDOUT 1;
9902086f
JK
58ok($|, "handle auto-flushing current output channel");
59
4457f3fc 60{
9902086f
JK
61 my ($rd,$wr) = FileHandle::pipe;
62 my $non_forking = (
63 $^O eq 'VMS' || $^O eq 'os2' || $^O eq 'amigaos' ||
2eb109a4 64 $^O eq 'MSWin32' || $Config{d_fork} ne 'define'
9902086f
JK
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 }
4457f3fc 91}
f21dc558 92
9902086f 93ok(!FileHandle->new('', 'r'), "Can't open empty filename");
70cbce25
MB
94
95done_testing();