This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[perl #98092] Fix unreferenced scalar warnings in clone.t
[perl5.git] / t / op / coresubs.t
CommitLineData
7fa5bd9b
FC
1#!./perl
2
bfce6a3e
FC
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
5# file, too.
7fa5bd9b
FC
6
7BEGIN {
8 chdir 't' if -d 't';
9 @INC = qw(. ../lib);
10 require "test.pl";
47ac839d 11 skip_all_without_dynamic_extension('B');
7fa5bd9b
FC
12 $^P |= 0x100;
13}
7fa5bd9b 14
47ac839d
FC
15use B::Deparse;
16my $bd = new B::Deparse '-p';
7fa5bd9b 17
47ac839d
FC
18my %unsupported = map +($_=>1), qw (CORE and cmp dump eq ge gt le
19 lt ne or x xor);
20my %args_for = (
21 dbmopen => '%1,$2,$3',
22 dbmclose => '%1',
46e00a91 23);
46e00a91 24
47ac839d
FC
25use File::Spec::Functions;
26my $keywords_file = catfile(updir,'regen','keywords.pl');
27open my $kh, $keywords_file
28 or die "$0 cannot open $keywords_file: $!";
29while(<$kh>) {
30 if (m?__END__?..${\0} and /^[+-]/) {
31 chomp(my $word = $');
32 if($& eq '+' || $unsupported{$word}) {
33 $tests ++;
34 ok !defined &{\&{"CORE::$word"}}, "no CORE::$word";
46e00a91 35 }
47ac839d
FC
36 else {
37 $tests += 3;
38
39 my $proto = prototype "CORE::$word";
40 *{"my$word"} = \&{"CORE::$word"};
41 is prototype \&{"my$word"}, $proto, "prototype of &CORE::$word";
42
43 CORE::state $protochar = qr/([^\\]|\\(?:[^[]|\[[^]]+\]))/;
44 my $numargs =
45 () = $proto =~ s/;.*//r =~ /\G$protochar/g;
46 my $code =
47 "#line 1 This-line-makes-__FILE__-easier-to-test.
48 sub { () = (my$word("
49 . ($args_for{$word} || join ",", map "\$$_", 1..$numargs)
50 . "))}";
51 my $core = $bd->coderef2text(eval $code =~ s/my/CORE::/r or die);
52 my $my = $bd->coderef2text(eval $code or die);
53 is $my, $core, "inlinability of CORE::$word with parens";
54
55 $code =
56 "#line 1 This-line-makes-__FILE__-easier-to-test.
57 sub { () = (my$word "
58 . ($args_for{$word} || join ",", map "\$$_", 1..$numargs)
59 . ")}";
60 $core = $bd->coderef2text(eval $code =~ s/my/CORE::/r or die);
61 $my = $bd->coderef2text(eval $code or die);
62 is $my, $core, "inlinability of CORE::$word without parens";
63
64 # High-precedence tests
65 my $hpcode;
66 if (!$proto && defined $proto) { # nullary
67 $hpcode = "sub { () = my$word + 1 }";
46e00a91 68 }
47ac839d
FC
69 elsif ($proto =~ /^;?$protochar\z/) { # unary
70 $hpcode = "sub { () = my$word "
71 . ($args_for{$word}||'$a') . ' > $b'
72 .'}';
73 }
74 if ($hpcode) {
75 $tests ++;
76 $core = $bd->coderef2text(eval $hpcode =~ s/my/CORE::/r or die);
77 $my = $bd->coderef2text(eval $hpcode or die);
78 is $my, $core, "precedence of CORE::$word without parens";
46e00a91 79 }
93f0bc49 80
47ac839d
FC
81 next if ($proto =~ /\@/);
82 # These ops currently accept any number of args, despite their
83 # prototypes, if they have any:
84 next if $word =~ /^(?:chom?p|exec|keys|each|not|read(?:lin|pip)e
85 |reset|system|values|l?stat)/x;
bf0571fd 86
bccb6c7b 87 $tests ++;
47ac839d
FC
88 $code =
89 "sub { () = (my$word("
90 . (
91 $args_for{$word}
92 ? $args_for{$word}.',$7'
93 : join ",", map "\$$_", 1..$numargs+5+(
94 $proto =~ /;/
95 ? () = $' =~ /\G$protochar/g
96 : 0
97 )
98 )
99 . "))}";
100 eval $code;
101 like $@, qr/^Too many arguments for $word/,
102 "inlined CORE::$word with too many args"
103 or warn $code;
104
bccb6c7b
FC
105 }
106 }
107}
7fa5bd9b 108
7fa5bd9b
FC
109is curr_test, $tests+1, 'right number of tests';
110done_testing;
111
47ac839d 112CORE::__END__