1*0Sstevel@tonic-gate#!./perl 2*0Sstevel@tonic-gate 3*0Sstevel@tonic-gateBEGIN { 4*0Sstevel@tonic-gate chdir 't' if -d 't'; 5*0Sstevel@tonic-gate @INC = '../lib'; 6*0Sstevel@tonic-gate unless (find PerlIO::Layer 'perlio') { 7*0Sstevel@tonic-gate print "1..0 # Skip: not perlio\n"; 8*0Sstevel@tonic-gate exit 0; 9*0Sstevel@tonic-gate } 10*0Sstevel@tonic-gate} 11*0Sstevel@tonic-gate 12*0Sstevel@tonic-gate$| = 1; 13*0Sstevel@tonic-gateprint "1..25\n"; 14*0Sstevel@tonic-gate 15*0Sstevel@tonic-gatemy $fh; 16*0Sstevel@tonic-gatemy $var = "ok 2\n"; 17*0Sstevel@tonic-gateopen($fh,"+<",\$var) or print "not "; 18*0Sstevel@tonic-gateprint "ok 1\n"; 19*0Sstevel@tonic-gateprint <$fh>; 20*0Sstevel@tonic-gateprint "not " unless eof($fh); 21*0Sstevel@tonic-gateprint "ok 3\n"; 22*0Sstevel@tonic-gateseek($fh,0,0) or print "not "; 23*0Sstevel@tonic-gateprint "not " if eof($fh); 24*0Sstevel@tonic-gateprint "ok 4\n"; 25*0Sstevel@tonic-gateprint "ok 5\n"; 26*0Sstevel@tonic-gateprint $fh "ok 7\n" or print "not "; 27*0Sstevel@tonic-gateprint "ok 6\n"; 28*0Sstevel@tonic-gateprint $var; 29*0Sstevel@tonic-gate$var = "foo\nbar\n"; 30*0Sstevel@tonic-gateseek($fh,0,0) or print "not "; 31*0Sstevel@tonic-gateprint "not " if eof($fh); 32*0Sstevel@tonic-gateprint "ok 8\n"; 33*0Sstevel@tonic-gateprint "not " unless <$fh> eq "foo\n"; 34*0Sstevel@tonic-gateprint "ok 9\n"; 35*0Sstevel@tonic-gatemy $rv = close $fh; 36*0Sstevel@tonic-gateif (!$rv) { 37*0Sstevel@tonic-gate print "# Close on scalar failed: $!\n"; 38*0Sstevel@tonic-gate print "not "; 39*0Sstevel@tonic-gate} 40*0Sstevel@tonic-gateprint "ok 10\n"; 41*0Sstevel@tonic-gate 42*0Sstevel@tonic-gate# Test that semantics are similar to normal file-based I/O 43*0Sstevel@tonic-gate# Check that ">" clobbers the scalar 44*0Sstevel@tonic-gate$var = "Something"; 45*0Sstevel@tonic-gateopen $fh, ">", \$var; 46*0Sstevel@tonic-gateprint "# Got [$var], expect []\n"; 47*0Sstevel@tonic-gateprint "not " unless $var eq ""; 48*0Sstevel@tonic-gateprint "ok 11\n"; 49*0Sstevel@tonic-gate# Check that file offset set to beginning of scalar 50*0Sstevel@tonic-gatemy $off = tell($fh); 51*0Sstevel@tonic-gateprint "# Got $off, expect 0\n"; 52*0Sstevel@tonic-gateprint "not " unless $off == 0; 53*0Sstevel@tonic-gateprint "ok 12\n"; 54*0Sstevel@tonic-gate# Check that writes go where they should and update the offset 55*0Sstevel@tonic-gate$var = "Something"; 56*0Sstevel@tonic-gateprint $fh "Brea"; 57*0Sstevel@tonic-gate$off = tell($fh); 58*0Sstevel@tonic-gateprint "# Got $off, expect 4\n"; 59*0Sstevel@tonic-gateprint "not " unless $off == 4; 60*0Sstevel@tonic-gateprint "ok 13\n"; 61*0Sstevel@tonic-gateprint "# Got [$var], expect [Breathing]\n"; 62*0Sstevel@tonic-gateprint "not " unless $var eq "Breathing"; 63*0Sstevel@tonic-gateprint "ok 14\n"; 64*0Sstevel@tonic-gateclose $fh; 65*0Sstevel@tonic-gate 66*0Sstevel@tonic-gate# Check that ">>" appends to the scalar 67*0Sstevel@tonic-gate$var = "Something "; 68*0Sstevel@tonic-gateopen $fh, ">>", \$var; 69*0Sstevel@tonic-gate$off = tell($fh); 70*0Sstevel@tonic-gateprint "# Got $off, expect 10\n"; 71*0Sstevel@tonic-gateprint "not " unless $off == 10; 72*0Sstevel@tonic-gateprint "ok 15\n"; 73*0Sstevel@tonic-gateprint "# Got [$var], expect [Something ]\n"; 74*0Sstevel@tonic-gateprint "not " unless $var eq "Something "; 75*0Sstevel@tonic-gateprint "ok 16\n"; 76*0Sstevel@tonic-gate# Check that further writes go to the very end of the scalar 77*0Sstevel@tonic-gate$var .= "else "; 78*0Sstevel@tonic-gateprint "# Got [$var], expect [Something else ]\n"; 79*0Sstevel@tonic-gateprint "not " unless $var eq "Something else "; 80*0Sstevel@tonic-gateprint "ok 17\n"; 81*0Sstevel@tonic-gate$off = tell($fh); 82*0Sstevel@tonic-gateprint "# Got $off, expect 10\n"; 83*0Sstevel@tonic-gateprint "not " unless $off == 10; 84*0Sstevel@tonic-gateprint "ok 18\n"; 85*0Sstevel@tonic-gateprint $fh "is here"; 86*0Sstevel@tonic-gateprint "# Got [$var], expect [Something else is here]\n"; 87*0Sstevel@tonic-gateprint "not " unless $var eq "Something else is here"; 88*0Sstevel@tonic-gateprint "ok 19\n"; 89*0Sstevel@tonic-gateclose $fh; 90*0Sstevel@tonic-gate 91*0Sstevel@tonic-gate# Check that updates to the scalar from elsewhere do not 92*0Sstevel@tonic-gate# cause problems 93*0Sstevel@tonic-gate$var = "line one\nline two\line three\n"; 94*0Sstevel@tonic-gateopen $fh, "<", \$var; 95*0Sstevel@tonic-gatewhile (<$fh>) { 96*0Sstevel@tonic-gate $var = "foo"; 97*0Sstevel@tonic-gate} 98*0Sstevel@tonic-gateclose $fh; 99*0Sstevel@tonic-gateprint "# Got [$var], expect [foo]\n"; 100*0Sstevel@tonic-gateprint "not " unless $var eq "foo"; 101*0Sstevel@tonic-gateprint "ok 20\n"; 102*0Sstevel@tonic-gate 103*0Sstevel@tonic-gate# Check that dup'ing the handle works 104*0Sstevel@tonic-gate 105*0Sstevel@tonic-gate$var = ''; 106*0Sstevel@tonic-gate 107*0Sstevel@tonic-gateopen $fh, "+>", \$var; 108*0Sstevel@tonic-gateprint $fh "ok 21\n"; 109*0Sstevel@tonic-gateopen $dup,'+<&',$fh; 110*0Sstevel@tonic-gateprint $dup "ok 22\n"; 111*0Sstevel@tonic-gateseek($dup,0,0); 112*0Sstevel@tonic-gatewhile (<$dup>) { 113*0Sstevel@tonic-gate print; 114*0Sstevel@tonic-gate} 115*0Sstevel@tonic-gateclose($fh); 116*0Sstevel@tonic-gateclose($dup); 117*0Sstevel@tonic-gate 118*0Sstevel@tonic-gate# Check reading from non-string scalars 119*0Sstevel@tonic-gate 120*0Sstevel@tonic-gateopen $fh, '<', \42; 121*0Sstevel@tonic-gateprint <$fh> eq "42" ? "ok 23\n" : "not ok 23\n"; 122*0Sstevel@tonic-gateclose $fh; 123*0Sstevel@tonic-gate 124*0Sstevel@tonic-gate# reading from magic scalars 125*0Sstevel@tonic-gate 126*0Sstevel@tonic-gate{ package P; sub TIESCALAR {bless{}} sub FETCH {"ok 24\n"} } 127*0Sstevel@tonic-gatetie $p, P; open $fh, '<', \$p; 128*0Sstevel@tonic-gateprint <$fh>; 129*0Sstevel@tonic-gate 130*0Sstevel@tonic-gate# don't warn when writing to an undefined scalar 131*0Sstevel@tonic-gate 132*0Sstevel@tonic-gate{ 133*0Sstevel@tonic-gate use warnings; 134*0Sstevel@tonic-gate my $ok = 1; 135*0Sstevel@tonic-gate local $SIG{__WARN__} = sub { $ok = 0; }; 136*0Sstevel@tonic-gate open my $fh, '>', \my $scalar; 137*0Sstevel@tonic-gate print $fh "foo"; 138*0Sstevel@tonic-gate close $fh; 139*0Sstevel@tonic-gate print $ok ? "ok 25\n" : "not ok 25\n"; 140*0Sstevel@tonic-gate} 141