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