xref: /openbsd/gnu/usr.bin/perl/t/comp/proto.t (revision 9e6efb0a)
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.
10#
11
12BEGIN {
13    chdir 't' if -d 't';
14    @INC = '../lib';
15}
16
17# We need this, as in places we're testing the interaction of prototypes with
18# strict
19use strict;
20
21print "1..216\n";
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    return unless defined $_[0];
282    &{$_[0]};
283}
284
285sub tmp_sub_1 { printf "ok %d\n",$i++ }
286
287a_sub { printf "ok %d\n",$i++ };
288a_sub \&tmp_sub_1;
289a_sub \(&tmp_sub_1);
290
291@array = ( \&tmp_sub_1 );
292eval 'a_sub @array';
293print "not " unless $@;
294printf "ok %d\n",$i++;
295eval 'a_sub \@array';
296print "not " unless $@ =~ /Type of arg/;
297printf "ok %d\n",$i++;
298eval 'a_sub \%hash';
299print "not " unless $@ =~ /Type of arg/;
300printf "ok %d\n",$i++;
301eval 'a_sub \$scalar';
302print "not " unless $@ =~ /Type of arg/;
303printf "ok %d\n",$i++;
304eval 'a_sub \($list, %of, @refs)';
305print "not " unless $@ =~ /Type of arg/;
306printf "ok %d\n",$i++;
307eval 'a_sub undef';
308print "not " if $@;
309printf "ok %d\n",$i++;
310
311##
312##
313##
314
315testing \&a_subx, '\&';
316
317sub a_subx (\&) {
318    print "# \@_ = (",join(",",@_),")\n";
319    &{$_[0]};
320}
321
322sub tmp_sub_2 { printf "ok %d\n",$i++ }
323a_subx &tmp_sub_2;
324
325@array = ( \&tmp_sub_2 );
326eval 'a_subx @array';
327print "not " unless $@;
328printf "ok %d\n",$i++;
329my $bad =
330    qr/Type of arg 1 to .* must be subroutine \(not subroutine entry\)/;
331eval 'a_subx &tmp_sub_2()';
332print "not " unless $@ =~ $bad;
333printf "ok %d - \\& prohibits &foo()\n",$i++;
334eval 'a_subx tmp_sub_2()';
335print "not " unless $@ =~ $bad;
336printf "ok %d - \\& prohibits foo()\n",$i++;
337eval 'a_subx tmp_sub_2';
338print "not " unless $@ =~ $bad;
339printf "ok %d - \\& prohibits foo where foo is an existing sub\n",$i++;
340
341##
342##
343##
344
345testing \&sub_aref, '&\@';
346
347sub sub_aref (&\@) {
348    print "# \@_ = (",join(",",@_),")\n";
349    my($sub,$array) = @_;
350    print "not " unless @_ == 2 && @{$array} == 4;
351    print map { &{$sub}($_) } @{$array}
352}
353
354@array = (qw(O K)," ", $i++);
355sub_aref { lc shift } @array;
356print "\n";
357
358##
359##
360##
361
362testing \&sub_array, '&@';
363
364sub sub_array (&@) {
365    print "# \@_ = (",join(",",@_),")\n";
366    print "not " unless @_ == 5;
367    my $sub = shift;
368    print map { &{$sub}($_) } @_
369}
370
371@array = (qw(O K)," ", $i++);
372sub_array { lc shift } @array;
373sub_array { lc shift } ('O', 'K', ' ', $i++);
374print "\n";
375
376##
377##
378##
379
380testing \&a_hash, '%';
381
382sub a_hash (%) {
383    print "# \@_ = (",join(",",@_),")\n";
384    scalar(@_);
385}
386
387print "not " unless 1 == a_hash 'a';
388printf "ok %d\n",$i++;
389
390print "not " unless 2 == a_hash 'a','b';
391printf "ok %d\n",$i++;
392
393##
394##
395##
396
397testing \&a_hash_ref, '\%';
398
399sub a_hash_ref (\%) {
400    print "# \@_ = (",join(",",@_),")\n";
401    print "not " unless ref($_[0]) && $_[0]->{'a'};
402    printf "ok %d\n",$i++;
403    $_[0]->{'b'} = 2;
404}
405
406%hash = ( a => 1);
407a_hash_ref %hash;
408print "not " unless $hash{'b'} == 2;
409printf "ok %d\n",$i++;
410
411%hash = ( a => 1);
412a_hash_ref +(%hash);
413print "not " unless $hash{'b'} == 2;
414printf "ok %d\n",$i++;
415
416##
417##
418##
419
420testing \&array_ref_plus, '\@@';
421
422sub array_ref_plus (\@@) {
423    print "# \@_ = (",join(",",@_),")\n";
424    print "not " unless @_ == 2 && ref($_[0]) && 1 == @{$_[0]} && $_[1] eq 'x';
425    printf "ok %d\n",$i++;
426    @{$_[0]} = (qw(ok)," ",$i++,"\n");
427}
428
429@array = ('a');
430{ my @more = ('x');
431  array_ref_plus @array, @more; }
432print "not " unless @array == 4;
433print @array;
434
435@array = ('a');
436{ my @more = ('x');
437  array_ref_plus +(@array), @more; }
438print "not " unless @array == 4;
439print @array;
440
441##
442##
443##
444
445my $p;
446print "not " if defined prototype('CORE::print');
447print "ok ", $i++, "\n";
448
449print "not " if defined prototype('CORE::system');
450print "ok ", $i++, "\n";
451
452print "# CORE::open => ($p)\nnot " if ($p = prototype('CORE::open')) ne '*;$@';
453print "ok ", $i++, "\n";
454
455print "# CORE::Foo => ($p), \$@ => '$@'\nnot "
456    if defined ($p = eval { prototype('CORE::Foo') or 1 }) or $@ !~ /^Can't find an opnumber/;
457print "ok ", $i++, "\n";
458
459eval { prototype("CORE::a\0b") };
460print "# CORE::a\\0b: \$@ => '$@'\nnot "
461    if $@ !~ /^Can't find an opnumber for "a\0b"/;
462print "ok ", $i++, "\n";
463
464eval { prototype("CORE::\x{100}") };
465print "# CORE::\\x{100}: => ($p), \$@ => '$@'\nnot "
466    if $@ !~ /^Can't find an opnumber for "\x{100}"/;
467print "ok ", $i++, "\n";
468
469"CORE::Foo" =~ /(.*)/;
470print "# \$1 containing CORE::Foo => ($p), \$@ => '$@'\nnot "
471    if defined ($p = eval { prototype($1) or 1 })
472    or $@ !~ /^Can't find an opnumber/;
473print "ok ", $i++, " - \$1 containing CORE::Foo\n";
474
475# correctly note too-short parameter lists that don't end with '$',
476#  a possible regression.
477
478sub foo1 ($\@);
479eval q{ foo1 "s" };
480print "not " unless $@ =~ /^Not enough/;
481print "ok ", $i++, "\n";
482
483sub foo2 ($\%);
484eval q{ foo2 "s" };
485print "not " unless $@ =~ /^Not enough/;
486print "ok ", $i++, "\n";
487
488sub X::foo3;
489*X::foo3 = sub {'ok'};
490print "# $@not " unless eval {X->foo3} eq 'ok';
491print "ok ", $i++, "\n";
492
493sub X::foo4 ($);
494*X::foo4 = sub ($) {'ok'};
495print "not " unless X->foo4 eq 'ok';
496print "ok ", $i++, "\n";
497
498# test if the (*) prototype allows barewords, constants, scalar expressions,
499# globs and globrefs (just as CORE::open() does), all under stricture
500sub star (*&) { &{$_[1]} }
501sub star2 (**&) { &{$_[2]} }
502sub BAR { "quux" }
503sub Bar::BAZ { "quuz" }
504my $star = 'FOO';
505star FOO, sub {
506    print "not " unless $_[0] eq 'FOO';
507    print "ok $i - star FOO\n";
508}; $i++;
509star(FOO, sub {
510	print "not " unless $_[0] eq 'FOO';
511	print "ok $i - star(FOO)\n";
512    }); $i++;
513star "FOO", sub {
514    print "not " unless $_[0] eq 'FOO';
515    print qq/ok $i - star "FOO"\n/;
516}; $i++;
517star("FOO", sub {
518	print "not " unless $_[0] eq 'FOO';
519	print qq/ok $i - star("FOO")\n/;
520    }); $i++;
521star $star, sub {
522    print "not " unless $_[0] eq 'FOO';
523    print "ok $i - star \$star\n";
524}; $i++;
525star($star, sub {
526	print "not " unless $_[0] eq 'FOO';
527	print "ok $i - star(\$star)\n";
528    }); $i++;
529star *FOO, sub {
530    print "not " unless $_[0] eq \*FOO;
531    print "ok $i - star *FOO\n";
532}; $i++;
533star(*FOO, sub {
534	print "not " unless $_[0] eq \*FOO;
535	print "ok $i - star(*FOO)\n";
536    }); $i++;
537star \*FOO, sub {
538    print "not " unless $_[0] eq \*FOO;
539    print "ok $i - star \\*FOO\n";
540}; $i++;
541star(\*FOO, sub {
542	print "not " unless $_[0] eq \*FOO;
543	print "ok $i - star(\\*FOO)\n";
544    }); $i++;
545star2 FOO, BAR, sub {
546    print "not " unless $_[0] eq 'FOO' and $_[1] eq 'quux';
547    print "ok $i - star2 FOO, BAR\n";
548}; $i++;
549star2(Bar::BAZ, FOO, sub {
550	print "not " unless $_[0] eq 'quuz' and $_[1] eq 'FOO';
551	print "ok $i - star2(Bar::BAZ, FOO)\n"
552    }); $i++;
553star2 BAR(), FOO, sub {
554    print "not " unless $_[0] eq 'quux' and $_[1] eq 'FOO';
555    print "ok $i - star2 BAR(), FOO\n"
556}; $i++;
557star2(FOO, BAR(), sub {
558	print "not " unless $_[0] eq 'FOO' and $_[1] eq 'quux';
559	print "ok $i - star2(FOO, BAR())\n";
560    }); $i++;
561star2 "FOO", "BAR", sub {
562    print "not " unless $_[0] eq 'FOO' and $_[1] eq 'BAR';
563    print qq/ok $i - star2 "FOO", "BAR"\n/;
564}; $i++;
565star2("FOO", "BAR", sub {
566	print "not " unless $_[0] eq 'FOO' and $_[1] eq 'BAR';
567	print qq/ok $i - star2("FOO", "BAR")\n/;
568    }); $i++;
569star2 $star, $star, sub {
570    print "not " unless $_[0] eq 'FOO' and $_[1] eq 'FOO';
571    print "ok $i - star2 \$star, \$star\n";
572}; $i++;
573star2($star, $star, sub {
574	print "not " unless $_[0] eq 'FOO' and $_[1] eq 'FOO';
575	print "ok $i - star2(\$star, \$star)\n";
576    }); $i++;
577star2 *FOO, *BAR, sub {
578    print "not " unless $_[0] eq \*FOO and $_[1] eq \*BAR;
579    print "ok $i - star2 *FOO, *BAR\n";
580}; $i++;
581star2(*FOO, *BAR, sub {
582	print "not " unless $_[0] eq \*FOO and $_[1] eq \*BAR;
583	print "ok $i - star2(*FOO, *BAR)\n";
584    }); $i++;
585star2 \*FOO, \*BAR, sub {
586    no strict 'refs';
587    print "not " unless $_[0] eq \*{'FOO'} and $_[1] eq \*{'BAR'};
588    print "ok $i - star2 \*FOO, \*BAR\n";
589}; $i++;
590star2(\*FOO, \*BAR, sub {
591	no strict 'refs';
592	print "not " unless $_[0] eq \*{'FOO'} and $_[1] eq \*{'BAR'};
593	print "ok $i - star2(\*FOO, \*BAR)\n";
594    }); $i++;
595
596# [perl #118585]
597# Test that multiple semicolons are treated as one with *
598sub star3(;;;*){}
599sub star4( ; ; ; ; *){}
600print "not " unless eval 'star3 STDERR; 1';
601print "ok ", $i++, " star3 STDERR\n";
602print "not " unless eval 'star4 STDERR; 1';
603print "ok ", $i++, " star4 STDERR\n";
604
605# [perl #2726]
606# Test that prototype binding is late
607print "not " unless eval 'sub l564($){ l564(); } 1';
608print "ok ", $i++, " prototype checking not done within initial definition\n";
609print "not " if eval 'sub l566($); sub l566($){ l566(); } 1';
610print "ok ", $i++, " prototype checking done if sub pre-declared\n";
611
612# test scalarref prototype
613sub sreftest (\$$) {
614    print "not " unless ref $_[0];
615    print "ok $_[1] - sreftest\n";
616}
617{
618    no strict 'vars';
619    sreftest my $sref, $i++;
620    sreftest($helem{$i}, $i++);
621    sreftest $aelem[0], $i++;
622    sreftest sub { [0] }->()[0], $i++;
623    sreftest my $a = 'quidgley', $i++;
624    print "not " if eval 'return 1; sreftest(3+4)';
625    print "ok ", $i++, ' - \$ with invalid argument', "\n";
626}
627
628# test single term
629sub lazy (+$$) {
630    print "not " unless @_ == 3 && ref $_[0] eq $_[1];
631    print "ok $_[2] - non container test\n";
632}
633sub quietlazy (+) { return shift(@_) }
634sub give_aref { [] }
635sub list_or_scalar { wantarray ? (1..10) : [] }
636{
637    my @multiarray = ("a".."z");
638    my %bighash = @multiarray;
639    lazy(\@multiarray, 'ARRAY', $i++);
640    lazy(\%bighash, 'HASH', $i++);
641    lazy({}, 'HASH', $i++);
642    lazy(give_aref, 'ARRAY', $i++);
643    lazy(3, '', $i++); # allowed by prototype, even if runtime error
644    lazy(list_or_scalar, 'ARRAY', $i++); # propagate scalar context
645}
646
647# test prototypes when they are evaled and there is a syntax error
648# Byacc generates the string "syntax error".  Bison gives the
649# string "parse error".
650#
651for my $p ( "", qw{ () ($) ($@) ($%) ($;$) (&) (&\@) (&@) (%) (\%) (\@) } ) {
652  my $warn = "";
653  local $SIG{__WARN__} = sub {
654    my $thiswarn = join("",@_);
655    return if $thiswarn =~ /^Prototype mismatch: sub main::evaled_subroutine/;
656    $warn .= $thiswarn;
657  };
658  my $eval = "sub evaled_subroutine $p { &void *; }";
659  eval $eval;
660  print "# eval[$eval]\nnot " unless $@ && $@ =~ /(parse|syntax) error/i;
661  print "ok ", $i++, "\n";
662  if ($warn eq '') {
663     print "ok ", $i++, "\n";
664  } else {
665    print "not ok ", $i++, "# $warn \n";
666  }
667}
668
669{
670    my $myvar;
671    my @myarray;
672    my %myhash;
673    sub mysub { print "not calling mysub I hope\n" }
674    local *myglob;
675
676    sub myref (\[$@%&*]) { print "# $_[0]\n"; return "$_[0]" }
677
678    print "not " unless myref($myvar)   =~ /^SCALAR\(/;
679    print "ok ", $i++, "\n";
680    print "not " unless myref($myvar=7) =~ /^SCALAR\(/;
681    print "ok ", $i++, "\n";
682    print "not " unless myref(@myarray) =~ /^ARRAY\(/;
683    print "ok ", $i++, "\n";
684    print "not " unless myref(%myhash)  =~ /^HASH\(/;
685    print "ok ", $i++, "\n";
686    print "not " unless myref(&mysub)   =~ /^CODE\(/;
687    print "ok ", $i++, "\n";
688    print "not " unless myref(*myglob)  =~ /^GLOB\(/;
689    print "ok ", $i++, "\n";
690
691    eval q/sub multi1 (\[%@]) { 1 } multi1 $myvar;/;
692    print "not "
693	unless $@ =~ /Type of arg 1 to main::multi1 must be one of \[%\@\] /;
694    print "ok ", $i++, "\n";
695    eval q/sub multi2 (\[$*&]) { 1 } multi2 @myarray;/;
696    print "not "
697	unless $@ =~ /Type of arg 1 to main::multi2 must be one of \[\$\*&\] /;
698    print "ok ", $i++, "\n";
699    eval q/sub multi3 (\[$@]) { 1 } multi3 %myhash;/;
700    print "not "
701	unless $@ =~ /Type of arg 1 to main::multi3 must be one of \[\$\@\] /;
702    print "ok ", $i++, "\n";
703    eval q/sub multi4 ($\[%]) { 1 } multi4 1, &mysub;/;
704    print "not "
705	unless $@ =~ /Type of arg 2 to main::multi4 must be one of \[%\] /;
706    print "ok ", $i++, "\n";
707    eval q/sub multi5 (\[$@]$) { 1 } multi5 *myglob;/;
708    print "not "
709	unless $@ =~ /Type of arg 1 to main::multi5 must be one of \[\$\@\] /
710	    && $@ =~ /Not enough arguments/;
711    print "ok ", $i++, "\n";
712}
713
714# check that obviously bad prototypes are getting warnings
715{
716  local $^W = 1;
717  my $warn = "";
718  local $SIG{__WARN__} = sub { $warn .= join("",@_) };
719
720  eval 'sub badproto (@bar) { 1; }';
721  print "not " unless $warn =~ /Illegal character in prototype for main::badproto : \@bar/;
722  print "ok ", $i++, " checking badproto - (\@bar)\n";
723
724  eval 'sub badproto2 (bar) { 1; }';
725  print "not " unless $warn =~ /Illegal character in prototype for main::badproto2 : bar/;
726  print "ok ", $i++, " checking badproto2 - (bar)\n";
727
728  eval 'sub badproto3 (&$bar$@) { 1; }';
729  print "not " unless $warn =~ /Illegal character in prototype for main::badproto3 : &\$bar\$\@/;
730  print "ok ", $i++, " checking badproto3 - (&\$bar\$\@)\n";
731
732  eval 'sub badproto4 (@ $b ar) { 1; }';
733  # This one emits two warnings
734  print "not " unless $warn =~ /Illegal character in prototype for main::badproto4 : \@ \$b ar/;
735  print "ok ", $i++, " checking badproto4 - (\@ \$b ar) - illegal character\n";
736  print "not " unless $warn =~ /Prototype after '\@' for main::badproto4 : \@ \$b ar/;
737  print "ok ", $i++, " checking badproto4 - (\@ \$b ar) - prototype after '\@'\n";
738
739  eval 'sub badproto5 ($_$) { 1; }';
740  print "not " unless $warn =~ /Illegal character after '_' in prototype for main::badproto5 : \$_\$/;
741  print "ok ", $i++, " checking badproto5 - (\$_\$) - illegal character after '_'\n";
742  print "not " if $warn =~ /Illegal character in prototype for main::badproto5 : \$_\$/;
743  print "ok ", $i++, " checking badproto5 - (\$_\$) - but not just illegal character\n";
744
745  eval 'sub badproto6 (bar_) { 1; }';
746  print "not " unless $warn =~ /Illegal character in prototype for main::badproto6 : bar_/;
747  print "ok ", $i++, " checking badproto6 - (bar_) - illegal character\n";
748  print "not " if $warn =~ /Illegal character after '_' in prototype for main::badproto6 : bar_/;
749  print "ok ", $i++, " checking badproto6 - (bar_) - shouldn't add \"after '_'\"\n";
750
751  eval 'sub badproto7 (_;bar) { 1; }';
752  print "not " unless $warn =~ /Illegal character in prototype for main::badproto7 : _;bar/;
753  print "ok ", $i++, " checking badproto7 - (_;bar) - illegal character\n";
754  print "not " if $warn =~ /Illegal character after '_' in prototype for main::badproto7 : _;bar/;
755  print "ok ", $i++, " checking badproto7 - (_;bar) - shouldn't add \"after '_'\"\n";
756
757  eval 'sub badproto8 (_b) { 1; }';
758  print "not " unless $warn =~ /Illegal character after '_' in prototype for main::badproto8 : _b/;
759  print "ok ", $i++, " checking badproto8 - (_b) - illegal character after '_'\n";
760  print "not " unless $warn =~ /Illegal character in prototype for main::badproto8 : _b/;
761  print "ok ", $i++, " checking badproto8 - (_b) - just illegal character\n";
762
763  eval 'sub badproto9 ([) { 1; }';
764  print "not " unless $warn =~ /Missing '\]' in prototype for main::badproto9 : \[/;
765  print "ok ", $i++, " checking for matching bracket\n";
766
767  eval 'sub badproto10 ([_]) { 1; }';
768  print "not " if $warn =~ /Missing '\]' in prototype for main::badproto10 : \[/;
769  print "ok ", $i++, " checking badproto10 - ([_]) - shouldn't trigger matching bracket\n";
770  print "not " unless $warn =~ /Illegal character after '_' in prototype for main::badproto10 : \[_\]/;
771  print "ok ", $i++, " checking badproto10 - ([_]) - should trigger after '_' warnings\n";
772}
773
774# make sure whitespace in prototypes works
775eval "sub good (\$\t\$\n\$) { 1; }";
776print "not " if $@;
777print "ok ", $i++, "\n";
778# [perl #118629]
779{
780  my $warnings = 0;
781  local $SIG{__WARN__} = sub { $warnings++;};
782  $::{ckproto_test} = ' $ $ ';
783	eval 'sub ckproto_test($$){1;}';
784  print "not " if $warnings;
785  print "ok ", $i++, " Check that ckproto ignores spaces in comparisons\n";
786}
787
788# Ought to fail, doesn't in 5.8.1.
789eval 'sub bug (\[%@]) {  } my $array = [0 .. 1]; bug %$array;';
790print "not " unless $@ =~ /Not a HASH reference/;
791print "ok ", $i++, "\n";
792
793# [perl #75904]
794# Test that the following prototypes make subs parse as unary functions:
795#  * \sigil \[...] ;$ ;* ;\sigil ;\[...]
796# [perl #118585]
797# As a special case, make sure that ;;* is treated the same as ;*
798print "not "
799 unless eval 'sub uniproto1 (*) {} uniproto1 $_, 1' or warn $@;
800print "ok ", $i++, "\n";
801print "not "
802 unless eval 'sub uniproto2 (\$) {} uniproto2 $_, 1' or warn $@;
803print "ok ", $i++, "\n";
804print "not "
805 unless eval 'sub uniproto3 (\[$%]) {} uniproto3 %_, 1' or warn $@;
806print "ok ", $i++, "\n";
807print "not "
808 unless eval 'sub uniproto4 (;$) {} uniproto4 $_, 1' or warn $@;
809print "ok ", $i++, "\n";
810print "not "
811 unless eval 'sub uniproto5 (;*) {} uniproto5 $_, 1' or warn $@;
812print "ok ", $i++, "\n";
813print "not "
814 unless eval 'sub uniproto6 (;\@) {} uniproto6 @_, 1' or warn $@;
815print "ok ", $i++, "\n";
816print "not "
817 unless eval 'sub uniproto7 (;\[$%@]) {} uniproto7 @_, 1' or warn $@;
818print "ok ", $i++, "\n";
819print "not "
820 unless eval 'sub uniproto8 (+) {} uniproto8 $_, 1' or warn $@;
821print "ok ", $i++, "\n";
822print "not "
823 unless eval 'sub uniproto9 (;+) {} uniproto9 $_, 1' or warn $@;
824print "ok ", $i++, "\n";
825print "not "
826 unless eval 'sub uniproto10 (;;;*) {} uniproto10 $_, 1' or warn $@;
827print "ok ", $i++, " - uniproto10 (;;;*)\n";
828print "not "
829 unless eval 'sub uniproto11 ( ; ; ; * ) {} uniproto10 $_, 1' or warn $@;
830print "ok ", $i++, " - uniproto11 ( ; ; ;  *)\n";
831print "not "
832 unless eval 'sub uniproto12 (;;;+) {} uniproto12 $_, 1' or warn $@;
833print "ok ", $i++, " - uniproto12 (;;;*)\n";
834print "not "
835 unless eval 'sub uniproto13 ( ; ; ; + ) {} uniproto13 $_, 1' or warn $@;
836print "ok ", $i++, " - uniproto13 ( ; ; ; * )\n";
837
838
839# Test that a trailing semicolon makes a sub have listop precedence
840sub unilist ($;)  { $_[0]+1 }
841sub unilist2(_;)  { $_[0]+1 }
842sub unilist3(;$;) { $_[0]+1 }
843print "not " unless (unilist 0 || 5) == 6;
844print "ok ", $i++, "\n";
845print "not " unless (unilist2 0 || 5) == 6;
846print "ok ", $i++, "\n";
847print "not " unless (unilist3 0 || 5) == 6;
848print "ok ", $i++, "\n";
849
850{
851  # Lack of prototype on a subroutine definition should override any prototype
852  # on the declaration.
853  sub z_zwap (&);
854
855  local $SIG{__WARN__} = sub {
856    my $thiswarn = join "",@_;
857    if ($thiswarn =~ /^Prototype mismatch: sub main::z_zwap/) {
858      print 'ok ', $i++, "\n";
859    } else {
860      print 'not ok ', $i++, "\n";
861      print STDERR $thiswarn;
862    }
863  };
864
865  eval q{sub z_zwap {return @_}};
866
867  if ($@) {
868    print "not ok ", $i++, "# $@";
869  } else {
870    print "ok ", $i++, "\n";
871  }
872
873
874  my @a = (6,4,2);
875  my @got  = eval q{z_zwap(@a)};
876
877  if ($@) {
878    print "not ok ", $i++, " # $@";
879  } else {
880    print "ok ", $i++, "\n";
881  }
882
883  if ("@got" eq "@a") {
884    print "ok ", $i++, "\n";
885  } else {
886    print "not ok ", $i++, " # >@got<\n";
887  }
888}
889
890# [perl #123514] prototype with no arguments
891$_ = sub ($$$$$$$) {};
892@_ = (1, 2, 3, prototype(), 4, 5, 6);
893print "not " unless "@_" eq '1 2 3 $$$$$$$ 4 5 6';
894print "ok ", $i++, " - [perl #123514] (got @_)\n";
895
896{
897    my $weird_failure = q<>;
898
899    if (eval 'sub scalarref (\$) { }; scalarref( sub {} ); 1') {
900        print "not ok $i: Unexpected scalarref success!\n";
901    }
902    else {
903        if ($@ !~ m/anonymous subroutine/ || $@ !~ m/scalarref/) {
904            $weird_failure = $@;
905
906            $weird_failure =~ s/^/# /m;
907
908            print "# Unexpected error (or none):$/$weird_failure";
909        }
910
911        print( ($weird_failure ? 'not ok' : 'ok') . " $i - anonsub passed to \\\$\n" );
912    }
913
914    $i++;
915}
916