1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
|
#!./perl
BEGIN {
chdir 't' if -d 't';
@INC = '../lib';
unless (find PerlIO::Layer 'perlio') {
print "1..0 # Skip: not perlio\n";
exit 0;
}
require Config;
if (($Config::Config{'extensions'} !~ m!\bPerlIO/scalar\b!) ){
print "1..0 # Skip -- Perl configured without PerlIO::scalar module\n";
exit 0;
}
}
use Fcntl qw(SEEK_SET SEEK_CUR SEEK_END); # Not 0, 1, 2 everywhere.
$| = 1;
use Test::More tests => 55;
my $fh;
my $var = "aaa\n";
ok(open($fh,"+<",\$var));
is(<$fh>, $var);
ok(eof($fh));
ok(seek($fh,0,SEEK_SET));
ok(!eof($fh));
ok(print $fh "bbb\n");
is($var, "bbb\n");
$var = "foo\nbar\n";
ok(seek($fh,0,SEEK_SET));
ok(!eof($fh));
is(<$fh>, "foo\n");
ok(close $fh, $!);
# Test that semantics are similar to normal file-based I/O
# Check that ">" clobbers the scalar
$var = "Something";
open $fh, ">", \$var;
is($var, "");
# Check that file offset set to beginning of scalar
my $off = tell($fh);
is($off, 0);
# Check that writes go where they should and update the offset
$var = "Something";
print $fh "Brea";
$off = tell($fh);
is($off, 4);
is($var, "Breathing");
close $fh;
# Check that ">>" appends to the scalar
$var = "Something ";
open $fh, ">>", \$var;
$off = tell($fh);
is($off, 10);
is($var, "Something ");
# Check that further writes go to the very end of the scalar
$var .= "else ";
is($var, "Something else ");
$off = tell($fh);
is($off, 10);
print $fh "is here";
is($var, "Something else is here");
close $fh;
# Check that updates to the scalar from elsewhere do not
# cause problems
$var = "line one\nline two\line three\n";
open $fh, "<", \$var;
while (<$fh>) {
$var = "foo";
}
close $fh;
is($var, "foo");
# Check that dup'ing the handle works
$var = '';
open $fh, "+>", \$var;
print $fh "xxx\n";
open $dup,'+<&',$fh;
print $dup "yyy\n";
seek($dup,0,SEEK_SET);
is(<$dup>, "xxx\n");
is(<$dup>, "yyy\n");
close($fh);
close($dup);
open $fh, '<', \42;
is(<$fh>, "42", "reading from non-string scalars");
close $fh;
{ package P; sub TIESCALAR {bless{}} sub FETCH { "shazam" } }
tie $p, P; open $fh, '<', \$p;
is(<$fh>, "shazam", "reading from magic scalars");
{
use warnings;
my $warn = 0;
local $SIG{__WARN__} = sub { $warn++ };
open my $fh, '>', \my $scalar;
print $fh "foo";
close $fh;
is($warn, 0, "no warnings when writing to an undefined scalar");
}
{
use warnings;
my $warn = 0;
local $SIG{__WARN__} = sub { $warn++ };
for (1..2) {
open my $fh, '>', \my $scalar;
close $fh;
}
is($warn, 0, "no warnings when reusing a lexical");
}
{
use warnings;
my $warn = 0;
local $SIG{__WARN__} = sub { $warn++ };
my $fetch = 0;
{
package MgUndef;
sub TIESCALAR { bless [] }
sub FETCH { $fetch++; return undef }
}
tie my $scalar, MgUndef;
open my $fh, '<', \$scalar;
close $fh;
is($warn, 0, "no warnings reading a magical undef scalar");
is($fetch, 1, "FETCH only called once");
}
{
use warnings;
my $warn = 0;
local $SIG{__WARN__} = sub { $warn++ };
my $scalar = 3;
undef $scalar;
open my $fh, '<', \$scalar;
close $fh;
is($warn, 0, "no warnings reading an undef, allocated scalar");
}
my $data = "a non-empty PV";
$data = undef;
open(MEM, '<', \$data) or die "Fail: $!\n";
my $x = join '', <MEM>;
is($x, '');
{
# [perl #35929] verify that works with $/ (i.e. test PerlIOScalar_unread)
my $s = <<'EOF';
line A
line B
a third line
EOF
open(F, '<', \$s) or die "Could not open string as a file";
local $/ = "";
my $ln = <F>;
close F;
is($ln, $s, "[perl #35929]");
}
# [perl #40267] PerlIO::scalar doesn't respect readonly-ness
{
ok(!(defined open(F, '>', \undef)), "[perl #40267] - $!");
close F;
my $ro = \43;
ok(!(defined open(F, '>', $ro)), $!);
close F;
# but we can read from it
ok(open(F, '<', $ro), $!);
is(<F>, 43);
close F;
}
{
# Check that we zero fill when needed when seeking,
# and that seeking negative off the string does not do bad things.
my $foo;
ok(open(F, '>', \$foo));
# Seeking forward should zero fill.
ok(seek(F, 50, SEEK_SET));
print F "x";
is(length($foo), 51);
like($foo, qr/^\0{50}x$/);
is(tell(F), 51);
ok(seek(F, 0, SEEK_SET));
is(length($foo), 51);
# Seeking forward again should zero fill but only the new bytes.
ok(seek(F, 100, SEEK_SET));
print F "y";
is(length($foo), 101);
like($foo, qr/^\0{50}x\0{49}y$/);
is(tell(F), 101);
# Seeking back and writing should not zero fill.
ok(seek(F, 75, SEEK_SET));
print F "z";
is(length($foo), 101);
like($foo, qr/^\0{50}x\0{24}z\0{24}y$/);
is(tell(F), 76);
# Seeking negative should not do funny business.
ok(!seek(F, -50, SEEK_SET), $!);
ok(seek(F, 0, SEEK_SET));
ok(!seek(F, -50, SEEK_CUR), $!);
ok(!seek(F, -150, SEEK_END), $!);
}
|