4 # test method calls and autoloading.
9 @INC = qw(. ../lib lib);
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.
90 eval 'sub B::d {"B::d2.5"}';
91 A->d; # Update hash table;
92 my $glob = \delete $B::{d}; # non-void context; hang on to the glob
93 is(A->d, "C::d"); # Update hash table;
95 eval 'sub B::d {"B::d3"}'; # Import now.
96 is(A->d, "B::d3"); # Update hash table;
99 *dummy::dummy = sub {}; # Mark as updated
102 eval 'sub B::d {"B::d4"}'; # Import now.
103 is(A->d, "B::d4"); # Update hash table;
105 delete $B::{d}; # Should work without any help too
110 is(eval { A->d } || "nope", "nope");
116 is(eval { A->x } || "nope", "nope", 'cache should not follow synonyms');
122 BEGIN { *B::e = \&C::e } # Shouldn't prevent AUTOLOAD in original pkg
131 my $method = $B::AUTOLOAD;
132 my $msg = "B: In $method, $c";
133 eval "sub $method { \$msg }";
138 my $method = $C::AUTOLOAD;
139 my $msg = "C: In $method, $c";
140 eval "sub $method { \$msg }";
145 is(A->e(), "C: In C::e, 1"); # We get a correct autoload
146 is(A->e(), "C: In C::e, 1"); # Which sticks
148 is(A->ee(), "B: In A::ee, 2"); # We get a generic autoload, method in top
149 is(A->ee(), "B: In A::ee, 2"); # Which sticks
151 is(Y->f(), "B: In Y::f, 3"); # We vivify a correct method
152 is(Y->f(), "B: In Y::f, 3"); # Which sticks
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.
159 no warnings 'redefine';
163 my $method = $::AUTOLOAD;
165 *$::AUTOLOAD = sub { "new B: In $method, $c" };
170 is(A->eee(), "new B: In A::eee, 4"); # We get a correct $autoload
171 is(A->eee(), "new B: In A::eee, 4"); # Which sticks
175 no warnings 'deprecated';
176 # this test added due to bug discovery (in 5.004_04, fb73857aa0bfa8ed)
177 # Possibly kill this test now that defined @::array is finally properly
179 is(defined(@{"unknown_package::ISA"}) ? "defined" : "undefined", "undefined");
182 # test that failed subroutine calls don't affect method calls
189 is(A2->foo(), "foo");
190 is(do { eval 'A2::foo()'; $@ ? 1 : 0}, 1);
191 is(A2->foo(), "foo");
194 ## This test was totally misguided. It passed before only because the
195 ## code to determine if a package was loaded used to look for the hash
196 ## %Foo::Bar instead of the package Foo::Bar:: -- and Config.pm just
197 ## happens to export %Config.
199 # is(do { use Config; eval 'Config->foo()';
200 # $@ =~ /^\QCan't locate object method "foo" via package "Config" at/ ? 1 : $@}, 1);
201 # is(do { use Config; eval '$d = bless {}, "Config"; $d->foo()';
202 # $@ =~ /^\QCan't locate object method "foo" via package "Config" at/ ? 1 : $@}, 1);
205 # test error messages if method loading fails
208 eval '$e = bless {}, "E::A"; E::A->foo()';
209 like ($@, qr/^\QCan't locate object method "foo" via package "E::A" at/);
210 eval '$e = bless {}, "E::B"; $e->foo()';
211 like ($@, qr/^\QCan't locate object method "foo" via package "E::B" at/);
213 like ($@, qr/^\QCan't locate object method "foo" via package "E::C" (perhaps /);
215 eval 'UNIVERSAL->E::D::foo()';
216 like ($@, qr/^\QCan't locate object method "foo" via package "E::D" (perhaps /);
217 eval 'my $e = bless {}, "UNIVERSAL"; $e->E::E::foo()';
218 like ($@, qr/^\QCan't locate object method "foo" via package "E::E" (perhaps /);
220 $e = bless {}, "E::F"; # force package to exist
221 eval 'UNIVERSAL->E::F::foo()';
222 like ($@, qr/^\QCan't locate object method "foo" via package "E::F" at/);
223 eval '$e = bless {}, "UNIVERSAL"; $e->E::F::foo()';
224 like ($@, qr/^\QCan't locate object method "foo" via package "E::F" at/);
226 # SUPER:: pseudoclass
227 @Saab::ISA = "Souper";
228 sub Souper::method { @_ }
229 @OtherSaab::ISA = "OtherSouper";
230 sub OtherSouper::method { "Isidore Ropen, Draft Manager" }
232 my $o = bless [], "Saab";
234 my @ret = $o->SUPER::method('whatever');
235 ::is $ret[0], $o, 'object passed to SUPER::method';
236 ::is $ret[1], 'whatever', 'argument passed to SUPER::method';
237 @ret = $o->SUPER'method('whatever');
238 ::is $ret[0], $o, "object passed to SUPER'method";
239 ::is $ret[1], 'whatever', "argument passed to SUPER'method";
240 @ret = Saab->SUPER::method;
241 ::is $ret[0], 'Saab', "package name passed to SUPER::method";
242 @ret = OtherSaab->SUPER::method;
243 ::is $ret[0], 'OtherSaab',
244 "->SUPER::method uses current package, not invocant";
248 local our @ISA = "Souper";
249 is eval { (main->SUPER::method)[0] }, 'main',
250 'Mentioning *SUPER:: does not stop ->SUPER from working in main';
254 *Mover:: = *Mover2::;
259 # Not our(@ISA), because the bug we are testing for interacts with an
260 # our() bug that cancels this bug out.
262 sub door::dohtem { 'dohtem' }
263 ::is eval { Mover->SUPER::dohtem; }, 'dohtem',
264 'SUPER inside moved package';
266 *door::dohtem = sub { 'method' };
267 ::is eval { Mover->SUPER::dohtem; }, 'method',
268 'SUPER inside moved package respects method changes';
271 # failed method call or UNIVERSAL::can() should not autovivify packages
272 is( $::{"Foo::"} || "none", "none"); # sanity check 1
273 is( $::{"Foo::"} || "none", "none"); # sanity check 2
275 is( UNIVERSAL::can("Foo", "boogie") ? "yes":"no", "no" );
276 is( $::{"Foo::"} || "none", "none"); # still missing?
278 is( Foo->UNIVERSAL::can("boogie") ? "yes":"no", "no" );
279 is( $::{"Foo::"} || "none", "none"); # still missing?
281 is( Foo->can("boogie") ? "yes":"no", "no" );
282 is( $::{"Foo::"} || "none", "none"); # still missing?
284 is( eval 'Foo->boogie(); 1' ? "yes":"no", "no" );
285 is( $::{"Foo::"} || "none", "none"); # still missing?
287 is(do { eval 'Foo->boogie()';
288 $@ =~ /^\QCan't locate object method "boogie" via package "Foo" (perhaps / ? 1 : $@}, 1);
290 eval 'sub Foo::boogie { "yes, sir!" }';
291 is( $::{"Foo::"} ? "ok" : "none", "ok"); # should exist now
292 is( Foo->boogie(), "yes, sir!");
294 # TODO: universal.t should test NoSuchPackage->isa()/can()
296 # This is actually testing parsing of indirect objects and undefined subs
297 # print foo("bar") where foo does not exist is not an indirect object.
298 # print foo "bar" where foo does not exist is an indirect object.
299 eval 'sub AUTOLOAD { "ok ", shift, "\n"; }';
302 # Bug ID 20010902.002
305 my $x = 'x'; # Lexical or package variable, 5.6.1 panics.
306 sub Foo::x : lvalue { $x }
311 # An autoloaded, inherited DESTROY may be invoked differently than normal
312 # methods, and has been known to give rise to spurious warnings
313 # eg <200203121600.QAA11064@gizmo.fdgroup.co.uk>
318 local $SIG{__WARN__} = sub { $w = $_[0] };
320 sub AutoDest::Base::AUTOLOAD {}
321 @AutoDest::ISA = qw(AutoDest::Base);
322 { my $x = bless {}, 'AutoDest'; }
327 # [ID 20020305.025] PACKAGE::SUPER doesn't work anymore
333 push @main::X, 'Amajor', @_;
339 $_[0]->Bminor::SUPER::test('x', 'y');
340 push @main::X, 'Bminor', @_;
342 Bminor->test('y', 'z');
343 is("@X", "Amajor Bminor x y Bminor Bminor y z");
346 for my $meth (['Bar', 'Foo::Bar'],
347 ['SUPER::Bar', 'main::SUPER::Bar'],
348 ['Xyz::SUPER::Bar', 'Xyz::SUPER::Bar'])
351 package UNIVERSAL; sub AUTOLOAD { my \$c = shift; print "\$c \$AUTOLOAD\\n" }
353 package main; Foo->$meth->[0]();
356 { switches => [ '-w' ] },
357 "check if UNIVERSAL::AUTOLOAD works",
361 # Test for #71952: crash when looking for a nonexistent destructor
362 # Regression introduced by fbb3ee5af3d4
364 fresh_perl_is(<<'EOT',
365 sub M::DESTROY; bless {}, "M" ; print "survived\n";
369 "no crash with a declared but missing DESTROY method"
373 # Test for calling a method on a packag name return by a magic variable
374 sub TIESCALAR{bless[]}
377 sub bolgy { ++$kalled; }
380 is $kalled, 1, 'calling a class method via a magic variable';
388 NulTest->${ \"method\0Whoops" };
390 like $@, qr/Can't locate object method "method\0Whoops" via package "NulTest" at/,
391 "method lookup is nul-clean";
393 *NulTest::AUTOLOAD = sub { our $AUTOLOAD; return $AUTOLOAD };
395 like(NulTest->${ \"nul\0test" }, "nul\0test", "AUTOLOAD is nul-clean");
401 q! sub T::DESTROY { $x = $_[0]; } bless [], "T";!,
402 "DESTROY created new reference to dead object 'T' during global destruction.",
404 "DESTROY creating a new reference to the object generates a warning."
411 sub Just::a_japh { return "$_[0] another Perl hacker," }
412 is eval { "Just"->a_japh }, "Just another Perl hacker,",
413 'constants do not interfere with class methods';
420 sub lbiggles :lvalue { index "foo", "f" }
421 ok eval { main->bliggles(my($foo,$bar)) },
422 'foo->bar(my($foo,$bar)) is not called in lvalue context';
423 ok eval { main->bliggles(our($foo,$bar)) },
424 'foo->bar(our($foo,$bar)) is not called in lvalue context';
425 ok eval { main->bliggles(local($foo,$bar)) },
426 'foo->bar(local($foo,$bar)) is not called in lvalue context';
427 ok eval { () = main->lbiggles(my($foo,$bar)); 1 },
428 'foo->lv(my($foo,$bar)) is not called in lvalue context';
429 ok eval { () = main->lbiggles(our($foo,$bar)); 1 },
430 'foo->lv(our($foo,$bar)) is not called in lvalue context';
431 ok eval { () = main->lbiggles(local($foo,$bar)); 1 },
432 'foo->lv(local($foo,$bar)) is not called in lvalue context';
436 # AUTOLOAD and DESTROY can be declared without a leading sub,
437 # like BEGIN and friends.
440 eval 'AUTOLOAD { our $AUTOLOAD; return $AUTOLOAD }';
441 ::ok( !$@, "AUTOLOAD without a leading sub is legal" );
443 eval "DESTROY { ::pass( q!DESTROY without a leading sub is legal and gets called! ) }";
445 ::ok( NoSub->can("AUTOLOAD"), "...and sets up an AUTOLOAD normally" );
446 ::is( eval { NoSub->bluh }, "NoSub::bluh", "...which works as expected" );
448 { bless {}, "NoSub"; }
451 eval { () = 3; new {} };
453 qr/^Can't call method "new" without a package or object reference/,
454 'Err msg from new{} when stack contains a number';
455 eval { () = "foo"; new {} };
457 qr/^Can't call method "new" without a package or object reference/,
458 'Err msg from new{} when stack contains a word';
459 eval { () = undef; new {} };
461 qr/^Can't call method "new" without a package or object reference/,
462 'Err msg from new{} when stack contains undef';
466 sub ASI::m { shift; "@_" };
467 my @a = (bless([]), 'arg');
468 my $r = SUPER::m{@a};
469 ::is $r, 'arg', 'method{@array}';
471 ::is $r, 'arg', 'method{}@array';
472 $r = SUPER::m{@a}"b";
473 ::is $r, 'arg b', 'method{@array}$more_args';
476 # [perl #114924] SUPER->method
477 @SUPER::ISA = "SUPPER";
478 sub SUPPER::foo { "supper" }
479 is "SUPER"->foo, 'supper', 'SUPER->method';
481 sub flomp { "flimp" }
482 sub main::::flomp { "flump" }
483 is "::"->flomp, 'flump', 'method call on ::';
484 is "::main"->flomp, 'flimp', 'method call on ::main';
487 qr/^Can't call method "flomp" without a package or object reference/,
488 'method call on empty string';
489 is "3foo"->CORE::uc, '3FOO', '"3foo"->CORE::uc';
490 { no strict; @{"3foo::ISA"} = "CORE"; }
491 is "3foo"->uc, '3FOO', '"3foo"->uc (autobox style!)';
493 # Test that PL_stashcache doesn't change the resolution behaviour for file
494 # handles and package names.
496 skip_if_miniperl('file handles as methods requires loading IO::File', 25);
499 foreach (qw (Count::DATA Count Colour::H1 Color::H1 C3::H1)) {
504 return "method in $_";
512 *The::Count:: = \*Count::;
515 is(Count::DATA->getline(), 'method in Count::DATA',
516 'initial resolution is a method');
517 is(The::Count::DATA->getline(), 'method in Count::DATA',
518 'initial resolution is a method in aliased classes');
522 is(Count::DATA->getline(), "one! ha ha ha\n", 'file handles take priority');
523 is(The::Count::DATA->getline(), "two! ha ha ha\n",
524 'file handles take priority in aliased classes');
526 eval q{close Count::DATA} or die $!;
530 is(Count::DATA->getline(), undef,
531 "closing a file handle doesn't change object resolution");
532 is(The::Count::DATA->getline(), undef,
533 "closing a file handle doesn't change object resolution in aliased classes");
537 is(Count::DATA->getline(), 'method in Count::DATA',
538 'undefining the typeglob does change object resolution');
539 is(The::Count::DATA->getline(), 'method in Count::DATA',
540 'undefining the typeglob does change object resolution in aliased classes');
542 is(Count->getline(), 'method in Count',
543 'initial resolution is a method');
544 is(The::Count->getline(), 'method in Count',
545 'initial resolution is a method in aliased classes');
548 open Count, '<', $INC{'Count.pm'}
549 or die "Can't open $INC{'Count.pm'}: $!";
553 is(Count->getline(), "# zero! ha ha ha\n", 'file handles take priority');
554 is(The::Count->getline(), 'method in Count', 'but not in an aliased class');
556 eval q{close Count} or die $!;
560 is(Count->getline(), undef,
561 "closing a file handle doesn't change object resolution");
565 is(Count->getline(), 'method in Count',
566 'undefining the typeglob does change object resolution');
568 open Colour::H1, 'op/method.t' or die $!;
569 while (<Colour::H1>) {
572 open CLOSED, 'TEST' or die $!;
573 close CLOSED or die $!;
575 my $fh_start = tell Colour::H1;
576 my $data_start = tell DATA;
577 is(Colour::H1->getline(), <DATA>, 'read from a file');
578 is(Color::H1->getline(), 'method in Color::H1',
579 'initial resolution is a method');
581 *Color::H1 = *Colour::H1{IO};
583 is(Colour::H1->getline(), <DATA>, 'read from a file');
584 is(Color::H1->getline(), <DATA>,
585 'file handles take priority after typeglob assignment');
587 *Color::H1 = *CLOSED{IO};
590 is(Color::H1->getline(), undef,
591 "assigning a closed a file handle doesn't change object resolution");
595 is(Color::H1->getline(), 'method in Color::H1',
596 'undefining the typeglob does change object resolution');
598 seek Colour::H1, $fh_start, Fcntl::SEEK_SET() or die $!;
599 seek DATA, $data_start, Fcntl::SEEK_SET() or die $!;
601 is(Colour::H1->getline(), <DATA>, 'read from a file');
602 is(C3::H1->getline(), 'method in C3::H1', 'intial resolution is a method');
607 is(Colour::H1->getline(), <DATA>, 'read from a file');
608 is(C3::H1->getline(), <DATA>,
609 'file handles take priority after stash aliasing');
613 is(C3::H1->getline(), 'method in C3::H1',
614 'restoring the stash returns to a method');