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