This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Have newCONSTSUB pass the length to newXS
[perl5.git] / t / op / override.t
CommitLineData
2adfde11
GS
1#!./perl
2
3BEGIN {
4 chdir 't' if -d 't';
30fe34ed
RGS
5 @INC = '../lib';
6 require './test.pl';
2adfde11
GS
7}
8
7c864bb3 9plan tests => 28;
2adfde11
GS
10
11#
12# This file tries to test builtin override using CORE::GLOBAL
13#
14my $dirsep = "/";
15
16BEGIN { package Foo; *main::getlogin = sub { "kilroy"; } }
17
30fe34ed 18is( getlogin, "kilroy" );
2adfde11
GS
19
20my $t = 42;
21BEGIN { *CORE::GLOBAL::time = sub () { $t; } }
22
30fe34ed 23is( 45, time + 3 );
2adfde11
GS
24
25#
26# require has special behaviour
27#
28my $r;
29BEGIN { *CORE::GLOBAL::require = sub { $r = shift; 1; } }
30
31require Foo;
30fe34ed 32is( $r, "Foo.pm" );
2adfde11
GS
33
34require Foo::Bar;
30fe34ed 35is( $r, join($dirsep, "Foo", "Bar.pm") );
2adfde11
GS
36
37require 'Foo';
30fe34ed 38is( $r, "Foo" );
2adfde11 39
88e9444c
NC
40require 5.006;
41is( $r, "5.006" );
2adfde11
GS
42
43require v5.6;
30fe34ed 44ok( abs($r - 5.006) < 0.001 && $r eq "\x05\x06" );
2adfde11
GS
45
46eval "use Foo";
30fe34ed 47is( $r, "Foo.pm" );
2adfde11
GS
48
49eval "use Foo::Bar";
30fe34ed 50is( $r, join($dirsep, "Foo", "Bar.pm") );
2adfde11 51
88e9444c
NC
52eval "use 5.006";
53is( $r, "5.006" );
b9f751c0 54
7c864bb3
VP
55{
56 local $_ = 'foo.pm';
57 require;
58 is( $r, 'foo.pm' );
59}
60
61{
62 my $_ = 'bar.pm';
63 require;
64 is( $r, 'bar.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;
80BEGIN { *CORE::GLOBAL::readline = sub (;*) { ++$r }; }
30fe34ed
RGS
81is( <FH> , 12 );
82is( <$fh> , 13 );
9b3023bc 83my $pad_fh;
30fe34ed 84is( <$pad_fh> , 14 );
9b3023bc
RGS
85
86# Non-global readline() override
87BEGIN { *Rgs::readline = sub (;*) { --$r }; }
149c1637
RGS
88{
89 package Rgs;
90 ::is( <FH> , 13 );
91 ::is( <$fh> , 12 );
92 ::is( <$pad_fh> , 11 );
93}
30fe34ed 94
e3f73d4e
RGS
95# Global readpipe() override
96BEGIN { *CORE::GLOBAL::readpipe = sub ($) { "$_[0] " . --$r }; }
97is( `rm`, "rm 10", '``' );
98is( qx/cp/, "cp 9", 'qx' );
99
100# Non-global readpipe() override
101BEGIN { *Rgs::readpipe = sub ($) { ++$r . " $_[0]" }; }
102{
103 package Rgs;
104 ::is( `rm`, "10 rm", '``' );
105 ::is( qx/cp/, "11 cp", 'qx' );
106}
107
93f09d7b 108# Verify that the parsing of overridden keywords isn't messed up
30fe34ed
RGS
109# by the indirect object notation
110{
111 local $SIG{__WARN__} = sub {
112 ::like( $_[0], qr/^ok overriden at/ );
113 };
114 BEGIN { *OverridenWarn::warn = sub { CORE::warn "@_ overriden"; }; }
115 package OverridenWarn;
116 sub foo { "ok" }
117 warn( OverridenWarn->foo() );
118 warn OverridenWarn->foo();
119}
120BEGIN { *OverridenPop::pop = sub { ::is( $_[0][0], "ok" ) }; }
149c1637
RGS
121{
122 package OverridenPop;
123 sub foo { [ "ok" ] }
124 pop( OverridenPop->foo() );
125 pop OverridenPop->foo();
126}
127
128{
129 eval {
130 local *CORE::GLOBAL::require = sub {
131 CORE::require($_[0]);
132 };
133 require 5;
134 require Text::ParseWords;
135 };
136 is $@, '';
137}