Commit | Line | Data |
---|---|---|
2adfde11 GS |
1 | #!./perl |
2 | ||
3 | BEGIN { | |
4 | chdir 't' if -d 't'; | |
30fe34ed | 5 | require './test.pl'; |
43ece5b1 | 6 | set_up_inc(qw '../lib ../cpan/Text-ParseWords/lib'); |
069abe75 FC |
7 | require Config; # load these before we mess with *CORE::GLOBAL::require |
8 | require 'Config_heavy.pl'; # since runperl will need them | |
2adfde11 GS |
9 | } |
10 | ||
7fba2966 | 11 | plan tests => 36; |
2adfde11 GS |
12 | |
13 | # | |
14 | # This file tries to test builtin override using CORE::GLOBAL | |
15 | # | |
16 | my $dirsep = "/"; | |
17 | ||
18 | BEGIN { package Foo; *main::getlogin = sub { "kilroy"; } } | |
19 | ||
30fe34ed | 20 | is( getlogin, "kilroy" ); |
2adfde11 GS |
21 | |
22 | my $t = 42; | |
23 | BEGIN { *CORE::GLOBAL::time = sub () { $t; } } | |
24 | ||
30fe34ed | 25 | is( 45, time + 3 ); |
2adfde11 GS |
26 | |
27 | # | |
28 | # require has special behaviour | |
29 | # | |
30 | my $r; | |
31 | BEGIN { *CORE::GLOBAL::require = sub { $r = shift; 1; } } | |
32 | ||
33 | require Foo; | |
30fe34ed | 34 | is( $r, "Foo.pm" ); |
2adfde11 GS |
35 | |
36 | require Foo::Bar; | |
30fe34ed | 37 | is( $r, join($dirsep, "Foo", "Bar.pm") ); |
2adfde11 GS |
38 | |
39 | require 'Foo'; | |
30fe34ed | 40 | is( $r, "Foo" ); |
2adfde11 | 41 | |
88e9444c NC |
42 | require 5.006; |
43 | is( $r, "5.006" ); | |
2adfde11 GS |
44 | |
45 | require v5.6; | |
30fe34ed | 46 | ok( abs($r - 5.006) < 0.001 && $r eq "\x05\x06" ); |
2adfde11 GS |
47 | |
48 | eval "use Foo"; | |
30fe34ed | 49 | is( $r, "Foo.pm" ); |
2adfde11 GS |
50 | |
51 | eval "use Foo::Bar"; | |
30fe34ed | 52 | is( $r, join($dirsep, "Foo", "Bar.pm") ); |
2adfde11 | 53 | |
81bccfe4 | 54 | { |
7d69d4a6 FC |
55 | my @r; |
56 | local *CORE::GLOBAL::require = sub { push @r, shift; 1; }; | |
81bccfe4 | 57 | eval "use 5.006"; |
7d69d4a6 | 58 | like( " @r ", qr " 5\.006 " ); |
81bccfe4 | 59 | } |
b9f751c0 | 60 | |
7c864bb3 VP |
61 | { |
62 | local $_ = 'foo.pm'; | |
63 | require; | |
64 | is( $r, 'foo.pm' ); | |
65 | } | |
66 | ||
b9f751c0 GS |
67 | # localizing *CORE::GLOBAL::foo should revert to finding CORE::foo |
68 | { | |
69 | local(*CORE::GLOBAL::require); | |
70 | $r = ''; | |
71 | eval "require NoNeXiSt;"; | |
30fe34ed | 72 | ok( ! ( $r or $@ !~ /^Can't locate NoNeXiSt/i ) ); |
b9f751c0 | 73 | } |
9b3023bc RGS |
74 | |
75 | # | |
76 | # readline() has special behaviour too | |
77 | # | |
78 | ||
79 | $r = 11; | |
80 | BEGIN { *CORE::GLOBAL::readline = sub (;*) { ++$r }; } | |
30fe34ed RGS |
81 | is( <FH> , 12 ); |
82 | is( <$fh> , 13 ); | |
9b3023bc | 83 | my $pad_fh; |
30fe34ed | 84 | is( <$pad_fh> , 14 ); |
f63a114b RGS |
85 | { |
86 | my $buf = ''; $buf .= <FH>; | |
87 | is( $buf, 15, 'rcatline' ); | |
88 | } | |
9b3023bc RGS |
89 | |
90 | # Non-global readline() override | |
91 | BEGIN { *Rgs::readline = sub (;*) { --$r }; } | |
149c1637 RGS |
92 | { |
93 | package Rgs; | |
f63a114b RGS |
94 | ::is( <FH> , 14 ); |
95 | ::is( <$fh> , 13 ); | |
96 | ::is( <$pad_fh> , 12 ); | |
97 | my $buf = ''; $buf .= <FH>; | |
98 | ::is( $buf, 11, 'rcatline' ); | |
149c1637 | 99 | } |
30fe34ed | 100 | |
e3f73d4e RGS |
101 | # Global readpipe() override |
102 | BEGIN { *CORE::GLOBAL::readpipe = sub ($) { "$_[0] " . --$r }; } | |
103 | is( `rm`, "rm 10", '``' ); | |
104 | is( qx/cp/, "cp 9", 'qx' ); | |
105 | ||
106 | # Non-global readpipe() override | |
107 | BEGIN { *Rgs::readpipe = sub ($) { ++$r . " $_[0]" }; } | |
108 | { | |
109 | package Rgs; | |
110 | ::is( `rm`, "10 rm", '``' ); | |
111 | ::is( qx/cp/, "11 cp", 'qx' ); | |
112 | } | |
113 | ||
93f09d7b | 114 | # Verify that the parsing of overridden keywords isn't messed up |
30fe34ed RGS |
115 | # by the indirect object notation |
116 | { | |
117 | local $SIG{__WARN__} = sub { | |
118 | ::like( $_[0], qr/^ok overriden at/ ); | |
119 | }; | |
120 | BEGIN { *OverridenWarn::warn = sub { CORE::warn "@_ overriden"; }; } | |
121 | package OverridenWarn; | |
122 | sub foo { "ok" } | |
123 | warn( OverridenWarn->foo() ); | |
124 | warn OverridenWarn->foo(); | |
125 | } | |
126 | BEGIN { *OverridenPop::pop = sub { ::is( $_[0][0], "ok" ) }; } | |
149c1637 RGS |
127 | { |
128 | package OverridenPop; | |
129 | sub foo { [ "ok" ] } | |
130 | pop( OverridenPop->foo() ); | |
131 | pop OverridenPop->foo(); | |
132 | } | |
133 | ||
134 | { | |
135 | eval { | |
136 | local *CORE::GLOBAL::require = sub { | |
137 | CORE::require($_[0]); | |
138 | }; | |
139 | require 5; | |
140 | require Text::ParseWords; | |
141 | }; | |
142 | is $@, ''; | |
143 | } | |
70e5f2b5 FC |
144 | |
145 | # Constant inlining should not countermand "use subs" overrides | |
146 | BEGIN { package other; *::caller = \&::caller } | |
147 | sub caller() { 42 } | |
148 | caller; # inline the constant | |
149 | is caller, 42, 'constant inlining does not undo "use subs" on keywords'; | |
069abe75 FC |
150 | |
151 | is runperl(prog => 'sub CORE::GLOBAL::do; do file; print qq-ok\n-'), | |
152 | "ok\n", | |
153 | 'no crash with CORE::GLOBAL::do stub'; | |
ae2c6838 FC |
154 | is runperl(prog => 'sub CORE::GLOBAL::glob; glob; print qq-ok\n-'), |
155 | "ok\n", | |
156 | 'no crash with CORE::GLOBAL::glob stub'; | |
fcea1f7a FC |
157 | is runperl(prog => 'sub CORE::GLOBAL::require; require re; print qq-o\n-'), |
158 | "o\n", | |
159 | 'no crash with CORE::GLOBAL::require stub'; | |
952ad5fe FC |
160 | |
161 | like runperl(prog => 'use constant foo=>1; ' | |
162 | .'BEGIN { *{q|CORE::GLOBAL::readpipe|} = \&{q|foo|};1}' | |
163 | .'warn ``', | |
164 | stderr => 1), | |
165 | qr/Too many arguments/, | |
166 | '`` does not ignore &CORE::GLOBAL::readpipe aliased to a constant'; | |
a93a1bfd FC |
167 | like runperl(prog => 'use constant foo=>1; ' |
168 | .'BEGIN { *{q|CORE::GLOBAL::readline|} = \&{q|foo|};1}' | |
169 | .'warn <a>', | |
170 | stderr => 1), | |
171 | qr/Too many arguments/, | |
172 | '<> does not ignore &CORE::GLOBAL::readline aliased to a constant'; | |
60417971 FC |
173 | |
174 | is runperl(prog => 'use constant t=>42; ' | |
175 | .'BEGIN { *{q|CORE::GLOBAL::time|} = \&{q|t|};1}' | |
26b21b99 | 176 | .'print time, chr utf8::unicode_to_native(10)', |
60417971 FC |
177 | stderr => 1), |
178 | "42\n", | |
179 | 'keywords respect global constant overrides'; |