This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Treat a consecutive semicolons in a prototype as 1
[perl5.git] / t / comp / proto.t
CommitLineData
28757baa
PP
1#!./perl
2#
3# Contributed by Graham Barr <Graham.Barr@tiuk.ti.com>
4#
5# So far there are tests for the following prototypes.
6# none, () ($) ($@) ($%) ($;$) (&) (&\@) (&@) (%) (\%) (\@)
7#
8# It is impossible to test every prototype that can be specified, but
9# we should test as many as we can.
44a8e56a
PP
10#
11
12BEGIN {
13 chdir 't' if -d 't';
20822f61 14 @INC = '../lib';
44a8e56a 15}
28757baa 16
f3ca7bab
NC
17# We need this, as in places we're testing the interaction of prototypes with
18# strict
28757baa
PP
19use strict;
20
80e09529 21print "1..186\n";
28757baa
PP
22
23my $i = 1;
24
25sub testing (&$) {
26 my $p = prototype(shift);
27 my $c = shift;
28 my $what = defined $c ? '(' . $p . ')' : 'no prototype';
29 print '#' x 25,"\n";
30 print '# Testing ',$what,"\n";
31 print '#' x 25,"\n";
32 print "not "
33 if((defined($p) && defined($c) && $p ne $c)
34 || (defined($p) != defined($c)));
35 printf "ok %d\n",$i++;
36}
37
38@_ = qw(a b c d);
39my @array;
40my %hash;
41
42##
43##
44##
45
46testing \&no_proto, undef;
47
48sub no_proto {
49 print "# \@_ = (",join(",",@_),")\n";
50 scalar(@_)
51}
52
53print "not " unless 0 == no_proto();
54printf "ok %d\n",$i++;
55
56print "not " unless 1 == no_proto(5);
57printf "ok %d\n",$i++;
58
59print "not " unless 4 == &no_proto;
60printf "ok %d\n",$i++;
61
62print "not " unless 1 == no_proto +6;
63printf "ok %d\n",$i++;
64
65print "not " unless 4 == no_proto(@_);
66printf "ok %d\n",$i++;
67
68##
69##
70##
71
72
73testing \&no_args, '';
74
75sub no_args () {
76 print "# \@_ = (",join(",",@_),")\n";
77 scalar(@_)
78}
79
80print "not " unless 0 == no_args();
81printf "ok %d\n",$i++;
82
83print "not " unless 0 == no_args;
84printf "ok %d\n",$i++;
85
86print "not " unless 5 == no_args +5;
87printf "ok %d\n",$i++;
88
89print "not " unless 4 == &no_args;
90printf "ok %d\n",$i++;
91
92print "not " unless 2 == &no_args(1,2);
93printf "ok %d\n",$i++;
94
95eval "no_args(1)";
96print "not " unless $@;
97printf "ok %d\n",$i++;
98
99##
100##
101##
102
103testing \&one_args, '$';
104
105sub one_args ($) {
106 print "# \@_ = (",join(",",@_),")\n";
107 scalar(@_)
108}
109
110print "not " unless 1 == one_args(1);
111printf "ok %d\n",$i++;
112
113print "not " unless 1 == one_args +5;
114printf "ok %d\n",$i++;
115
116print "not " unless 4 == &one_args;
117printf "ok %d\n",$i++;
118
119print "not " unless 2 == &one_args(1,2);
120printf "ok %d\n",$i++;
121
122eval "one_args(1,2)";
123print "not " unless $@;
124printf "ok %d\n",$i++;
125
126eval "one_args()";
127print "not " unless $@;
128printf "ok %d\n",$i++;
129
130sub one_a_args ($) {
131 print "# \@_ = (",join(",",@_),")\n";
132 print "not " unless @_ == 1 && $_[0] == 4;
133 printf "ok %d\n",$i++;
134}
135
136one_a_args(@_);
137
138##
139##
140##
141
142testing \&over_one_args, '$@';
143
144sub over_one_args ($@) {
145 print "# \@_ = (",join(",",@_),")\n";
146 scalar(@_)
147}
148
149print "not " unless 1 == over_one_args(1);
150printf "ok %d\n",$i++;
151
152print "not " unless 2 == over_one_args(1,2);
153printf "ok %d\n",$i++;
154
155print "not " unless 1 == over_one_args +5;
156printf "ok %d\n",$i++;
157
158print "not " unless 4 == &over_one_args;
159printf "ok %d\n",$i++;
160
161print "not " unless 2 == &over_one_args(1,2);
162printf "ok %d\n",$i++;
163
164print "not " unless 5 == &over_one_args(1,@_);
165printf "ok %d\n",$i++;
166
167eval "over_one_args()";
168print "not " unless $@;
169printf "ok %d\n",$i++;
170
171sub over_one_a_args ($@) {
172 print "# \@_ = (",join(",",@_),")\n";
173 print "not " unless @_ >= 1 && $_[0] == 4;
174 printf "ok %d\n",$i++;
175}
176
177over_one_a_args(@_);
178over_one_a_args(@_,1);
179over_one_a_args(@_,1,2);
180over_one_a_args(@_,@_);
181
182##
183##
184##
185
186testing \&scalar_and_hash, '$%';
187
188sub scalar_and_hash ($%) {
189 print "# \@_ = (",join(",",@_),")\n";
190 scalar(@_)
191}
192
193print "not " unless 1 == scalar_and_hash(1);
194printf "ok %d\n",$i++;
195
196print "not " unless 3 == scalar_and_hash(1,2,3);
197printf "ok %d\n",$i++;
198
199print "not " unless 1 == scalar_and_hash +5;
200printf "ok %d\n",$i++;
201
202print "not " unless 4 == &scalar_and_hash;
203printf "ok %d\n",$i++;
204
205print "not " unless 2 == &scalar_and_hash(1,2);
206printf "ok %d\n",$i++;
207
208print "not " unless 5 == &scalar_and_hash(1,@_);
209printf "ok %d\n",$i++;
210
211eval "scalar_and_hash()";
212print "not " unless $@;
213printf "ok %d\n",$i++;
214
215sub scalar_and_hash_a ($@) {
216 print "# \@_ = (",join(",",@_),")\n";
217 print "not " unless @_ >= 1 && $_[0] == 4;
218 printf "ok %d\n",$i++;
219}
220
221scalar_and_hash_a(@_);
222scalar_and_hash_a(@_,1);
223scalar_and_hash_a(@_,1,2);
224scalar_and_hash_a(@_,@_);
225
226##
227##
228##
229
230testing \&one_or_two, '$;$';
231
232sub one_or_two ($;$) {
233 print "# \@_ = (",join(",",@_),")\n";
234 scalar(@_)
235}
236
237print "not " unless 1 == one_or_two(1);
238printf "ok %d\n",$i++;
239
240print "not " unless 2 == one_or_two(1,3);
241printf "ok %d\n",$i++;
242
243print "not " unless 1 == one_or_two +5;
244printf "ok %d\n",$i++;
245
246print "not " unless 4 == &one_or_two;
247printf "ok %d\n",$i++;
248
249print "not " unless 3 == &one_or_two(1,2,3);
250printf "ok %d\n",$i++;
251
252print "not " unless 5 == &one_or_two(1,@_);
253printf "ok %d\n",$i++;
254
255eval "one_or_two()";
256print "not " unless $@;
257printf "ok %d\n",$i++;
258
259eval "one_or_two(1,2,3)";
260print "not " unless $@;
261printf "ok %d\n",$i++;
262
263sub one_or_two_a ($;$) {
264 print "# \@_ = (",join(",",@_),")\n";
265 print "not " unless @_ >= 1 && $_[0] == 4;
266 printf "ok %d\n",$i++;
267}
268
269one_or_two_a(@_);
270one_or_two_a(@_,1);
271one_or_two_a(@_,@_);
272
273##
274##
275##
276
277testing \&a_sub, '&';
278
279sub a_sub (&) {
280 print "# \@_ = (",join(",",@_),")\n";
281 &{$_[0]};
282}
283
284sub tmp_sub_1 { printf "ok %d\n",$i++ }
285
286a_sub { printf "ok %d\n",$i++ };
287a_sub \&tmp_sub_1;
288
289@array = ( \&tmp_sub_1 );
290eval 'a_sub @array';
291print "not " unless $@;
292printf "ok %d\n",$i++;
293
294##
295##
296##
297
75fc29ea
GS
298testing \&a_subx, '\&';
299
300sub a_subx (\&) {
301 print "# \@_ = (",join(",",@_),")\n";
302 &{$_[0]};
303}
304
305sub tmp_sub_2 { printf "ok %d\n",$i++ }
306a_subx &tmp_sub_2;
307
308@array = ( \&tmp_sub_2 );
309eval 'a_subx @array';
310print "not " unless $@;
311printf "ok %d\n",$i++;
312
313##
314##
315##
316
28757baa
PP
317testing \&sub_aref, '&\@';
318
319sub sub_aref (&\@) {
320 print "# \@_ = (",join(",",@_),")\n";
321 my($sub,$array) = @_;
322 print "not " unless @_ == 2 && @{$array} == 4;
323 print map { &{$sub}($_) } @{$array}
324}
325
326@array = (qw(O K)," ", $i++);
327sub_aref { lc shift } @array;
328print "\n";
329
330##
331##
332##
333
334testing \&sub_array, '&@';
335
336sub sub_array (&@) {
337 print "# \@_ = (",join(",",@_),")\n";
338 print "not " unless @_ == 5;
339 my $sub = shift;
340 print map { &{$sub}($_) } @_
341}
342
343@array = (qw(O K)," ", $i++);
344sub_array { lc shift } @array;
36a5d4ba 345sub_array { lc shift } ('O', 'K', ' ', $i++);
28757baa
PP
346print "\n";
347
348##
349##
350##
351
352testing \&a_hash, '%';
353
354sub a_hash (%) {
355 print "# \@_ = (",join(",",@_),")\n";
356 scalar(@_);
357}
358
359print "not " unless 1 == a_hash 'a';
360printf "ok %d\n",$i++;
361
362print "not " unless 2 == a_hash 'a','b';
363printf "ok %d\n",$i++;
364
365##
366##
367##
368
369testing \&a_hash_ref, '\%';
370
371sub a_hash_ref (\%) {
372 print "# \@_ = (",join(",",@_),")\n";
373 print "not " unless ref($_[0]) && $_[0]->{'a'};
374 printf "ok %d\n",$i++;
375 $_[0]->{'b'} = 2;
376}
377
378%hash = ( a => 1);
379a_hash_ref %hash;
380print "not " unless $hash{'b'} == 2;
381printf "ok %d\n",$i++;
382
383##
384##
385##
386
69dcf70c 387testing \&array_ref_plus, '\@@';
28757baa 388
69dcf70c 389sub array_ref_plus (\@@) {
28757baa 390 print "# \@_ = (",join(",",@_),")\n";
69dcf70c 391 print "not " unless @_ == 2 && ref($_[0]) && 1 == @{$_[0]} && $_[1] eq 'x';
28757baa
PP
392 printf "ok %d\n",$i++;
393 @{$_[0]} = (qw(ok)," ",$i++,"\n");
394}
395
396@array = ('a');
69dcf70c
MB
397{ my @more = ('x');
398 array_ref_plus @array, @more; }
28757baa
PP
399print "not " unless @array == 4;
400print @array;
fb73857a 401
b6c543e3
IZ
402my $p;
403print "not " if defined prototype('CORE::print');
404print "ok ", $i++, "\n";
405
406print "not " if defined prototype('CORE::system');
407print "ok ", $i++, "\n";
408
1c1fc3ea 409print "# CORE::open => ($p)\nnot " if ($p = prototype('CORE::open')) ne '*;$@';
b6c543e3
IZ
410print "ok ", $i++, "\n";
411
6954f42f 412print "# CORE::Foo => ($p), \$@ => '$@'\nnot "
ba5aeb3a 413 if defined ($p = eval { prototype('CORE::Foo') or 1 }) or $@ !~ /^Can't find an opnumber/;
b6c543e3
IZ
414print "ok ", $i++, "\n";
415
1b08e051
FC
416eval { prototype("CORE::a\0b") };
417print "# CORE::a\\0b: \$@ => '$@'\nnot "
418 if $@ !~ /^Can't find an opnumber for "a\0b"/;
419print "ok ", $i++, "\n";
420
421eval { prototype("CORE::\x{100}") };
422print "# CORE::\\x{100}: => ($p), \$@ => '$@'\nnot "
423 if $@ !~ /^Can't find an opnumber for "\x{100}"/;
424print "ok ", $i++, "\n";
425
6954f42f
FC
426"CORE::Foo" =~ /(.*)/;
427print "# \$1 containing CORE::Foo => ($p), \$@ => '$@'\nnot "
428 if defined ($p = eval { prototype($1) or 1 })
429 or $@ !~ /^Can't find an opnumber/;
430print "ok ", $i++, " - \$1 containing CORE::Foo\n";
431
fb73857a
PP
432# correctly note too-short parameter lists that don't end with '$',
433# a possible regression.
434
435sub foo1 ($\@);
436eval q{ foo1 "s" };
437print "not " unless $@ =~ /^Not enough/;
438print "ok ", $i++, "\n";
439
440sub foo2 ($\%);
441eval q{ foo2 "s" };
442print "not " unless $@ =~ /^Not enough/;
443print "ok ", $i++, "\n";
57ff9a15
IZ
444
445sub X::foo3;
446*X::foo3 = sub {'ok'};
447print "# $@not " unless eval {X->foo3} eq 'ok';
448print "ok ", $i++, "\n";
449
450sub X::foo4 ($);
451*X::foo4 = sub ($) {'ok'};
452print "not " unless X->foo4 eq 'ok';
453print "ok ", $i++, "\n";
2ba6ecf4
GS
454
455# test if the (*) prototype allows barewords, constants, scalar expressions,
456# globs and globrefs (just as CORE::open() does), all under stricture
457sub star (*&) { &{$_[1]} }
18228614
GS
458sub star2 (**&) { &{$_[2]} }
459sub BAR { "quux" }
2692f720 460sub Bar::BAZ { "quuz" }
2ba6ecf4 461my $star = 'FOO';
13fc5c14
RGS
462star FOO, sub {
463 print "not " unless $_[0] eq 'FOO';
464 print "ok $i - star FOO\n";
465}; $i++;
466star(FOO, sub {
467 print "not " unless $_[0] eq 'FOO';
468 print "ok $i - star(FOO)\n";
469 }); $i++;
470star "FOO", sub {
471 print "not " unless $_[0] eq 'FOO';
472 print qq/ok $i - star "FOO"\n/;
473}; $i++;
474star("FOO", sub {
475 print "not " unless $_[0] eq 'FOO';
476 print qq/ok $i - star("FOO")\n/;
477 }); $i++;
478star $star, sub {
479 print "not " unless $_[0] eq 'FOO';
480 print "ok $i - star \$star\n";
481}; $i++;
482star($star, sub {
483 print "not " unless $_[0] eq 'FOO';
484 print "ok $i - star(\$star)\n";
485 }); $i++;
486star *FOO, sub {
487 print "not " unless $_[0] eq \*FOO;
488 print "ok $i - star *FOO\n";
489}; $i++;
490star(*FOO, sub {
491 print "not " unless $_[0] eq \*FOO;
492 print "ok $i - star(*FOO)\n";
493 }); $i++;
494star \*FOO, sub {
495 print "not " unless $_[0] eq \*FOO;
496 print "ok $i - star \\*FOO\n";
497}; $i++;
498star(\*FOO, sub {
499 print "not " unless $_[0] eq \*FOO;
500 print "ok $i - star(\\*FOO)\n";
501 }); $i++;
502star2 FOO, BAR, sub {
503 print "not " unless $_[0] eq 'FOO' and $_[1] eq 'BAR';
504 print "ok $i - star2 FOO, BAR\n";
505}; $i++;
506star2(Bar::BAZ, FOO, sub {
507 print "not " unless $_[0] eq 'Bar::BAZ' and $_[1] eq 'FOO';
508 print "ok $i - star2(Bar::BAZ, FOO)\n"
509 }); $i++;
510star2 BAR(), FOO, sub {
511 print "not " unless $_[0] eq 'quux' and $_[1] eq 'FOO';
512 print "ok $i - star2 BAR(), FOO\n"
513}; $i++;
514star2(FOO, BAR(), sub {
515 print "not " unless $_[0] eq 'FOO' and $_[1] eq 'quux';
516 print "ok $i - star2(FOO, BAR())\n";
517 }); $i++;
518star2 "FOO", "BAR", sub {
519 print "not " unless $_[0] eq 'FOO' and $_[1] eq 'BAR';
520 print qq/ok $i - star2 "FOO", "BAR"\n/;
521}; $i++;
522star2("FOO", "BAR", sub {
523 print "not " unless $_[0] eq 'FOO' and $_[1] eq 'BAR';
524 print qq/ok $i - star2("FOO", "BAR")\n/;
525 }); $i++;
526star2 $star, $star, sub {
527 print "not " unless $_[0] eq 'FOO' and $_[1] eq 'FOO';
528 print "ok $i - star2 \$star, \$star\n";
529}; $i++;
530star2($star, $star, sub {
531 print "not " unless $_[0] eq 'FOO' and $_[1] eq 'FOO';
532 print "ok $i - star2(\$star, \$star)\n";
533 }); $i++;
534star2 *FOO, *BAR, sub {
535 print "not " unless $_[0] eq \*FOO and $_[1] eq \*BAR;
536 print "ok $i - star2 *FOO, *BAR\n";
537}; $i++;
538star2(*FOO, *BAR, sub {
539 print "not " unless $_[0] eq \*FOO and $_[1] eq \*BAR;
540 print "ok $i - star2(*FOO, *BAR)\n";
541 }); $i++;
542star2 \*FOO, \*BAR, sub {
543 no strict 'refs';
544 print "not " unless $_[0] eq \*{'FOO'} and $_[1] eq \*{'BAR'};
545 print "ok $i - star2 \*FOO, \*BAR\n";
546}; $i++;
547star2(\*FOO, \*BAR, sub {
548 no strict 'refs';
549 print "not " unless $_[0] eq \*{'FOO'} and $_[1] eq \*{'BAR'};
550 print "ok $i - star2(\*FOO, \*BAR)\n";
551 }); $i++;
18228614 552
80e09529
PM
553# [perl #118585]
554# Test that multiple semicolons are treated as one with *
555sub star3(;;;*){}
556sub star4( ; ; ; ; *){}
557print "not " unless eval 'star3 STDERR; 1';
558print "ok ", $i++, " star3 STDERR\n";
559print "not " unless eval 'star4 STDERR; 1';
560print "ok ", $i++, " star4 STDERR\n";
561
1c01eb51
GS
562# test scalarref prototype
563sub sreftest (\$$) {
13fc5c14
RGS
564 print "not " unless ref $_[0];
565 print "ok $_[1] - sreftest\n";
1c01eb51
GS
566}
567{
568 no strict 'vars';
569 sreftest my $sref, $i++;
570 sreftest($helem{$i}, $i++);
571 sreftest $aelem[0], $i++;
63983e4c 572 sreftest sub { [0] }->()[0], $i++;
062678b2
FC
573 sreftest my $a = 'quidgley', $i++;
574 print "not " if eval 'return 1; sreftest(3+4)';
575 print "ok ", $i++, ' - \$ with invalid argument', "\n";
1c01eb51 576}
c2b35b10 577
c035a075
DG
578# test single term
579sub lazy (+$$) {
580 print "not " unless @_ == 3 && ref $_[0] eq $_[1];
581 print "ok $_[2] - non container test\n";
582}
583sub quietlazy (+) { return shift(@_) }
584sub give_aref { [] }
585sub list_or_scalar { wantarray ? (1..10) : [] }
586{
587 my @multiarray = ("a".."z");
588 my %bighash = @multiarray;
589 lazy(\@multiarray, 'ARRAY', $i++);
590 lazy(\%bighash, 'HASH', $i++);
591 lazy({}, 'HASH', $i++);
592 lazy(give_aref, 'ARRAY', $i++);
593 lazy(3, '', $i++); # allowed by prototype, even if runtime error
594 lazy(list_or_scalar, 'ARRAY', $i++); # propagate scalar context
595}
596
c2b35b10 597# test prototypes when they are evaled and there is a syntax error
24cc8ef6
AD
598# Byacc generates the string "syntax error". Bison gives the
599# string "parse error".
5279fd7b 600#
c2b35b10 601for my $p ( "", qw{ () ($) ($@) ($%) ($;$) (&) (&\@) (&@) (%) (\%) (\@) } ) {
f3ca7bab
NC
602 my $warn = "";
603 local $SIG{__WARN__} = sub {
604 my $thiswarn = join("",@_);
605 return if $thiswarn =~ /^Prototype mismatch: sub main::evaled_subroutine/;
606 $warn .= $thiswarn;
607 };
c2b35b10
A
608 my $eval = "sub evaled_subroutine $p { &void *; }";
609 eval $eval;
24cc8ef6 610 print "# eval[$eval]\nnot " unless $@ && $@ =~ /(parse|syntax) error/i;
c2b35b10 611 print "ok ", $i++, "\n";
f3ca7bab
NC
612 if ($warn eq '') {
613 print "ok ", $i++, "\n";
614 } else {
615 print "not ok ", $i++, "# $warn \n";
616 }
c2b35b10 617}
337449a8 618
5b794e05
JH
619{
620 my $myvar;
621 my @myarray;
622 my %myhash;
623 sub mysub { print "not calling mysub I hope\n" }
624 local *myglob;
625
626 sub myref (\[$@%&*]) { print "# $_[0]\n"; return "$_[0]" }
627
628 print "not " unless myref($myvar) =~ /^SCALAR\(/;
629 print "ok ", $i++, "\n";
062678b2
FC
630 print "not " unless myref($myvar=7) =~ /^SCALAR\(/;
631 print "ok ", $i++, "\n";
5b794e05
JH
632 print "not " unless myref(@myarray) =~ /^ARRAY\(/;
633 print "ok ", $i++, "\n";
634 print "not " unless myref(%myhash) =~ /^HASH\(/;
635 print "ok ", $i++, "\n";
636 print "not " unless myref(&mysub) =~ /^CODE\(/;
637 print "ok ", $i++, "\n";
638 print "not " unless myref(*myglob) =~ /^GLOB\(/;
639 print "ok ", $i++, "\n";
4eba7d22
RGS
640
641 eval q/sub multi1 (\[%@]) { 1 } multi1 $myvar;/;
a0751766
NC
642 print "not "
643 unless $@ =~ /Type of arg 1 to main::multi1 must be one of \[%\@\] /;
4eba7d22
RGS
644 print "ok ", $i++, "\n";
645 eval q/sub multi2 (\[$*&]) { 1 } multi2 @myarray;/;
a0751766
NC
646 print "not "
647 unless $@ =~ /Type of arg 1 to main::multi2 must be one of \[\$\*&\] /;
4eba7d22
RGS
648 print "ok ", $i++, "\n";
649 eval q/sub multi3 (\[$@]) { 1 } multi3 %myhash;/;
a0751766
NC
650 print "not "
651 unless $@ =~ /Type of arg 1 to main::multi3 must be one of \[\$\@\] /;
4eba7d22
RGS
652 print "ok ", $i++, "\n";
653 eval q/sub multi4 ($\[%]) { 1 } multi4 1, &mysub;/;
a0751766
NC
654 print "not "
655 unless $@ =~ /Type of arg 2 to main::multi4 must be one of \[%\] /;
4eba7d22
RGS
656 print "ok ", $i++, "\n";
657 eval q/sub multi5 (\[$@]$) { 1 } multi5 *myglob;/;
a0751766
NC
658 print "not "
659 unless $@ =~ /Type of arg 1 to main::multi5 must be one of \[\$\@\] /
660 && $@ =~ /Not enough arguments/;
4eba7d22 661 print "ok ", $i++, "\n";
5b794e05 662}
2f758a16 663
d37a9538
ST
664# check that obviously bad prototypes are getting warnings
665{
f3ca7bab 666 local $^W = 1;
d37a9538
ST
667 my $warn = "";
668 local $SIG{__WARN__} = sub { $warn .= join("",@_) };
669
670 eval 'sub badproto (@bar) { 1; }';
671 print "not " unless $warn =~ /Illegal character in prototype for main::badproto : \@bar/;
672 print "ok ", $i++, "\n";
2f758a16 673
d37a9538
ST
674 eval 'sub badproto2 (bar) { 1; }';
675 print "not " unless $warn =~ /Illegal character in prototype for main::badproto2 : bar/;
676 print "ok ", $i++, "\n";
677
678 eval 'sub badproto3 (&$bar$@) { 1; }';
679 print "not " unless $warn =~ /Illegal character in prototype for main::badproto3 : &\$bar\$\@/;
680 print "ok ", $i++, "\n";
681
682 eval 'sub badproto4 (@ $b ar) { 1; }';
683 print "not " unless $warn =~ /Illegal character in prototype for main::badproto4 : \@\$bar/;
684 print "ok ", $i++, "\n";
685}
2f758a16 686
d37a9538
ST
687# make sure whitespace in prototypes works
688eval "sub good (\$\t\$\n\$) { 1; }";
689print "not " if $@;
d731386a 690print "ok ", $i++, "\n";
b8ec4db0
A
691
692# Ought to fail, doesn't in 5.8.1.
693eval 'sub bug (\[%@]) { } my $array = [0 .. 1]; bug %$array;';
694print "not " unless $@ =~ /Not a HASH reference/;
695print "ok ", $i++, "\n";
649d02de
FC
696
697# [perl #75904]
698# Test that the following prototypes make subs parse as unary functions:
699# * \sigil \[...] ;$ ;* ;\sigil ;\[...]
80e09529
PM
700# [perl #118585]
701# As a special case, make sure that ;;* is treated the same as ;*
649d02de
FC
702print "not "
703 unless eval 'sub uniproto1 (*) {} uniproto1 $_, 1' or warn $@;
704print "ok ", $i++, "\n";
705print "not "
706 unless eval 'sub uniproto2 (\$) {} uniproto2 $_, 1' or warn $@;
707print "ok ", $i++, "\n";
708print "not "
709 unless eval 'sub uniproto3 (\[$%]) {} uniproto3 %_, 1' or warn $@;
710print "ok ", $i++, "\n";
711print "not "
712 unless eval 'sub uniproto4 (;$) {} uniproto4 $_, 1' or warn $@;
713print "ok ", $i++, "\n";
714print "not "
715 unless eval 'sub uniproto5 (;*) {} uniproto5 $_, 1' or warn $@;
716print "ok ", $i++, "\n";
717print "not "
718 unless eval 'sub uniproto6 (;\@) {} uniproto6 @_, 1' or warn $@;
719print "ok ", $i++, "\n";
720print "not "
721 unless eval 'sub uniproto7 (;\[$%@]) {} uniproto7 @_, 1' or warn $@;
722print "ok ", $i++, "\n";
c035a075
DG
723print "not "
724 unless eval 'sub uniproto8 (+) {} uniproto8 $_, 1' or warn $@;
725print "ok ", $i++, "\n";
726print "not "
727 unless eval 'sub uniproto9 (;+) {} uniproto9 $_, 1' or warn $@;
728print "ok ", $i++, "\n";
80e09529
PM
729print "not "
730 unless eval 'sub uniproto10 (;;;*) {} uniproto10 $_, 1' or warn $@;
731print "ok ", $i++, " - uniproto10 (;;;*)\n";
732print "not "
733 unless eval 'sub uniproto11 ( ; ; ; * ) {} uniproto10 $_, 1' or warn $@;
734print "ok ", $i++, " - uniproto11 ( ; ; ; *)\n";
735print "not "
736 unless eval 'sub uniproto12 (;;;+) {} uniproto12 $_, 1' or warn $@;
737print "ok ", $i++, " - uniproto12 (;;;*)\n";
738print "not "
739 unless eval 'sub uniproto12 ( ; ; ; + ) {} uniproto12 $_, 1' or warn $@;
740print "ok ", $i++, " - uniproto12 ( ; ; ; * )\n";
3fa17e3f 741
3a8944db
FC
742# Test that a trailing semicolon makes a sub have listop precedence
743sub unilist ($;) { $_[0]+1 }
744sub unilist2(_;) { $_[0]+1 }
745sub unilist3(;$;) { $_[0]+1 }
746print "not " unless (unilist 0 || 5) == 6;
747print "ok ", $i++, "\n";
748print "not " unless (unilist2 0 || 5) == 6;
749print "ok ", $i++, "\n";
750print "not " unless (unilist3 0 || 5) == 6;
751print "ok ", $i++, "\n";
752
3fa17e3f
NC
753{
754 # Lack of prototype on a subroutine definition should override any prototype
755 # on the declaration.
756 sub z_zwap (&);
757
758 local $SIG{__WARN__} = sub {
759 my $thiswarn = join "",@_;
760 if ($thiswarn =~ /^Prototype mismatch: sub main::z_zwap/) {
761 print 'ok ', $i++, "\n";
762 } else {
763 print 'not ok ', $i++, "\n";
764 print STDERR $thiswarn;
765 }
766 };
767
768 eval q{sub z_zwap {return @_}};
769
770 if ($@) {
771 print "not ok ", $i++, "# $@";
772 } else {
773 print "ok ", $i++, "\n";
774 }
775
776
777 my @a = (6,4,2);
778 my @got = eval q{z_zwap(@a)};
779
780 if ($@) {
781 print "not ok ", $i++, " # $@";
782 } else {
783 print "ok ", $i++, "\n";
784 }
785
786 if ("@got" eq "@a") {
787 print "ok ", $i++, "\n";
788 } else {
789 print "not ok ", $i++, " # >@got<\n";
790 }
791}