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 do dump else elsif eq eval for foreach
21 format ge given goto grep gt if last le local lt m map my ne next
22 no or our package print printf q qq qr qw qx redo require
23 return s say sort state sub tr unless until use
27 dbmopen => '%1,$2,$3',
31 each =>)[0,1,2,1,3,1,4,1],
38 splice =>)[0,1,2,1,3,1,4,1,5,1],
41 pos => 'match position',
44 use File::Spec::Functions;
45 my $keywords_file = catfile(updir,'regen','keywords.pl');
46 open my $kh, $keywords_file
47 or die "$0 cannot open $keywords_file: $!";
49 if (m?__END__?..${\0} and /^[+-]/) {
51 if($unsupported{$word}) {
53 ok !defined &{"CORE::$word"}, "no CORE::$word";
58 ok defined &{"CORE::$word"}, "defined &{'CORE::$word'}";
60 my $proto = prototype "CORE::$word";
61 *{"my$word"} = \&{"CORE::$word"};
62 is prototype \&{"my$word"}, $proto, "prototype of &CORE::$word";
64 CORE::state $protochar = qr/([^\\]|\\(?:[^[]|\[[^]]+\]))/;
66 $word eq 'delete' || $word eq 'exists' ? 1 :
67 (() = $proto =~ s/;.*//r =~ /\G$protochar/g);
69 "#line 1 This-line-makes-__FILE__-easier-to-test.
71 . ($args_for{$word} || join ",", map "\$$_", 1..$numargs)
73 my $core = $bd->coderef2text(eval $code =~ s/my/CORE::/r or die);
74 my $my = $bd->coderef2text(eval $code or die);
75 is $my, $core, "inlinability of CORE::$word with parens";
78 "#line 1 This-line-makes-__FILE__-easier-to-test.
80 . ($args_for{$word} || join ",", map "\$$_", 1..$numargs)
82 $core = $bd->coderef2text(eval $code =~ s/my/CORE::/r or die);
83 $my = $bd->coderef2text(eval $code or die);
84 is $my, $core, "inlinability of CORE::$word without parens";
86 # High-precedence tests
88 if (!$proto && defined $proto) { # nullary
89 $hpcode = "sub { () = my$word + 1 }";
91 elsif ($proto =~ /^;?$protochar\z/) { # unary
92 $hpcode = "sub { () = my$word "
93 . ($args_for{$word}||'$a') . ' > $b'
98 $core = $bd->coderef2text(eval $hpcode =~ s/my/CORE::/r or die);
99 $my = $bd->coderef2text(eval $hpcode or die);
100 is $my, $core, "precedence of CORE::$word without parens";
103 next if ($proto =~ /\@/);
104 # These ops currently accept any number of args, despite their
105 # prototypes, if they have any:
106 next if $word =~ /^(?:chom?p|exec|keys|each|not
107 |(?:prototyp|read(?:lin|pip))e
108 |reset|system|values|l?stat)|evalbytes/x;
112 "sub { () = (my$word("
115 ? $args_for{$word}.',$7'
116 : join ",", map "\$$_", 1..$numargs+5+(
118 ? () = $' =~ /\G$protochar/g
124 my $desc = $desc{$word} || $word;
125 like $@, qr/^Too many arguments for $desc/,
126 "inlined CORE::$word with too many args"
134 # This subroutine is outside the warnings scope:
135 sub foo { goto &CORE::abs }
137 $SIG{__WARN__} = sub { like shift, qr\^Use of uninitialized\ };
141 is runperl(prog => 'print CORE->lc, qq-\n-'), "core\n",
142 'methods calls autovivify coresubs';
143 is runperl(prog => '@ISA=CORE; print main->uc, qq-\n-'), "MAIN\n",
144 'inherted method calls autovivify coresubs';
148 like runperl(prog => '$foo/; \&CORE::lc', stderr => 1),
149 qr/^syntax error/, "RT #117607: \\&CORE::foo doesn't crash in error context";
153 ok eval { *CORE::exit = \42 },
154 '[rt.cpan.org #74289] *CORE::foo is not accidentally made read-only';
156 @UNIVERSAL::ISA = CORE;
157 is "just another "->ucfirst . "perl hacker,\n"->ucfirst,
158 "Just another Perl hacker,\n", 'coresubs do not return TARG';