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 # __FILE__ won’t fold with warnings on, and then we get
84 # ‘(eval 21)’ vs ‘(eval 22)’.
85 no warnings 'numeric';
86 $core = $bd->coderef2text(eval $hpcode =~ s/my/CORE::/r or die);
87 $my = $bd->coderef2text(eval $hpcode or die);
88 is $my, $core, "precedence of CORE::$word without parens";
91 next if ($proto =~ /\@/);
92 # These ops currently accept any number of args, despite their
93 # prototypes, if they have any:
94 next if $word =~ /^(?:chom?p|exec|keys|each|not
95 |(?:prototyp|read(?:lin|pip))e
96 |reset|system|values|l?stat)|evalbytes/x;
100 "sub { () = (my$word("
103 ? $args_for{$word}.',$7'
104 : join ",", map "\$$_", 1..$numargs+5+(
106 ? () = $' =~ /\G$protochar/g
112 my $desc = $desc{$word} || $word;
113 like $@, qr/^Too many arguments for $desc/,
114 "inlined CORE::$word with too many args"
122 my ($word, $args, $desc_suffix) = @_;
127 for ([with => "($args)"], [without => " $args"]) {
128 my ($preposition, $full_args) = @$_;
130 "#line 1 This-line-makes-__FILE__-easier-to-test.
131 sub { () = (CORE::$word$full_args) }";
132 my $my_code = $core_code =~ s/CORE::$word/my$word/r;
133 my $core = $bd->coderef2text(eval $core_code or die);
134 my $my = $bd->coderef2text(eval $my_code or die);
135 is $my, $core, "inlinability of CORE::$word $preposition parens $desc_suffix";
140 # This subroutine is outside the warnings scope:
141 sub foo { goto &CORE::abs }
143 $SIG{__WARN__} = sub { like shift, qr\^Use of uninitialized\ };
147 is runperl(prog => 'print CORE->lc, qq-\n-'), "core\n",
148 'methods calls autovivify coresubs';
149 is runperl(prog => '@ISA=CORE; print main->uc, qq-\n-'), "MAIN\n",
150 'inherted method calls autovivify coresubs';
154 like runperl(prog => '$foo/; \&CORE::lc', stderr => 1),
155 qr/^syntax error/, "RT #117607: \\&CORE::foo doesn't crash in error context";
159 ok eval { *CORE::exit = \42 },
160 '[rt.cpan.org #74289] *CORE::foo is not accidentally made read-only';
162 for my $word (qw<keys values each>) {
163 # mykeys() etc were aliased to \&CORE::keys etc above
165 no warnings 'experimental::autoderef';
171 is(eval($code), 'ok', "inlined $word() on autoderef array") or diag $@;
174 inlinable_ok($_, '$_{k}', 'on hash')
175 for qw<delete exists>;
177 @UNIVERSAL::ISA = CORE;
178 is "just another "->ucfirst . "perl hacker,\n"->ucfirst,
179 "Just another Perl hacker,\n", 'coresubs do not return TARG';