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 " );
67 # localizing *CORE::GLOBAL::foo should revert to finding CORE::foo
69 local(*CORE::GLOBAL::require);
71 eval "require NoNeXiSt;";
72 ok( ! ( $r or $@ !~ /^Can't locate NoNeXiSt/i ) );
76 # readline() has special behaviour too
80 BEGIN { *CORE::GLOBAL::readline = sub (;*) { ++$r }; }
86 my $buf = ''; $buf .= <FH>;
87 is( $buf, 15, 'rcatline' );
90 # Non-global readline() override
91 BEGIN { *Rgs::readline = sub (;*) { --$r }; }
96 ::is( <$pad_fh> , 12 );
97 my $buf = ''; $buf .= <FH>;
98 ::is( $buf, 11, 'rcatline' );
101 # Global readpipe() override
102 BEGIN { *CORE::GLOBAL::readpipe = sub ($) { "$_[0] " . --$r }; }
103 is( `rm`, "rm 10", '``' );
104 is( qx/cp/, "cp 9", 'qx' );
106 # Non-global readpipe() override
107 BEGIN { *Rgs::readpipe = sub ($) { ++$r . " $_[0]" }; }
110 ::is( `rm`, "10 rm", '``' );
111 ::is( qx/cp/, "11 cp", 'qx' );
114 # Verify that the parsing of overridden keywords isn't messed up
115 # by the indirect object notation
117 local $SIG{__WARN__} = sub {
118 ::like( $_[0], qr/^ok overriden at/ );
120 BEGIN { *OverridenWarn::warn = sub { CORE::warn "@_ overriden"; }; }
121 package OverridenWarn;
123 warn( OverridenWarn->foo() );
124 warn OverridenWarn->foo();
126 BEGIN { *OverridenPop::pop = sub { ::is( $_[0][0], "ok" ) }; }
128 package OverridenPop;
130 pop( OverridenPop->foo() );
131 pop OverridenPop->foo();
136 local *CORE::GLOBAL::require = sub {
137 CORE::require($_[0]);
140 require Text::ParseWords;
145 # Constant inlining should not countermand "use subs" overrides
146 BEGIN { package other; *::caller = \&::caller }
148 caller; # inline the constant
149 is caller, 42, 'constant inlining does not undo "use subs" on keywords';
151 is runperl(prog => 'sub CORE::GLOBAL::do; do file; print qq-ok\n-'),
153 'no crash with CORE::GLOBAL::do stub';
154 is runperl(prog => 'sub CORE::GLOBAL::glob; glob; print qq-ok\n-'),
156 'no crash with CORE::GLOBAL::glob stub';
157 is runperl(prog => 'sub CORE::GLOBAL::require; require re; print qq-o\n-'),
159 'no crash with CORE::GLOBAL::require stub';
161 like runperl(prog => 'use constant foo=>1; '
162 .'BEGIN { *{q|CORE::GLOBAL::readpipe|} = \&{q|foo|};1}'
165 qr/Too many arguments/,
166 '`` does not ignore &CORE::GLOBAL::readpipe aliased to a constant';
167 like runperl(prog => 'use constant foo=>1; '
168 .'BEGIN { *{q|CORE::GLOBAL::readline|} = \&{q|foo|};1}'
171 qr/Too many arguments/,
172 '<> does not ignore &CORE::GLOBAL::readline aliased to a constant';
174 is runperl(prog => 'use constant t=>42; '
175 .'BEGIN { *{q|CORE::GLOBAL::time|} = \&{q|t|};1}'
176 .'print time, chr utf8::unicode_to_native(10)',
179 'keywords respect global constant overrides';