xref: /onnv-gate/usr/src/cmd/perl/5.8.4/distrib/t/op/override.t (revision 0:68f95e015346)
1*0Sstevel@tonic-gate#!./perl
2*0Sstevel@tonic-gate
3*0Sstevel@tonic-gateBEGIN {
4*0Sstevel@tonic-gate    chdir 't' if -d 't';
5*0Sstevel@tonic-gate    @INC = '../lib';
6*0Sstevel@tonic-gate    require './test.pl';
7*0Sstevel@tonic-gate}
8*0Sstevel@tonic-gate
9*0Sstevel@tonic-gateplan tests => 21;
10*0Sstevel@tonic-gate
11*0Sstevel@tonic-gate#
12*0Sstevel@tonic-gate# This file tries to test builtin override using CORE::GLOBAL
13*0Sstevel@tonic-gate#
14*0Sstevel@tonic-gatemy $dirsep = "/";
15*0Sstevel@tonic-gate
16*0Sstevel@tonic-gateBEGIN { package Foo; *main::getlogin = sub { "kilroy"; } }
17*0Sstevel@tonic-gate
18*0Sstevel@tonic-gateis( getlogin, "kilroy" );
19*0Sstevel@tonic-gate
20*0Sstevel@tonic-gatemy $t = 42;
21*0Sstevel@tonic-gateBEGIN { *CORE::GLOBAL::time = sub () { $t; } }
22*0Sstevel@tonic-gate
23*0Sstevel@tonic-gateis( 45, time + 3 );
24*0Sstevel@tonic-gate
25*0Sstevel@tonic-gate#
26*0Sstevel@tonic-gate# require has special behaviour
27*0Sstevel@tonic-gate#
28*0Sstevel@tonic-gatemy $r;
29*0Sstevel@tonic-gateBEGIN { *CORE::GLOBAL::require = sub { $r = shift; 1; } }
30*0Sstevel@tonic-gate
31*0Sstevel@tonic-gaterequire Foo;
32*0Sstevel@tonic-gateis( $r, "Foo.pm" );
33*0Sstevel@tonic-gate
34*0Sstevel@tonic-gaterequire Foo::Bar;
35*0Sstevel@tonic-gateis( $r, join($dirsep, "Foo", "Bar.pm") );
36*0Sstevel@tonic-gate
37*0Sstevel@tonic-gaterequire 'Foo';
38*0Sstevel@tonic-gateis( $r, "Foo" );
39*0Sstevel@tonic-gate
40*0Sstevel@tonic-gaterequire 5.6;
41*0Sstevel@tonic-gateis( $r, "5.6" );
42*0Sstevel@tonic-gate
43*0Sstevel@tonic-gaterequire v5.6;
44*0Sstevel@tonic-gateok( abs($r - 5.006) < 0.001 && $r eq "\x05\x06" );
45*0Sstevel@tonic-gate
46*0Sstevel@tonic-gateeval "use Foo";
47*0Sstevel@tonic-gateis( $r, "Foo.pm" );
48*0Sstevel@tonic-gate
49*0Sstevel@tonic-gateeval "use Foo::Bar";
50*0Sstevel@tonic-gateis( $r, join($dirsep, "Foo", "Bar.pm") );
51*0Sstevel@tonic-gate
52*0Sstevel@tonic-gateeval "use 5.6";
53*0Sstevel@tonic-gateis( $r, "5.6" );
54*0Sstevel@tonic-gate
55*0Sstevel@tonic-gate# localizing *CORE::GLOBAL::foo should revert to finding CORE::foo
56*0Sstevel@tonic-gate{
57*0Sstevel@tonic-gate    local(*CORE::GLOBAL::require);
58*0Sstevel@tonic-gate    $r = '';
59*0Sstevel@tonic-gate    eval "require NoNeXiSt;";
60*0Sstevel@tonic-gate    ok( ! ( $r or $@ !~ /^Can't locate NoNeXiSt/i ) );
61*0Sstevel@tonic-gate}
62*0Sstevel@tonic-gate
63*0Sstevel@tonic-gate#
64*0Sstevel@tonic-gate# readline() has special behaviour too
65*0Sstevel@tonic-gate#
66*0Sstevel@tonic-gate
67*0Sstevel@tonic-gate$r = 11;
68*0Sstevel@tonic-gateBEGIN { *CORE::GLOBAL::readline = sub (;*) { ++$r }; }
69*0Sstevel@tonic-gateis( <FH>	, 12 );
70*0Sstevel@tonic-gateis( <$fh>	, 13 );
71*0Sstevel@tonic-gatemy $pad_fh;
72*0Sstevel@tonic-gateis( <$pad_fh>	, 14 );
73*0Sstevel@tonic-gate
74*0Sstevel@tonic-gate# Non-global readline() override
75*0Sstevel@tonic-gateBEGIN { *Rgs::readline = sub (;*) { --$r }; }
76*0Sstevel@tonic-gatepackage Rgs;
77*0Sstevel@tonic-gate::is( <FH>	, 13 );
78*0Sstevel@tonic-gate::is( <$fh>	, 12 );
79*0Sstevel@tonic-gate::is( <$pad_fh>	, 11 );
80*0Sstevel@tonic-gate
81*0Sstevel@tonic-gate# Verify that the parsing of overriden keywords isn't messed up
82*0Sstevel@tonic-gate# by the indirect object notation
83*0Sstevel@tonic-gate{
84*0Sstevel@tonic-gate    local $SIG{__WARN__} = sub {
85*0Sstevel@tonic-gate	::like( $_[0], qr/^ok overriden at/ );
86*0Sstevel@tonic-gate    };
87*0Sstevel@tonic-gate    BEGIN { *OverridenWarn::warn = sub { CORE::warn "@_ overriden"; }; }
88*0Sstevel@tonic-gate    package OverridenWarn;
89*0Sstevel@tonic-gate    sub foo { "ok" }
90*0Sstevel@tonic-gate    warn( OverridenWarn->foo() );
91*0Sstevel@tonic-gate    warn OverridenWarn->foo();
92*0Sstevel@tonic-gate}
93*0Sstevel@tonic-gateBEGIN { *OverridenPop::pop = sub { ::is( $_[0][0], "ok" ) }; }
94*0Sstevel@tonic-gatepackage OverridenPop;
95*0Sstevel@tonic-gatesub foo { [ "ok" ] }
96*0Sstevel@tonic-gatepop( OverridenPop->foo() );
97*0Sstevel@tonic-gatepop OverridenPop->foo();
98