1 | #!./perl
|
---|
2 |
|
---|
3 | BEGIN {
|
---|
4 | chdir 't' if -d 't';
|
---|
5 | @INC = '../lib';
|
---|
6 | require './test.pl';
|
---|
7 | }
|
---|
8 |
|
---|
9 | # Cheat. Until we figure out a solution for BEGIN blocks not setting a new
|
---|
10 | # stack (and thus perl API calls possibly moving the stack by extending it)
|
---|
11 | # which doesn't in turn break calling exit from inside a signal handler inside
|
---|
12 | # a BEGIN block.
|
---|
13 | eval {require Errno};
|
---|
14 |
|
---|
15 | $| = 1;
|
---|
16 | use warnings;
|
---|
17 | use Config;
|
---|
18 | $Is_VMS = $^O eq 'VMS';
|
---|
19 | $Is_MacOS = $^O eq 'MacOS';
|
---|
20 |
|
---|
21 | plan tests => 108;
|
---|
22 |
|
---|
23 | my $Perl = which_perl();
|
---|
24 |
|
---|
25 | {
|
---|
26 | unlink("afile") if -f "afile";
|
---|
27 |
|
---|
28 | $! = 0; # the -f above will set $! if 'afile' doesn't exist.
|
---|
29 | ok( open(my $f,"+>afile"), 'open(my $f, "+>...")' );
|
---|
30 |
|
---|
31 | binmode $f;
|
---|
32 | ok( -f "afile", ' its a file');
|
---|
33 | ok( (print $f "SomeData\n"), ' we can print to it');
|
---|
34 | is( tell($f), 9, ' tell()' );
|
---|
35 | ok( seek($f,0,0), ' seek set' );
|
---|
36 |
|
---|
37 | $b = <$f>;
|
---|
38 | is( $b, "SomeData\n", ' readline' );
|
---|
39 | ok( -f $f, ' still a file' );
|
---|
40 |
|
---|
41 | eval { die "Message" };
|
---|
42 | like( $@, qr/<\$f> line 1/, ' die message correct' );
|
---|
43 |
|
---|
44 | ok( close($f), ' close()' );
|
---|
45 | ok( unlink("afile"), ' unlink()' );
|
---|
46 | }
|
---|
47 |
|
---|
48 | {
|
---|
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' );
|
---|
53 | }
|
---|
54 |
|
---|
55 | {
|
---|
56 | ok( open(my $f,'>>', 'afile'), "open(my \$f, '>>', 'afile')" );
|
---|
57 | ok( (print $f "a row\n"), ' print' );
|
---|
58 | ok( close($f), ' close' );
|
---|
59 | ok( -s 'afile' > 10, ' -s' );
|
---|
60 | }
|
---|
61 |
|
---|
62 | {
|
---|
63 | ok( open(my $f, '<', 'afile'), "open(my \$f, '<', 'afile')" );
|
---|
64 | my @rows = <$f>;
|
---|
65 | is( scalar @rows, 2, ' readline, list context' );
|
---|
66 | is( $rows[0], "a row\n", ' first line read' );
|
---|
67 | is( $rows[1], "a row\n", ' second line' );
|
---|
68 | ok( close($f), ' close' );
|
---|
69 | }
|
---|
70 |
|
---|
71 | {
|
---|
72 | ok( -s 'afile' < 20, '-s' );
|
---|
73 |
|
---|
74 | ok( open(my $f, '+<', 'afile'), 'open +<' );
|
---|
75 | my @rows = <$f>;
|
---|
76 | is( scalar @rows, 2, ' readline, list context' );
|
---|
77 | ok( seek($f, 0, 1), ' seek cur' );
|
---|
78 | ok( (print $f "yet another row\n"), ' print' );
|
---|
79 | ok( close($f), ' close' );
|
---|
80 | ok( -s 'afile' > 20, ' -s' );
|
---|
81 |
|
---|
82 | unlink("afile");
|
---|
83 | }
|
---|
84 |
|
---|
85 | SKIP: {
|
---|
86 | skip "open -| busted and noisy on VMS", 3 if $Is_VMS;
|
---|
87 |
|
---|
88 | ok( open(my $f, '-|', <<EOC), 'open -|' );
|
---|
89 | $Perl -e "print qq(a row\\n); print qq(another row\\n)"
|
---|
90 | EOC
|
---|
91 |
|
---|
92 | my @rows = <$f>;
|
---|
93 | is( scalar @rows, 2, ' readline, list context' );
|
---|
94 | ok( close($f), ' close' );
|
---|
95 | }
|
---|
96 |
|
---|
97 | SKIP: {
|
---|
98 | skip "Output for |- doesn't go to shell on MacOS", 5 if $Is_MacOS;
|
---|
99 |
|
---|
100 | ok( open(my $f, '|-', <<EOC), 'open |-' );
|
---|
101 | $Perl -pe "s/^not //"
|
---|
102 | EOC
|
---|
103 |
|
---|
104 | my @rows = <$f>;
|
---|
105 | my $test = curr_test;
|
---|
106 | print $f "not ok $test - piped in\n";
|
---|
107 | next_test;
|
---|
108 |
|
---|
109 | $test = curr_test;
|
---|
110 | print $f "not ok $test - piped in\n";
|
---|
111 | next_test;
|
---|
112 | ok( close($f), ' close' );
|
---|
113 | sleep 1;
|
---|
114 | pass('flushing');
|
---|
115 | }
|
---|
116 |
|
---|
117 |
|
---|
118 | ok( !eval { open my $f, '<&', 'afile'; 1; }, '<& on a non-filehandle' );
|
---|
119 | like( $@, qr/Bad filehandle:\s+afile/, ' right error' );
|
---|
120 |
|
---|
121 |
|
---|
122 | # local $file tests
|
---|
123 | {
|
---|
124 | unlink("afile") if -f "afile";
|
---|
125 |
|
---|
126 | ok( open(local $f,"+>afile"), 'open local $f, "+>", ...' );
|
---|
127 | binmode $f;
|
---|
128 |
|
---|
129 | ok( -f "afile", ' -f' );
|
---|
130 | ok( (print $f "SomeData\n"), ' print' );
|
---|
131 | is( tell($f), 9, ' tell' );
|
---|
132 | ok( seek($f,0,0), ' seek set' );
|
---|
133 |
|
---|
134 | $b = <$f>;
|
---|
135 | is( $b, "SomeData\n", ' readline' );
|
---|
136 | ok( -f $f, ' still a file' );
|
---|
137 |
|
---|
138 | eval { die "Message" };
|
---|
139 | like( $@, qr/<\$f> line 1/, ' proper die message' );
|
---|
140 | ok( close($f), ' close' );
|
---|
141 |
|
---|
142 | unlink("afile");
|
---|
143 | }
|
---|
144 |
|
---|
145 | {
|
---|
146 | ok( open(local $f,'>', 'afile'), 'open local $f, ">", ...' );
|
---|
147 | ok( (print $f "a row\n"), ' print');
|
---|
148 | ok( close($f), ' close');
|
---|
149 | ok( -s 'afile' < 10, ' -s' );
|
---|
150 | }
|
---|
151 |
|
---|
152 | {
|
---|
153 | ok( open(local $f,'>>', 'afile'), 'open local $f, ">>", ...' );
|
---|
154 | ok( (print $f "a row\n"), ' print');
|
---|
155 | ok( close($f), ' close');
|
---|
156 | ok( -s 'afile' > 10, ' -s' );
|
---|
157 | }
|
---|
158 |
|
---|
159 | {
|
---|
160 | ok( open(local $f, '<', 'afile'), 'open local $f, "<", ...' );
|
---|
161 | my @rows = <$f>;
|
---|
162 | is( scalar @rows, 2, ' readline list context' );
|
---|
163 | ok( close($f), ' close' );
|
---|
164 | }
|
---|
165 |
|
---|
166 | ok( -s 'afile' < 20, ' -s' );
|
---|
167 |
|
---|
168 | {
|
---|
169 | ok( open(local $f, '+<', 'afile'), 'open local $f, "+<", ...' );
|
---|
170 | my @rows = <$f>;
|
---|
171 | is( scalar @rows, 2, ' readline list context' );
|
---|
172 | ok( seek($f, 0, 1), ' seek cur' );
|
---|
173 | ok( (print $f "yet another row\n"), ' print' );
|
---|
174 | ok( close($f), ' close' );
|
---|
175 | ok( -s 'afile' > 20, ' -s' );
|
---|
176 |
|
---|
177 | unlink("afile");
|
---|
178 | }
|
---|
179 |
|
---|
180 | SKIP: {
|
---|
181 | skip "open -| busted and noisy on VMS", 3 if $Is_VMS;
|
---|
182 |
|
---|
183 | ok( open(local $f, '-|', <<EOC), 'open local $f, "-|", ...' );
|
---|
184 | $Perl -e "print qq(a row\\n); print qq(another row\\n)"
|
---|
185 | EOC
|
---|
186 | my @rows = <$f>;
|
---|
187 |
|
---|
188 | is( scalar @rows, 2, ' readline list context' );
|
---|
189 | ok( close($f), ' close' );
|
---|
190 | }
|
---|
191 |
|
---|
192 | SKIP: {
|
---|
193 | skip "Output for |- doesn't go to shell on MacOS", 5 if $Is_MacOS;
|
---|
194 |
|
---|
195 | ok( open(local $f, '|-', <<EOC), 'open local $f, "|-", ...' );
|
---|
196 | $Perl -pe "s/^not //"
|
---|
197 | EOC
|
---|
198 |
|
---|
199 | my @rows = <$f>;
|
---|
200 | my $test = curr_test;
|
---|
201 | print $f "not ok $test - piping\n";
|
---|
202 | next_test;
|
---|
203 |
|
---|
204 | $test = curr_test;
|
---|
205 | print $f "not ok $test - piping\n";
|
---|
206 | next_test;
|
---|
207 | ok( close($f), ' close' );
|
---|
208 | sleep 1;
|
---|
209 | pass("Flush");
|
---|
210 | }
|
---|
211 |
|
---|
212 |
|
---|
213 | ok( !eval { open local $f, '<&', 'afile'; 1 }, 'local <& on non-filehandle');
|
---|
214 | like( $@, qr/Bad filehandle:\s+afile/, ' right error' );
|
---|
215 |
|
---|
216 | {
|
---|
217 | local *F;
|
---|
218 | for (1..2) {
|
---|
219 | ok( open(F, qq{$Perl -le "print 'ok'"|}), 'open to pipe' );
|
---|
220 | is(scalar <F>, "ok\n", ' readline');
|
---|
221 | ok( close F, ' close' );
|
---|
222 | }
|
---|
223 |
|
---|
224 | for (1..2) {
|
---|
225 | ok( open(F, "-|", qq{$Perl -le "print 'ok'"}), 'open -|');
|
---|
226 | is( scalar <F>, "ok\n", ' readline');
|
---|
227 | ok( close F, ' close' );
|
---|
228 | }
|
---|
229 | }
|
---|
230 |
|
---|
231 |
|
---|
232 | # other dupping techniques
|
---|
233 | {
|
---|
234 | ok( open(my $stdout, ">&", \*STDOUT), 'dup \*STDOUT into lexical fh');
|
---|
235 | ok( open(STDOUT, ">&", $stdout), 'restore dupped STDOUT from lexical fh');
|
---|
236 |
|
---|
237 | {
|
---|
238 | use strict; # the below should not warn
|
---|
239 | ok( open(my $stdout, ">&", STDOUT), 'dup STDOUT into lexical fh');
|
---|
240 | }
|
---|
241 |
|
---|
242 | # used to try to open a file [perl #17830]
|
---|
243 | ok( open(my $stdin, "<&", fileno STDIN), 'dup fileno(STDIN) into lexical fh') or _diag $!;
|
---|
244 | }
|
---|
245 |
|
---|
246 | SKIP: {
|
---|
247 | skip "This perl uses perlio", 1 if $Config{useperlio};
|
---|
248 | skip "miniperl cannot be relied on to load %Errno"
|
---|
249 | if $ENV{PERL_CORE_MINITEST};
|
---|
250 | # Force the reference to %! to be run time by writing ! as {"!"}
|
---|
251 | skip "This system doesn't understand EINVAL", 1
|
---|
252 | unless exists ${"!"}{EINVAL};
|
---|
253 |
|
---|
254 | no warnings 'io';
|
---|
255 | ok(!open(F,'>',\my $s) && ${"!"}{EINVAL}, 'open(reference) raises EINVAL');
|
---|
256 | }
|
---|
257 |
|
---|
258 | {
|
---|
259 | ok( !eval { open F, "BAR", "QUUX" }, 'Unknown open() mode' );
|
---|
260 | like( $@, qr/\QUnknown open() mode 'BAR'/, ' right error' );
|
---|
261 | }
|
---|
262 |
|
---|
263 | {
|
---|
264 | local $SIG{__WARN__} = sub { $@ = shift };
|
---|
265 |
|
---|
266 | sub gimme {
|
---|
267 | my $tmphandle = shift;
|
---|
268 | my $line = scalar <$tmphandle>;
|
---|
269 | warn "gimme";
|
---|
270 | return $line;
|
---|
271 | }
|
---|
272 |
|
---|
273 | open($fh0[0], "TEST");
|
---|
274 | gimme($fh0[0]);
|
---|
275 | like($@, qr/<\$fh0\[...\]> line 1\./, "autoviv fh package aelem");
|
---|
276 |
|
---|
277 | open($fh1{k}, "TEST");
|
---|
278 | gimme($fh1{k});
|
---|
279 | like($@, qr/<\$fh1{...}> line 1\./, "autoviv fh package helem");
|
---|
280 |
|
---|
281 | my @fh2;
|
---|
282 | open($fh2[0], "TEST");
|
---|
283 | gimme($fh2[0]);
|
---|
284 | like($@, qr/<\$fh2\[...\]> line 1\./, "autoviv fh lexical aelem");
|
---|
285 |
|
---|
286 | my %fh3;
|
---|
287 | open($fh3{k}, "TEST");
|
---|
288 | gimme($fh3{k});
|
---|
289 | like($@, qr/<\$fh3{...}> line 1\./, "autoviv fh lexical helem");
|
---|
290 | }
|
---|
291 |
|
---|
292 | SKIP: {
|
---|
293 | skip("These tests use perlio", 5) unless $Config{useperlio};
|
---|
294 | my $w;
|
---|
295 | use warnings 'layer';
|
---|
296 | local $SIG{__WARN__} = sub { $w = shift };
|
---|
297 |
|
---|
298 | eval { open(F, ">>>", "afile") };
|
---|
299 | like($w, qr/Invalid separator character '>' in PerlIO layer spec/,
|
---|
300 | "bad open (>>>) warning");
|
---|
301 | like($@, qr/Unknown open\(\) mode '>>>'/,
|
---|
302 | "bad open (>>>) failure");
|
---|
303 |
|
---|
304 | eval { open(F, ">:u", "afile" ) };
|
---|
305 | like($w, qr/Unknown PerlIO layer "u"/,
|
---|
306 | 'bad layer ">:u" warning');
|
---|
307 | eval { open(F, "<:u", "afile" ) };
|
---|
308 | like($w, qr/Unknown PerlIO layer "u"/,
|
---|
309 | 'bad layer "<:u" warning');
|
---|
310 | eval { open(F, ":c", "afile" ) };
|
---|
311 | like($@, qr/Unknown open\(\) mode ':c'/,
|
---|
312 | 'bad layer ":c" failure');
|
---|
313 | }
|
---|
314 |
|
---|
315 | # [perl #28986] "open m" crashes Perl
|
---|
316 |
|
---|
317 | fresh_perl_like('open m', qr/^Search pattern not terminated at/,
|
---|
318 | { stderr => 1 }, 'open m test');
|
---|
319 |
|
---|
320 | fresh_perl_is(
|
---|
321 | 'sub f { open(my $fh, "xxx"); $fh = "f"; } f; f;print "ok"',
|
---|
322 | 'ok', { stderr => 1 },
|
---|
323 | '#29102: Crash on assignment to lexical filehandle');
|
---|
324 |
|
---|
325 | # [perl #31767] Using $1 as a filehandle via open $1, "file" doesn't raise
|
---|
326 | # an exception
|
---|
327 |
|
---|
328 | eval { open $99, "foo" };
|
---|
329 | like($@, qr/Modification of a read-only value attempted/, "readonly fh");
|
---|