1#!./perl 2 3BEGIN { 4 chdir 't' if -d 't'; 5 @INC = '../lib'; 6 unless (find PerlIO::Layer 'perlio') { 7 print "1..0 # Skip: not perlio\n"; 8 exit 0; 9 } 10} 11 12$| = 1; 13print "1..25\n"; 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"; 35my $rv = close $fh; 36if (!$rv) { 37 print "# Close on scalar failed: $!\n"; 38 print "not "; 39} 40print "ok 10\n"; 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"; 64close $fh; 65 66# Check that ">>" appends to the scalar 67$var = "Something "; 68open $fh, ">>", \$var; 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"; 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"; 102 103# Check that dup'ing the handle works 104 105$var = ''; 106 107open $fh, "+>", \$var; 108print $fh "ok 21\n"; 109open $dup,'+<&',$fh; 110print $dup "ok 22\n"; 111seek($dup,0,0); 112while (<$dup>) { 113 print; 114} 115close($fh); 116close($dup); 117 118# Check reading from non-string scalars 119 120open $fh, '<', \42; 121print <$fh> eq "42" ? "ok 23\n" : "not ok 23\n"; 122close $fh; 123 124# reading from magic scalars 125 126{ package P; sub TIESCALAR {bless{}} sub FETCH {"ok 24\n"} } 127tie $p, P; open $fh, '<', \$p; 128print <$fh>; 129 130# don't warn when writing to an undefined scalar 131 132{ 133 use warnings; 134 my $ok = 1; 135 local $SIG{__WARN__} = sub { $ok = 0; }; 136 open my $fh, '>', \my $scalar; 137 print $fh "foo"; 138 close $fh; 139 print $ok ? "ok 25\n" : "not ok 25\n"; 140} 141