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 inlinable_ok($word, $args_for{$word} || join ",", map "\$$_", 1..$numargs);
71 # High-precedence tests
73 if (!$proto && defined $proto) { # nullary
74 $hpcode = "sub { () = my$word + 1 }";
76 elsif ($proto =~ /^;?$protochar\z/) { # unary
77 $hpcode = "sub { () = my$word "
78 . ($args_for{$word}||'$a') . ' > $b'
83 $core = $bd->coderef2text(eval $hpcode =~ s/my/CORE::/r or die);
84 $my = $bd->coderef2text(eval $hpcode or die);
85 is $my, $core, "precedence of CORE::$word without parens";
88 next if ($proto =~ /\@/);
89 # These ops currently accept any number of args, despite their
90 # prototypes, if they have any:
91 next if $word =~ /^(?:chom?p|exec|keys|each|not
92 |(?:prototyp|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 my $desc = $desc{$word} || $word;
110 like $@, qr/^Too many arguments for $desc/,
111 "inlined CORE::$word with too many args"
119 my ($word, $args, $desc_suffix) = @_;
124 for ([with => "($args)"], [without => " $args"]) {
125 my ($preposition, $full_args) = @$_;
127 "#line 1 This-line-makes-__FILE__-easier-to-test.
128 sub { () = (CORE::$word$full_args) }";
129 my $my_code = $core_code =~ s/CORE::$word/my$word/r;
130 my $core = $bd->coderef2text(eval $core_code or die);
131 my $my = $bd->coderef2text(eval $my_code or die);
132 is $my, $core, "inlinability of CORE::$word $preposition parens $desc_suffix";
137 # This subroutine is outside the warnings scope:
138 sub foo { goto &CORE::abs }
140 $SIG{__WARN__} = sub { like shift, qr\^Use of uninitialized\ };
144 is runperl(prog => 'print CORE->lc, qq-\n-'), "core\n",
145 'methods calls autovivify coresubs';
146 is runperl(prog => '@ISA=CORE; print main->uc, qq-\n-'), "MAIN\n",
147 'inherted method calls autovivify coresubs';
151 like runperl(prog => '$foo/; \&CORE::lc', stderr => 1),
152 qr/^syntax error/, "RT #117607: \\&CORE::foo doesn't crash in error context";
156 ok eval { *CORE::exit = \42 },
157 '[rt.cpan.org #74289] *CORE::foo is not accidentally made read-only';
159 for my $word (qw<keys values each>) {
160 # mykeys() etc were aliased to \&CORE::keys etc above
162 no warnings 'experimental::autoderef';
168 is(eval($code), 'ok', "inlined $word() on autoderef array") or diag $@;
171 inlinable_ok($_, '$_{k}', 'on hash')
172 for qw<delete exists>;
174 @UNIVERSAL::ISA = CORE;
175 is "just another "->ucfirst . "perl hacker,\n"->ucfirst,
176 "Just another Perl hacker,\n", 'coresubs do not return TARG';