This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Add test for File::stat.
[perl5.git] / t / lib / io_scalar.t
CommitLineData
f6c77cf1
NIS
1#!./perl
2
3BEGIN {
4 chdir 't' if -d 't';
5 @INC = '../lib';
e0e62c2a 6 unless (exists $open::layers{'perlio'}) {
f6c77cf1
NIS
7 print "1..0 # Skip: not perlio\n";
8 exit 0;
9 }
10}
11
12$| = 1;
23a2eb0a 13print "1..20\n";
f6c77cf1
NIS
14
15my $fh;
16my $var = "ok 2\n";
17open($fh,"+<",\$var) or print "not ";
18print "ok 1\n";
19print <$fh>;
20print "not " unless eof($fh);
21print "ok 3\n";
22seek($fh,0,0) or print "not ";
23print "not " if eof($fh);
24print "ok 4\n";
25print "ok 5\n";
26print $fh "ok 7\n" or print "not ";
27print "ok 6\n";
28print $var;
29$var = "foo\nbar\n";
30seek($fh,0,0) or print "not ";
31print "not " if eof($fh);
32print "ok 8\n";
33print "not " unless <$fh> eq "foo\n";
34print "ok 9\n";
ae1204bf
BS
35my $rv = close $fh;
36if (!$rv) {
37 print "# Close on scalar failed: $!\n";
38 print "not ";
39}
773aa825 40print "ok 10\n";
ae1204bf
BS
41
42# Test that semantics are similar to normal file-based I/O
43# Check that ">" clobbers the scalar
44$var = "Something";
45open $fh, ">", \$var;
46print "# Got [$var], expect []\n";
47print "not " unless $var eq "";
48print "ok 11\n";
49# Check that file offset set to beginning of scalar
50my $off = tell($fh);
51print "# Got $off, expect 0\n";
52print "not " unless $off == 0;
53print "ok 12\n";
54# Check that writes go where they should and update the offset
55$var = "Something";
56print $fh "Brea";
57$off = tell($fh);
58print "# Got $off, expect 4\n";
59print "not " unless $off == 4;
60print "ok 13\n";
61print "# Got [$var], expect [Breathing]\n";
62print "not " unless $var eq "Breathing";
63print "ok 14\n";
c350b88c 64close $fh;
ae1204bf
BS
65
66# Check that ">>" appends to the scalar
67$var = "Something ";
c350b88c 68open $fh, ">>", \$var;
ae1204bf
BS
69$off = tell($fh);
70print "# Got $off, expect 10\n";
71print "not " unless $off == 10;
72print "ok 15\n";
73print "# Got [$var], expect [Something ]\n";
74print "not " unless $var eq "Something ";
75print "ok 16\n";
76# Check that further writes go to the very end of the scalar
77$var .= "else ";
78print "# Got [$var], expect [Something else ]\n";
79print "not " unless $var eq "Something else ";
80print "ok 17\n";
81$off = tell($fh);
82print "# Got $off, expect 10\n";
83print "not " unless $off == 10;
84print "ok 18\n";
85print $fh "is here";
86print "# Got [$var], expect [Something else is here]\n";
87print "not " unless $var eq "Something else is here";
88print "ok 19\n";
23a2eb0a
BS
89close $fh;
90
91# Check that updates to the scalar from elsewhere do not
92# cause problems
93$var = "line one\nline two\line three\n";
94open $fh, "<", \$var;
95while (<$fh>) {
96 $var = "foo";
97}
98close $fh;
99print "# Got [$var], expect [foo]\n";
100print "not " unless $var eq "foo";
101print "ok 20\n";