12 $Is_VMS = $^O eq 'VMS';
13 $Is_MacOS = $^O eq 'MacOS';
17 my $Perl = which_perl();
20 unlink("afile") if -f "afile";
22 $! = 0; # the -f above will set $! if 'afile' doesn't exist.
23 ok( open(my $f,"+>afile"), 'open(my $f, "+>...")' );
26 ok( -f "afile", ' its a file');
27 ok( (print $f "SomeData\n"), ' we can print to it');
28 is( tell($f), 9, ' tell()' );
29 ok( seek($f,0,0), ' seek set' );
32 is( $b, "SomeData\n", ' readline' );
33 ok( -f $f, ' still a file' );
35 eval { die "Message" };
36 like( $@, qr/<\$f> line 1/, ' die message correct' );
38 ok( close($f), ' close()' );
39 ok( unlink("afile"), ' unlink()' );
43 ok( open(my $f,'>', 'afile'), "open(my \$f, '>', 'afile')" );
44 ok( (print $f "a row\n"), ' print');
45 ok( close($f), ' close' );
46 ok( -s 'afile' < 10, ' -s' );
50 ok( open(my $f,'>>', 'afile'), "open(my \$f, '>>', 'afile')" );
51 ok( (print $f "a row\n"), ' print' );
52 ok( close($f), ' close' );
53 ok( -s 'afile' > 10, ' -s' );
57 ok( open(my $f, '<', 'afile'), "open(my \$f, '<', 'afile')" );
59 is( scalar @rows, 2, ' readline, list context' );
60 is( $rows[0], "a row\n", ' first line read' );
61 is( $rows[1], "a row\n", ' second line' );
62 ok( close($f), ' close' );
66 ok( -s 'afile' < 20, '-s' );
68 ok( open(my $f, '+<', 'afile'), 'open +<' );
70 is( scalar @rows, 2, ' readline, list context' );
71 ok( seek($f, 0, 1), ' seek cur' );
72 ok( (print $f "yet another row\n"), ' print' );
73 ok( close($f), ' close' );
74 ok( -s 'afile' > 20, ' -s' );
80 skip "open -| busted and noisy on VMS", 3 if $Is_VMS;
82 ok( open(my $f, '-|', <<EOC), 'open -|' );
83 $Perl -e "print qq(a row\\n); print qq(another row\\n)"
87 is( scalar @rows, 2, ' readline, list context' );
88 ok( close($f), ' close' );
92 skip "Output for |- doesn't go to shell on MacOS", 5 if $Is_MacOS;
94 ok( open(my $f, '|-', <<EOC), 'open |-' );
100 print $f "not ok $test - piped in\n";
104 print $f "not ok $test - piped in\n";
106 ok( close($f), ' close' );
112 ok( !eval { open my $f, '<&', 'afile'; 1; }, '<& on a non-filehandle' );
113 like( $@, qr/Bad filehandle:\s+afile/, ' right error' );
118 unlink("afile") if -f "afile";
120 ok( open(local $f,"+>afile"), 'open local $f, "+>", ...' );
123 ok( -f "afile", ' -f' );
124 ok( (print $f "SomeData\n"), ' print' );
125 is( tell($f), 9, ' tell' );
126 ok( seek($f,0,0), ' seek set' );
129 is( $b, "SomeData\n", ' readline' );
130 ok( -f $f, ' still a file' );
132 eval { die "Message" };
133 like( $@, qr/<\$f> line 1/, ' proper die message' );
134 ok( close($f), ' close' );
140 ok( open(local $f,'>', 'afile'), 'open local $f, ">", ...' );
141 ok( (print $f "a row\n"), ' print');
142 ok( close($f), ' close');
143 ok( -s 'afile' < 10, ' -s' );
147 ok( open(local $f,'>>', 'afile'), 'open local $f, ">>", ...' );
148 ok( (print $f "a row\n"), ' print');
149 ok( close($f), ' close');
150 ok( -s 'afile' > 10, ' -s' );
154 ok( open(local $f, '<', 'afile'), 'open local $f, "<", ...' );
156 is( scalar @rows, 2, ' readline list context' );
157 ok( close($f), ' close' );
160 ok( -s 'afile' < 20, ' -s' );
163 ok( open(local $f, '+<', 'afile'), 'open local $f, "+<", ...' );
165 is( scalar @rows, 2, ' readline list context' );
166 ok( seek($f, 0, 1), ' seek cur' );
167 ok( (print $f "yet another row\n"), ' print' );
168 ok( close($f), ' close' );
169 ok( -s 'afile' > 20, ' -s' );
175 skip "open -| busted and noisy on VMS", 3 if $Is_VMS;
177 ok( open(local $f, '-|', <<EOC), 'open local $f, "-|", ...' );
178 $Perl -e "print qq(a row\\n); print qq(another row\\n)"
182 is( scalar @rows, 2, ' readline list context' );
183 ok( close($f), ' close' );
187 skip "Output for |- doesn't go to shell on MacOS", 5 if $Is_MacOS;
189 ok( open(local $f, '|-', <<EOC), 'open local $f, "|-", ...' );
190 $Perl -pe "s/^not //"
194 my $test = curr_test;
195 print $f "not ok $test - piping\n";
199 print $f "not ok $test - piping\n";
201 ok( close($f), ' close' );
207 ok( !eval { open local $f, '<&', 'afile'; 1 }, 'local <& on non-filehandle');
208 like( $@, qr/Bad filehandle:\s+afile/, ' right error' );
213 ok( open(F, qq{$Perl -le "print 'ok'"|}), 'open to pipe' );
214 is(scalar <F>, "ok\n", ' readline');
215 ok( close F, ' close' );
219 ok( open(F, "-|", qq{$Perl -le "print 'ok'"}), 'open -|');
220 is( scalar <F>, "ok\n", ' readline');
221 ok( close F, ' close' );
226 # other dupping techniques
228 ok( open(my $stdout, ">&", \*STDOUT), 'dup \*STDOUT into lexical fh');
229 ok( open(STDOUT, ">&", $stdout), 'restore dupped STDOUT from lexical fh');
232 use strict; # the below should not warn
233 ok( open(my $stdout, ">&", STDOUT), 'dup STDOUT into lexical fh');
236 # used to try to open a file [perl #17830]
237 ok( open(my $stdin, "<&", fileno STDIN), 'dup fileno(STDIN) into lexical fh');
241 skip "This perl uses perlio", 1 if $Config{useperlio};
242 skip "miniperl cannot be relied on to load %Errno"
243 if $ENV{PERL_CORE_MINITEST};
244 # Force the reference to %! to be run time by writing ! as {"!"}
245 skip "This system doesn't understand EINVAL", 1
246 unless exists ${"!"}{EINVAL};
249 ok(!open(F,'>',\my $s) && ${"!"}{EINVAL}, 'open(reference) raises EINVAL');
253 ok( !eval { open F, "BAR", "QUUX" }, 'Unknown open() mode' );
254 like( $@, qr/\QUnknown open() mode 'BAR'/, ' right error' );
258 local $SIG{__WARN__} = sub { $@ = shift };
261 my $tmphandle = shift;
262 my $line = scalar <$tmphandle>;
267 open($fh0[0], "TEST");
269 like($@, qr/<\$fh0\[...\]> line 1\./, "autoviv fh package aelem");
271 open($fh1{k}, "TEST");
273 like($@, qr/<\$fh1{...}> line 1\./, "autoviv fh package helem");
276 open($fh2[0], "TEST");
278 like($@, qr/<\$fh2\[...\]> line 1\./, "autoviv fh lexical aelem");
281 open($fh3{k}, "TEST");
283 like($@, qr/<\$fh3{...}> line 1\./, "autoviv fh lexical helem");
287 skip("These tests use perlio", 5) unless $Config{useperlio};
289 use warnings 'layer';
290 local $SIG{__WARN__} = sub { $w = shift };
292 eval { open(F, ">>>", "afile") };
293 like($w, qr/Invalid separator character '>' in PerlIO layer spec/,
294 "bad open (>>>) warning");
295 like($@, qr/Unknown open\(\) mode '>>>'/,
296 "bad open (>>>) failure");
298 eval { open(F, ">:u", "afile" ) };
299 like($w, qr/Unknown PerlIO layer "u"/,
300 'bad layer ">:u" warning');
301 eval { open(F, "<:u", "afile" ) };
302 like($w, qr/Unknown PerlIO layer "u"/,
303 'bad layer "<:u" warning');
304 eval { open(F, ":c", "afile" ) };
305 like($@, qr/Unknown open\(\) mode ':c'/,
306 'bad layer ":c" failure');
309 # [perl #28986] "open m" crashes Perl
311 fresh_perl_like('open m', qr/^Search pattern not terminated at/,
312 { stderr => 1 }, 'open m test');
315 'sub f { open(my $fh, "xxx"); $fh = "f"; } f; f;print "ok"',
316 'ok', { stderr => 1 },
317 '#29102: Crash on assignment to lexical filehandle');
319 # [perl #31767] Using $1 as a filehandle via open $1, "file" doesn't raise
322 eval { open $99, "foo" };
323 like($@, qr/Modification of a read-only value attempted/, "readonly fh");