6 set_up_inc(qw '../lib ../cpan/Text-ParseWords/lib');
7 require Config; # load these before we mess with *CORE::GLOBAL::require
8 require 'Config_heavy.pl'; # since runperl will need them
14 # This file tries to test builtin override using CORE::GLOBAL
18 BEGIN { package Foo; *main::getlogin = sub { "kilroy"; } }
20 is( getlogin, "kilroy" );
23 BEGIN { *CORE::GLOBAL::time = sub () { $t; } }
28 # require has special behaviour
31 BEGIN { *CORE::GLOBAL::require = sub { $r = shift; 1; } }
37 is( $r, join($dirsep, "Foo", "Bar.pm") );
46 ok( abs($r - 5.006) < 0.001 && $r eq "\x05\x06" );
52 is( $r, join($dirsep, "Foo", "Bar.pm") );
56 local *CORE::GLOBAL::require = sub { push @r, shift; 1; };
58 like( " @r ", qr " 5\.006 " );
69 # Can’t do ‘no warnings’ with CORE::GLOBAL::require overridden. :-)
70 CORE::require warnings;
71 unimport warnings 'experimental::lexical_topic';
78 # localizing *CORE::GLOBAL::foo should revert to finding CORE::foo
80 local(*CORE::GLOBAL::require);
82 eval "require NoNeXiSt;";
83 ok( ! ( $r or $@ !~ /^Can't locate NoNeXiSt/i ) );
87 # readline() has special behaviour too
91 BEGIN { *CORE::GLOBAL::readline = sub (;*) { ++$r }; }
97 my $buf = ''; $buf .= <FH>;
98 is( $buf, 15, 'rcatline' );
101 # Non-global readline() override
102 BEGIN { *Rgs::readline = sub (;*) { --$r }; }
107 ::is( <$pad_fh> , 12 );
108 my $buf = ''; $buf .= <FH>;
109 ::is( $buf, 11, 'rcatline' );
112 # Global readpipe() override
113 BEGIN { *CORE::GLOBAL::readpipe = sub ($) { "$_[0] " . --$r }; }
114 is( `rm`, "rm 10", '``' );
115 is( qx/cp/, "cp 9", 'qx' );
117 # Non-global readpipe() override
118 BEGIN { *Rgs::readpipe = sub ($) { ++$r . " $_[0]" }; }
121 ::is( `rm`, "10 rm", '``' );
122 ::is( qx/cp/, "11 cp", 'qx' );
125 # Verify that the parsing of overridden keywords isn't messed up
126 # by the indirect object notation
128 local $SIG{__WARN__} = sub {
129 ::like( $_[0], qr/^ok overriden at/ );
131 BEGIN { *OverridenWarn::warn = sub { CORE::warn "@_ overriden"; }; }
132 package OverridenWarn;
134 warn( OverridenWarn->foo() );
135 warn OverridenWarn->foo();
137 BEGIN { *OverridenPop::pop = sub { ::is( $_[0][0], "ok" ) }; }
139 package OverridenPop;
141 pop( OverridenPop->foo() );
142 pop OverridenPop->foo();
147 local *CORE::GLOBAL::require = sub {
148 CORE::require($_[0]);
151 require Text::ParseWords;
156 # Constant inlining should not countermand "use subs" overrides
157 BEGIN { package other; *::caller = \&::caller }
159 caller; # inline the constant
160 is caller, 42, 'constant inlining does not undo "use subs" on keywords';
162 is runperl(prog => 'sub CORE::GLOBAL::do; do file; print qq-ok\n-'),
164 'no crash with CORE::GLOBAL::do stub';
165 is runperl(prog => 'sub CORE::GLOBAL::glob; glob; print qq-ok\n-'),
167 'no crash with CORE::GLOBAL::glob stub';
168 is runperl(prog => 'sub CORE::GLOBAL::require; require re; print qq-o\n-'),
170 'no crash with CORE::GLOBAL::require stub';
172 like runperl(prog => 'use constant foo=>1; '
173 .'BEGIN { *{q|CORE::GLOBAL::readpipe|} = \&{q|foo|};1}'
176 qr/Too many arguments/,
177 '`` does not ignore &CORE::GLOBAL::readpipe aliased to a constant';
178 like runperl(prog => 'use constant foo=>1; '
179 .'BEGIN { *{q|CORE::GLOBAL::readline|} = \&{q|foo|};1}'
182 qr/Too many arguments/,
183 '<> does not ignore &CORE::GLOBAL::readline aliased to a constant';
185 is runperl(prog => 'use constant t=>42; '
186 .'BEGIN { *{q|CORE::GLOBAL::time|} = \&{q|t|};1}'
187 .'print time, chr utf8::unicode_to_native(10)',
190 'keywords respect global constant overrides';