#!./perl
chdir 't' if -d 't';
}
$| = 1;
{
unlink("afile") if -f "afile";
$! = 0; # the -f above will set $! if 'afile' doesn't exist.
ok( open(my $f,"+>afile"), 'open(my $f, "+>...")' );
ok( -f "afile", ' its a file'); ok( (print $f "SomeData\n"), ' we can print to it'); ok( seek($f,0,0), ' seek set' );
$b = <$f>;
is( $b, "SomeData\n", ' readline' ); ok( -f $f, ' still a file' );
like( $@, qr/<\$f> line 1/, ' die message correct' );
}
{
ok( open(my $f,'>', 'afile'), "open(my \$f, '>', 'afile')" ); ok( (print $f "a row\n"), ' print'); ok( -s 'afile' < 10, ' -s' ); }
{
ok( open(my $f,'>>', 'afile'), "open(my \$f, '>>', 'afile')" ); ok( (print $f "a row\n"), ' print' ); ok( -s 'afile' > 10, ' -s' ); }
{
ok( open(my $f, '<', 'afile'), "open(my \$f, '<', 'afile')" ); is( $rows[0], "a row\n", ' first line read' ); is( $rows[1], "a row\n", ' second line' ); }
{
ok( -s 'afile' < 20, '-s' );
ok( open(my $f, '+<', 'afile'), 'open +<' ); ok( seek($f, 0, 1), ' seek cur' ); ok( (print $f "yet another row\n"), ' print' ); ok( -s 'afile' > 20, ' -s' );
}
ok( open(my $f, '-|', <<EOC), 'open -|' ); $Perl -e "print qq(a row\\n); print qq(another row\\n)"
EOC
}
skip "Output for |- doesn't go to shell on MacOS", 5 if $Is_MacOS;
ok( open(my $f, '|-', <<EOC), 'open |-' ); $Perl -pe "s/^not //"
EOC
print $f "not ok $test - piped in\n";
print $f "not ok $test - piped in\n"; }
ok( !eval { open my $f, '<&', 'afile'; 1; }, '<& on a non-filehandle' );
# local $file tests
{
unlink("afile") if -f "afile";
ok( open(local $f,"+>afile"), 'open local $f, "+>", ...' );
ok( (print $f "SomeData\n"), ' print' ); ok( seek($f,0,0), ' seek set' );
$b = <$f>;
is( $b, "SomeData\n", ' readline' ); ok( -f $f, ' still a file' );
like( $@, qr/<\$f> line 1/, ' proper die message' );
}
{
ok( open(local $f,'>', 'afile'), 'open local $f, ">", ...' ); ok( (print $f "a row\n"), ' print'); ok( -s 'afile' < 10, ' -s' ); }
{
ok( open(local $f,'>>', 'afile'), 'open local $f, ">>", ...' ); ok( (print $f "a row\n"), ' print'); ok( -s 'afile' > 10, ' -s' ); }
{
ok( open(local $f, '<', 'afile'), 'open local $f, "<", ...' ); }
ok( -s 'afile' < 20, ' -s' );
{
ok( open(local $f, '+<', 'afile'), 'open local $f, "+<", ...' ); ok( seek($f, 0, 1), ' seek cur' ); ok( (print $f "yet another row\n"), ' print' ); ok( -s 'afile' > 20, ' -s' );
}
ok( open(local $f, '-|', <<EOC), 'open local $f, "-|", ...' ); $Perl -e "print qq(a row\\n); print qq(another row\\n)"
EOC
}
skip "Output for |- doesn't go to shell on MacOS", 5 if $Is_MacOS;
ok( open(local $f, '|-', <<EOC), 'open local $f, "|-", ...' ); $Perl -pe "s/^not //"
EOC
print $f "not ok $test - piping\n";
print $f "not ok $test - piping\n"; }
ok( !eval { open local $f, '<&', 'afile'; 1 }, 'local <& on non-filehandle');
{
local *F;
for (1..2) {
}
for (1..2) {
}
}
# other dupping techniques
{
{
use strict; # the below should not warn }
# used to try to open a file [perl #17830]
}
skip "miniperl cannot be relied on to load %Errno" # Force the reference to %! to be run time by writing ! as {"!"}
skip "This system doesn't understand EINVAL", 1
ok(!open(F,'>',\my $s) && ${"!"}{EINVAL}, 'open(reference) raises EINVAL'); }
{
ok( !eval { open F, "BAR", "QUUX" }, 'Unknown open() mode' ); }
{
}
like($@, qr/<\$fh0\[...\]> line 1\./, "autoviv fh package aelem");
like($@, qr/<\$fh2\[...\]> line 1\./, "autoviv fh lexical aelem");
}
my $w;
eval { open(F, ">>>", "afile") }; "bad open (>>>) warning");
"bad open (>>>) failure");
eval { open(F, ">:u", "afile" ) }; 'bad layer ">:u" warning');
eval { open(F, "<:u", "afile" ) }; 'bad layer "<:u" warning');
eval { open(F, ":c", "afile" ) }; 'bad layer ":c" failure');
}
# [perl #28986] "open m" crashes Perl
{ stderr => 1 }, 'open m test');
'sub f { open(my $fh, "xxx"); $fh = "f"; } f; f;print "ok"',
'#29102: Crash on assignment to lexical filehandle');