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 # Non-global readline() override
98 BEGIN { *Rgs::readline = sub (;*) { --$r }; }
103 ::is( <$pad_fh> , 11 );
106 # Global readpipe() override
107 BEGIN { *CORE::GLOBAL::readpipe = sub ($) { "$_[0] " . --$r }; }
108 is( `rm`, "rm 10", '``' );
109 is( qx/cp/, "cp 9", 'qx' );
111 # Non-global readpipe() override
112 BEGIN { *Rgs::readpipe = sub ($) { ++$r . " $_[0]" }; }
115 ::is( `rm`, "10 rm", '``' );
116 ::is( qx/cp/, "11 cp", 'qx' );
119 # Verify that the parsing of overridden keywords isn't messed up
120 # by the indirect object notation
122 local $SIG{__WARN__} = sub {
123 ::like( $_[0], qr/^ok overriden at/ );
125 BEGIN { *OverridenWarn::warn = sub { CORE::warn "@_ overriden"; }; }
126 package OverridenWarn;
128 warn( OverridenWarn->foo() );
129 warn OverridenWarn->foo();
131 BEGIN { *OverridenPop::pop = sub { ::is( $_[0][0], "ok" ) }; }
133 package OverridenPop;
135 pop( OverridenPop->foo() );
136 pop OverridenPop->foo();
141 local *CORE::GLOBAL::require = sub {
142 CORE::require($_[0]);
145 require Text::ParseWords;
150 # Constant inlining should not countermand "use subs" overrides
151 BEGIN { package other; *::caller = \&::caller }
153 caller; # inline the constant
154 is caller, 42, 'constant inlining does not undo "use subs" on keywords';
156 is runperl(prog => 'sub CORE::GLOBAL::do; do file; print qq-ok\n-'),
158 'no crash with CORE::GLOBAL::do stub';
159 is runperl(prog => 'sub CORE::GLOBAL::glob; glob; print qq-ok\n-'),
161 'no crash with CORE::GLOBAL::glob stub';
162 is runperl(prog => 'sub CORE::GLOBAL::require; require re; print qq-o\n-'),
164 'no crash with CORE::GLOBAL::require stub';
166 like runperl(prog => 'use constant foo=>1; '
167 .'BEGIN { *{q|CORE::GLOBAL::readpipe|} = \&{q|foo|};1}'
170 qr/Too many arguments/,
171 '`` does not ignore &CORE::GLOBAL::readpipe aliased to a constant';
172 like runperl(prog => 'use constant foo=>1; '
173 .'BEGIN { *{q|CORE::GLOBAL::readline|} = \&{q|foo|};1}'
176 qr/Too many arguments/,
177 '<> does not ignore &CORE::GLOBAL::readline aliased to a constant';
179 is runperl(prog => 'use constant t=>42; '
180 .'BEGIN { *{q|CORE::GLOBAL::time|} = \&{q|t|};1}'
181 .'print time, chr 10',
184 'keywords respect global constant overrides';