15 my $Perl = which_perl();
17 my $afile = tempfile();
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 ok( open(my $f, '|-', <<EOC), 'open |-' );
93 print $f "not ok $test - piped in\n";
97 print $f "not ok $test - piped in\n";
99 ok( close($f), ' close' );
105 ok( !eval { open my $f, '<&', $afile; 1; }, '<& on a non-filehandle' );
106 like( $@, qr/Bad filehandle:\s+$afile/, ' right error' );
111 unlink($afile) if -f $afile;
113 ok( open(local $f,"+>$afile"), 'open local $f, "+>", ...' );
116 ok( -f $afile, ' -f' );
117 ok( (print $f "SomeData\n"), ' print' );
118 is( tell($f), 9, ' tell' );
119 ok( seek($f,0,0), ' seek set' );
122 is( $b, "SomeData\n", ' readline' );
123 ok( -f $f, ' still a file' );
125 eval { die "Message" };
126 like( $@, qr/<\$f> line 1/, ' proper die message' );
127 ok( close($f), ' close' );
133 ok( open(local $f,'>', $afile), 'open local $f, ">", ...' );
134 ok( (print $f "a row\n"), ' print');
135 ok( close($f), ' close');
136 ok( -s $afile < 10, ' -s' );
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, "<", ...' );
149 is( scalar @rows, 2, ' readline list context' );
150 ok( close($f), ' close' );
153 ok( -s $afile < 20, ' -s' );
156 ok( open(local $f, '+<', $afile), 'open local $f, "+<", ...' );
158 is( scalar @rows, 2, ' readline list context' );
159 ok( seek($f, 0, 1), ' seek cur' );
160 ok( (print $f "yet another row\n"), ' print' );
161 ok( close($f), ' close' );
162 ok( -s $afile > 20, ' -s' );
168 ok( open(local $f, '-|', <<EOC), 'open local $f, "-|", ...' );
169 $Perl -e "print qq(a row\\n); print qq(another row\\n)"
173 is( scalar @rows, 2, ' readline list context' );
174 ok( close($f), ' close' );
178 ok( open(local $f, '|-', <<EOC), 'open local $f, "|-", ...' );
179 $Perl -pe "s/^not //"
183 my $test = curr_test;
184 print $f "not ok $test - piping\n";
188 print $f "not ok $test - piping\n";
190 ok( close($f), ' close' );
196 ok( !eval { open local $f, '<&', $afile; 1 }, 'local <& on non-filehandle');
197 like( $@, qr/Bad filehandle:\s+$afile/, ' right error' );
202 ok( open(F, qq{$Perl -le "print 'ok'"|}), 'open to pipe' );
203 is(scalar <F>, "ok\n", ' readline');
204 ok( close F, ' close' );
208 ok( open(F, "-|", qq{$Perl -le "print 'ok'"}), 'open -|');
209 is( scalar <F>, "ok\n", ' readline');
210 ok( close F, ' close' );
215 # other dupping techniques
217 ok( open(my $stdout, ">&", \*STDOUT), 'dup \*STDOUT into lexical fh');
218 ok( open(STDOUT, ">&", $stdout), 'restore dupped STDOUT from lexical fh');
221 use strict; # the below should not warn
222 ok( open(my $stdout, ">&", STDOUT), 'dup STDOUT into lexical fh');
225 # used to try to open a file [perl #17830]
226 ok( open(my $stdin, "<&", fileno STDIN), 'dup fileno(STDIN) into lexical fh') or _diag $!;
230 skip "This perl uses perlio", 1 if $Config{useperlio};
231 skip "miniperl cannot be relied on to load %Errno"
232 if $ENV{PERL_CORE_MINITEST};
233 # Force the reference to %! to be run time by writing ! as {"!"}
234 skip "This system doesn't understand EINVAL", 1
235 unless exists ${"!"}{EINVAL};
238 ok(!open(F,'>',\my $s) && ${"!"}{EINVAL}, 'open(reference) raises EINVAL');
242 ok( !eval { open F, "BAR", "QUUX" }, 'Unknown open() mode' );
243 like( $@, qr/\QUnknown open() mode 'BAR'/, ' right error' );
247 local $SIG{__WARN__} = sub { $@ = shift };
250 my $tmphandle = shift;
251 my $line = scalar <$tmphandle>;
256 open($fh0[0], "TEST");
258 like($@, qr/<\$fh0\[...\]> line 1\./, "autoviv fh package aelem");
260 open($fh1{k}, "TEST");
262 like($@, qr/<\$fh1{...}> line 1\./, "autoviv fh package helem");
265 open($fh2[0], "TEST");
267 like($@, qr/<\$fh2\[...\]> line 1\./, "autoviv fh lexical aelem");
270 open($fh3{k}, "TEST");
272 like($@, qr/<\$fh3{...}> line 1\./, "autoviv fh lexical helem");
276 skip("These tests use perlio", 5) unless $Config{useperlio};
278 use warnings 'layer';
279 local $SIG{__WARN__} = sub { $w = shift };
281 eval { open(F, ">>>", $afile) };
282 like($w, qr/Invalid separator character '>' in PerlIO layer spec/,
283 "bad open (>>>) warning");
284 like($@, qr/Unknown open\(\) mode '>>>'/,
285 "bad open (>>>) failure");
287 eval { open(F, ">:u", $afile ) };
288 like($w, qr/Unknown PerlIO layer "u"/,
289 'bad layer ">:u" warning');
290 eval { open(F, "<:u", $afile ) };
291 like($w, qr/Unknown PerlIO layer "u"/,
292 'bad layer "<:u" warning');
293 eval { open(F, ":c", $afile ) };
294 like($@, qr/Unknown open\(\) mode ':c'/,
295 'bad layer ":c" failure');
298 # [perl #28986] "open m" crashes Perl
300 fresh_perl_like('open m', qr/^Search pattern not terminated at/,
301 { stderr => 1 }, 'open m test');
304 'sub f { open(my $fh, "xxx"); $fh = "f"; } f; f;print "ok"',
305 'ok', { stderr => 1 },
306 '#29102: Crash on assignment to lexical filehandle');
308 # [perl #31767] Using $1 as a filehandle via open $1, "file" doesn't raise
311 eval { open $99, "foo" };
312 like($@, qr/Modification of a read-only value attempted/, "readonly fh");