4 # test method calls and autoloading.
9 @INC = qw(. ../lib lib ../dist/base/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
173 # test that failed subroutine calls don't affect method calls
180 is(A2->foo(), "foo");
181 is(do { eval 'A2::foo()'; $@ ? 1 : 0}, 1);
182 is(A2->foo(), "foo");
185 ## This test was totally misguided. It passed before only because the
186 ## code to determine if a package was loaded used to look for the hash
187 ## %Foo::Bar instead of the package Foo::Bar:: -- and Config.pm just
188 ## happens to export %Config.
190 # is(do { use Config; eval 'Config->foo()';
191 # $@ =~ /^\QCan't locate object method "foo" via package "Config" at/ ? 1 : $@}, 1);
192 # is(do { use Config; eval '$d = bless {}, "Config"; $d->foo()';
193 # $@ =~ /^\QCan't locate object method "foo" via package "Config" at/ ? 1 : $@}, 1);
196 # test error messages if method loading fails
199 eval '$e = bless {}, "E::A"; E::A->foo()';
200 like ($@, qr/^\QCan't locate object method "foo" via package "E::A" at/);
201 eval '$e = bless {}, "E::B"; $e->foo()';
202 like ($@, qr/^\QCan't locate object method "foo" via package "E::B" at/);
204 like ($@, qr/^\QCan't locate object method "foo" via package "E::C" (perhaps /);
206 eval 'UNIVERSAL->E::D::foo()';
207 like ($@, qr/^\QCan't locate object method "foo" via package "E::D" (perhaps /);
208 eval 'my $e = bless {}, "UNIVERSAL"; $e->E::E::foo()';
209 like ($@, qr/^\QCan't locate object method "foo" via package "E::E" (perhaps /);
211 $e = bless {}, "E::F"; # force package to exist
212 eval 'UNIVERSAL->E::F::foo()';
213 like ($@, qr/^\QCan't locate object method "foo" via package "E::F" at/);
214 eval '$e = bless {}, "UNIVERSAL"; $e->E::F::foo()';
215 like ($@, qr/^\QCan't locate object method "foo" via package "E::F" at/);
217 # SUPER:: pseudoclass
218 @Saab::ISA = "Souper";
219 sub Souper::method { @_ }
220 @OtherSaab::ISA = "OtherSouper";
221 sub OtherSouper::method { "Isidore Ropen, Draft Manager" }
223 my $o = bless [], "Saab";
225 my @ret = $o->SUPER::method('whatever');
226 ::is $ret[0], $o, 'object passed to SUPER::method';
227 ::is $ret[1], 'whatever', 'argument passed to SUPER::method';
228 @ret = $o->SUPER'method('whatever');
229 ::is $ret[0], $o, "object passed to SUPER'method";
230 ::is $ret[1], 'whatever', "argument passed to SUPER'method";
231 @ret = Saab->SUPER::method;
232 ::is $ret[0], 'Saab', "package name passed to SUPER::method";
233 @ret = OtherSaab->SUPER::method;
234 ::is $ret[0], 'OtherSaab',
235 "->SUPER::method uses current package, not invocant";
239 local our @ISA = "Souper";
240 is eval { (main->SUPER::method)[0] }, 'main',
241 'Mentioning *SUPER:: does not stop ->SUPER from working in main';
245 *Mover:: = *Mover2::;
250 # Not our(@ISA), because the bug we are testing for interacts with an
251 # our() bug that cancels this bug out.
253 sub door::dohtem { 'dohtem' }
254 ::is eval { Mover->SUPER::dohtem; }, 'dohtem',
255 'SUPER inside moved package';
257 *door::dohtem = sub { 'method' };
258 ::is eval { Mover->SUPER::dohtem; }, 'method',
259 'SUPER inside moved package respects method changes';
263 BEGIN { our @ISA = qw(bar120694) }
267 local our $recursive = $recursive;
268 return "recursive" if $recursive++;
269 return if our $AUTOLOAD eq 'DESTROY';
270 $AUTOLOAD = "SUPER:" . substr $AUTOLOAD, rindex($AUTOLOAD, ':');
271 return $self->$AUTOLOAD(@_);
279 is bless( [] => "foo120694" )->plugh, 'xyzzy',
280 '->SUPER::method autoloading uses parent of current pkg';
283 # failed method call or UNIVERSAL::can() should not autovivify packages
284 is( $::{"Foo::"} || "none", "none"); # sanity check 1
285 is( $::{"Foo::"} || "none", "none"); # sanity check 2
287 is( UNIVERSAL::can("Foo", "boogie") ? "yes":"no", "no" );
288 is( $::{"Foo::"} || "none", "none"); # still missing?
290 is( Foo->UNIVERSAL::can("boogie") ? "yes":"no", "no" );
291 is( $::{"Foo::"} || "none", "none"); # still missing?
293 is( Foo->can("boogie") ? "yes":"no", "no" );
294 is( $::{"Foo::"} || "none", "none"); # still missing?
296 is( eval 'Foo->boogie(); 1' ? "yes":"no", "no" );
297 is( $::{"Foo::"} || "none", "none"); # still missing?
299 is(do { eval 'Foo->boogie()';
300 $@ =~ /^\QCan't locate object method "boogie" via package "Foo" (perhaps / ? 1 : $@}, 1);
302 eval 'sub Foo::boogie { "yes, sir!" }';
303 is( $::{"Foo::"} ? "ok" : "none", "ok"); # should exist now
304 is( Foo->boogie(), "yes, sir!");
306 # TODO: universal.t should test NoSuchPackage->isa()/can()
308 # This is actually testing parsing of indirect objects and undefined subs
309 # print foo("bar") where foo does not exist is not an indirect object.
310 # print foo "bar" where foo does not exist is an indirect object.
311 eval 'sub AUTOLOAD { "ok ", shift, "\n"; }';
314 # Bug ID 20010902.002
317 my $x = 'x'; # Lexical or package variable, 5.6.1 panics.
318 sub Foo::x : lvalue { $x }
323 # An autoloaded, inherited DESTROY may be invoked differently than normal
324 # methods, and has been known to give rise to spurious warnings
325 # eg <200203121600.QAA11064@gizmo.fdgroup.co.uk>
330 local $SIG{__WARN__} = sub { $w = $_[0] };
332 sub AutoDest::Base::AUTOLOAD {}
333 @AutoDest::ISA = qw(AutoDest::Base);
334 { my $x = bless {}, 'AutoDest'; }
339 # [ID 20020305.025] PACKAGE::SUPER doesn't work anymore
345 push @main::X, 'Amajor', @_;
351 $_[0]->Bminor::SUPER::test('x', 'y');
352 push @main::X, 'Bminor', @_;
354 Bminor->test('y', 'z');
355 is("@X", "Amajor Bminor x y Bminor Bminor y z");
358 for my $meth (['Bar', 'Foo::Bar'],
359 ['SUPER::Bar', 'main::SUPER::Bar'],
360 ['Xyz::SUPER::Bar', 'Xyz::SUPER::Bar'])
363 package UNIVERSAL; sub AUTOLOAD { my \$c = shift; print "\$c \$AUTOLOAD\\n" }
365 package main; Foo->$meth->[0]();
368 { switches => [ '-w' ] },
369 "check if UNIVERSAL::AUTOLOAD works",
373 # Test for #71952: crash when looking for a nonexistent destructor
374 # Regression introduced by fbb3ee5af3d4
376 fresh_perl_is(<<'EOT',
377 sub M::DESTROY; bless {}, "M" ; print "survived\n";
381 "no crash with a declared but missing DESTROY method"
385 # Test for calling a method on a packag name return by a magic variable
386 sub TIESCALAR{bless[]}
389 sub bolgy { ++$kalled; }
392 is $kalled, 1, 'calling a class method via a magic variable';
400 NulTest->${ \"method\0Whoops" };
402 like $@, qr/Can't locate object method "method\0Whoops" via package "NulTest" at/,
403 "method lookup is nul-clean";
405 *NulTest::AUTOLOAD = sub { our $AUTOLOAD; return $AUTOLOAD };
407 like(NulTest->${ \"nul\0test" }, qr/nul\0test/, "AUTOLOAD is nul-clean");
413 q! sub T::DESTROY { $x = $_[0]; } bless [], "T";!,
414 "DESTROY created new reference to dead object 'T' during global destruction.",
416 "DESTROY creating a new reference to the object generates a warning."
423 sub Just::a_japh { return "$_[0] another Perl hacker," }
424 is eval { "Just"->a_japh }, "Just another Perl hacker,",
425 'constants do not interfere with class methods';
432 sub lbiggles :lvalue { index "foo", "f" }
433 ok eval { main->bliggles(my($foo,$bar)) },
434 'foo->bar(my($foo,$bar)) is not called in lvalue context';
435 ok eval { main->bliggles(our($foo,$bar)) },
436 'foo->bar(our($foo,$bar)) is not called in lvalue context';
437 ok eval { main->bliggles(local($foo,$bar)) },
438 'foo->bar(local($foo,$bar)) is not called in lvalue context';
439 ok eval { () = main->lbiggles(my($foo,$bar)); 1 },
440 'foo->lv(my($foo,$bar)) is not called in lvalue context';
441 ok eval { () = main->lbiggles(our($foo,$bar)); 1 },
442 'foo->lv(our($foo,$bar)) is not called in lvalue context';
443 ok eval { () = main->lbiggles(local($foo,$bar)); 1 },
444 'foo->lv(local($foo,$bar)) is not called in lvalue context';
448 # AUTOLOAD and DESTROY can be declared without a leading sub,
449 # like BEGIN and friends.
452 eval 'AUTOLOAD { our $AUTOLOAD; return $AUTOLOAD }';
453 ::ok( !$@, "AUTOLOAD without a leading sub is legal" );
455 eval "DESTROY { ::pass( q!DESTROY without a leading sub is legal and gets called! ) }";
457 ::ok( NoSub->can("AUTOLOAD"), "...and sets up an AUTOLOAD normally" );
458 ::is( eval { NoSub->bluh }, "NoSub::bluh", "...which works as expected" );
460 { bless {}, "NoSub"; }
463 eval { () = 3; new {} };
465 qr/^Can't call method "new" without a package or object reference/,
466 'Err msg from new{} when stack contains a number';
467 eval { () = "foo"; new {} };
469 qr/^Can't call method "new" without a package or object reference/,
470 'Err msg from new{} when stack contains a word';
471 eval { () = undef; new {} };
473 qr/^Can't call method "new" without a package or object reference/,
474 'Err msg from new{} when stack contains undef';
478 sub ASI::m { shift; "@_" };
479 my @a = (bless([]), 'arg');
480 my $r = SUPER::m{@a};
481 ::is $r, 'arg', 'method{@array}';
483 ::is $r, 'arg', 'method{}@array';
484 $r = SUPER::m{@a}"b";
485 ::is $r, 'arg b', 'method{@array}$more_args';
488 # [perl #114924] SUPER->method
489 @SUPER::ISA = "SUPPER";
490 sub SUPPER::foo { "supper" }
491 is "SUPER"->foo, 'supper', 'SUPER->method';
493 sub flomp { "flimp" }
494 sub main::::flomp { "flump" }
495 is "::"->flomp, 'flump', 'method call on ::';
496 is "::main"->flomp, 'flimp', 'method call on ::main';
499 qr/^Can't call method "flomp" without a package or object reference/,
500 'method call on empty string';
501 is "3foo"->CORE::uc, '3FOO', '"3foo"->CORE::uc';
502 { no strict; @{"3foo::ISA"} = "CORE"; }
503 is "3foo"->uc, '3FOO', '"3foo"->uc (autobox style!)';
506 sub myclass::squeak { 'eek' }
507 eval { *myclass->squeak };
509 qr/^Can't call method "squeak" without a package or object reference/,
510 'method call on typeglob ignores package';
511 eval { (\*myclass)->squeak };
513 qr/^Can't call method "squeak" on unblessed reference/,
514 'method call on \*typeglob';
515 *stdout2 = *STDOUT; # stdout2 now stringifies as *main::STDOUT
516 sub IO::Handle::self { $_[0] }
517 # This used to stringify the glob:
518 is *stdout2->self, (\*stdout2)->self,
519 '*glob->method is equiv to (\*glob)->method';
520 sub { $_[0] = *STDOUT; is $_[0]->self, \$::h{k}, '$pvlv_glob->method' }
523 # Test that PL_stashcache doesn't change the resolution behaviour for file
524 # handles and package names.
526 skip_if_miniperl('file handles as methods requires loading IO::File', 26);
529 foreach (qw (Count::DATA Count Colour::H1 Color::H1 C3::H1)) {
534 return "method in $_";
542 *The::Count:: = \*Count::;
545 is(Count::DATA->getline(), 'method in Count::DATA',
546 'initial resolution is a method');
547 is(The::Count::DATA->getline(), 'method in Count::DATA',
548 'initial resolution is a method in aliased classes');
552 is(Count::DATA->getline(), "one! ha ha ha\n", 'file handles take priority');
553 is(The::Count::DATA->getline(), "two! ha ha ha\n",
554 'file handles take priority in aliased classes');
556 eval q{close Count::DATA} or die $!;
560 is(Count::DATA->getline(), undef,
561 "closing a file handle doesn't change object resolution");
562 is(The::Count::DATA->getline(), undef,
563 "closing a file handle doesn't change object resolution in aliased classes");
567 is(Count::DATA->getline(), 'method in Count::DATA',
568 'undefining the typeglob does change object resolution');
569 is(The::Count::DATA->getline(), 'method in Count::DATA',
570 'undefining the typeglob does change object resolution in aliased classes');
572 is(Count->getline(), 'method in Count',
573 'initial resolution is a method');
574 is(The::Count->getline(), 'method in Count',
575 'initial resolution is a method in aliased classes');
578 open Count, '<', $INC{'Count.pm'}
579 or die "Can't open $INC{'Count.pm'}: $!";
583 is(Count->getline(), "# zero! ha ha ha\n", 'file handles take priority');
584 is(The::Count->getline(), 'method in Count', 'but not in an aliased class');
586 eval q{close Count} or die $!;
590 is(Count->getline(), undef,
591 "closing a file handle doesn't change object resolution");
595 is(Count->getline(), 'method in Count',
596 'undefining the typeglob does change object resolution');
598 open Colour::H1, 'op/method.t' or die $!;
599 while (<Colour::H1>) {
602 open CLOSED, 'TEST' or die $!;
603 close CLOSED or die $!;
605 my $fh_start = tell Colour::H1;
606 my $data_start = tell DATA;
607 is(Colour::H1->getline(), <DATA>, 'read from a file');
608 is(Color::H1->getline(), 'method in Color::H1',
609 'initial resolution is a method');
611 *Color::H1 = *Colour::H1{IO};
613 is(Colour::H1->getline(), <DATA>, 'read from a file');
614 is(Color::H1->getline(), <DATA>,
615 'file handles take priority after io-to-typeglob assignment');
617 *Color::H1 = *CLOSED{IO};
620 is(Color::H1->getline(), undef,
621 "assigning a closed a file handle doesn't change object resolution");
625 is(Color::H1->getline(), 'method in Color::H1',
626 'undefining the typeglob does change object resolution');
628 *Color::H1 = *Colour::H1;
630 is(Color::H1->getline(), <DATA>,
631 'file handles take priority after typeglob-to-typeglob assignment');
633 seek Colour::H1, $fh_start, Fcntl::SEEK_SET() or die $!;
634 seek DATA, $data_start, Fcntl::SEEK_SET() or die $!;
636 is(Colour::H1->getline(), <DATA>, 'read from a file');
637 is(C3::H1->getline(), 'method in C3::H1', 'initial resolution is a method');
642 is(Colour::H1->getline(), <DATA>, 'read from a file');
643 is(C3::H1->getline(), <DATA>,
644 'file handles take priority after stash aliasing');
648 is(C3::H1->getline(), 'method in C3::H1',
649 'restoring the stash returns to a method');