12 $Is_MacOS = $^O eq 'MacOS';
16 my $Perl = which_perl();
19 unlink("afile") if -f "afile";
21 $! = 0; # the -f above will set $! if 'afile' doesn't exist.
22 ok( open(my $f,"+>afile"), 'open(my $f, "+>...")' );
25 ok( -f "afile", ' its a file');
26 ok( (print $f "SomeData\n"), ' we can print to it');
27 is( tell($f), 9, ' tell()' );
28 ok( seek($f,0,0), ' seek set' );
31 is( $b, "SomeData\n", ' readline' );
32 ok( -f $f, ' still a file' );
34 eval { die "Message" };
35 like( $@, qr/<\$f> line 1/, ' die message correct' );
37 ok( close($f), ' close()' );
38 ok( unlink("afile"), ' unlink()' );
42 ok( open(my $f,'>', 'afile'), "open(my \$f, '>', 'afile')" );
43 ok( (print $f "a row\n"), ' print');
44 ok( close($f), ' close' );
45 ok( -s 'afile' < 10, ' -s' );
49 ok( open(my $f,'>>', 'afile'), "open(my \$f, '>>', 'afile')" );
50 ok( (print $f "a row\n"), ' print' );
51 ok( close($f), ' close' );
52 ok( -s 'afile' > 10, ' -s' );
56 ok( open(my $f, '<', 'afile'), "open(my \$f, '<', 'afile')" );
58 is( scalar @rows, 2, ' readline, list context' );
59 is( $rows[0], "a row\n", ' first line read' );
60 is( $rows[1], "a row\n", ' second line' );
61 ok( close($f), ' close' );
65 ok( -s 'afile' < 20, '-s' );
67 ok( open(my $f, '+<', 'afile'), 'open +<' );
69 is( scalar @rows, 2, ' readline, list context' );
70 ok( seek($f, 0, 1), ' seek cur' );
71 ok( (print $f "yet another row\n"), ' print' );
72 ok( close($f), ' close' );
73 ok( -s 'afile' > 20, ' -s' );
78 ok( open(my $f, '-|', <<EOC), 'open -|' );
79 $Perl -e "print qq(a row\\n); print qq(another row\\n)"
83 is( scalar @rows, 2, ' readline, list context' );
84 ok( close($f), ' close' );
87 skip "Output for |- doesn't go to shell on MacOS", 5 if $Is_MacOS;
89 ok( open(my $f, '|-', <<EOC), 'open |-' );
95 print $f "not ok $test - piped in\n";
99 print $f "not ok $test - piped in\n";
101 ok( close($f), ' close' );
107 ok( !eval { open my $f, '<&', 'afile'; 1; }, '<& on a non-filehandle' );
108 like( $@, qr/Bad filehandle:\s+afile/, ' right error' );
113 unlink("afile") if -f "afile";
115 ok( open(local $f,"+>afile"), 'open local $f, "+>", ...' );
118 ok( -f "afile", ' -f' );
119 ok( (print $f "SomeData\n"), ' print' );
120 is( tell($f), 9, ' tell' );
121 ok( seek($f,0,0), ' seek set' );
124 is( $b, "SomeData\n", ' readline' );
125 ok( -f $f, ' still a file' );
127 eval { die "Message" };
128 like( $@, qr/<\$f> line 1/, ' proper die message' );
129 ok( close($f), ' close' );
135 ok( open(local $f,'>', 'afile'), 'open local $f, ">", ...' );
136 ok( (print $f "a row\n"), ' print');
137 ok( close($f), ' close');
138 ok( -s 'afile' < 10, ' -s' );
142 ok( open(local $f,'>>', 'afile'), 'open local $f, ">>", ...' );
143 ok( (print $f "a row\n"), ' print');
144 ok( close($f), ' close');
145 ok( -s 'afile' > 10, ' -s' );
149 ok( open(local $f, '<', 'afile'), 'open local $f, "<", ...' );
151 is( scalar @rows, 2, ' readline list context' );
152 ok( close($f), ' close' );
155 ok( -s 'afile' < 20, ' -s' );
158 ok( open(local $f, '+<', 'afile'), 'open local $f, "+<", ...' );
160 is( scalar @rows, 2, ' readline list context' );
161 ok( seek($f, 0, 1), ' seek cur' );
162 ok( (print $f "yet another row\n"), ' print' );
163 ok( close($f), ' close' );
164 ok( -s 'afile' > 20, ' -s' );
170 ok( open(local $f, '-|', <<EOC), 'open local $f, "-|", ...' );
171 $Perl -e "print qq(a row\\n); print qq(another row\\n)"
175 is( scalar @rows, 2, ' readline list context' );
176 ok( close($f), ' close' );
180 skip "Output for |- doesn't go to shell on MacOS", 5 if $Is_MacOS;
182 ok( open(local $f, '|-', <<EOC), 'open local $f, "|-", ...' );
183 $Perl -pe "s/^not //"
187 my $test = curr_test;
188 print $f "not ok $test - piping\n";
192 print $f "not ok $test - piping\n";
194 ok( close($f), ' close' );
200 ok( !eval { open local $f, '<&', 'afile'; 1 }, 'local <& on non-filehandle');
201 like( $@, qr/Bad filehandle:\s+afile/, ' right error' );
206 ok( open(F, qq{$Perl -le "print 'ok'"|}), 'open to pipe' );
207 is(scalar <F>, "ok\n", ' readline');
208 ok( close F, ' close' );
212 ok( open(F, "-|", qq{$Perl -le "print 'ok'"}), 'open -|');
213 is( scalar <F>, "ok\n", ' readline');
214 ok( close F, ' close' );
219 # other dupping techniques
221 ok( open(my $stdout, ">&", \*STDOUT), 'dup \*STDOUT into lexical fh');
222 ok( open(STDOUT, ">&", $stdout), 'restore dupped STDOUT from lexical fh');
225 use strict; # the below should not warn
226 ok( open(my $stdout, ">&", STDOUT), 'dup STDOUT into lexical fh');
229 # used to try to open a file [perl #17830]
230 ok( open(my $stdin, "<&", fileno STDIN), 'dup fileno(STDIN) into lexical fh') or _diag $!;
234 skip "This perl uses perlio", 1 if $Config{useperlio};
235 skip "miniperl cannot be relied on to load %Errno"
236 if $ENV{PERL_CORE_MINITEST};
237 # Force the reference to %! to be run time by writing ! as {"!"}
238 skip "This system doesn't understand EINVAL", 1
239 unless exists ${"!"}{EINVAL};
242 ok(!open(F,'>',\my $s) && ${"!"}{EINVAL}, 'open(reference) raises EINVAL');
246 ok( !eval { open F, "BAR", "QUUX" }, 'Unknown open() mode' );
247 like( $@, qr/\QUnknown open() mode 'BAR'/, ' right error' );
251 local $SIG{__WARN__} = sub { $@ = shift };
254 my $tmphandle = shift;
255 my $line = scalar <$tmphandle>;
260 open($fh0[0], "TEST");
262 like($@, qr/<\$fh0\[...\]> line 1\./, "autoviv fh package aelem");
264 open($fh1{k}, "TEST");
266 like($@, qr/<\$fh1{...}> line 1\./, "autoviv fh package helem");
269 open($fh2[0], "TEST");
271 like($@, qr/<\$fh2\[...\]> line 1\./, "autoviv fh lexical aelem");
274 open($fh3{k}, "TEST");
276 like($@, qr/<\$fh3{...}> line 1\./, "autoviv fh lexical helem");
280 skip("These tests use perlio", 5) unless $Config{useperlio};
282 use warnings 'layer';
283 local $SIG{__WARN__} = sub { $w = shift };
285 eval { open(F, ">>>", "afile") };
286 like($w, qr/Invalid separator character '>' in PerlIO layer spec/,
287 "bad open (>>>) warning");
288 like($@, qr/Unknown open\(\) mode '>>>'/,
289 "bad open (>>>) failure");
291 eval { open(F, ">:u", "afile" ) };
292 like($w, qr/Unknown PerlIO layer "u"/,
293 'bad layer ">:u" warning');
294 eval { open(F, "<:u", "afile" ) };
295 like($w, qr/Unknown PerlIO layer "u"/,
296 'bad layer "<:u" warning');
297 eval { open(F, ":c", "afile" ) };
298 like($@, qr/Unknown open\(\) mode ':c'/,
299 'bad layer ":c" failure');
302 # [perl #28986] "open m" crashes Perl
304 fresh_perl_like('open m', qr/^Search pattern not terminated at/,
305 { stderr => 1 }, 'open m test');
308 'sub f { open(my $fh, "xxx"); $fh = "f"; } f; f;print "ok"',
309 'ok', { stderr => 1 },
310 '#29102: Crash on assignment to lexical filehandle');
312 # [perl #31767] Using $1 as a filehandle via open $1, "file" doesn't raise
315 eval { open $99, "foo" };
316 like($@, qr/Modification of a read-only value attempted/, "readonly fh");