This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[perl #127494] TODO test for $AUTOLOAD being set for DESTROY
[perl5.git] / t / op / method.t
1 #!./perl -w
2
3 #
4 # test method calls and autoloading.
5 #
6
7 BEGIN {
8     chdir 't' if -d 't';
9     @INC = qw(. ../lib lib ../dist/base/lib);
10     require "./test.pl";
11 }
12
13 use strict;
14 no warnings 'once';
15
16 plan(tests => 150);
17
18 @A::ISA = 'B';
19 @B::ISA = 'C';
20
21 sub C::d {"C::d"}
22 sub D::d {"D::d"}
23
24 # First, some basic checks of method-calling syntax:
25 my $obj = bless [], "Pack";
26 sub Pack::method { shift; join(",", "method", @_) }
27 my $mname = "method";
28
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");
33
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");
40
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");
45
46 is($obj->method(0), "method,0");
47 is($obj->method(1), "method,1");
48
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");
55
56 is( A->d, "C::d");              # Update hash table;
57
58 *B::d = \&D::d;                 # Import now.
59 is(A->d, "D::d");               # Update hash table;
60
61 {
62     local @A::ISA = qw(C);      # Update hash table with split() assignment
63     is(A->d, "C::d");
64     $#A::ISA = -1;
65     is(eval { A->d } || "fail", "fail");
66 }
67 is(A->d, "D::d");
68
69 {
70     local *B::d;
71     eval 'sub B::d {"B::d1"}';  # Import now.
72     is(A->d, "B::d1");  # Update hash table;
73     undef &B::d;
74     is((eval { A->d }, ($@ =~ /Undefined subroutine/)), 1);
75 }
76
77 is(A->d, "D::d");               # Back to previous state
78
79 eval 'no warnings "redefine"; sub B::d {"B::d2"}';      # Import now.
80 is(A->d, "B::d2");              # Update hash table;
81
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.
85
86 undef &B::d;
87 delete $B::{d};
88 is(A->d, "C::d");
89
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;
94
95 eval 'sub B::d {"B::d3"}';      # Import now.
96 is(A->d, "B::d3");              # Update hash table;
97
98 delete $B::{d};
99 *dummy::dummy = sub {};         # Mark as updated
100 is(A->d, "C::d");
101
102 eval 'sub B::d {"B::d4"}';      # Import now.
103 is(A->d, "B::d4");              # Update hash table;
104
105 delete $B::{d};                 # Should work without any help too
106 is(A->d, "C::d");
107
108 {
109     local *C::d;
110     is(eval { A->d } || "nope", "nope");
111 }
112 is(A->d, "C::d");
113
114 *A::x = *A::d;
115 A->d;
116 is(eval { A->x } || "nope", "nope", 'cache should not follow synonyms');
117
118 my $counter;
119
120 eval <<'EOF';
121 sub C::e;
122 BEGIN { *B::e = \&C::e }        # Shouldn't prevent AUTOLOAD in original pkg
123 sub Y::f;
124 $counter = 0;
125
126 @X::ISA = 'Y';
127 @Y::ISA = 'B';
128
129 sub B::AUTOLOAD {
130   my $c = ++$counter;
131   my $method = $B::AUTOLOAD; 
132   my $msg = "B: In $method, $c";
133   eval "sub $method { \$msg }";
134   goto &$method;
135 }
136 sub C::AUTOLOAD {
137   my $c = ++$counter;
138   my $method = $C::AUTOLOAD; 
139   my $msg = "C: In $method, $c";
140   eval "sub $method { \$msg }";
141   goto &$method;
142 }
143 EOF
144
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
147
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
150
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
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
158 {
159 no warnings 'redefine';
160 *B::AUTOLOAD = sub {
161   use warnings;
162   my $c = ++$counter;
163   my $method = $::AUTOLOAD; 
164   no strict 'refs';
165   *$::AUTOLOAD = sub { "new B: In $method, $c" };
166   goto &$::AUTOLOAD;
167 };
168 }
169
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
172
173 # test that failed subroutine calls don't affect method calls
174 {
175     package A1;
176     sub foo { "foo" }
177     package A2;
178     @A2::ISA = 'A1';
179     package main;
180     is(A2->foo(), "foo");
181     is(do { eval 'A2::foo()'; $@ ? 1 : 0}, 1);
182     is(A2->foo(), "foo");
183 }
184
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.
189 #  {
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);
194 #  }
195
196 # test error messages if method loading fails
197 my $e;
198
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/);
203 eval 'E::C->foo()';
204 like ($@, qr/^\QCan't locate object method "foo" via package "E::C" (perhaps /);
205
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 /);
210
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/);
216
217 # SUPER:: pseudoclass
218 @Saab::ISA = "Souper";
219 sub Souper::method { @_ }
220 @OtherSaab::ISA = "OtherSouper";
221 sub OtherSouper::method { "Isidore Ropen, Draft Manager" }
222 {
223    my $o = bless [], "Saab";
224    package 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";
236 }  
237 () = *SUPER::;
238 {
239    local our @ISA = "Souper";
240    is eval { (main->SUPER::method)[0] }, 'main',
241       'Mentioning *SUPER:: does not stop ->SUPER from working in main';
242 }
243 {
244     BEGIN {
245         *Mover:: = *Mover2::;
246         *Mover2:: = *foo;
247     }
248     package Mover;
249     no strict;
250     # Not our(@ISA), because the bug we are testing for interacts with an
251     # our() bug that cancels this bug out.
252     @ISA = 'door';
253     sub door::dohtem { 'dohtem' }
254     ::is eval { Mover->SUPER::dohtem; }, 'dohtem',
255         'SUPER inside moved package';
256     undef *door::dohtem;
257     *door::dohtem = sub { 'method' };
258     ::is eval { Mover->SUPER::dohtem; }, 'method',
259         'SUPER inside moved package respects method changes';
260 }
261
262 package foo120694 {
263     BEGIN { our @ISA = qw(bar120694) }
264
265     sub AUTOLOAD {
266         my $self = shift;
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(@_);
272     }
273 }
274 package bar120694 {
275     sub AUTOLOAD {
276         return "xyzzy";
277     }
278 }
279 is bless( [] => "foo120694" )->plugh, 'xyzzy',
280     '->SUPER::method autoloading uses parent of current pkg';
281
282
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
286
287 is( UNIVERSAL::can("Foo", "boogie") ? "yes":"no", "no" );
288 is( $::{"Foo::"} || "none", "none");  # still missing?
289
290 is( Foo->UNIVERSAL::can("boogie")   ? "yes":"no", "no" );
291 is( $::{"Foo::"} || "none", "none");  # still missing?
292
293 is( Foo->can("boogie")              ? "yes":"no", "no" );
294 is( $::{"Foo::"} || "none", "none");  # still missing?
295
296 is( eval 'Foo->boogie(); 1'         ? "yes":"no", "no" );
297 is( $::{"Foo::"} || "none", "none");  # still missing?
298
299 is(do { eval 'Foo->boogie()';
300           $@ =~ /^\QCan't locate object method "boogie" via package "Foo" (perhaps / ? 1 : $@}, 1);
301
302 eval 'sub Foo::boogie { "yes, sir!" }';
303 is( $::{"Foo::"} ? "ok" : "none", "ok");  # should exist now
304 is( Foo->boogie(), "yes, sir!");
305
306 # TODO: universal.t should test NoSuchPackage->isa()/can()
307
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"; }';
312 ok(1);
313
314 # Bug ID 20010902.002
315 is(
316     eval q[
317         my $x = 'x'; # Lexical or package variable, 5.6.1 panics.
318         sub Foo::x : lvalue { $x }
319         Foo->$x = 'ok';
320     ] || $@, 'ok'
321 );
322
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>
326
327 {
328     use warnings;
329     my $w = '';
330     local $SIG{__WARN__} = sub { $w = $_[0] };
331
332     sub AutoDest::Base::AUTOLOAD {}
333     @AutoDest::ISA = qw(AutoDest::Base);
334     { my $x = bless {}, 'AutoDest'; }
335     $w =~ s/\n//g;
336     is($w, '');
337 }
338
339 # [ID 20020305.025] PACKAGE::SUPER doesn't work anymore
340
341 package main;
342 our @X;
343 package Amajor;
344 sub test {
345     push @main::X, 'Amajor', @_;
346 }
347 package Bminor;
348 use base qw(Amajor);
349 package main;
350 sub Bminor::test {
351     $_[0]->Bminor::SUPER::test('x', 'y');
352     push @main::X, 'Bminor', @_;
353 }
354 Bminor->test('y', 'z');
355 is("@X", "Amajor Bminor x y Bminor Bminor y z");
356
357 package main;
358 for my $meth (['Bar', 'Foo::Bar'],
359               ['SUPER::Bar', 'main::SUPER::Bar'],
360               ['Xyz::SUPER::Bar', 'Xyz::SUPER::Bar'])
361 {
362     fresh_perl_is(<<EOT,
363 package UNIVERSAL; sub AUTOLOAD { my \$c = shift; print "\$c \$AUTOLOAD\\n" }
364 sub DESTROY {} # prevent AUTOLOAD being called on DESTROY
365 package Xyz;
366 package main; Foo->$meth->[0]();
367 EOT
368         "Foo $meth->[1]",
369         { switches => [ '-w' ] },
370         "check if UNIVERSAL::AUTOLOAD works",
371     );
372 }
373
374 # Test for #71952: crash when looking for a nonexistent destructor
375 # Regression introduced by fbb3ee5af3d4
376 {
377     fresh_perl_is(<<'EOT',
378 sub M::DESTROY; bless {}, "M" ; print "survived\n";
379 EOT
380     "survived",
381     {},
382         "no crash with a declared but missing DESTROY method"
383     );
384 }
385
386 # Test for calling a method on a packag name return by a magic variable
387 sub TIESCALAR{bless[]}
388 sub FETCH{"main"}
389 my $kalled;
390 sub bolgy { ++$kalled; }
391 tie my $a, "";
392 $a->bolgy;
393 is $kalled, 1, 'calling a class method via a magic variable';
394
395 {
396     package NulTest;
397     sub method { 1 }
398
399     package main;
400     eval {
401         NulTest->${ \"method\0Whoops" };
402     };
403     like $@, qr/Can't locate object method "method\0Whoops" via package "NulTest" at/,
404             "method lookup is nul-clean";
405
406     *NulTest::AUTOLOAD = sub { our $AUTOLOAD; return $AUTOLOAD };
407
408     like(NulTest->${ \"nul\0test" }, qr/nul\0test/, "AUTOLOAD is nul-clean");
409 }
410
411
412 {
413     fresh_perl_is(
414     q! sub T::DESTROY { $x = $_[0]; } bless [], "T";!,
415     "DESTROY created new reference to dead object 'T' during global destruction.",
416     {},
417         "DESTROY creating a new reference to the object generates a warning."
418     );
419 }
420
421 # [perl #43663]
422 {
423     $::{"Just"} = \1;
424     sub Just::a_japh { return "$_[0] another Perl hacker," }
425     is eval { "Just"->a_japh }, "Just another Perl hacker,",
426         'constants do not interfere with class methods';
427 }
428
429 # [perl #109264]
430 {
431     no strict 'vars';
432     sub bliggles { 1 }
433     sub lbiggles :lvalue { index "foo", "f" }
434     ok eval { main->bliggles(my($foo,$bar)) },
435       'foo->bar(my($foo,$bar)) is not called in lvalue context';
436     ok eval { main->bliggles(our($foo,$bar)) },
437       'foo->bar(our($foo,$bar)) is not called in lvalue context';
438     ok eval { main->bliggles(local($foo,$bar)) },
439       'foo->bar(local($foo,$bar)) is not called in lvalue context';
440     ok eval { () = main->lbiggles(my($foo,$bar)); 1 },
441       'foo->lv(my($foo,$bar)) is not called in lvalue context';
442     ok eval { () = main->lbiggles(our($foo,$bar)); 1 },
443       'foo->lv(our($foo,$bar)) is not called in lvalue context';
444     ok eval { () = main->lbiggles(local($foo,$bar)); 1 },
445       'foo->lv(local($foo,$bar)) is not called in lvalue context';
446 }
447
448 {
449    # AUTOLOAD and DESTROY can be declared without a leading sub,
450    # like BEGIN and friends.
451    package NoSub;
452
453    eval 'AUTOLOAD { our $AUTOLOAD; return $AUTOLOAD }';
454    ::ok( !$@, "AUTOLOAD without a leading sub is legal" );
455
456    eval "DESTROY { ::pass( q!DESTROY without a leading sub is legal and gets called! ) }";
457    {
458       ::ok( NoSub->can("AUTOLOAD"), "...and sets up an AUTOLOAD normally" );
459       ::is( eval { NoSub->bluh }, "NoSub::bluh", "...which works as expected" );
460    }
461    { bless {}, "NoSub"; }
462 }
463
464 {
465     # [perl #124387]
466     my $autoloaded;
467     package AutoloadDestroy;
468     sub AUTOLOAD { $autoloaded = 1 }
469     package main;
470     bless {}, "AutoloadDestroy";
471     ok($autoloaded, "AUTOLOAD called for DESTROY");
472
473     # 127494 - AUTOLOAD for DESTROY was called without setting $AUTOLOAD
474     local $::TODO = "caching of AUTOLOAD for DESTROY didn't set \$AUTOLOAD";
475     my %methods;
476     package AutoloadDestroy2;
477     sub AUTOLOAD {
478         our $AUTOLOAD;
479         (my $method = $AUTOLOAD) =~ s/.*:://;
480         ++$methods{$method};
481     }
482     package main;
483     # this cached AUTOLOAD as the DESTROY method
484     bless {}, "AutoloadDestroy2";
485     %methods = ();
486     my $o = bless {}, "AutoloadDestroy2";
487     # this sets $AUTOLOAD to "AutoloadDestroy2::foo"
488     $o->foo;
489     # this would call AUTOLOAD without setting $AUTOLOAD
490     undef $o;
491     ok($methods{DESTROY}, "\$AUTOLOAD set correctly for DESTROY");
492 }
493
494 eval { () = 3; new {} };
495 like $@,
496      qr/^Can't call method "new" without a package or object reference/,
497     'Err msg from new{} when stack contains a number';
498 eval { () = "foo"; new {} };
499 like $@,
500      qr/^Can't call method "new" without a package or object reference/,
501     'Err msg from new{} when stack contains a word';
502 eval { () = undef; new {} };
503 like $@,
504      qr/^Can't call method "new" without a package or object reference/,
505     'Err msg from new{} when stack contains undef';
506
507 package egakacp {
508   our @ISA = 'ASI';
509   sub ASI::m { shift; "@_" };
510   my @a = (bless([]), 'arg');
511   my $r = SUPER::m{@a};
512   ::is $r, 'arg', 'method{@array}';
513   $r = SUPER::m{}@a;
514   ::is $r, 'arg', 'method{}@array';
515   $r = SUPER::m{@a}"b";
516   ::is $r, 'arg b', 'method{@array}$more_args';
517 }
518
519 # [perl #114924] SUPER->method
520 @SUPER::ISA = "SUPPER";
521 sub SUPPER::foo { "supper" }
522 is "SUPER"->foo, 'supper', 'SUPER->method';
523
524 sub flomp { "flimp" }
525 sub main::::flomp { "flump" }
526 is "::"->flomp, 'flump', 'method call on ::';
527 is "::main"->flomp, 'flimp', 'method call on ::main';
528 eval { ""->flomp };
529 like $@,
530      qr/^Can't call method "flomp" without a package or object reference/,
531     'method call on empty string';
532 is "3foo"->CORE::uc, '3FOO', '"3foo"->CORE::uc';
533 { no strict; @{"3foo::ISA"} = "CORE"; }
534 is "3foo"->uc, '3FOO', '"3foo"->uc (autobox style!)';
535
536 # *foo vs (\*foo)
537 sub myclass::squeak { 'eek' }
538 eval { *myclass->squeak };
539 like $@,
540      qr/^Can't call method "squeak" without a package or object reference/,
541     'method call on typeglob ignores package';
542 eval { (\*myclass)->squeak };
543 like $@,
544      qr/^Can't call method "squeak" on unblessed reference/,
545     'method call on \*typeglob';
546 *stdout2 = *STDOUT;  # stdout2 now stringifies as *main::STDOUT
547 sub IO::Handle::self { $_[0] }
548 # This used to stringify the glob:
549 is *stdout2->self, (\*stdout2)->self,
550   '*glob->method is equiv to (\*glob)->method';
551 sub { $_[0] = *STDOUT; is $_[0]->self, \$::h{k}, '$pvlv_glob->method' }
552  ->($::h{k});
553
554 # Test that PL_stashcache doesn't change the resolution behaviour for file
555 # handles and package names.
556 SKIP: {
557     skip_if_miniperl('file handles as methods requires loading IO::File', 26);
558     require Fcntl;
559
560     foreach (qw (Count::DATA Count Colour::H1 Color::H1 C3::H1)) {
561         eval qq{
562             package $_;
563
564             sub getline {
565                 return "method in $_";
566             }
567
568             1;
569         } or die $@;
570     }
571
572     BEGIN {
573         *The::Count:: = \*Count::;
574     }
575
576     is(Count::DATA->getline(), 'method in Count::DATA',
577        'initial resolution is a method');
578     is(The::Count::DATA->getline(), 'method in Count::DATA',
579        'initial resolution is a method in aliased classes');
580
581     require Count;
582
583     is(Count::DATA->getline(), "one! ha ha ha\n", 'file handles take priority');
584     is(The::Count::DATA->getline(), "two! ha ha ha\n",
585        'file handles take priority in aliased classes');
586
587     eval q{close Count::DATA} or die $!;
588
589     {
590         no warnings 'io';
591         is(Count::DATA->getline(), undef,
592            "closing a file handle doesn't change object resolution");
593         is(The::Count::DATA->getline(), undef,
594            "closing a file handle doesn't change object resolution in aliased classes");
595 }
596
597     undef *Count::DATA;
598     is(Count::DATA->getline(), 'method in Count::DATA',
599        'undefining the typeglob does change object resolution');
600     is(The::Count::DATA->getline(), 'method in Count::DATA',
601        'undefining the typeglob does change object resolution in aliased classes');
602
603     is(Count->getline(), 'method in Count',
604        'initial resolution is a method');
605     is(The::Count->getline(), 'method in Count',
606        'initial resolution is a method in aliased classes');
607
608     eval q{
609         open Count, '<', $INC{'Count.pm'}
610             or die "Can't open $INC{'Count.pm'}: $!";
611 1;
612     } or die $@;
613
614     is(Count->getline(), "# zero! ha ha ha\n", 'file handles take priority');
615     is(The::Count->getline(), 'method in Count', 'but not in an aliased class');
616
617     eval q{close Count} or die $!;
618
619     {
620         no warnings 'io';
621         is(Count->getline(), undef,
622            "closing a file handle doesn't change object resolution");
623     }
624
625     undef *Count;
626     is(Count->getline(), 'method in Count',
627        'undefining the typeglob does change object resolution');
628
629     open Colour::H1, 'op/method.t' or die $!;
630     while (<Colour::H1>) {
631         last if /^__END__/;
632     }
633     open CLOSED, 'TEST' or die $!;
634     close CLOSED or die $!;
635
636     my $fh_start = tell Colour::H1;
637     my $data_start = tell DATA;
638     is(Colour::H1->getline(), <DATA>, 'read from a file');
639     is(Color::H1->getline(), 'method in Color::H1',
640        'initial resolution is a method');
641
642     *Color::H1 = *Colour::H1{IO};
643
644     is(Colour::H1->getline(), <DATA>, 'read from a file');
645     is(Color::H1->getline(), <DATA>,
646        'file handles take priority after io-to-typeglob assignment');
647
648     *Color::H1 = *CLOSED{IO};
649     {
650         no warnings 'io';
651         is(Color::H1->getline(), undef,
652            "assigning a closed a file handle doesn't change object resolution");
653     }
654
655     undef *Color::H1;
656     is(Color::H1->getline(), 'method in Color::H1',
657        'undefining the typeglob does change object resolution');
658
659     *Color::H1 = *Colour::H1;
660
661     is(Color::H1->getline(), <DATA>,
662        'file handles take priority after typeglob-to-typeglob assignment');
663
664     seek Colour::H1, $fh_start, Fcntl::SEEK_SET() or die $!;
665     seek DATA, $data_start, Fcntl::SEEK_SET() or die $!;
666
667     is(Colour::H1->getline(), <DATA>, 'read from a file');
668     is(C3::H1->getline(), 'method in C3::H1', 'initial resolution is a method');
669
670     *Copy:: = \*C3::;
671     *C3:: = \*Colour::;
672
673     is(Colour::H1->getline(), <DATA>, 'read from a file');
674     is(C3::H1->getline(), <DATA>,
675        'file handles take priority after stash aliasing');
676
677     *C3:: = \*Copy::;
678
679     is(C3::H1->getline(), 'method in C3::H1',
680        'restoring the stash returns to a method');
681 }
682
683 # RT #123619 constant class name should be read-only
684
685 {
686     sub RT123619::f { chop $_[0] }
687     eval { 'RT123619'->f(); };
688     like ($@, qr/Modification of a read-only value attempted/, 'RT #123619');
689 }
690
691 {
692     # RT #126042 &{1==1} * &{1==1} would crash
693
694     # pp_entersub and pp_method_named cooperate to prevent calls to an
695     # undefined import() or unimport() method from croaking.
696     # If pp_method_named can't find the method it pushes &PL_sv_yes, and
697     # pp_entersub checks for that specific SV to avoid croaking.
698     # Ideally they wouldn't use that hack but...
699     # Unfortunately pp_entersub's handling of that case is broken in scalar context.
700
701     # Rather than using the test case from the ticket, since &{1==1}
702     # isn't documented (and may not be supported in future perls) test
703     # calls to undefined import method, which also crashes.
704     fresh_perl_is('Unknown->import() * Unknown->unimport(); print "ok\n"', "ok\n", {},
705                   "check unknown import() methods don't corrupt the stack");
706 }
707
708 __END__
709 #FF9900
710 #F78C08
711 #FFA500
712 #FF4D00
713 #FC5100
714 #FF5D00