This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Warnings in tests @25605 due to cmp_ok behavior change.
[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
30fe34ed 9plan tests => 21;
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
GS
39
40require 5.6;
30fe34ed 41is( $r, "5.6" );
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
GS
51
52eval "use 5.6";
30fe34ed 53is( $r, "5.6" );
b9f751c0
GS
54
55# localizing *CORE::GLOBAL::foo should revert to finding CORE::foo
56{
57 local(*CORE::GLOBAL::require);
58 $r = '';
59 eval "require NoNeXiSt;";
30fe34ed 60 ok( ! ( $r or $@ !~ /^Can't locate NoNeXiSt/i ) );
b9f751c0 61}
9b3023bc
RGS
62
63#
64# readline() has special behaviour too
65#
66
67$r = 11;
68BEGIN { *CORE::GLOBAL::readline = sub (;*) { ++$r }; }
30fe34ed
RGS
69is( <FH> , 12 );
70is( <$fh> , 13 );
9b3023bc 71my $pad_fh;
30fe34ed 72is( <$pad_fh> , 14 );
9b3023bc
RGS
73
74# Non-global readline() override
75BEGIN { *Rgs::readline = sub (;*) { --$r }; }
76package Rgs;
30fe34ed
RGS
77::is( <FH> , 13 );
78::is( <$fh> , 12 );
79::is( <$pad_fh> , 11 );
80
81# Verify that the parsing of overriden keywords isn't messed up
82# by the indirect object notation
83{
84 local $SIG{__WARN__} = sub {
85 ::like( $_[0], qr/^ok overriden at/ );
86 };
87 BEGIN { *OverridenWarn::warn = sub { CORE::warn "@_ overriden"; }; }
88 package OverridenWarn;
89 sub foo { "ok" }
90 warn( OverridenWarn->foo() );
91 warn OverridenWarn->foo();
92}
93BEGIN { *OverridenPop::pop = sub { ::is( $_[0][0], "ok" ) }; }
94package OverridenPop;
95sub foo { [ "ok" ] }
96pop( OverridenPop->foo() );
97pop OverridenPop->foo();