This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[perl #92254, #92256] Fix SAVE_DEFSV to do refcounting
[perl5.git] / t / op / method.t
CommitLineData
9bfdb36e 1#!./perl -w
92d69e20
IZ
2
3#
4# test method calls and autoloading.
5#
6
4755096e
GS
7BEGIN {
8 chdir 't' if -d 't';
6dc4e5ce
JH
9 @INC = qw(. ../lib);
10 require "test.pl";
4755096e
GS
11}
12
9bfdb36e
NC
13use strict;
14no warnings 'once';
15
5743f2a3 16plan(tests => 84);
92d69e20
IZ
17
18@A::ISA = 'B';
19@B::ISA = 'C';
20
21sub C::d {"C::d"}
22sub D::d {"D::d"}
23
567ce7b1 24# First, some basic checks of method-calling syntax:
9bfdb36e 25my $obj = bless [], "Pack";
567ce7b1 26sub Pack::method { shift; join(",", "method", @_) }
9bfdb36e 27my $mname = "method";
567ce7b1 28
6dc4e5ce
JH
29is(Pack->method("a","b","c"), "method,a,b,c");
30is(Pack->$mname("a","b","c"), "method,a,b,c");
31is(method Pack ("a","b","c"), "method,a,b,c");
32is((method Pack "a","b","c"), "method,a,b,c");
567ce7b1 33
6dc4e5ce
JH
34is(Pack->method(), "method");
35is(Pack->$mname(), "method");
36is(method Pack (), "method");
37is(Pack->method, "method");
38is(Pack->$mname, "method");
39is(method Pack, "method");
567ce7b1 40
6dc4e5ce
JH
41is($obj->method("a","b","c"), "method,a,b,c");
42is($obj->$mname("a","b","c"), "method,a,b,c");
43is((method $obj ("a","b","c")), "method,a,b,c");
44is((method $obj "a","b","c"), "method,a,b,c");
145eb477 45
6dc4e5ce
JH
46is($obj->method(0), "method,0");
47is($obj->method(1), "method,1");
567ce7b1 48
6dc4e5ce
JH
49is($obj->method(), "method");
50is($obj->$mname(), "method");
51is((method $obj ()), "method");
52is($obj->method, "method");
53is($obj->$mname, "method");
54is(method $obj, "method");
567ce7b1 55
6dc4e5ce 56is( A->d, "C::d"); # Update hash table;
92d69e20
IZ
57
58*B::d = \&D::d; # Import now.
6dc4e5ce 59is(A->d, "D::d"); # Update hash table;
92d69e20 60
44a8e56a 61{
fb73857a 62 local @A::ISA = qw(C); # Update hash table with split() assignment
6dc4e5ce 63 is(A->d, "C::d");
fb73857a 64 $#A::ISA = -1;
6dc4e5ce 65 is(eval { A->d } || "fail", "fail");
fb73857a 66}
6dc4e5ce 67is(A->d, "D::d");
fb73857a
PP
68
69{
44a8e56a
PP
70 local *B::d;
71 eval 'sub B::d {"B::d1"}'; # Import now.
6dc4e5ce 72 is(A->d, "B::d1"); # Update hash table;
44a8e56a 73 undef &B::d;
6dc4e5ce 74 is((eval { A->d }, ($@ =~ /Undefined subroutine/)), 1);
44a8e56a 75}
92d69e20 76
6dc4e5ce 77is(A->d, "D::d"); # Back to previous state
92d69e20 78
9bfdb36e 79eval 'no warnings "redefine"; sub B::d {"B::d2"}'; # Import now.
6dc4e5ce 80is(A->d, "B::d2"); # Update hash table;
92d69e20
IZ
81
82# What follows is hardly guarantied to work, since the names in scripts
94e1fbae
JK
83# are already linked to "pruned" globs. Say, 'undef &B::d' if it were
84# after 'delete $B::{d}; sub B::d {}' would reach an old subroutine.
92d69e20
IZ
85
86undef &B::d;
87delete $B::{d};
5743f2a3
FC
88is(A->d, "C::d");
89
90eval 'sub B::d {"B::d2.5"}';
91A->d; # Update hash table;
92my $glob = \delete $B::{d}; # non-void context; hang on to the glob
6dc4e5ce 93is(A->d, "C::d"); # Update hash table;
92d69e20
IZ
94
95eval 'sub B::d {"B::d3"}'; # Import now.
6dc4e5ce 96is(A->d, "B::d3"); # Update hash table;
92d69e20
IZ
97
98delete $B::{d};
99*dummy::dummy = sub {}; # Mark as updated
6dc4e5ce 100is(A->d, "C::d");
92d69e20
IZ
101
102eval 'sub B::d {"B::d4"}'; # Import now.
6dc4e5ce 103is(A->d, "B::d4"); # Update hash table;
92d69e20
IZ
104
105delete $B::{d}; # Should work without any help too
6dc4e5ce 106is(A->d, "C::d");
92d69e20 107
fae75791
CS
108{
109 local *C::d;
6dc4e5ce 110 is(eval { A->d } || "nope", "nope");
fae75791 111}
6dc4e5ce 112is(A->d, "C::d");
fae75791 113
9bfdb36e 114*A::x = *A::d;
44a8e56a 115A->d;
9bfdb36e
NC
116is(eval { A->x } || "nope", "nope", 'cache should not follow synonyms');
117
118my $counter;
44a8e56a 119
92d69e20
IZ
120eval <<'EOF';
121sub C::e;
09280a33 122BEGIN { *B::e = \&C::e } # Shouldn't prevent AUTOLOAD in original pkg
92d69e20
IZ
123sub Y::f;
124$counter = 0;
125
54310121 126@X::ISA = 'Y';
dc848c6f 127@Y::ISA = 'B';
92d69e20
IZ
128
129sub B::AUTOLOAD {
130 my $c = ++$counter;
131 my $method = $B::AUTOLOAD;
09280a33
CS
132 my $msg = "B: In $method, $c";
133 eval "sub $method { \$msg }";
134 goto &$method;
92d69e20
IZ
135}
136sub C::AUTOLOAD {
137 my $c = ++$counter;
138 my $method = $C::AUTOLOAD;
09280a33
CS
139 my $msg = "C: In $method, $c";
140 eval "sub $method { \$msg }";
141 goto &$method;
92d69e20
IZ
142}
143EOF
144
6dc4e5ce
JH
145is(A->e(), "C: In C::e, 1"); # We get a correct autoload
146is(A->e(), "C: In C::e, 1"); # Which sticks
92d69e20 147
6dc4e5ce
JH
148is(A->ee(), "B: In A::ee, 2"); # We get a generic autoload, method in top
149is(A->ee(), "B: In A::ee, 2"); # Which sticks
92d69e20 150
6dc4e5ce
JH
151is(Y->f(), "B: In Y::f, 3"); # We vivify a correct method
152is(Y->f(), "B: In Y::f, 3"); # Which sticks
92d69e20
IZ
153
154# This test is not intended to be reasonable. It is here just to let you
155# know that you broke some old construction. Feel free to rewrite the test
156# if your patch breaks it.
157
9bfdb36e
NC
158{
159no warnings 'redefine';
92d69e20 160*B::AUTOLOAD = sub {
9bfdb36e 161 use warnings;
92d69e20 162 my $c = ++$counter;
9bfdb36e
NC
163 my $method = $::AUTOLOAD;
164 no strict 'refs';
165 *$::AUTOLOAD = sub { "new B: In $method, $c" };
166 goto &$::AUTOLOAD;
92d69e20 167};
9bfdb36e 168}
92d69e20 169
6dc4e5ce
JH
170is(A->eee(), "new B: In A::eee, 4"); # We get a correct $autoload
171is(A->eee(), "new B: In A::eee, 4"); # Which sticks
fb73857a 172
9bfdb36e
NC
173{
174 no strict 'refs';
175 # this test added due to bug discovery (in 5.004_04, fb73857aa0bfa8ed)
176 is(defined(@{"unknown_package::ISA"}) ? "defined" : "undefined", "undefined");
177}
f6ec51f7
GS
178
179# test that failed subroutine calls don't affect method calls
180{
181 package A1;
182 sub foo { "foo" }
183 package A2;
9bfdb36e 184 @A2::ISA = 'A1';
f6ec51f7 185 package main;
6dc4e5ce
JH
186 is(A2->foo(), "foo");
187 is(do { eval 'A2::foo()'; $@ ? 1 : 0}, 1);
188 is(A2->foo(), "foo");
f6ec51f7 189}
c1899e02 190
af09ea45
IK
191## This test was totally misguided. It passed before only because the
192## code to determine if a package was loaded used to look for the hash
193## %Foo::Bar instead of the package Foo::Bar:: -- and Config.pm just
194## happens to export %Config.
195# {
6dc4e5ce 196# is(do { use Config; eval 'Config->foo()';
af09ea45 197# $@ =~ /^\QCan't locate object method "foo" via package "Config" at/ ? 1 : $@}, 1);
6dc4e5ce 198# is(do { use Config; eval '$d = bless {}, "Config"; $d->foo()';
af09ea45
IK
199# $@ =~ /^\QCan't locate object method "foo" via package "Config" at/ ? 1 : $@}, 1);
200# }
201
af09ea45 202# test error messages if method loading fails
9bfdb36e
NC
203my $e;
204
2f907243
NC
205eval '$e = bless {}, "E::A"; E::A->foo()';
206like ($@, qr/^\QCan't locate object method "foo" via package "E::A" at/);
207eval '$e = bless {}, "E::B"; $e->foo()';
208like ($@, qr/^\QCan't locate object method "foo" via package "E::B" at/);
209eval 'E::C->foo()';
210like ($@, qr/^\QCan't locate object method "foo" via package "E::C" (perhaps /);
211
212eval 'UNIVERSAL->E::D::foo()';
213like ($@, qr/^\QCan't locate object method "foo" via package "E::D" (perhaps /);
9bfdb36e 214eval 'my $e = bless {}, "UNIVERSAL"; $e->E::E::foo()';
2f907243 215like ($@, qr/^\QCan't locate object method "foo" via package "E::E" (perhaps /);
af09ea45
IK
216
217$e = bless {}, "E::F"; # force package to exist
2f907243
NC
218eval 'UNIVERSAL->E::F::foo()';
219like ($@, qr/^\QCan't locate object method "foo" via package "E::F" at/);
220eval '$e = bless {}, "UNIVERSAL"; $e->E::F::foo()';
221like ($@, qr/^\QCan't locate object method "foo" via package "E::F" at/);
af09ea45
IK
222
223# TODO: we need some tests for the SUPER:: pseudoclass
224
225# failed method call or UNIVERSAL::can() should not autovivify packages
6dc4e5ce
JH
226is( $::{"Foo::"} || "none", "none"); # sanity check 1
227is( $::{"Foo::"} || "none", "none"); # sanity check 2
c1899e02 228
6dc4e5ce
JH
229is( UNIVERSAL::can("Foo", "boogie") ? "yes":"no", "no" );
230is( $::{"Foo::"} || "none", "none"); # still missing?
af09ea45 231
6dc4e5ce
JH
232is( Foo->UNIVERSAL::can("boogie") ? "yes":"no", "no" );
233is( $::{"Foo::"} || "none", "none"); # still missing?
af09ea45 234
6dc4e5ce
JH
235is( Foo->can("boogie") ? "yes":"no", "no" );
236is( $::{"Foo::"} || "none", "none"); # still missing?
af09ea45 237
6dc4e5ce
JH
238is( eval 'Foo->boogie(); 1' ? "yes":"no", "no" );
239is( $::{"Foo::"} || "none", "none"); # still missing?
af09ea45 240
6dc4e5ce 241is(do { eval 'Foo->boogie()';
af09ea45
IK
242 $@ =~ /^\QCan't locate object method "boogie" via package "Foo" (perhaps / ? 1 : $@}, 1);
243
244eval 'sub Foo::boogie { "yes, sir!" }';
6dc4e5ce
JH
245is( $::{"Foo::"} ? "ok" : "none", "ok"); # should exist now
246is( Foo->boogie(), "yes, sir!");
af09ea45
IK
247
248# TODO: universal.t should test NoSuchPackage->isa()/can()
c1899e02 249
f0670693
SC
250# This is actually testing parsing of indirect objects and undefined subs
251# print foo("bar") where foo does not exist is not an indirect object.
252# print foo "bar" where foo does not exist is an indirect object.
84251760 253eval 'sub AUTOLOAD { "ok ", shift, "\n"; }';
6dc4e5ce 254ok(1);
af09ea45 255
a397c3d9 256# Bug ID 20010902.002
6dc4e5ce 257is(
a397c3d9 258 eval q[
9bfdb36e 259 my $x = 'x'; # Lexical or package variable, 5.6.1 panics.
a397c3d9
RGS
260 sub Foo::x : lvalue { $x }
261 Foo->$x = 'ok';
262 ] || $@, 'ok'
263);
264
3ad83ce7
AMS
265# An autoloaded, inherited DESTROY may be invoked differently than normal
266# methods, and has been known to give rise to spurious warnings
267# eg <200203121600.QAA11064@gizmo.fdgroup.co.uk>
268
269{
270 use warnings;
271 my $w = '';
272 local $SIG{__WARN__} = sub { $w = $_[0] };
273
274 sub AutoDest::Base::AUTOLOAD {}
275 @AutoDest::ISA = qw(AutoDest::Base);
276 { my $x = bless {}, 'AutoDest'; }
277 $w =~ s/\n//g;
6dc4e5ce 278 is($w, '');
3ad83ce7
AMS
279}
280
e189a56d
IK
281# [ID 20020305.025] PACKAGE::SUPER doesn't work anymore
282
283package main;
284our @X;
285package Amajor;
286sub test {
287 push @main::X, 'Amajor', @_;
288}
289package Bminor;
290use base qw(Amajor);
291package main;
292sub Bminor::test {
293 $_[0]->Bminor::SUPER::test('x', 'y');
294 push @main::X, 'Bminor', @_;
295}
296Bminor->test('y', 'z');
297is("@X", "Amajor Bminor x y Bminor Bminor y z");
298
0dae17bd
GS
299package main;
300for my $meth (['Bar', 'Foo::Bar'],
301 ['SUPER::Bar', 'main::SUPER::Bar'],
302 ['Xyz::SUPER::Bar', 'Xyz::SUPER::Bar'])
303{
304 fresh_perl_is(<<EOT,
305package UNIVERSAL; sub AUTOLOAD { my \$c = shift; print "\$c \$AUTOLOAD\\n" }
306package Xyz;
307package main; Foo->$meth->[0]();
308EOT
309 "Foo $meth->[1]",
310 { switches => [ '-w' ] },
311 "check if UNIVERSAL::AUTOLOAD works",
312 );
313}
1f15e670
NT
314
315# Test for #71952: crash when looking for a nonexistent destructor
316# Regression introduced by fbb3ee5af3d4
317{
318 fresh_perl_is(<<'EOT',
319sub M::DESTROY; bless {}, "M" ; print "survived\n";
320EOT
321 "survived",
322 {},
323 "no crash with a declared but missing DESTROY method"
324 );
325}
326
da6b625f
FC
327# Test for calling a method on a packag name return by a magic variable
328sub TIESCALAR{bless[]}
329sub FETCH{"main"}
330my $kalled;
331sub bolgy { ++$kalled; }
332tie my $a, "";
333$a->bolgy;
334is $kalled, 1, 'calling a class method via a magic variable';
f937af42
BF
335
336{
337 package NulTest;
338 sub method { 1 }
339
340 package main;
341 eval {
342 NulTest->${ \"method\0Whoops" };
343 };
344 like $@, qr/Can't locate object method "method\0Whoops" via package "NulTest" at/,
345 "method lookup is nul-clean";
346
347 *NulTest::AUTOLOAD = sub { our $AUTOLOAD; return $AUTOLOAD };
348
349 like(NulTest->${ \"nul\0test" }, "nul\0test", "AUTOLOAD is nul-clean");
350}
2a0f5ef0
BF
351
352
353{
354 fresh_perl_is(
355 q! sub T::DESTROY { $x = $_[0]; } bless [], "T";!,
356 "DESTROY created new reference to dead object 'T' during global destruction.",
357 {},
358 "DESTROY creating a new reference to the object generates a warning."
359 );
360}