3 # This script tests the inlining and prototype of CORE:: subs. Any generic
4 # tests that are not specific to &foo-style calls should go in this
11 skip_all_without_dynamic_extension('B');
16 my $bd = new B::Deparse '-p';
18 my %unsupported = map +($_=>1), qw (
19 __DATA__ __END__ AUTOLOAD BEGIN UNITCHECK CORE DESTROY END INIT CHECK and
20 cmp default delete do dump else elsif eq eval exists for foreach
21 format ge given glob goto grep gt if last le local lt m map my ne next
22 no or our package pos print printf prototype q qq qr qw qx redo require
23 return s say scalar sort split state study sub tr undef unless until use
27 dbmopen => '%1,$2,$3',
31 use File::Spec::Functions;
32 my $keywords_file = catfile(updir,'regen','keywords.pl');
33 open my $kh, $keywords_file
34 or die "$0 cannot open $keywords_file: $!";
36 if (m?__END__?..${\0} and /^[+-]/) {
38 if($unsupported{$word}) {
40 ok !defined &{"CORE::$word"}, "no CORE::$word";
45 ok defined &{"CORE::$word"}, "defined &{'CORE::$word'}";
47 my $proto = prototype "CORE::$word";
48 *{"my$word"} = \&{"CORE::$word"};
49 is prototype \&{"my$word"}, $proto, "prototype of &CORE::$word";
51 CORE::state $protochar = qr/([^\\]|\\(?:[^[]|\[[^]]+\]))/;
53 () = $proto =~ s/;.*//r =~ /\G$protochar/g;
55 "#line 1 This-line-makes-__FILE__-easier-to-test.
57 . ($args_for{$word} || join ",", map "\$$_", 1..$numargs)
59 my $core = $bd->coderef2text(eval $code =~ s/my/CORE::/r or die);
60 my $my = $bd->coderef2text(eval $code or die);
61 is $my, $core, "inlinability of CORE::$word with parens";
64 "#line 1 This-line-makes-__FILE__-easier-to-test.
66 . ($args_for{$word} || join ",", map "\$$_", 1..$numargs)
68 $core = $bd->coderef2text(eval $code =~ s/my/CORE::/r or die);
69 $my = $bd->coderef2text(eval $code or die);
70 is $my, $core, "inlinability of CORE::$word without parens";
72 # High-precedence tests
74 if (!$proto && defined $proto) { # nullary
75 $hpcode = "sub { () = my$word + 1 }";
77 elsif ($proto =~ /^;?$protochar\z/) { # unary
78 $hpcode = "sub { () = my$word "
79 . ($args_for{$word}||'$a') . ' > $b'
84 $core = $bd->coderef2text(eval $hpcode =~ s/my/CORE::/r or die);
85 $my = $bd->coderef2text(eval $hpcode or die);
86 is $my, $core, "precedence of CORE::$word without parens";
89 next if ($proto =~ /\@/);
90 # These ops currently accept any number of args, despite their
91 # prototypes, if they have any:
92 next if $word =~ /^(?:chom?p|exec|keys|each|not|read(?:lin|pip)e
93 |reset|system|values|l?stat)|evalbytes/x;
97 "sub { () = (my$word("
100 ? $args_for{$word}.',$7'
101 : join ",", map "\$$_", 1..$numargs+5+(
103 ? () = $' =~ /\G$protochar/g
109 like $@, qr/^Too many arguments for $word/,
110 "inlined CORE::$word with too many args"
118 # This subroutine is outside the warnings scope:
119 sub foo { goto &CORE::abs }
121 $SIG{__WARN__} = sub { like shift, qr\^Use of uninitialized\ };
125 is runperl(prog => 'print CORE->lc, qq-\n-'), "core\n",
126 'methods calls autovivify coresubs';
127 is runperl(prog => '@ISA=CORE; print main->uc, qq-\n-'), "MAIN\n",
128 'inherted method calls autovivify coresubs';
131 ok eval { *CORE::exit = \42 },
132 '[rt.cpan.org #74289] *CORE::foo is not accidentally made read-only';
134 @UNIVERSAL::ISA = CORE;
135 is "just another "->ucfirst . "perl hacker,\n"->ucfirst,
136 "Just another Perl hacker,\n", 'coresubs do not return TARG';