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',
33 pos => 'match position',
36 use File::Spec::Functions;
37 my $keywords_file = catfile(updir,'regen','keywords.pl');
38 open my $kh, $keywords_file
39 or die "$0 cannot open $keywords_file: $!";
41 if (m?__END__?..${\0} and /^[+-]/) {
43 if($unsupported{$word}) {
45 ok !defined &{"CORE::$word"}, "no CORE::$word";
50 ok defined &{"CORE::$word"}, "defined &{'CORE::$word'}";
52 my $proto = prototype "CORE::$word";
53 *{"my$word"} = \&{"CORE::$word"};
54 is prototype \&{"my$word"}, $proto, "prototype of &CORE::$word";
56 CORE::state $protochar = qr/([^\\]|\\(?:[^[]|\[[^]]+\]))/;
58 $word eq 'delete' || $word eq 'exists' ? 1 :
59 (() = $proto =~ s/;.*//r =~ /\G$protochar/g);
61 "#line 1 This-line-makes-__FILE__-easier-to-test.
63 . ($args_for{$word} || join ",", map "\$$_", 1..$numargs)
65 my $core = $bd->coderef2text(eval $code =~ s/my/CORE::/r or die);
66 my $my = $bd->coderef2text(eval $code or die);
67 is $my, $core, "inlinability of CORE::$word with parens";
70 "#line 1 This-line-makes-__FILE__-easier-to-test.
72 . ($args_for{$word} || join ",", map "\$$_", 1..$numargs)
74 $core = $bd->coderef2text(eval $code =~ s/my/CORE::/r or die);
75 $my = $bd->coderef2text(eval $code or die);
76 is $my, $core, "inlinability of CORE::$word without parens";
78 # High-precedence tests
80 if (!$proto && defined $proto) { # nullary
81 $hpcode = "sub { () = my$word + 1 }";
83 elsif ($proto =~ /^;?$protochar\z/) { # unary
84 $hpcode = "sub { () = my$word "
85 . ($args_for{$word}||'$a') . ' > $b'
90 $core = $bd->coderef2text(eval $hpcode =~ s/my/CORE::/r or die);
91 $my = $bd->coderef2text(eval $hpcode or die);
92 is $my, $core, "precedence of CORE::$word without parens";
95 next if ($proto =~ /\@/);
96 # These ops currently accept any number of args, despite their
97 # prototypes, if they have any:
98 next if $word =~ /^(?:chom?p|exec|keys|each|not
99 |(?:prototyp|read(?:lin|pip))e
100 |reset|system|values|l?stat)|evalbytes/x;
104 "sub { () = (my$word("
107 ? $args_for{$word}.',$7'
108 : join ",", map "\$$_", 1..$numargs+5+(
110 ? () = $' =~ /\G$protochar/g
116 my $desc = $desc{$word} || $word;
117 like $@, qr/^Too many arguments for $desc/,
118 "inlined CORE::$word with too many args"
126 # This subroutine is outside the warnings scope:
127 sub foo { goto &CORE::abs }
129 $SIG{__WARN__} = sub { like shift, qr\^Use of uninitialized\ };
133 is runperl(prog => 'print CORE->lc, qq-\n-'), "core\n",
134 'methods calls autovivify coresubs';
135 is runperl(prog => '@ISA=CORE; print main->uc, qq-\n-'), "MAIN\n",
136 'inherted method calls autovivify coresubs';
139 ok eval { *CORE::exit = \42 },
140 '[rt.cpan.org #74289] *CORE::foo is not accidentally made read-only';
142 @UNIVERSAL::ISA = CORE;
143 is "just another "->ucfirst . "perl hacker,\n"->ucfirst,
144 "Just another Perl hacker,\n", 'coresubs do not return TARG';