4 # test method calls and autoloading.
24 # First, some basic checks of method-calling syntax:
25 my $obj = bless [], "Pack";
26 sub Pack::method { shift; join(",", "method", @_) }
29 is(Pack->method("a","b","c"), "method,a,b,c");
30 is(Pack->$mname("a","b","c"), "method,a,b,c");
31 is(method Pack ("a","b","c"), "method,a,b,c");
32 is((method Pack "a","b","c"), "method,a,b,c");
34 is(Pack->method(), "method");
35 is(Pack->$mname(), "method");
36 is(method Pack (), "method");
37 is(Pack->method, "method");
38 is(Pack->$mname, "method");
39 is(method Pack, "method");
41 is($obj->method("a","b","c"), "method,a,b,c");
42 is($obj->$mname("a","b","c"), "method,a,b,c");
43 is((method $obj ("a","b","c")), "method,a,b,c");
44 is((method $obj "a","b","c"), "method,a,b,c");
46 is($obj->method(0), "method,0");
47 is($obj->method(1), "method,1");
49 is($obj->method(), "method");
50 is($obj->$mname(), "method");
51 is((method $obj ()), "method");
52 is($obj->method, "method");
53 is($obj->$mname, "method");
54 is(method $obj, "method");
56 is( A->d, "C::d"); # Update hash table;
58 *B::d = \&D::d; # Import now.
59 is(A->d, "D::d"); # Update hash table;
62 local @A::ISA = qw(C); # Update hash table with split() assignment
65 is(eval { A->d } || "fail", "fail");
71 eval 'sub B::d {"B::d1"}'; # Import now.
72 is(A->d, "B::d1"); # Update hash table;
74 is((eval { A->d }, ($@ =~ /Undefined subroutine/)), 1);
77 is(A->d, "D::d"); # Back to previous state
79 eval 'no warnings "redefine"; sub B::d {"B::d2"}'; # Import now.
80 is(A->d, "B::d2"); # Update hash table;
82 # What follows is hardly guarantied to work, since the names in scripts
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.
88 is(A->d, "C::d"); # Update hash table;
90 eval 'sub B::d {"B::d3"}'; # Import now.
91 is(A->d, "B::d3"); # Update hash table;
94 *dummy::dummy = sub {}; # Mark as updated
97 eval 'sub B::d {"B::d4"}'; # Import now.
98 is(A->d, "B::d4"); # Update hash table;
100 delete $B::{d}; # Should work without any help too
105 is(eval { A->d } || "nope", "nope");
111 is(eval { A->x } || "nope", "nope", 'cache should not follow synonyms');
117 BEGIN { *B::e = \&C::e } # Shouldn't prevent AUTOLOAD in original pkg
126 my $method = $B::AUTOLOAD;
127 my $msg = "B: In $method, $c";
128 eval "sub $method { \$msg }";
133 my $method = $C::AUTOLOAD;
134 my $msg = "C: In $method, $c";
135 eval "sub $method { \$msg }";
140 is(A->e(), "C: In C::e, 1"); # We get a correct autoload
141 is(A->e(), "C: In C::e, 1"); # Which sticks
143 is(A->ee(), "B: In A::ee, 2"); # We get a generic autoload, method in top
144 is(A->ee(), "B: In A::ee, 2"); # Which sticks
146 is(Y->f(), "B: In Y::f, 3"); # We vivify a correct method
147 is(Y->f(), "B: In Y::f, 3"); # Which sticks
149 # This test is not intended to be reasonable. It is here just to let you
150 # know that you broke some old construction. Feel free to rewrite the test
151 # if your patch breaks it.
154 no warnings 'redefine';
158 my $method = $::AUTOLOAD;
160 *$::AUTOLOAD = sub { "new B: In $method, $c" };
165 is(A->eee(), "new B: In A::eee, 4"); # We get a correct $autoload
166 is(A->eee(), "new B: In A::eee, 4"); # Which sticks
170 # this test added due to bug discovery (in 5.004_04, fb73857aa0bfa8ed)
171 is(defined(@{"unknown_package::ISA"}) ? "defined" : "undefined", "undefined");
174 # test that failed subroutine calls don't affect method calls
181 is(A2->foo(), "foo");
182 is(do { eval 'A2::foo()'; $@ ? 1 : 0}, 1);
183 is(A2->foo(), "foo");
186 ## This test was totally misguided. It passed before only because the
187 ## code to determine if a package was loaded used to look for the hash
188 ## %Foo::Bar instead of the package Foo::Bar:: -- and Config.pm just
189 ## happens to export %Config.
191 # is(do { use Config; eval 'Config->foo()';
192 # $@ =~ /^\QCan't locate object method "foo" via package "Config" at/ ? 1 : $@}, 1);
193 # is(do { use Config; eval '$d = bless {}, "Config"; $d->foo()';
194 # $@ =~ /^\QCan't locate object method "foo" via package "Config" at/ ? 1 : $@}, 1);
197 # test error messages if method loading fails
200 eval '$e = bless {}, "E::A"; E::A->foo()';
201 like ($@, qr/^\QCan't locate object method "foo" via package "E::A" at/);
202 eval '$e = bless {}, "E::B"; $e->foo()';
203 like ($@, qr/^\QCan't locate object method "foo" via package "E::B" at/);
205 like ($@, qr/^\QCan't locate object method "foo" via package "E::C" (perhaps /);
207 eval 'UNIVERSAL->E::D::foo()';
208 like ($@, qr/^\QCan't locate object method "foo" via package "E::D" (perhaps /);
209 eval 'my $e = bless {}, "UNIVERSAL"; $e->E::E::foo()';
210 like ($@, qr/^\QCan't locate object method "foo" via package "E::E" (perhaps /);
212 $e = bless {}, "E::F"; # force package to exist
213 eval 'UNIVERSAL->E::F::foo()';
214 like ($@, qr/^\QCan't locate object method "foo" via package "E::F" at/);
215 eval '$e = bless {}, "UNIVERSAL"; $e->E::F::foo()';
216 like ($@, qr/^\QCan't locate object method "foo" via package "E::F" at/);
218 # TODO: we need some tests for the SUPER:: pseudoclass
220 # failed method call or UNIVERSAL::can() should not autovivify packages
221 is( $::{"Foo::"} || "none", "none"); # sanity check 1
222 is( $::{"Foo::"} || "none", "none"); # sanity check 2
224 is( UNIVERSAL::can("Foo", "boogie") ? "yes":"no", "no" );
225 is( $::{"Foo::"} || "none", "none"); # still missing?
227 is( Foo->UNIVERSAL::can("boogie") ? "yes":"no", "no" );
228 is( $::{"Foo::"} || "none", "none"); # still missing?
230 is( Foo->can("boogie") ? "yes":"no", "no" );
231 is( $::{"Foo::"} || "none", "none"); # still missing?
233 is( eval 'Foo->boogie(); 1' ? "yes":"no", "no" );
234 is( $::{"Foo::"} || "none", "none"); # still missing?
236 is(do { eval 'Foo->boogie()';
237 $@ =~ /^\QCan't locate object method "boogie" via package "Foo" (perhaps / ? 1 : $@}, 1);
239 eval 'sub Foo::boogie { "yes, sir!" }';
240 is( $::{"Foo::"} ? "ok" : "none", "ok"); # should exist now
241 is( Foo->boogie(), "yes, sir!");
243 # TODO: universal.t should test NoSuchPackage->isa()/can()
245 # This is actually testing parsing of indirect objects and undefined subs
246 # print foo("bar") where foo does not exist is not an indirect object.
247 # print foo "bar" where foo does not exist is an indirect object.
248 eval 'sub AUTOLOAD { "ok ", shift, "\n"; }';
251 # Bug ID 20010902.002
254 my $x = 'x'; # Lexical or package variable, 5.6.1 panics.
255 sub Foo::x : lvalue { $x }
260 # An autoloaded, inherited DESTROY may be invoked differently than normal
261 # methods, and has been known to give rise to spurious warnings
262 # eg <200203121600.QAA11064@gizmo.fdgroup.co.uk>
267 local $SIG{__WARN__} = sub { $w = $_[0] };
269 sub AutoDest::Base::AUTOLOAD {}
270 @AutoDest::ISA = qw(AutoDest::Base);
271 { my $x = bless {}, 'AutoDest'; }
276 # [ID 20020305.025] PACKAGE::SUPER doesn't work anymore
282 push @main::X, 'Amajor', @_;
288 $_[0]->Bminor::SUPER::test('x', 'y');
289 push @main::X, 'Bminor', @_;
291 Bminor->test('y', 'z');
292 is("@X", "Amajor Bminor x y Bminor Bminor y z");
295 for my $meth (['Bar', 'Foo::Bar'],
296 ['SUPER::Bar', 'main::SUPER::Bar'],
297 ['Xyz::SUPER::Bar', 'Xyz::SUPER::Bar'])
300 package UNIVERSAL; sub AUTOLOAD { my \$c = shift; print "\$c \$AUTOLOAD\\n" }
302 package main; Foo->$meth->[0]();
305 { switches => [ '-w' ] },
306 "check if UNIVERSAL::AUTOLOAD works",
310 # Test for #71952: crash when looking for a nonexistent destructor
311 # Regression introduced by fbb3ee5af3d4
313 fresh_perl_is(<<'EOT',
314 sub M::DESTROY; bless {}, "M" ; print "survived\n";
318 "no crash with a declared but missing DESTROY method"