File::Copy under OS/2
[p5sagit/p5-mst-13.2.git] / t / op / method.t
CommitLineData
92d69e20 1#!./perl
2
3#
4# test method calls and autoloading.
5#
6
7print "1..18\n";
8
9@A::ISA = 'B';
10@B::ISA = 'C';
11
12sub C::d {"C::d"}
13sub D::d {"D::d"}
14
15my $cnt = 0;
16sub test {
17 print "# got `$_[0]', expected `$_[1]'\nnot " unless $_[0] eq $_[1];
18 # print "not " unless shift eq shift;
19 print "ok ", ++$cnt, "\n"
20}
21
22test( A->d, "C::d"); # Update hash table;
23
24*B::d = \&D::d; # Import now.
25test (A->d, "D::d"); # Update hash table;
26
27eval 'sub B::d {"B::d1"}'; # Import now.
28test (A->d, "B::d1"); # Update hash table;
29
30undef &B::d; # Should work without any help too
31test (A->d, "C::d");
32
33eval 'sub B::d {"B::d2"}'; # Import now.
34test (A->d, "B::d2"); # Update hash table;
35
36# What follows is hardly guarantied to work, since the names in scripts
37# are already linked to "pruned" globs. Say, `undef &B::d' if it were
38# after `delete $B::{d}; sub B::d {}' would reach an old subroutine.
39
40undef &B::d;
41delete $B::{d};
42test (A->d, "C::d"); # Update hash table;
43
44eval 'sub B::d {"B::d3"}'; # Import now.
45test (A->d, "B::d3"); # Update hash table;
46
47delete $B::{d};
48*dummy::dummy = sub {}; # Mark as updated
49test (A->d, "C::d");
50
51eval 'sub B::d {"B::d4"}'; # Import now.
52test (A->d, "B::d4"); # Update hash table;
53
54delete $B::{d}; # Should work without any help too
55test (A->d, "C::d");
56
57eval <<'EOF';
58sub C::e;
59sub Y::f;
60$counter = 0;
61
62@X::ISA = 'Y';
63@Y::ISA = 'B';
64
65sub B::AUTOLOAD {
66 my $c = ++$counter;
67 my $method = $B::AUTOLOAD;
68 *$B::AUTOLOAD = sub { "B: In $method, $c" };
69 goto &$B::AUTOLOAD;
70}
71sub C::AUTOLOAD {
72 my $c = ++$counter;
73 my $method = $C::AUTOLOAD;
74 *$C::AUTOLOAD = sub { "C: In $method, $c" };
75 goto &$C::AUTOLOAD;
76}
77EOF
78
79test(A->e(), "C: In C::e, 1"); # We get a correct autoload
80test(A->e(), "C: In C::e, 1"); # Which sticks
81
82test(A->ee(), "B: In A::ee, 2"); # We get a generic autoload, method in top
83test(A->ee(), "B: In A::ee, 2"); # Which sticks
84
85test(Y->f(), "B: In Y::f, 3"); # We vivify a correct method
86test(Y->f(), "B: In Y::f, 3"); # Which sticks
87
88# This test is not intended to be reasonable. It is here just to let you
89# know that you broke some old construction. Feel free to rewrite the test
90# if your patch breaks it.
91
92*B::AUTOLOAD = sub {
93 my $c = ++$counter;
94 my $method = $main::__ANON__;
95 *$main::__ANON__ = sub { "new B: In $method, $c" };
96 goto &$main::__ANON__;
97};
98
99test(A->eee(), "new B: In A::eee, 4"); # We get a correct $autoload
100test(A->eee(), "new B: In A::eee, 4"); # Which sticks