6 unless (find PerlIO::Layer 'perlio') {
7 print "1..0 # Skip: not perlio\n";
11 if (($Config::Config{'extensions'} !~ m!\bPerlIO/scalar\b!) ){
12 print "1..0 # Skip -- Perl configured without PerlIO::scalar module\n";
17 use Fcntl qw(SEEK_SET SEEK_CUR SEEK_END); # Not 0, 1, 2 everywhere.
21 use Test::More tests => 55;
25 ok(open($fh,"+<",\$var));
31 ok(seek($fh,0,SEEK_SET));
34 ok(print $fh "bbb\n");
37 ok(seek($fh,0,SEEK_SET));
42 # Test that semantics are similar to normal file-based I/O
43 # Check that ">" clobbers the scalar
47 # Check that file offset set to beginning of scalar
50 # Check that writes go where they should and update the offset
55 is($var, "Breathing");
58 # Check that ">>" appends to the scalar
60 open $fh, ">>", \$var;
63 is($var, "Something ");
64 # Check that further writes go to the very end of the scalar
66 is($var, "Something else ");
72 is($var, "Something else is here");
75 # Check that updates to the scalar from elsewhere do not
77 $var = "line one\nline two\line three\n";
85 # Check that dup'ing the handle works
88 open $fh, "+>", \$var;
92 seek($dup,0,SEEK_SET);
99 is(<$fh>, "42", "reading from non-string scalars");
102 { package P; sub TIESCALAR {bless{}} sub FETCH { "shazam" } }
103 tie $p, P; open $fh, '<', \$p;
104 is(<$fh>, "shazam", "reading from magic scalars");
109 local $SIG{__WARN__} = sub { $warn++ };
110 open my $fh, '>', \my $scalar;
113 is($warn, 0, "no warnings when writing to an undefined scalar");
119 local $SIG{__WARN__} = sub { $warn++ };
121 open my $fh, '>', \my $scalar;
124 is($warn, 0, "no warnings when reusing a lexical");
130 local $SIG{__WARN__} = sub { $warn++ };
135 sub TIESCALAR { bless [] }
136 sub FETCH { $fetch++; return undef }
138 tie my $scalar, MgUndef;
140 open my $fh, '<', \$scalar;
142 is($warn, 0, "no warnings reading a magical undef scalar");
143 is($fetch, 1, "FETCH only called once");
149 local $SIG{__WARN__} = sub { $warn++ };
152 open my $fh, '<', \$scalar;
154 is($warn, 0, "no warnings reading an undef, allocated scalar");
157 my $data = "a non-empty PV";
159 open(MEM, '<', \$data) or die "Fail: $!\n";
160 my $x = join '', <MEM>;
164 # [perl #35929] verify that works with $/ (i.e. test PerlIOScalar_unread)
170 open(F, '<', \$s) or die "Could not open string as a file";
174 is($ln, $s, "[perl #35929]");
177 # [perl #40267] PerlIO::scalar doesn't respect readonly-ness
179 ok(!(defined open(F, '>', \undef)), "[perl #40267] - $!");
183 ok(!(defined open(F, '>', $ro)), $!);
185 # but we can read from it
186 ok(open(F, '<', $ro), $!);
192 # Check that we zero fill when needed when seeking,
193 # and that seeking negative off the string does not do bad things.
197 ok(open(F, '>', \$foo));
199 # Seeking forward should zero fill.
201 ok(seek(F, 50, SEEK_SET));
203 is(length($foo), 51);
204 like($foo, qr/^\0{50}x$/);
207 ok(seek(F, 0, SEEK_SET));
208 is(length($foo), 51);
210 # Seeking forward again should zero fill but only the new bytes.
212 ok(seek(F, 100, SEEK_SET));
214 is(length($foo), 101);
215 like($foo, qr/^\0{50}x\0{49}y$/);
218 # Seeking back and writing should not zero fill.
220 ok(seek(F, 75, SEEK_SET));
222 is(length($foo), 101);
223 like($foo, qr/^\0{50}x\0{24}z\0{24}y$/);
226 # Seeking negative should not do funny business.
228 ok(!seek(F, -50, SEEK_SET), $!);
229 ok(seek(F, 0, SEEK_SET));
230 ok(!seek(F, -50, SEEK_CUR), $!);
231 ok(!seek(F, -150, SEEK_END), $!);