This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
5.004_56: Patch to Tie::Hash and docs
[perl5.git] / t / op / method.t
CommitLineData
92d69e20
IZ
1#!./perl
2
3#
4# test method calls and autoloading.
5#
6
fb73857a 7print "1..24\n";
92d69e20
IZ
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
44a8e56a 27{
fb73857a 28 local @A::ISA = qw(C); # Update hash table with split() assignment
29 test (A->d, "C::d");
30 $#A::ISA = -1;
31 test (eval { A->d } || "fail", "fail");
32}
33test (A->d, "D::d");
34
35{
44a8e56a 36 local *B::d;
37 eval 'sub B::d {"B::d1"}'; # Import now.
38 test (A->d, "B::d1"); # Update hash table;
39 undef &B::d;
40 test ((eval { A->d }, ($@ =~ /Undefined subroutine/)), 1);
41}
92d69e20 42
44a8e56a 43test (A->d, "D::d"); # Back to previous state
92d69e20
IZ
44
45eval 'sub B::d {"B::d2"}'; # Import now.
46test (A->d, "B::d2"); # Update hash table;
47
48# What follows is hardly guarantied to work, since the names in scripts
49# are already linked to "pruned" globs. Say, `undef &B::d' if it were
50# after `delete $B::{d}; sub B::d {}' would reach an old subroutine.
51
52undef &B::d;
53delete $B::{d};
54test (A->d, "C::d"); # Update hash table;
55
56eval 'sub B::d {"B::d3"}'; # Import now.
57test (A->d, "B::d3"); # Update hash table;
58
59delete $B::{d};
60*dummy::dummy = sub {}; # Mark as updated
61test (A->d, "C::d");
62
63eval 'sub B::d {"B::d4"}'; # Import now.
64test (A->d, "B::d4"); # Update hash table;
65
66delete $B::{d}; # Should work without any help too
67test (A->d, "C::d");
68
44a8e56a 69*A::x = *A::d; # See if cache incorrectly follows synonyms
70A->d;
71test (eval { A->x } || "nope", "nope");
72
92d69e20
IZ
73eval <<'EOF';
74sub C::e;
09280a33 75BEGIN { *B::e = \&C::e } # Shouldn't prevent AUTOLOAD in original pkg
92d69e20
IZ
76sub Y::f;
77$counter = 0;
78
54310121 79@X::ISA = 'Y';
dc848c6f 80@Y::ISA = 'B';
92d69e20
IZ
81
82sub B::AUTOLOAD {
83 my $c = ++$counter;
84 my $method = $B::AUTOLOAD;
09280a33
CS
85 my $msg = "B: In $method, $c";
86 eval "sub $method { \$msg }";
87 goto &$method;
92d69e20
IZ
88}
89sub C::AUTOLOAD {
90 my $c = ++$counter;
91 my $method = $C::AUTOLOAD;
09280a33
CS
92 my $msg = "C: In $method, $c";
93 eval "sub $method { \$msg }";
94 goto &$method;
92d69e20
IZ
95}
96EOF
97
98test(A->e(), "C: In C::e, 1"); # We get a correct autoload
99test(A->e(), "C: In C::e, 1"); # Which sticks
100
101test(A->ee(), "B: In A::ee, 2"); # We get a generic autoload, method in top
102test(A->ee(), "B: In A::ee, 2"); # Which sticks
103
104test(Y->f(), "B: In Y::f, 3"); # We vivify a correct method
105test(Y->f(), "B: In Y::f, 3"); # Which sticks
106
107# This test is not intended to be reasonable. It is here just to let you
108# know that you broke some old construction. Feel free to rewrite the test
109# if your patch breaks it.
110
111*B::AUTOLOAD = sub {
112 my $c = ++$counter;
44a8e56a 113 my $method = $AUTOLOAD;
114 *$AUTOLOAD = sub { "new B: In $method, $c" };
115 goto &$AUTOLOAD;
92d69e20
IZ
116};
117
118test(A->eee(), "new B: In A::eee, 4"); # We get a correct $autoload
119test(A->eee(), "new B: In A::eee, 4"); # Which sticks
fb73857a 120
121# this test added due to bug discovery
122test(defined(@{"unknown_package::ISA"}) ? "defined" : "undefined", "undefined");