This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
gv:gv_try_downgrade: Leave PL_stderrgv alone
[perl5.git] / t / op / method.t
CommitLineData
9bfdb36e 1#!./perl -w
92d69e20
IZ
2
3#
4# test method calls and autoloading.
5#
6
4755096e
GS
7BEGIN {
8 chdir 't' if -d 't';
6f908f1b 9 @INC = qw(. ../lib lib);
6dc4e5ce 10 require "test.pl";
4755096e
GS
11}
12
9bfdb36e
NC
13use strict;
14no warnings 'once';
15
a77c16f7 16plan(tests => 146);
92d69e20
IZ
17
18@A::ISA = 'B';
19@B::ISA = 'C';
20
21sub C::d {"C::d"}
22sub D::d {"D::d"}
23
567ce7b1 24# First, some basic checks of method-calling syntax:
9bfdb36e 25my $obj = bless [], "Pack";
567ce7b1 26sub Pack::method { shift; join(",", "method", @_) }
9bfdb36e 27my $mname = "method";
567ce7b1 28
6dc4e5ce
JH
29is(Pack->method("a","b","c"), "method,a,b,c");
30is(Pack->$mname("a","b","c"), "method,a,b,c");
31is(method Pack ("a","b","c"), "method,a,b,c");
32is((method Pack "a","b","c"), "method,a,b,c");
567ce7b1 33
6dc4e5ce
JH
34is(Pack->method(), "method");
35is(Pack->$mname(), "method");
36is(method Pack (), "method");
37is(Pack->method, "method");
38is(Pack->$mname, "method");
39is(method Pack, "method");
567ce7b1 40
6dc4e5ce
JH
41is($obj->method("a","b","c"), "method,a,b,c");
42is($obj->$mname("a","b","c"), "method,a,b,c");
43is((method $obj ("a","b","c")), "method,a,b,c");
44is((method $obj "a","b","c"), "method,a,b,c");
145eb477 45
6dc4e5ce
JH
46is($obj->method(0), "method,0");
47is($obj->method(1), "method,1");
567ce7b1 48
6dc4e5ce
JH
49is($obj->method(), "method");
50is($obj->$mname(), "method");
51is((method $obj ()), "method");
52is($obj->method, "method");
53is($obj->$mname, "method");
54is(method $obj, "method");
567ce7b1 55
6dc4e5ce 56is( A->d, "C::d"); # Update hash table;
92d69e20
IZ
57
58*B::d = \&D::d; # Import now.
6dc4e5ce 59is(A->d, "D::d"); # Update hash table;
92d69e20 60
44a8e56a 61{
fb73857a 62 local @A::ISA = qw(C); # Update hash table with split() assignment
6dc4e5ce 63 is(A->d, "C::d");
fb73857a 64 $#A::ISA = -1;
6dc4e5ce 65 is(eval { A->d } || "fail", "fail");
fb73857a 66}
6dc4e5ce 67is(A->d, "D::d");
fb73857a 68
69{
44a8e56a 70 local *B::d;
71 eval 'sub B::d {"B::d1"}'; # Import now.
6dc4e5ce 72 is(A->d, "B::d1"); # Update hash table;
44a8e56a 73 undef &B::d;
6dc4e5ce 74 is((eval { A->d }, ($@ =~ /Undefined subroutine/)), 1);
44a8e56a 75}
92d69e20 76
6dc4e5ce 77is(A->d, "D::d"); # Back to previous state
92d69e20 78
9bfdb36e 79eval 'no warnings "redefine"; sub B::d {"B::d2"}'; # Import now.
6dc4e5ce 80is(A->d, "B::d2"); # Update hash table;
92d69e20
IZ
81
82# What follows is hardly guarantied to work, since the names in scripts
94e1fbae
JK
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.
92d69e20
IZ
85
86undef &B::d;
87delete $B::{d};
5743f2a3
FC
88is(A->d, "C::d");
89
90eval 'sub B::d {"B::d2.5"}';
91A->d; # Update hash table;
92my $glob = \delete $B::{d}; # non-void context; hang on to the glob
6dc4e5ce 93is(A->d, "C::d"); # Update hash table;
92d69e20
IZ
94
95eval 'sub B::d {"B::d3"}'; # Import now.
6dc4e5ce 96is(A->d, "B::d3"); # Update hash table;
92d69e20
IZ
97
98delete $B::{d};
99*dummy::dummy = sub {}; # Mark as updated
6dc4e5ce 100is(A->d, "C::d");
92d69e20
IZ
101
102eval 'sub B::d {"B::d4"}'; # Import now.
6dc4e5ce 103is(A->d, "B::d4"); # Update hash table;
92d69e20
IZ
104
105delete $B::{d}; # Should work without any help too
6dc4e5ce 106is(A->d, "C::d");
92d69e20 107
fae75791
CS
108{
109 local *C::d;
6dc4e5ce 110 is(eval { A->d } || "nope", "nope");
fae75791 111}
6dc4e5ce 112is(A->d, "C::d");
fae75791 113
9bfdb36e 114*A::x = *A::d;
44a8e56a 115A->d;
9bfdb36e
NC
116is(eval { A->x } || "nope", "nope", 'cache should not follow synonyms');
117
118my $counter;
44a8e56a 119
92d69e20
IZ
120eval <<'EOF';
121sub C::e;
09280a33 122BEGIN { *B::e = \&C::e } # Shouldn't prevent AUTOLOAD in original pkg
92d69e20
IZ
123sub Y::f;
124$counter = 0;
125
54310121 126@X::ISA = 'Y';
dc848c6f 127@Y::ISA = 'B';
92d69e20
IZ
128
129sub B::AUTOLOAD {
130 my $c = ++$counter;
131 my $method = $B::AUTOLOAD;
09280a33
CS
132 my $msg = "B: In $method, $c";
133 eval "sub $method { \$msg }";
134 goto &$method;
92d69e20
IZ
135}
136sub C::AUTOLOAD {
137 my $c = ++$counter;
138 my $method = $C::AUTOLOAD;
09280a33
CS
139 my $msg = "C: In $method, $c";
140 eval "sub $method { \$msg }";
141 goto &$method;
92d69e20
IZ
142}
143EOF
144
6dc4e5ce
JH
145is(A->e(), "C: In C::e, 1"); # We get a correct autoload
146is(A->e(), "C: In C::e, 1"); # Which sticks
92d69e20 147
6dc4e5ce
JH
148is(A->ee(), "B: In A::ee, 2"); # We get a generic autoload, method in top
149is(A->ee(), "B: In A::ee, 2"); # Which sticks
92d69e20 150
6dc4e5ce
JH
151is(Y->f(), "B: In Y::f, 3"); # We vivify a correct method
152is(Y->f(), "B: In Y::f, 3"); # Which sticks
92d69e20
IZ
153
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.
157
9bfdb36e
NC
158{
159no warnings 'redefine';
92d69e20 160*B::AUTOLOAD = sub {
9bfdb36e 161 use warnings;
92d69e20 162 my $c = ++$counter;
9bfdb36e
NC
163 my $method = $::AUTOLOAD;
164 no strict 'refs';
165 *$::AUTOLOAD = sub { "new B: In $method, $c" };
166 goto &$::AUTOLOAD;
92d69e20 167};
9bfdb36e 168}
92d69e20 169
6dc4e5ce
JH
170is(A->eee(), "new B: In A::eee, 4"); # We get a correct $autoload
171is(A->eee(), "new B: In A::eee, 4"); # Which sticks
fb73857a 172
9bfdb36e
NC
173{
174 no strict 'refs';
33f89799 175 no warnings 'deprecated';
9bfdb36e 176 # this test added due to bug discovery (in 5.004_04, fb73857aa0bfa8ed)
33f89799
NC
177 # Possibly kill this test now that defined @::array is finally properly
178 # deprecated?
9bfdb36e
NC
179 is(defined(@{"unknown_package::ISA"}) ? "defined" : "undefined", "undefined");
180}
f6ec51f7
GS
181
182# test that failed subroutine calls don't affect method calls
183{
184 package A1;
185 sub foo { "foo" }
186 package A2;
9bfdb36e 187 @A2::ISA = 'A1';
f6ec51f7 188 package main;
6dc4e5ce
JH
189 is(A2->foo(), "foo");
190 is(do { eval 'A2::foo()'; $@ ? 1 : 0}, 1);
191 is(A2->foo(), "foo");
f6ec51f7 192}
c1899e02 193
af09ea45
IK
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.
198# {
6dc4e5ce 199# is(do { use Config; eval 'Config->foo()';
af09ea45 200# $@ =~ /^\QCan't locate object method "foo" via package "Config" at/ ? 1 : $@}, 1);
6dc4e5ce 201# is(do { use Config; eval '$d = bless {}, "Config"; $d->foo()';
af09ea45
IK
202# $@ =~ /^\QCan't locate object method "foo" via package "Config" at/ ? 1 : $@}, 1);
203# }
204
af09ea45 205# test error messages if method loading fails
9bfdb36e
NC
206my $e;
207
2f907243
NC
208eval '$e = bless {}, "E::A"; E::A->foo()';
209like ($@, qr/^\QCan't locate object method "foo" via package "E::A" at/);
210eval '$e = bless {}, "E::B"; $e->foo()';
211like ($@, qr/^\QCan't locate object method "foo" via package "E::B" at/);
212eval 'E::C->foo()';
213like ($@, qr/^\QCan't locate object method "foo" via package "E::C" (perhaps /);
214
215eval 'UNIVERSAL->E::D::foo()';
216like ($@, qr/^\QCan't locate object method "foo" via package "E::D" (perhaps /);
9bfdb36e 217eval 'my $e = bless {}, "UNIVERSAL"; $e->E::E::foo()';
2f907243 218like ($@, qr/^\QCan't locate object method "foo" via package "E::E" (perhaps /);
af09ea45
IK
219
220$e = bless {}, "E::F"; # force package to exist
2f907243
NC
221eval 'UNIVERSAL->E::F::foo()';
222like ($@, qr/^\QCan't locate object method "foo" via package "E::F" at/);
223eval '$e = bless {}, "UNIVERSAL"; $e->E::F::foo()';
224like ($@, qr/^\QCan't locate object method "foo" via package "E::F" at/);
af09ea45 225
697efb9b
FC
226# SUPER:: pseudoclass
227@Saab::ISA = "Souper";
228sub Souper::method { @_ }
229@OtherSaab::ISA = "OtherSouper";
230sub OtherSouper::method { "Isidore Ropen, Draft Manager" }
231{
232 my $o = bless [], "Saab";
233 package 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";
245}
3c104e59
FC
246() = *SUPER::;
247{
248 local our @ISA = "Souper";
249 is eval { (main->SUPER::method)[0] }, 'main',
250 'Mentioning *SUPER:: does not stop ->SUPER from working in main';
251}
0308a534
FC
252{
253 BEGIN {
254 *Mover:: = *Mover2::;
255 *Mover2:: = *foo;
256 }
257 package Mover;
258 no strict;
259 # Not our(@ISA), because the bug we are testing for interacts with an
260 # our() bug that cancels this bug out.
261 @ISA = 'door';
262 sub door::dohtem { 'dohtem' }
263 ::is eval { Mover->SUPER::dohtem; }, 'dohtem',
264 'SUPER inside moved package';
37b0b3b2
FC
265 undef *door::dohtem;
266 *door::dohtem = sub { 'method' };
267 ::is eval { Mover->SUPER::dohtem; }, 'method',
268 'SUPER inside moved package respects method changes';
0308a534 269}
af09ea45
IK
270
271# failed method call or UNIVERSAL::can() should not autovivify packages
6dc4e5ce
JH
272is( $::{"Foo::"} || "none", "none"); # sanity check 1
273is( $::{"Foo::"} || "none", "none"); # sanity check 2
c1899e02 274
6dc4e5ce
JH
275is( UNIVERSAL::can("Foo", "boogie") ? "yes":"no", "no" );
276is( $::{"Foo::"} || "none", "none"); # still missing?
af09ea45 277
6dc4e5ce
JH
278is( Foo->UNIVERSAL::can("boogie") ? "yes":"no", "no" );
279is( $::{"Foo::"} || "none", "none"); # still missing?
af09ea45 280
6dc4e5ce
JH
281is( Foo->can("boogie") ? "yes":"no", "no" );
282is( $::{"Foo::"} || "none", "none"); # still missing?
af09ea45 283
6dc4e5ce
JH
284is( eval 'Foo->boogie(); 1' ? "yes":"no", "no" );
285is( $::{"Foo::"} || "none", "none"); # still missing?
af09ea45 286
6dc4e5ce 287is(do { eval 'Foo->boogie()';
af09ea45
IK
288 $@ =~ /^\QCan't locate object method "boogie" via package "Foo" (perhaps / ? 1 : $@}, 1);
289
290eval 'sub Foo::boogie { "yes, sir!" }';
6dc4e5ce
JH
291is( $::{"Foo::"} ? "ok" : "none", "ok"); # should exist now
292is( Foo->boogie(), "yes, sir!");
af09ea45
IK
293
294# TODO: universal.t should test NoSuchPackage->isa()/can()
c1899e02 295
f0670693
SC
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.
84251760 299eval 'sub AUTOLOAD { "ok ", shift, "\n"; }';
6dc4e5ce 300ok(1);
af09ea45 301
a397c3d9 302# Bug ID 20010902.002
6dc4e5ce 303is(
a397c3d9 304 eval q[
9bfdb36e 305 my $x = 'x'; # Lexical or package variable, 5.6.1 panics.
a397c3d9
RGS
306 sub Foo::x : lvalue { $x }
307 Foo->$x = 'ok';
308 ] || $@, 'ok'
309);
310
3ad83ce7
AMS
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>
314
315{
316 use warnings;
317 my $w = '';
318 local $SIG{__WARN__} = sub { $w = $_[0] };
319
320 sub AutoDest::Base::AUTOLOAD {}
321 @AutoDest::ISA = qw(AutoDest::Base);
322 { my $x = bless {}, 'AutoDest'; }
323 $w =~ s/\n//g;
6dc4e5ce 324 is($w, '');
3ad83ce7
AMS
325}
326
e189a56d
IK
327# [ID 20020305.025] PACKAGE::SUPER doesn't work anymore
328
329package main;
330our @X;
331package Amajor;
332sub test {
333 push @main::X, 'Amajor', @_;
334}
335package Bminor;
336use base qw(Amajor);
337package main;
338sub Bminor::test {
339 $_[0]->Bminor::SUPER::test('x', 'y');
340 push @main::X, 'Bminor', @_;
341}
342Bminor->test('y', 'z');
343is("@X", "Amajor Bminor x y Bminor Bminor y z");
344
0dae17bd
GS
345package main;
346for my $meth (['Bar', 'Foo::Bar'],
347 ['SUPER::Bar', 'main::SUPER::Bar'],
348 ['Xyz::SUPER::Bar', 'Xyz::SUPER::Bar'])
349{
350 fresh_perl_is(<<EOT,
351package UNIVERSAL; sub AUTOLOAD { my \$c = shift; print "\$c \$AUTOLOAD\\n" }
352package Xyz;
353package main; Foo->$meth->[0]();
354EOT
355 "Foo $meth->[1]",
356 { switches => [ '-w' ] },
357 "check if UNIVERSAL::AUTOLOAD works",
358 );
359}
1f15e670
NT
360
361# Test for #71952: crash when looking for a nonexistent destructor
362# Regression introduced by fbb3ee5af3d4
363{
364 fresh_perl_is(<<'EOT',
365sub M::DESTROY; bless {}, "M" ; print "survived\n";
366EOT
367 "survived",
368 {},
369 "no crash with a declared but missing DESTROY method"
370 );
371}
372
da6b625f
FC
373# Test for calling a method on a packag name return by a magic variable
374sub TIESCALAR{bless[]}
375sub FETCH{"main"}
376my $kalled;
377sub bolgy { ++$kalled; }
378tie my $a, "";
379$a->bolgy;
380is $kalled, 1, 'calling a class method via a magic variable';
f937af42
BF
381
382{
383 package NulTest;
384 sub method { 1 }
385
386 package main;
387 eval {
388 NulTest->${ \"method\0Whoops" };
389 };
390 like $@, qr/Can't locate object method "method\0Whoops" via package "NulTest" at/,
391 "method lookup is nul-clean";
392
393 *NulTest::AUTOLOAD = sub { our $AUTOLOAD; return $AUTOLOAD };
394
395 like(NulTest->${ \"nul\0test" }, "nul\0test", "AUTOLOAD is nul-clean");
396}
2a0f5ef0
BF
397
398
399{
400 fresh_perl_is(
401 q! sub T::DESTROY { $x = $_[0]; } bless [], "T";!,
402 "DESTROY created new reference to dead object 'T' during global destruction.",
403 {},
404 "DESTROY creating a new reference to the object generates a warning."
405 );
406}
71481574
FC
407
408# [perl #43663]
409{
410 $::{"Just"} = \1;
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';
414}
0865059d
FC
415
416# [perl #109264]
417{
418 no strict 'vars';
419 sub bliggles { 1 }
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';
433}
544b72e2
BF
434
435{
436 # AUTOLOAD and DESTROY can be declared without a leading sub,
437 # like BEGIN and friends.
438 package NoSub;
439
440 eval 'AUTOLOAD { our $AUTOLOAD; return $AUTOLOAD }';
441 ::ok( !$@, "AUTOLOAD without a leading sub is legal" );
442
443 eval "DESTROY { ::pass( q!DESTROY without a leading sub is legal and gets called! ) }";
444 {
445 ::ok( NoSub->can("AUTOLOAD"), "...and sets up an AUTOLOAD normally" );
446 ::is( eval { NoSub->bluh }, "NoSub::bluh", "...which works as expected" );
447 }
448 { bless {}, "NoSub"; }
449}
f226e9be
FC
450
451eval { () = 3; new {} };
452like $@,
453 qr/^Can't call method "new" without a package or object reference/,
454 'Err msg from new{} when stack contains a number';
455eval { () = "foo"; new {} };
456like $@,
457 qr/^Can't call method "new" without a package or object reference/,
458 'Err msg from new{} when stack contains a word';
459eval { () = undef; new {} };
460like $@,
461 qr/^Can't call method "new" without a package or object reference/,
462 'Err msg from new{} when stack contains undef';
bfde49d4
FC
463
464package egakacp {
465 our @ISA = 'ASI';
466 sub ASI::m { shift; "@_" };
467 my @a = (bless([]), 'arg');
468 my $r = SUPER::m{@a};
469 ::is $r, 'arg', 'method{@array}';
470 $r = SUPER::m{}@a;
471 ::is $r, 'arg', 'method{}@array';
472 $r = SUPER::m{@a}"b";
473 ::is $r, 'arg b', 'method{@array}$more_args';
474}
aae43805
FC
475
476# [perl #114924] SUPER->method
477@SUPER::ISA = "SUPPER";
478sub SUPPER::foo { "supper" }
479is "SUPER"->foo, 'supper', 'SUPER->method';
7156e69a
FC
480
481sub flomp { "flimp" }
482sub main::::flomp { "flump" }
483is "::"->flomp, 'flump', 'method call on ::';
484is "::main"->flomp, 'flimp', 'method call on ::main';
485eval { ""->flomp };
486like $@,
487 qr/^Can't call method "flomp" without a package or object reference/,
488 'method call on empty string';
489is "3foo"->CORE::uc, '3FOO', '"3foo"->CORE::uc';
490{ no strict; @{"3foo::ISA"} = "CORE"; }
491is "3foo"->uc, '3FOO', '"3foo"->uc (autobox style!)';
6f908f1b 492
a77c16f7
FC
493# *foo vs (\*foo)
494sub myclass::squeak { 'eek' }
495eval { *myclass->squeak };
496like $@,
497 qr/^Can't call method "squeak" without a package or object reference/,
498 'method call on typeglob ignores package';
499eval { (\*myclass)->squeak };
500like $@,
501 qr/^Can't call method "squeak" on unblessed reference/,
502 'method call on \*typeglob';
503*stdout2 = *STDOUT; # stdout2 now stringifies as *main::STDOUT
504sub IO::Handle::self { $_[0] }
505# This used to stringify the glob:
506is *stdout2->self, (\*stdout2)->self,
507 '*glob->method is equiv to (\*glob)->method';
508sub { $_[0] = *STDOUT; is $_[0]->self, \$::h{k}, '$pvlv_glob->method' }
509 ->($::h{k});
510
6f908f1b
NC
511# Test that PL_stashcache doesn't change the resolution behaviour for file
512# handles and package names.
513SKIP: {
b87eccc1 514 skip_if_miniperl('file handles as methods requires loading IO::File', 26);
6f908f1b
NC
515 require Fcntl;
516
517 foreach (qw (Count::DATA Count Colour::H1 Color::H1 C3::H1)) {
518 eval qq{
519 package $_;
520
521 sub getline {
522 return "method in $_";
523 }
524
525 1;
526 } or die $@;
527 }
528
529 BEGIN {
530 *The::Count:: = \*Count::;
531 }
532
533 is(Count::DATA->getline(), 'method in Count::DATA',
534 'initial resolution is a method');
535 is(The::Count::DATA->getline(), 'method in Count::DATA',
536 'initial resolution is a method in aliased classes');
537
538 require Count;
539
540 is(Count::DATA->getline(), "one! ha ha ha\n", 'file handles take priority');
541 is(The::Count::DATA->getline(), "two! ha ha ha\n",
542 'file handles take priority in aliased classes');
543
544 eval q{close Count::DATA} or die $!;
545
546 {
547 no warnings 'io';
548 is(Count::DATA->getline(), undef,
549 "closing a file handle doesn't change object resolution");
550 is(The::Count::DATA->getline(), undef,
551 "closing a file handle doesn't change object resolution in aliased classes");
552}
553
554 undef *Count::DATA;
555 is(Count::DATA->getline(), 'method in Count::DATA',
556 'undefining the typeglob does change object resolution');
557 is(The::Count::DATA->getline(), 'method in Count::DATA',
558 'undefining the typeglob does change object resolution in aliased classes');
559
560 is(Count->getline(), 'method in Count',
561 'initial resolution is a method');
562 is(The::Count->getline(), 'method in Count',
563 'initial resolution is a method in aliased classes');
564
565 eval q{
566 open Count, '<', $INC{'Count.pm'}
567 or die "Can't open $INC{'Count.pm'}: $!";
5681;
569 } or die $@;
570
571 is(Count->getline(), "# zero! ha ha ha\n", 'file handles take priority');
572 is(The::Count->getline(), 'method in Count', 'but not in an aliased class');
573
574 eval q{close Count} or die $!;
575
576 {
577 no warnings 'io';
578 is(Count->getline(), undef,
579 "closing a file handle doesn't change object resolution");
580 }
581
582 undef *Count;
583 is(Count->getline(), 'method in Count',
584 'undefining the typeglob does change object resolution');
585
586 open Colour::H1, 'op/method.t' or die $!;
587 while (<Colour::H1>) {
588 last if /^__END__/;
589 }
590 open CLOSED, 'TEST' or die $!;
591 close CLOSED or die $!;
592
593 my $fh_start = tell Colour::H1;
594 my $data_start = tell DATA;
595 is(Colour::H1->getline(), <DATA>, 'read from a file');
596 is(Color::H1->getline(), 'method in Color::H1',
597 'initial resolution is a method');
598
599 *Color::H1 = *Colour::H1{IO};
600
601 is(Colour::H1->getline(), <DATA>, 'read from a file');
602 is(Color::H1->getline(), <DATA>,
210fdecd 603 'file handles take priority after io-to-typeglob assignment');
6f908f1b
NC
604
605 *Color::H1 = *CLOSED{IO};
606 {
607 no warnings 'io';
608 is(Color::H1->getline(), undef,
609 "assigning a closed a file handle doesn't change object resolution");
610 }
611
612 undef *Color::H1;
613 is(Color::H1->getline(), 'method in Color::H1',
614 'undefining the typeglob does change object resolution');
615
210fdecd
FC
616 *Color::H1 = *Colour::H1;
617
618 is(Color::H1->getline(), <DATA>,
619 'file handles take priority after typeglob-to-typeglob assignment');
620
6f908f1b
NC
621 seek Colour::H1, $fh_start, Fcntl::SEEK_SET() or die $!;
622 seek DATA, $data_start, Fcntl::SEEK_SET() or die $!;
623
624 is(Colour::H1->getline(), <DATA>, 'read from a file');
625 is(C3::H1->getline(), 'method in C3::H1', 'intial resolution is a method');
626
627 *Copy:: = \*C3::;
628 *C3:: = \*Colour::;
629
630 is(Colour::H1->getline(), <DATA>, 'read from a file');
631 is(C3::H1->getline(), <DATA>,
632 'file handles take priority after stash aliasing');
633
634 *C3:: = \*Copy::;
635
636 is(C3::H1->getline(), 'method in C3::H1',
637 'restoring the stash returns to a method');
638}
639
640__END__
641#FF9900
642#F78C08
643#FFA500
644#FF4D00
645#FC5100
646#FF5D00