xref: /openbsd-src/gnu/usr.bin/perl/t/lib/feature/bareword_filehandles (revision 5486feefcc8cb79b19e014ab332cc5dfd05b3b33)
1Test no feature bareword_filehandles
2
3todo:
4
5print HANDLE
6print HANDLE LIST
7printf HANDLE
8printf HANDLE LIST
9say HANDLE
10say HANDLE LIST
11readline
12<> / <HANDLE>
13<<>> - has an implicit argument
14truncate
15stat
16-X
17lstat
18open
19close
20eof
21fileno
22flock
23getc
24read
25write ?
26seek
27tell
28select
29sysopen
30sysread
31syswrite
32sysseek
33pipe
34
35socket
36connect
37bind
38listen
39recv
40send
41setsockopt
42getsockopt
43shutdown
44socketpair
45accept
46getpeername
47getsockname
48
49binmode
50ioctl
51fcntl
52chmod - doesn't accept bareword handles
53chown - doesn't accept bareword handles
54
55opendir
56closedir
57readdir
58seekdir
59telldir
60rewinddir
61chdir
62
63also check
64
65sort
66map
67grep
68
69aren't modified
70
71
72__END__
73# NAME defaults and explicitly on
74#!perl -c
75use File::Temp qw(tempfile);
76use Fcntl qw(SEEK_SET);
77use Socket;
78my ($fh, $name) = tempfile;
79open FOO, ">", File::Spec->devnull;
80print FOO;
81print FOO "Hello";
82printf FOO "Hello";
83seek FOO, 0, SEEK_SET;
84truncate FOO, 0;
85print FOO "Something read\n";
86close FOO;
87<FOO>;
88{
89    local *ARGV;
90    local *ARGVOUT;
91    @ARGV = $name;
92    <<>>;
93    <>;
94}
95pipe FH1, FH2;
96socketpair S1, S2, AF_UNIX, SOCK_STREAM, PF_UNSPEC;
97shutdown S1, 0;
98
99use feature "bareword_filehandles";
100open FOO, ">", File::Spec->devnull;
101print FOO;
102print FOO "Hello";
103printf FOO "Hello";
104seek FOO, 0, SEEK_SET;
105truncate FOO, 0;
106print FOO "Something read\n";
107close FOO;
108<FOO>;
109{
110    local *ARGV;
111    local *ARGVOUT;
112    @ARGV = $name;
113    <<>>;
114    <>;
115}
116pipe FH3, FH4;
117socketpair S3, S4, AF_UNIX, SOCK_STREAM, PF_UNSPEC;
118shutdown S3, 0;
119
120EXPECT
121- syntax OK
122########
123# NAME check atan2() with a handle doesn't trigger bareword filehandle errors
124no feature "bareword_filehandles", "indirect";
125my $x = atan2(FOO 1, 2);
126# my original approach to this hooked newGVREF(), which the parsing for most LOPs (as with
127# atan2() above) could end up calling newGVREF(), producing an unexpected error message.
128EXPECT
129OPTIONS fatal
130Number found where operator expected (Do you need to predeclare "FOO"?) at - line 2, near "FOO 1"
131Missing comma after first argument to atan2 function at - line 2, near "2)"
132Execution of - aborted due to compilation errors.
133########
134# NAME print HANDLE LIST, printf HANDLE LIST, print HANDLE, printf HANDLE
135use File::Spec;
136open FOO, ">", File::Spec->devnull or die $!;
137$_ = "abc";
138print FOO "test\n";
139printf FOO "test\n";
140print FOO;
141printf FOO;
142no feature "bareword_filehandles";
143print FOO "test2\n";
144printf FOO "test2\n";
145print FOO;
146printf FOO;
147print STDERR;
148print STDOUT;
149print ARGV;
150print ARGVOUT;
151print DATA;
152print STDIN;
153EXPECT
154OPTIONS fatal
155Bareword filehandle "FOO" not allowed under 'no feature "bareword_filehandles"' at - line 9.
156Bareword filehandle "FOO" not allowed under 'no feature "bareword_filehandles"' at - line 10.
157Bareword filehandle "FOO" not allowed under 'no feature "bareword_filehandles"' at - line 11.
158Bareword filehandle "FOO" not allowed under 'no feature "bareword_filehandles"' at - line 12.
159Execution of - aborted due to compilation errors.
160########
161# NAME say HANDLE LIST, say HANDLE
162use File::Spec;
163use feature "say";
164open FOO, ">", File::Spec->devnull or die $!;
165$_ = "abc";
166say FOO "test\n";
167say FOO;
168no feature "bareword_filehandles";
169say FOO "test2\n";
170say FOO;
171EXPECT
172OPTIONS fatal
173Bareword filehandle "FOO" not allowed under 'no feature "bareword_filehandles"' at - line 8.
174Bareword filehandle "FOO" not allowed under 'no feature "bareword_filehandles"' at - line 9.
175Execution of - aborted due to compilation errors.
176########
177# NAME readline FOO, readline, <>, <FOO>
178use File::Spec;
179open FOO, "<", File::Spec->devnull or die $!;
180my $x = readline FOO;
181$x .= readline FOO; # rcatline
182$x = readline(FOO); # parsed a little differently with ()
183$x .= readline(FOO);
184$x = <FOO>;
185$x .= <FOO>;
186no feature "bareword_filehandles";
187$x = readline FOO;
188$x .= readline FOO; # rcatline
189$x = readline(FOO); # parsed a little differently with ()
190$x .= readline(FOO);
191$x = <FOO>;
192$x .= <FOO>;
193$x = readline STDIN;
194$x = <STDIN>;
195EXPECT
196OPTIONS fatal
197Bareword filehandle "FOO" not allowed under 'no feature "bareword_filehandles"' at - line 10.
198Bareword filehandle "FOO" not allowed under 'no feature "bareword_filehandles"' at - line 11.
199Bareword filehandle "FOO" not allowed under 'no feature "bareword_filehandles"' at - line 12.
200Bareword filehandle "FOO" not allowed under 'no feature "bareword_filehandles"' at - line 13.
201Bareword filehandle "FOO" not allowed under 'no feature "bareword_filehandles"' at - line 14.
202Bareword filehandle "FOO" not allowed under 'no feature "bareword_filehandles"' at - line 15.
203Execution of - aborted due to compilation errors.
204########
205# NAME truncate
206use strict;
207use warnings;
208# if all goes well this doesn't run anyway
209my $name = "bare$$.tmp";
210END { unlink $name if $name; }
211open FOO, ">", $name or die;
212print FOO "Non-zero length data\n";
213truncate FOO, 2;
214no feature "bareword_filehandles";
215truncate FOO, 1;
216EXPECT
217OPTIONS fatal
218Bareword filehandle "FOO" not allowed under 'no feature "bareword_filehandles"' at - line 10.
219Execution of - aborted due to compilation errors.
220########
221# NAME stat, lstat, -X
222use File::Spec;
223open FOO, "<", File::Spec->devnull;
224my @x = stat FOO;
225@x = lstat FOO;
226my $x = -s FOO;
227no feature "bareword_filehandles";
228@x = stat FOO;
229@x = lstat FOO;
230$x = -s FOO;
231EXPECT
232OPTIONS fatal
233Bareword filehandle "FOO" not allowed under 'no feature "bareword_filehandles"' at - line 7.
234Bareword filehandle "FOO" not allowed under 'no feature "bareword_filehandles"' at - line 8.
235Bareword filehandle "FOO" not allowed under 'no feature "bareword_filehandles"' at - line 9.
236Execution of - aborted due to compilation errors.
237########
238# NAME open, close, eof, fileno
239use File::Spec;
240open FOO, "<", File::Spec->devnull;
241my $x = eof FOO;
242$x = fileno FOO;
243close FOO;
244no feature "bareword_filehandles";
245open FOO, "<", File::Spec->devnull;
246$x = eof FOO;
247$x = fileno FOO;
248close FOO;
249EXPECT
250OPTIONS fatal
251Bareword filehandle "FOO" not allowed under 'no feature "bareword_filehandles"' at - line 7.
252Bareword filehandle "FOO" not allowed under 'no feature "bareword_filehandles"' at - line 8.
253Bareword filehandle "FOO" not allowed under 'no feature "bareword_filehandles"' at - line 9.
254Bareword filehandle "FOO" not allowed under 'no feature "bareword_filehandles"' at - line 10.
255Execution of - aborted due to compilation errors.
256########
257# NAME flock
258use Fcntl ":flock";
259open FOO, "<", $0 or die;
260flock FOO, LOCK_SH;
261no feature "bareword_filehandles";
262flock FOO, LOCK_UN;
263EXPECT
264OPTIONS fatal
265Bareword filehandle "FOO" not allowed under 'no feature "bareword_filehandles"' at - line 5.
266Execution of - aborted due to compilation errors.
267########
268# NAME getc, read, seek, tell
269open FOO, "<", $0 or die;
270my $x = getc FOO;
271read(FOO, $x, 1);
272$x = tell FOO;
273seek FOO, 0, 0;
274no feature "bareword_filehandles";
275$x = getc FOO;
276read(FOO, $x, 1);
277$x = tell FOO;
278seek FOO, 0, 0;
279EXPECT
280OPTIONS fatal
281Bareword filehandle "FOO" not allowed under 'no feature "bareword_filehandles"' at - line 7.
282Bareword filehandle "FOO" not allowed under 'no feature "bareword_filehandles"' at - line 8.
283Bareword filehandle "FOO" not allowed under 'no feature "bareword_filehandles"' at - line 9.
284Bareword filehandle "FOO" not allowed under 'no feature "bareword_filehandles"' at - line 10.
285Execution of - aborted due to compilation errors.
286########
287# NAME select
288open FOO, "<", $0 or die;
289my $old = select FOO;
290no feature "bareword_filehandles";
291select FOO;
292select $old;
293EXPECT
294OPTIONS fatal
295Bareword filehandle "FOO" not allowed under 'no feature "bareword_filehandles"' at - line 4.
296Execution of - aborted due to compilation errors.
297########
298# NAME sysopen, sysread, syswrite, sysseek
299use Fcntl;
300use File::Spec;
301sysopen FOO, File::Spec->devnull, O_RDWR or die;
302sysread FOO, my $x, 10;
303syswrite FOO, "Test";
304my $y = sysseek FOO, 0, SEEK_CUR;
305close FOO;
306no feature "bareword_filehandles";
307sysopen FOO, File::Spec->devnull, O_RDWR or die;
308sysread FOO, my $x, 10;
309syswrite FOO, "Test";
310my $y = sysseek FOO, 0, SEEK_CUR;
311EXPECT
312OPTIONS fatal
313Bareword filehandle "FOO" not allowed under 'no feature "bareword_filehandles"' at - line 9.
314Bareword filehandle "FOO" not allowed under 'no feature "bareword_filehandles"' at - line 10.
315Bareword filehandle "FOO" not allowed under 'no feature "bareword_filehandles"' at - line 11.
316Bareword filehandle "FOO" not allowed under 'no feature "bareword_filehandles"' at - line 12.
317Execution of - aborted due to compilation errors.
318########
319# NAME pipe
320my $fh;
321pipe IN, $fh;
322pipe $fh, OUT;
323pipe IN, OUT;
324no feature "bareword_filehandles";
325pipe IN, $fh;
326pipe $fh, OUT;
327pipe IN, OUT;
328EXPECT
329OPTIONS fatal
330Bareword filehandle "IN" not allowed under 'no feature "bareword_filehandles"' at - line 6.
331Bareword filehandle "OUT" not allowed under 'no feature "bareword_filehandles"' at - line 7.
332Bareword filehandle "IN" not allowed under 'no feature "bareword_filehandles"' at - line 8.
333Bareword filehandle "OUT" not allowed under 'no feature "bareword_filehandles"' at - line 8.
334Execution of - aborted due to compilation errors.
335########
336# NAME socket, connect, bind, listen
337my $fh;
338# this won't run, just use dummy values for domain, type, protocol
339socket(FOO, 0, 0,0);
340connect(FOO, "abc");
341bind(FOO, "abc");
342listen(FOO, 5);
343no feature "bareword_filehandles";
344socket(FOO, 0, 0,0);
345connect(FOO, "abc");
346bind(FOO, "abc");
347listen(FOO, 5);
348EXPECT
349OPTIONS fatal
350Bareword filehandle "FOO" not allowed under 'no feature "bareword_filehandles"' at - line 8.
351Bareword filehandle "FOO" not allowed under 'no feature "bareword_filehandles"' at - line 9.
352Bareword filehandle "FOO" not allowed under 'no feature "bareword_filehandles"' at - line 10.
353Bareword filehandle "FOO" not allowed under 'no feature "bareword_filehandles"' at - line 11.
354Execution of - aborted due to compilation errors.
355########
356# NAME accept
357accept(FOO, CHILD);
358accept($fh, CHILD);
359accept(FOO, $fh);
360no feature "bareword_filehandles";
361accept(FOO, CHILD);
362accept($fh, CHILD);
363accept(FOO, $fh);
364accept(*FOO, *CHILD);
365EXPECT
366OPTIONS fatal
367Bareword filehandle "FOO" not allowed under 'no feature "bareword_filehandles"' at - line 5.
368Bareword filehandle "CHILD" not allowed under 'no feature "bareword_filehandles"' at - line 5.
369Bareword filehandle "CHILD" not allowed under 'no feature "bareword_filehandles"' at - line 6.
370Bareword filehandle "FOO" not allowed under 'no feature "bareword_filehandles"' at - line 7.
371Execution of - aborted due to compilation errors.
372########
373# NAME accept some more
374accept FOO, CHILD ;
375accept $fh, CHILD ;
376accept FOO, $fh ;
377no feature "bareword_filehandles";
378accept FOO, CHILD ;
379accept $fh, CHILD ;
380accept FOO, $fh;
381package BAR {}
382accept QUUX BAR, $fh;
383sub BAR::QUUX { $fh }
384EXPECT
385OPTIONS fatal
386Bareword filehandle "FOO" not allowed under 'no feature "bareword_filehandles"' at - line 5.
387Bareword filehandle "CHILD" not allowed under 'no feature "bareword_filehandles"' at - line 5.
388Bareword filehandle "CHILD" not allowed under 'no feature "bareword_filehandles"' at - line 6.
389Bareword filehandle "FOO" not allowed under 'no feature "bareword_filehandles"' at - line 7.
390Execution of - aborted due to compilation errors.
391########
392# NAME send, recv, setsockopt, getsockopt
393send FOO, "abc", 0;
394recv FOO, my $x, 10, 0;
395setsockopt FOO, 0, 0, 0;
396my $y = getsockopt FOO, 0, 0;
397no feature "bareword_filehandles";
398send FOO, "abc", 0;
399recv FOO, my $x, 10, 0;
400setsockopt FOO, 0, 0, 0;
401my $y = getsockopt FOO, 0, 0;
402EXPECT
403OPTIONS fatal
404Bareword filehandle "FOO" not allowed under 'no feature "bareword_filehandles"' at - line 6.
405Bareword filehandle "FOO" not allowed under 'no feature "bareword_filehandles"' at - line 7.
406Bareword filehandle "FOO" not allowed under 'no feature "bareword_filehandles"' at - line 8.
407Bareword filehandle "FOO" not allowed under 'no feature "bareword_filehandles"' at - line 9.
408Execution of - aborted due to compilation errors.
409########
410# NAME shutdown, getsockname, getpeername
411shutdown FOO, 0;
412my $sockname = getsockname FOO;
413my $peername = getpeername FOO;
414no feature "bareword_filehandles";
415shutdown FOO, 0;
416$sockname = getsockname FOO;
417$peername = getpeername FOO;
418EXPECT
419OPTIONS fatal
420Bareword filehandle "FOO" not allowed under 'no feature "bareword_filehandles"' at - line 5.
421Bareword filehandle "FOO" not allowed under 'no feature "bareword_filehandles"' at - line 6.
422Bareword filehandle "FOO" not allowed under 'no feature "bareword_filehandles"' at - line 7.
423Execution of - aborted due to compilation errors.
424########
425# NAME socketpair
426my $fh;
427socketpair IN, $fh, 0, 0, 0;
428socketpair $fh, OUT, 0, 0, 0;
429socketpair IN, OUT, 0, 0, 0;
430no feature "bareword_filehandles";
431socketpair IN, $fh, 0, 0, 0;
432socketpair $fh, OUT, 0, 0, 0;
433socketpair IN, OUT, 0, 0, 0;
434EXPECT
435OPTIONS fatal
436Bareword filehandle "IN" not allowed under 'no feature "bareword_filehandles"' at - line 6.
437Bareword filehandle "OUT" not allowed under 'no feature "bareword_filehandles"' at - line 7.
438Bareword filehandle "IN" not allowed under 'no feature "bareword_filehandles"' at - line 8.
439Bareword filehandle "OUT" not allowed under 'no feature "bareword_filehandles"' at - line 8.
440Execution of - aborted due to compilation errors.
441########
442# NAME binmode, ioctl, fcntl
443binmode FOO;
444binmode FOO, ":raw";
445ioctl FOO, 0, 0;
446fcntl FOO, 0, 0;
447no feature "bareword_filehandles";
448binmode FOO;
449binmode FOO, ":raw";
450ioctl FOO, 0, 0;
451fcntl FOO, 0, 0;
452EXPECT
453OPTIONS fatal
454Bareword filehandle "FOO" not allowed under 'no feature "bareword_filehandles"' at - line 6.
455Bareword filehandle "FOO" not allowed under 'no feature "bareword_filehandles"' at - line 7.
456Bareword filehandle "FOO" not allowed under 'no feature "bareword_filehandles"' at - line 8.
457Bareword filehandle "FOO" not allowed under 'no feature "bareword_filehandles"' at - line 9.
458Execution of - aborted due to compilation errors.
459########
460# NAME opendir, closedir, readdir
461opendir FOO, ".";
462my @x = readdir FOO;
463chdir FOO;
464closedir FOO;
465no feature "bareword_filehandles";
466opendir FOO, ".";
467my @x = readdir FOO;
468chdir FOO;
469closedir FOO;
470EXPECT
471OPTIONS fatal
472Bareword filehandle "FOO" not allowed under 'no feature "bareword_filehandles"' at - line 6.
473Bareword filehandle "FOO" not allowed under 'no feature "bareword_filehandles"' at - line 7.
474Bareword filehandle "FOO" not allowed under 'no feature "bareword_filehandles"' at - line 8.
475Bareword filehandle "FOO" not allowed under 'no feature "bareword_filehandles"' at - line 9.
476Execution of - aborted due to compilation errors.
477########
478# NAME seekdir, telldir, rewinddir
479use strict;
480my $x = telldir FOO;
481seekdir FOO, $x;
482rewinddir FOO;
483no feature "bareword_filehandles";
484my $x = telldir FOO;
485seekdir FOO, $x;
486rewinddir FOO;
487EXPECT
488OPTIONS fatal
489Bareword filehandle "FOO" not allowed under 'no feature "bareword_filehandles"' at - line 6.
490Bareword filehandle "FOO" not allowed under 'no feature "bareword_filehandles"' at - line 7.
491Bareword filehandle "FOO" not allowed under 'no feature "bareword_filehandles"' at - line 8.
492Execution of - aborted due to compilation errors.
493########
494# NAME file tests
495-T FOO;
496-s FOO;
497no feature "bareword_filehandles";
498-T FOO;
499-s FOO;
500-s _;
501EXPECT
502OPTIONS fatal
503Bareword filehandle "FOO" not allowed under 'no feature "bareword_filehandles"' at - line 4.
504Bareword filehandle "FOO" not allowed under 'no feature "bareword_filehandles"' at - line 5.
505Execution of - aborted due to compilation errors.
506########
507# NAME subroutine calls
508use feature "say";
509no feature "bareword_filehandles";
510sub foo {}
511print foo();
512print foo;
513say foo();
514say foo;
515-x foo();
516-x foo;
517EXPECT
518########
519# NAME method calls
520# https://github.com/Perl/perl5/issues/19704
521use feature "say";
522no feature "bareword_filehandles";
523sub foo {}
524print main->foo();
525print main->foo;
526say main->foo();
527say main->foo;
528-x main->foo();
529-x main->foo;
530EXPECT
531########
532# NAME bareword method calls resolving to bareword filehandles
533# https://github.com/Perl/perl5/issues/19426
534use feature "say";
535my $x = "Foo";
536*Foo = *STDOUT; # make Foo an IO
537sub Foo::say {
538  say ">", @_[1..$#_];
539}
540Foo->say("Hello"); # calls IO::File->say()
541no feature "bareword_filehandles";
542"Foo"->say("Alpha"); # calls IO::File::say();
543$x->say("Beta"); # calls IO::File::say()
544Foo->say("Goodbye"); # calls Foo->say(), Foo now in stash cache
545"Foo"->say("Gamma"); # calls Foo->say()
546STDOUT->say("stdout"); # calls IO::File->say
547use feature "bareword_filehandles";
548Foo->say("Delta"); # calls Foo->say()
549$x->say("Epsilon"); # calls Foo->say()
550EXPECT
551Hello
552Alpha
553Beta
554>Goodbye
555>Gamma
556stdout
557>Delta
558>Epsilon
559