xref: /openbsd-src/gnu/usr.bin/perl/t/lib/warnings/pp_hot (revision db3296cf5c1dd9058ceecc3a29fe4aaa0bd26000)
1  pp_hot.c
2
3  print() on unopened filehandle abc		[pp_print]
4    $f = $a = "abc" ; print $f $a
5
6  Filehandle %s opened only for input		[pp_print]
7    print STDIN "abc" ;
8
9  Filehandle %s opened only for output		[pp_print]
10    $a = <STDOUT> ;
11
12  print() on closed filehandle %s		[pp_print]
13    close STDIN ; print STDIN "abc" ;
14
15  uninitialized					[pp_rv2av]
16	my $a = undef ; my @b = @$a
17
18  uninitialized					[pp_rv2hv]
19	my $a = undef ; my %b = %$a
20
21  Odd number of elements in hash list		[pp_aassign]
22	%X = (1,2,3) ;
23
24  Reference found where even-sized list expected [pp_aassign]
25	$X = [ 1 ..3 ];
26
27  Filehandle %s opened only for output		[Perl_do_readline]
28  	open (FH, ">./xcv") ;
29	my $a = <FH> ;
30
31  glob failed (can't start child: %s)		[Perl_do_readline] <<TODO
32
33  readline() on closed filehandle %s		[Perl_do_readline]
34    close STDIN ; $a = <STDIN>;
35
36  readline() on closed filehandle %s		[Perl_do_readline]
37    readline(NONESUCH);
38
39  glob failed (child exited with status %d%s)	[Perl_do_readline] <<TODO
40
41  Deep recursion on subroutine \"%s\"		[Perl_sub_crush_depth]
42    sub fred { fred() if $a++ < 200} fred()
43
44  Deep recursion on anonymous subroutine 	[Perl_sub_crush_depth]
45    $a = sub { &$a if $a++ < 200} &$a
46
47  Possible Y2K bug: about to append an integer to '19' [pp_concat]
48    $x     = "19$yy\n";
49
50  Use of reference "%s" as array index [pp_aelem]
51    $x[\1]
52
53__END__
54# pp_hot.c [pp_print]
55use warnings 'unopened' ;
56$f = $a = "abc" ;
57print $f $a;
58no warnings 'unopened' ;
59print $f $a;
60EXPECT
61print() on unopened filehandle abc at - line 4.
62########
63# pp_hot.c [pp_print]
64use warnings 'io' ;
65# There is no guarantee that STDOUT is output only, or STDIN input only.
66# Certainly on some BSDs (at least FreeBSD, Darwin, BSDi) file descriptors
67# 1 and 2 are opened read/write on the tty, and the IO layers may reflect this.
68# So we must make our own file handle that is read only.
69my $file = "./xcv" ; unlink $file ;
70open (FH, ">$file") or die $! ;
71close FH or die $! ;
72die "There is no file $file" unless -f $file ;
73open (FH, "<$file") or die $! ;
74print FH "anc" ;
75open(FOO, "<&FH") or die $! ;
76print FOO "anc" ;
77no warnings 'io' ;
78print FH "anc" ;
79print FOO "anc" ;
80use warnings 'io' ;
81print FH "anc" ;
82print FOO "anc" ;
83close (FH) or die $! ;
84close (FOO) or die $! ;
85unlink $file ;
86EXPECT
87Filehandle FH opened only for input at - line 12.
88Filehandle FOO opened only for input at - line 14.
89Filehandle FH opened only for input at - line 19.
90Filehandle FOO opened only for input at - line 20.
91########
92# pp_hot.c [pp_print]
93use warnings 'closed' ;
94close STDIN ;
95print STDIN "anc";
96opendir STDIN, ".";
97print STDIN "anc";
98closedir STDIN;
99no warnings 'closed' ;
100print STDIN "anc";
101opendir STDIN, ".";
102print STDIN "anc";
103EXPECT
104print() on closed filehandle STDIN at - line 4.
105print() on closed filehandle STDIN at - line 6.
106	(Are you trying to call print() on dirhandle STDIN?)
107########
108# pp_hot.c [pp_print]
109# [ID 20020425.012] from Dave Steiner <steiner@bakerst.rutgers.edu>
110# This goes segv on 5.7.3
111use warnings 'closed' ;
112my $fh = *STDOUT{IO};
113close STDOUT or die "Can't close STDOUT";
114print $fh "Shouldn't print anything, but shouldn't SEGV either\n";
115EXPECT
116print() on closed filehandle at - line 7.
117########
118# pp_hot.c [pp_print]
119package foo;
120use warnings 'closed';
121open my $fh1, "nonexistent";
122print $fh1 42;
123open $fh2, "nonexistent";
124print $fh2 42;
125open $bar::fh3, "nonexistent";
126print $bar::fh3 42;
127open bar::FH4, "nonexistent";
128print bar::FH4 42;
129EXPECT
130print() on closed filehandle $fh1 at - line 5.
131print() on closed filehandle $fh2 at - line 7.
132print() on closed filehandle $fh3 at - line 9.
133print() on closed filehandle FH4 at - line 11.
134########
135# pp_hot.c [pp_rv2av]
136use warnings 'uninitialized' ;
137my $a = undef ;
138my @b = @$a;
139no warnings 'uninitialized' ;
140my @c = @$a;
141EXPECT
142Use of uninitialized value in array dereference at - line 4.
143########
144# pp_hot.c [pp_rv2hv]
145use warnings 'uninitialized' ;
146my $a = undef ;
147my %b = %$a;
148no warnings 'uninitialized' ;
149my %c = %$a;
150EXPECT
151Use of uninitialized value in hash dereference at - line 4.
152########
153# pp_hot.c [pp_aassign]
154use warnings 'misc' ;
155my %X ; %X = (1,2,3) ;
156no warnings 'misc' ;
157my %Y ; %Y = (1,2,3) ;
158EXPECT
159Odd number of elements in hash assignment at - line 3.
160########
161# pp_hot.c [pp_aassign]
162use warnings 'misc' ;
163my %X ; %X = [1 .. 3] ;
164no warnings 'misc' ;
165my %Y ; %Y = [1 .. 3] ;
166EXPECT
167Reference found where even-sized list expected at - line 3.
168########
169# pp_hot.c [Perl_do_readline]
170use warnings 'closed' ;
171close STDIN        ; $a = <STDIN> ;
172opendir STDIN, "." ; $a = <STDIN> ;
173closedir STDIN;
174no warnings 'closed' ;
175opendir STDIN, "." ; $a = <STDIN> ;
176$a = <STDIN> ;
177EXPECT
178readline() on closed filehandle STDIN at - line 3.
179readline() on closed filehandle STDIN at - line 4.
180	(Are you trying to call readline() on dirhandle STDIN?)
181########
182# pp_hot.c [Perl_do_readline]
183use warnings 'io' ;
184my $file = "./xcv" ; unlink $file ;
185open (FH, ">$file") or die $! ;
186my $a = <FH> ;
187no warnings 'io' ;
188$a = <FH> ;
189use warnings 'io' ;
190open(FOO, ">&FH") or die $! ;
191$a = <FOO> ;
192no warnings 'io' ;
193$a = <FOO> ;
194use warnings 'io' ;
195$a = <FOO> ;
196$a = <FH> ;
197close (FH) or die $! ;
198close (FOO) or die $! ;
199unlink $file ;
200EXPECT
201Filehandle FH opened only for output at - line 5.
202Filehandle FOO opened only for output at - line 10.
203Filehandle FOO opened only for output at - line 14.
204Filehandle FH opened only for output at - line 15.
205########
206# pp_hot.c [Perl_sub_crush_depth]
207use warnings 'recursion' ;
208sub fred
209{
210    fred() if $a++ < 200
211}
212{
213  local $SIG{__WARN__} = sub {
214    die "ok\n" if $_[0] =~ /^Deep recursion on subroutine "main::fred"/
215  };
216  fred();
217}
218EXPECT
219ok
220########
221# pp_hot.c [Perl_sub_crush_depth]
222no warnings 'recursion' ;
223sub fred
224{
225    fred() if $a++ < 200
226}
227{
228  local $SIG{__WARN__} = sub {
229    die "ok\n" if $_[0] =~ /^Deep recursion on subroutine "main::fred"/
230  };
231  fred();
232}
233EXPECT
234
235########
236# pp_hot.c [Perl_sub_crush_depth]
237use warnings 'recursion' ;
238$b = sub
239{
240    &$b if $a++ < 200
241}  ;
242
243&$b ;
244EXPECT
245Deep recursion on anonymous subroutine at - line 5.
246########
247# pp_hot.c [Perl_sub_crush_depth]
248no warnings 'recursion' ;
249$b = sub
250{
251    &$b if $a++ < 200
252}  ;
253
254&$b ;
255EXPECT
256########
257# pp_hot.c [pp_concat]
258use warnings 'uninitialized';
259my($x, $y);
260sub a { shift }
261a($x . "x");	# should warn once
262a($x . $y);	# should warn twice
263$x .= $y;	# should warn once
264$y .= $y;	# should warn once
265EXPECT
266Use of uninitialized value in concatenation (.) or string at - line 5.
267Use of uninitialized value in concatenation (.) or string at - line 6.
268Use of uninitialized value in concatenation (.) or string at - line 6.
269Use of uninitialized value in concatenation (.) or string at - line 7.
270Use of uninitialized value in concatenation (.) or string at - line 8.
271########
272# pp_hot.c [pp_concat]
273use warnings 'y2k';
274use Config;
275BEGIN {
276    unless ($Config{ccflags} =~ /Y2KWARN/) {
277	print "SKIPPED\n# perl not built with -DPERL_Y2KWARN";
278	exit 0;
279    }
280}
281my $x;
282my $yy = 78;
283$x     = "19$yy\n";
284$x     = "19" . $yy . "\n";
285$x     = "319$yy\n";
286$x     = "319" . $yy . "\n";
287$yy = 19;
288$x     = "ok $yy\n";
289$yy = 9;
290$x     = 1 . $yy;
291no warnings 'y2k';
292$x     = "19$yy\n";
293$x     = "19" . $yy . "\n";
294EXPECT
295Possible Y2K bug: about to append an integer to '19' at - line 12.
296Possible Y2K bug: about to append an integer to '19' at - line 13.
297########
298# pp_hot.c [pp_aelem]
299{
300use warnings 'misc';
301print $x[\1];
302}
303{
304no warnings 'misc';
305print $x[\1];
306}
307
308EXPECT
309OPTION regex
310Use of reference ".*" as array index at - line 4.
311########
312# pp_hot.c [pp_aelem]
313package Foo;use overload q("") => sub {};package main;$a = bless {}, "Foo";
314$b = {};
315{
316use warnings 'misc';
317print $x[$a];
318print $x[$b];
319}
320{
321no warnings 'misc';
322print $x[$a];
323print $x[$b];
324}
325
326EXPECT
327OPTION regex
328Use of reference ".*" as array index at - line 7.
329