1*0Sstevel@tonic-gate#!./perl 2*0Sstevel@tonic-gate 3*0Sstevel@tonic-gate# 4*0Sstevel@tonic-gate# test method calls and autoloading. 5*0Sstevel@tonic-gate# 6*0Sstevel@tonic-gate 7*0Sstevel@tonic-gateBEGIN { 8*0Sstevel@tonic-gate chdir 't' if -d 't'; 9*0Sstevel@tonic-gate @INC = qw(. ../lib); 10*0Sstevel@tonic-gate require "test.pl"; 11*0Sstevel@tonic-gate} 12*0Sstevel@tonic-gate 13*0Sstevel@tonic-gateprint "1..78\n"; 14*0Sstevel@tonic-gate 15*0Sstevel@tonic-gate@A::ISA = 'B'; 16*0Sstevel@tonic-gate@B::ISA = 'C'; 17*0Sstevel@tonic-gate 18*0Sstevel@tonic-gatesub C::d {"C::d"} 19*0Sstevel@tonic-gatesub D::d {"D::d"} 20*0Sstevel@tonic-gate 21*0Sstevel@tonic-gate# First, some basic checks of method-calling syntax: 22*0Sstevel@tonic-gate$obj = bless [], "Pack"; 23*0Sstevel@tonic-gatesub Pack::method { shift; join(",", "method", @_) } 24*0Sstevel@tonic-gate$mname = "method"; 25*0Sstevel@tonic-gate 26*0Sstevel@tonic-gateis(Pack->method("a","b","c"), "method,a,b,c"); 27*0Sstevel@tonic-gateis(Pack->$mname("a","b","c"), "method,a,b,c"); 28*0Sstevel@tonic-gateis(method Pack ("a","b","c"), "method,a,b,c"); 29*0Sstevel@tonic-gateis((method Pack "a","b","c"), "method,a,b,c"); 30*0Sstevel@tonic-gate 31*0Sstevel@tonic-gateis(Pack->method(), "method"); 32*0Sstevel@tonic-gateis(Pack->$mname(), "method"); 33*0Sstevel@tonic-gateis(method Pack (), "method"); 34*0Sstevel@tonic-gateis(Pack->method, "method"); 35*0Sstevel@tonic-gateis(Pack->$mname, "method"); 36*0Sstevel@tonic-gateis(method Pack, "method"); 37*0Sstevel@tonic-gate 38*0Sstevel@tonic-gateis($obj->method("a","b","c"), "method,a,b,c"); 39*0Sstevel@tonic-gateis($obj->$mname("a","b","c"), "method,a,b,c"); 40*0Sstevel@tonic-gateis((method $obj ("a","b","c")), "method,a,b,c"); 41*0Sstevel@tonic-gateis((method $obj "a","b","c"), "method,a,b,c"); 42*0Sstevel@tonic-gate 43*0Sstevel@tonic-gateis($obj->method(0), "method,0"); 44*0Sstevel@tonic-gateis($obj->method(1), "method,1"); 45*0Sstevel@tonic-gate 46*0Sstevel@tonic-gateis($obj->method(), "method"); 47*0Sstevel@tonic-gateis($obj->$mname(), "method"); 48*0Sstevel@tonic-gateis((method $obj ()), "method"); 49*0Sstevel@tonic-gateis($obj->method, "method"); 50*0Sstevel@tonic-gateis($obj->$mname, "method"); 51*0Sstevel@tonic-gateis(method $obj, "method"); 52*0Sstevel@tonic-gate 53*0Sstevel@tonic-gateis( A->d, "C::d"); # Update hash table; 54*0Sstevel@tonic-gate 55*0Sstevel@tonic-gate*B::d = \&D::d; # Import now. 56*0Sstevel@tonic-gateis(A->d, "D::d"); # Update hash table; 57*0Sstevel@tonic-gate 58*0Sstevel@tonic-gate{ 59*0Sstevel@tonic-gate local @A::ISA = qw(C); # Update hash table with split() assignment 60*0Sstevel@tonic-gate is(A->d, "C::d"); 61*0Sstevel@tonic-gate $#A::ISA = -1; 62*0Sstevel@tonic-gate is(eval { A->d } || "fail", "fail"); 63*0Sstevel@tonic-gate} 64*0Sstevel@tonic-gateis(A->d, "D::d"); 65*0Sstevel@tonic-gate 66*0Sstevel@tonic-gate{ 67*0Sstevel@tonic-gate local *B::d; 68*0Sstevel@tonic-gate eval 'sub B::d {"B::d1"}'; # Import now. 69*0Sstevel@tonic-gate is(A->d, "B::d1"); # Update hash table; 70*0Sstevel@tonic-gate undef &B::d; 71*0Sstevel@tonic-gate is((eval { A->d }, ($@ =~ /Undefined subroutine/)), 1); 72*0Sstevel@tonic-gate} 73*0Sstevel@tonic-gate 74*0Sstevel@tonic-gateis(A->d, "D::d"); # Back to previous state 75*0Sstevel@tonic-gate 76*0Sstevel@tonic-gateeval 'sub B::d {"B::d2"}'; # Import now. 77*0Sstevel@tonic-gateis(A->d, "B::d2"); # Update hash table; 78*0Sstevel@tonic-gate 79*0Sstevel@tonic-gate# What follows is hardly guarantied to work, since the names in scripts 80*0Sstevel@tonic-gate# are already linked to "pruned" globs. Say, `undef &B::d' if it were 81*0Sstevel@tonic-gate# after `delete $B::{d}; sub B::d {}' would reach an old subroutine. 82*0Sstevel@tonic-gate 83*0Sstevel@tonic-gateundef &B::d; 84*0Sstevel@tonic-gatedelete $B::{d}; 85*0Sstevel@tonic-gateis(A->d, "C::d"); # Update hash table; 86*0Sstevel@tonic-gate 87*0Sstevel@tonic-gateeval 'sub B::d {"B::d3"}'; # Import now. 88*0Sstevel@tonic-gateis(A->d, "B::d3"); # Update hash table; 89*0Sstevel@tonic-gate 90*0Sstevel@tonic-gatedelete $B::{d}; 91*0Sstevel@tonic-gate*dummy::dummy = sub {}; # Mark as updated 92*0Sstevel@tonic-gateis(A->d, "C::d"); 93*0Sstevel@tonic-gate 94*0Sstevel@tonic-gateeval 'sub B::d {"B::d4"}'; # Import now. 95*0Sstevel@tonic-gateis(A->d, "B::d4"); # Update hash table; 96*0Sstevel@tonic-gate 97*0Sstevel@tonic-gatedelete $B::{d}; # Should work without any help too 98*0Sstevel@tonic-gateis(A->d, "C::d"); 99*0Sstevel@tonic-gate 100*0Sstevel@tonic-gate{ 101*0Sstevel@tonic-gate local *C::d; 102*0Sstevel@tonic-gate is(eval { A->d } || "nope", "nope"); 103*0Sstevel@tonic-gate} 104*0Sstevel@tonic-gateis(A->d, "C::d"); 105*0Sstevel@tonic-gate 106*0Sstevel@tonic-gate*A::x = *A::d; # See if cache incorrectly follows synonyms 107*0Sstevel@tonic-gateA->d; 108*0Sstevel@tonic-gateis(eval { A->x } || "nope", "nope"); 109*0Sstevel@tonic-gate 110*0Sstevel@tonic-gateeval <<'EOF'; 111*0Sstevel@tonic-gatesub C::e; 112*0Sstevel@tonic-gateBEGIN { *B::e = \&C::e } # Shouldn't prevent AUTOLOAD in original pkg 113*0Sstevel@tonic-gatesub Y::f; 114*0Sstevel@tonic-gate$counter = 0; 115*0Sstevel@tonic-gate 116*0Sstevel@tonic-gate@X::ISA = 'Y'; 117*0Sstevel@tonic-gate@Y::ISA = 'B'; 118*0Sstevel@tonic-gate 119*0Sstevel@tonic-gatesub B::AUTOLOAD { 120*0Sstevel@tonic-gate my $c = ++$counter; 121*0Sstevel@tonic-gate my $method = $B::AUTOLOAD; 122*0Sstevel@tonic-gate my $msg = "B: In $method, $c"; 123*0Sstevel@tonic-gate eval "sub $method { \$msg }"; 124*0Sstevel@tonic-gate goto &$method; 125*0Sstevel@tonic-gate} 126*0Sstevel@tonic-gatesub C::AUTOLOAD { 127*0Sstevel@tonic-gate my $c = ++$counter; 128*0Sstevel@tonic-gate my $method = $C::AUTOLOAD; 129*0Sstevel@tonic-gate my $msg = "C: In $method, $c"; 130*0Sstevel@tonic-gate eval "sub $method { \$msg }"; 131*0Sstevel@tonic-gate goto &$method; 132*0Sstevel@tonic-gate} 133*0Sstevel@tonic-gateEOF 134*0Sstevel@tonic-gate 135*0Sstevel@tonic-gateis(A->e(), "C: In C::e, 1"); # We get a correct autoload 136*0Sstevel@tonic-gateis(A->e(), "C: In C::e, 1"); # Which sticks 137*0Sstevel@tonic-gate 138*0Sstevel@tonic-gateis(A->ee(), "B: In A::ee, 2"); # We get a generic autoload, method in top 139*0Sstevel@tonic-gateis(A->ee(), "B: In A::ee, 2"); # Which sticks 140*0Sstevel@tonic-gate 141*0Sstevel@tonic-gateis(Y->f(), "B: In Y::f, 3"); # We vivify a correct method 142*0Sstevel@tonic-gateis(Y->f(), "B: In Y::f, 3"); # Which sticks 143*0Sstevel@tonic-gate 144*0Sstevel@tonic-gate# This test is not intended to be reasonable. It is here just to let you 145*0Sstevel@tonic-gate# know that you broke some old construction. Feel free to rewrite the test 146*0Sstevel@tonic-gate# if your patch breaks it. 147*0Sstevel@tonic-gate 148*0Sstevel@tonic-gate*B::AUTOLOAD = sub { 149*0Sstevel@tonic-gate my $c = ++$counter; 150*0Sstevel@tonic-gate my $method = $AUTOLOAD; 151*0Sstevel@tonic-gate *$AUTOLOAD = sub { "new B: In $method, $c" }; 152*0Sstevel@tonic-gate goto &$AUTOLOAD; 153*0Sstevel@tonic-gate}; 154*0Sstevel@tonic-gate 155*0Sstevel@tonic-gateis(A->eee(), "new B: In A::eee, 4"); # We get a correct $autoload 156*0Sstevel@tonic-gateis(A->eee(), "new B: In A::eee, 4"); # Which sticks 157*0Sstevel@tonic-gate 158*0Sstevel@tonic-gate# this test added due to bug discovery 159*0Sstevel@tonic-gateis(defined(@{"unknown_package::ISA"}) ? "defined" : "undefined", "undefined"); 160*0Sstevel@tonic-gate 161*0Sstevel@tonic-gate# test that failed subroutine calls don't affect method calls 162*0Sstevel@tonic-gate{ 163*0Sstevel@tonic-gate package A1; 164*0Sstevel@tonic-gate sub foo { "foo" } 165*0Sstevel@tonic-gate package A2; 166*0Sstevel@tonic-gate @ISA = 'A1'; 167*0Sstevel@tonic-gate package main; 168*0Sstevel@tonic-gate is(A2->foo(), "foo"); 169*0Sstevel@tonic-gate is(do { eval 'A2::foo()'; $@ ? 1 : 0}, 1); 170*0Sstevel@tonic-gate is(A2->foo(), "foo"); 171*0Sstevel@tonic-gate} 172*0Sstevel@tonic-gate 173*0Sstevel@tonic-gate## This test was totally misguided. It passed before only because the 174*0Sstevel@tonic-gate## code to determine if a package was loaded used to look for the hash 175*0Sstevel@tonic-gate## %Foo::Bar instead of the package Foo::Bar:: -- and Config.pm just 176*0Sstevel@tonic-gate## happens to export %Config. 177*0Sstevel@tonic-gate# { 178*0Sstevel@tonic-gate# is(do { use Config; eval 'Config->foo()'; 179*0Sstevel@tonic-gate# $@ =~ /^\QCan't locate object method "foo" via package "Config" at/ ? 1 : $@}, 1); 180*0Sstevel@tonic-gate# is(do { use Config; eval '$d = bless {}, "Config"; $d->foo()'; 181*0Sstevel@tonic-gate# $@ =~ /^\QCan't locate object method "foo" via package "Config" at/ ? 1 : $@}, 1); 182*0Sstevel@tonic-gate# } 183*0Sstevel@tonic-gate 184*0Sstevel@tonic-gate 185*0Sstevel@tonic-gate# test error messages if method loading fails 186*0Sstevel@tonic-gateis(do { eval '$e = bless {}, "E::A"; E::A->foo()'; 187*0Sstevel@tonic-gate $@ =~ /^\QCan't locate object method "foo" via package "E::A" at/ ? 1 : $@}, 1); 188*0Sstevel@tonic-gateis(do { eval '$e = bless {}, "E::B"; $e->foo()'; 189*0Sstevel@tonic-gate $@ =~ /^\QCan't locate object method "foo" via package "E::B" at/ ? 1 : $@}, 1); 190*0Sstevel@tonic-gateis(do { eval 'E::C->foo()'; 191*0Sstevel@tonic-gate $@ =~ /^\QCan't locate object method "foo" via package "E::C" (perhaps / ? 1 : $@}, 1); 192*0Sstevel@tonic-gate 193*0Sstevel@tonic-gateis(do { eval 'UNIVERSAL->E::D::foo()'; 194*0Sstevel@tonic-gate $@ =~ /^\QCan't locate object method "foo" via package "E::D" (perhaps / ? 1 : $@}, 1); 195*0Sstevel@tonic-gateis(do { eval '$e = bless {}, "UNIVERSAL"; $e->E::E::foo()'; 196*0Sstevel@tonic-gate $@ =~ /^\QCan't locate object method "foo" via package "E::E" (perhaps / ? 1 : $@}, 1); 197*0Sstevel@tonic-gate 198*0Sstevel@tonic-gate$e = bless {}, "E::F"; # force package to exist 199*0Sstevel@tonic-gateis(do { eval 'UNIVERSAL->E::F::foo()'; 200*0Sstevel@tonic-gate $@ =~ /^\QCan't locate object method "foo" via package "E::F" at/ ? 1 : $@}, 1); 201*0Sstevel@tonic-gateis(do { eval '$e = bless {}, "UNIVERSAL"; $e->E::F::foo()'; 202*0Sstevel@tonic-gate $@ =~ /^\QCan't locate object method "foo" via package "E::F" at/ ? 1 : $@}, 1); 203*0Sstevel@tonic-gate 204*0Sstevel@tonic-gate# TODO: we need some tests for the SUPER:: pseudoclass 205*0Sstevel@tonic-gate 206*0Sstevel@tonic-gate# failed method call or UNIVERSAL::can() should not autovivify packages 207*0Sstevel@tonic-gateis( $::{"Foo::"} || "none", "none"); # sanity check 1 208*0Sstevel@tonic-gateis( $::{"Foo::"} || "none", "none"); # sanity check 2 209*0Sstevel@tonic-gate 210*0Sstevel@tonic-gateis( UNIVERSAL::can("Foo", "boogie") ? "yes":"no", "no" ); 211*0Sstevel@tonic-gateis( $::{"Foo::"} || "none", "none"); # still missing? 212*0Sstevel@tonic-gate 213*0Sstevel@tonic-gateis( Foo->UNIVERSAL::can("boogie") ? "yes":"no", "no" ); 214*0Sstevel@tonic-gateis( $::{"Foo::"} || "none", "none"); # still missing? 215*0Sstevel@tonic-gate 216*0Sstevel@tonic-gateis( Foo->can("boogie") ? "yes":"no", "no" ); 217*0Sstevel@tonic-gateis( $::{"Foo::"} || "none", "none"); # still missing? 218*0Sstevel@tonic-gate 219*0Sstevel@tonic-gateis( eval 'Foo->boogie(); 1' ? "yes":"no", "no" ); 220*0Sstevel@tonic-gateis( $::{"Foo::"} || "none", "none"); # still missing? 221*0Sstevel@tonic-gate 222*0Sstevel@tonic-gateis(do { eval 'Foo->boogie()'; 223*0Sstevel@tonic-gate $@ =~ /^\QCan't locate object method "boogie" via package "Foo" (perhaps / ? 1 : $@}, 1); 224*0Sstevel@tonic-gate 225*0Sstevel@tonic-gateeval 'sub Foo::boogie { "yes, sir!" }'; 226*0Sstevel@tonic-gateis( $::{"Foo::"} ? "ok" : "none", "ok"); # should exist now 227*0Sstevel@tonic-gateis( Foo->boogie(), "yes, sir!"); 228*0Sstevel@tonic-gate 229*0Sstevel@tonic-gate# TODO: universal.t should test NoSuchPackage->isa()/can() 230*0Sstevel@tonic-gate 231*0Sstevel@tonic-gate# This is actually testing parsing of indirect objects and undefined subs 232*0Sstevel@tonic-gate# print foo("bar") where foo does not exist is not an indirect object. 233*0Sstevel@tonic-gate# print foo "bar" where foo does not exist is an indirect object. 234*0Sstevel@tonic-gateeval 'sub AUTOLOAD { "ok ", shift, "\n"; }'; 235*0Sstevel@tonic-gateok(1); 236*0Sstevel@tonic-gate 237*0Sstevel@tonic-gate# Bug ID 20010902.002 238*0Sstevel@tonic-gateis( 239*0Sstevel@tonic-gate eval q[ 240*0Sstevel@tonic-gate $x = 'x'; 241*0Sstevel@tonic-gate sub Foo::x : lvalue { $x } 242*0Sstevel@tonic-gate Foo->$x = 'ok'; 243*0Sstevel@tonic-gate ] || $@, 'ok' 244*0Sstevel@tonic-gate); 245*0Sstevel@tonic-gate 246*0Sstevel@tonic-gate# An autoloaded, inherited DESTROY may be invoked differently than normal 247*0Sstevel@tonic-gate# methods, and has been known to give rise to spurious warnings 248*0Sstevel@tonic-gate# eg <200203121600.QAA11064@gizmo.fdgroup.co.uk> 249*0Sstevel@tonic-gate 250*0Sstevel@tonic-gate{ 251*0Sstevel@tonic-gate use warnings; 252*0Sstevel@tonic-gate my $w = ''; 253*0Sstevel@tonic-gate local $SIG{__WARN__} = sub { $w = $_[0] }; 254*0Sstevel@tonic-gate 255*0Sstevel@tonic-gate sub AutoDest::Base::AUTOLOAD {} 256*0Sstevel@tonic-gate @AutoDest::ISA = qw(AutoDest::Base); 257*0Sstevel@tonic-gate { my $x = bless {}, 'AutoDest'; } 258*0Sstevel@tonic-gate $w =~ s/\n//g; 259*0Sstevel@tonic-gate is($w, ''); 260*0Sstevel@tonic-gate} 261*0Sstevel@tonic-gate 262*0Sstevel@tonic-gate# [ID 20020305.025] PACKAGE::SUPER doesn't work anymore 263*0Sstevel@tonic-gate 264*0Sstevel@tonic-gatepackage main; 265*0Sstevel@tonic-gateour @X; 266*0Sstevel@tonic-gatepackage Amajor; 267*0Sstevel@tonic-gatesub test { 268*0Sstevel@tonic-gate push @main::X, 'Amajor', @_; 269*0Sstevel@tonic-gate} 270*0Sstevel@tonic-gatepackage Bminor; 271*0Sstevel@tonic-gateuse base qw(Amajor); 272*0Sstevel@tonic-gatepackage main; 273*0Sstevel@tonic-gatesub Bminor::test { 274*0Sstevel@tonic-gate $_[0]->Bminor::SUPER::test('x', 'y'); 275*0Sstevel@tonic-gate push @main::X, 'Bminor', @_; 276*0Sstevel@tonic-gate} 277*0Sstevel@tonic-gateBminor->test('y', 'z'); 278*0Sstevel@tonic-gateis("@X", "Amajor Bminor x y Bminor Bminor y z"); 279*0Sstevel@tonic-gate 280*0Sstevel@tonic-gatepackage main; 281*0Sstevel@tonic-gatefor my $meth (['Bar', 'Foo::Bar'], 282*0Sstevel@tonic-gate ['SUPER::Bar', 'main::SUPER::Bar'], 283*0Sstevel@tonic-gate ['Xyz::SUPER::Bar', 'Xyz::SUPER::Bar']) 284*0Sstevel@tonic-gate{ 285*0Sstevel@tonic-gate fresh_perl_is(<<EOT, 286*0Sstevel@tonic-gatepackage UNIVERSAL; sub AUTOLOAD { my \$c = shift; print "\$c \$AUTOLOAD\\n" } 287*0Sstevel@tonic-gatesub DESTROY {} # IO object destructor called in MacOS, because of Mac::err 288*0Sstevel@tonic-gatepackage Xyz; 289*0Sstevel@tonic-gatepackage main; Foo->$meth->[0](); 290*0Sstevel@tonic-gateEOT 291*0Sstevel@tonic-gate "Foo $meth->[1]", 292*0Sstevel@tonic-gate { switches => [ '-w' ] }, 293*0Sstevel@tonic-gate "check if UNIVERSAL::AUTOLOAD works", 294*0Sstevel@tonic-gate ); 295*0Sstevel@tonic-gate} 296