This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
t/op/local.t: tests for RT #7615
[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';
1ae3d757 9 require "./test.pl";
624c42e2 10 set_up_inc( qw(. ../lib lib ../dist/base/lib) );
4755096e
GS
11}
12
9bfdb36e
NC
13use strict;
14no warnings 'once';
15
89bfd824 16plan(tests => 150);
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
f6ec51f7
GS
173# test that failed subroutine calls don't affect method calls
174{
175 package A1;
176 sub foo { "foo" }
177 package A2;
9bfdb36e 178 @A2::ISA = 'A1';
f6ec51f7 179 package main;
6dc4e5ce
JH
180 is(A2->foo(), "foo");
181 is(do { eval 'A2::foo()'; $@ ? 1 : 0}, 1);
182 is(A2->foo(), "foo");
f6ec51f7 183}
c1899e02 184
af09ea45
IK
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# {
6dc4e5ce 190# is(do { use Config; eval 'Config->foo()';
af09ea45 191# $@ =~ /^\QCan't locate object method "foo" via package "Config" at/ ? 1 : $@}, 1);
6dc4e5ce 192# is(do { use Config; eval '$d = bless {}, "Config"; $d->foo()';
af09ea45
IK
193# $@ =~ /^\QCan't locate object method "foo" via package "Config" at/ ? 1 : $@}, 1);
194# }
195
af09ea45 196# test error messages if method loading fails
9bfdb36e
NC
197my $e;
198
2f907243
NC
199eval '$e = bless {}, "E::A"; E::A->foo()';
200like ($@, qr/^\QCan't locate object method "foo" via package "E::A" at/);
201eval '$e = bless {}, "E::B"; $e->foo()';
202like ($@, qr/^\QCan't locate object method "foo" via package "E::B" at/);
203eval 'E::C->foo()';
204like ($@, qr/^\QCan't locate object method "foo" via package "E::C" (perhaps /);
205
206eval 'UNIVERSAL->E::D::foo()';
207like ($@, qr/^\QCan't locate object method "foo" via package "E::D" (perhaps /);
9bfdb36e 208eval 'my $e = bless {}, "UNIVERSAL"; $e->E::E::foo()';
2f907243 209like ($@, qr/^\QCan't locate object method "foo" via package "E::E" (perhaps /);
af09ea45
IK
210
211$e = bless {}, "E::F"; # force package to exist
2f907243
NC
212eval 'UNIVERSAL->E::F::foo()';
213like ($@, qr/^\QCan't locate object method "foo" via package "E::F" at/);
214eval '$e = bless {}, "UNIVERSAL"; $e->E::F::foo()';
215like ($@, qr/^\QCan't locate object method "foo" via package "E::F" at/);
af09ea45 216
697efb9b
FC
217# SUPER:: pseudoclass
218@Saab::ISA = "Souper";
219sub Souper::method { @_ }
220@OtherSaab::ISA = "OtherSouper";
221sub 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}
3c104e59
FC
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}
0308a534
FC
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';
37b0b3b2
FC
256 undef *door::dohtem;
257 *door::dohtem = sub { 'method' };
258 ::is eval { Mover->SUPER::dohtem; }, 'method',
259 'SUPER inside moved package respects method changes';
0308a534 260}
af09ea45 261
257dc59d
FC
262package 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}
274package bar120694 {
275 sub AUTOLOAD {
276 return "xyzzy";
277 }
278}
279is bless( [] => "foo120694" )->plugh, 'xyzzy',
280 '->SUPER::method autoloading uses parent of current pkg';
281
282
af09ea45 283# failed method call or UNIVERSAL::can() should not autovivify packages
6dc4e5ce
JH
284is( $::{"Foo::"} || "none", "none"); # sanity check 1
285is( $::{"Foo::"} || "none", "none"); # sanity check 2
c1899e02 286
6dc4e5ce
JH
287is( UNIVERSAL::can("Foo", "boogie") ? "yes":"no", "no" );
288is( $::{"Foo::"} || "none", "none"); # still missing?
af09ea45 289
6dc4e5ce
JH
290is( Foo->UNIVERSAL::can("boogie") ? "yes":"no", "no" );
291is( $::{"Foo::"} || "none", "none"); # still missing?
af09ea45 292
6dc4e5ce
JH
293is( Foo->can("boogie") ? "yes":"no", "no" );
294is( $::{"Foo::"} || "none", "none"); # still missing?
af09ea45 295
6dc4e5ce
JH
296is( eval 'Foo->boogie(); 1' ? "yes":"no", "no" );
297is( $::{"Foo::"} || "none", "none"); # still missing?
af09ea45 298
6dc4e5ce 299is(do { eval 'Foo->boogie()';
af09ea45
IK
300 $@ =~ /^\QCan't locate object method "boogie" via package "Foo" (perhaps / ? 1 : $@}, 1);
301
302eval 'sub Foo::boogie { "yes, sir!" }';
6dc4e5ce
JH
303is( $::{"Foo::"} ? "ok" : "none", "ok"); # should exist now
304is( Foo->boogie(), "yes, sir!");
af09ea45
IK
305
306# TODO: universal.t should test NoSuchPackage->isa()/can()
c1899e02 307
f0670693
SC
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.
84251760 311eval 'sub AUTOLOAD { "ok ", shift, "\n"; }';
6dc4e5ce 312ok(1);
af09ea45 313
ee95e30c 314# Bug ID 20010902.002 (#7609)
6dc4e5ce 315is(
a397c3d9 316 eval q[
9bfdb36e 317 my $x = 'x'; # Lexical or package variable, 5.6.1 panics.
a397c3d9
RGS
318 sub Foo::x : lvalue { $x }
319 Foo->$x = 'ok';
320 ] || $@, 'ok'
321);
322
3ad83ce7
AMS
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;
6dc4e5ce 336 is($w, '');
3ad83ce7
AMS
337}
338
ee95e30c 339# [ID 20020305.025 (#8788)] PACKAGE::SUPER doesn't work anymore
e189a56d
IK
340
341package main;
342our @X;
343package Amajor;
344sub test {
345 push @main::X, 'Amajor', @_;
346}
347package Bminor;
348use base qw(Amajor);
349package main;
350sub Bminor::test {
351 $_[0]->Bminor::SUPER::test('x', 'y');
352 push @main::X, 'Bminor', @_;
353}
354Bminor->test('y', 'z');
355is("@X", "Amajor Bminor x y Bminor Bminor y z");
356
0dae17bd
GS
357package main;
358for my $meth (['Bar', 'Foo::Bar'],
359 ['SUPER::Bar', 'main::SUPER::Bar'],
360 ['Xyz::SUPER::Bar', 'Xyz::SUPER::Bar'])
361{
362 fresh_perl_is(<<EOT,
363package UNIVERSAL; sub AUTOLOAD { my \$c = shift; print "\$c \$AUTOLOAD\\n" }
000814da 364sub DESTROY {} # prevent AUTOLOAD being called on DESTROY
0dae17bd
GS
365package Xyz;
366package main; Foo->$meth->[0]();
367EOT
368 "Foo $meth->[1]",
369 { switches => [ '-w' ] },
370 "check if UNIVERSAL::AUTOLOAD works",
371 );
372}
1f15e670
NT
373
374# Test for #71952: crash when looking for a nonexistent destructor
375# Regression introduced by fbb3ee5af3d4
376{
377 fresh_perl_is(<<'EOT',
378sub M::DESTROY; bless {}, "M" ; print "survived\n";
379EOT
380 "survived",
381 {},
382 "no crash with a declared but missing DESTROY method"
383 );
384}
385
da6b625f
FC
386# Test for calling a method on a packag name return by a magic variable
387sub TIESCALAR{bless[]}
388sub FETCH{"main"}
389my $kalled;
390sub bolgy { ++$kalled; }
391tie my $a, "";
392$a->bolgy;
393is $kalled, 1, 'calling a class method via a magic variable';
f937af42
BF
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
aaa63dae 408 like(NulTest->${ \"nul\0test" }, qr/nul\0test/, "AUTOLOAD is nul-clean");
f937af42 409}
2a0f5ef0
BF
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}
71481574
FC
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}
0865059d
FC
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}
544b72e2
BF
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}
f226e9be 463
f05081b8
TC
464{
465 # [perl #124387]
f05081b8
TC
466 my $autoloaded;
467 package AutoloadDestroy;
468 sub AUTOLOAD { $autoloaded = 1 }
469 package main;
470 bless {}, "AutoloadDestroy";
471 ok($autoloaded, "AUTOLOAD called for DESTROY");
7db8c4f1
TC
472
473 # 127494 - AUTOLOAD for DESTROY was called without setting $AUTOLOAD
7db8c4f1
TC
474 my %methods;
475 package AutoloadDestroy2;
476 sub AUTOLOAD {
477 our $AUTOLOAD;
478 (my $method = $AUTOLOAD) =~ s/.*:://;
479 ++$methods{$method};
480 }
481 package main;
482 # this cached AUTOLOAD as the DESTROY method
483 bless {}, "AutoloadDestroy2";
484 %methods = ();
485 my $o = bless {}, "AutoloadDestroy2";
486 # this sets $AUTOLOAD to "AutoloadDestroy2::foo"
487 $o->foo;
488 # this would call AUTOLOAD without setting $AUTOLOAD
489 undef $o;
490 ok($methods{DESTROY}, "\$AUTOLOAD set correctly for DESTROY");
f05081b8
TC
491}
492
f226e9be
FC
493eval { () = 3; new {} };
494like $@,
495 qr/^Can't call method "new" without a package or object reference/,
496 'Err msg from new{} when stack contains a number';
497eval { () = "foo"; new {} };
498like $@,
499 qr/^Can't call method "new" without a package or object reference/,
500 'Err msg from new{} when stack contains a word';
501eval { () = undef; new {} };
502like $@,
503 qr/^Can't call method "new" without a package or object reference/,
504 'Err msg from new{} when stack contains undef';
bfde49d4
FC
505
506package egakacp {
507 our @ISA = 'ASI';
508 sub ASI::m { shift; "@_" };
509 my @a = (bless([]), 'arg');
510 my $r = SUPER::m{@a};
511 ::is $r, 'arg', 'method{@array}';
512 $r = SUPER::m{}@a;
513 ::is $r, 'arg', 'method{}@array';
514 $r = SUPER::m{@a}"b";
515 ::is $r, 'arg b', 'method{@array}$more_args';
516}
aae43805
FC
517
518# [perl #114924] SUPER->method
519@SUPER::ISA = "SUPPER";
520sub SUPPER::foo { "supper" }
521is "SUPER"->foo, 'supper', 'SUPER->method';
7156e69a
FC
522
523sub flomp { "flimp" }
524sub main::::flomp { "flump" }
525is "::"->flomp, 'flump', 'method call on ::';
526is "::main"->flomp, 'flimp', 'method call on ::main';
527eval { ""->flomp };
528like $@,
529 qr/^Can't call method "flomp" without a package or object reference/,
530 'method call on empty string';
531is "3foo"->CORE::uc, '3FOO', '"3foo"->CORE::uc';
532{ no strict; @{"3foo::ISA"} = "CORE"; }
533is "3foo"->uc, '3FOO', '"3foo"->uc (autobox style!)';
6f908f1b 534
a77c16f7
FC
535# *foo vs (\*foo)
536sub myclass::squeak { 'eek' }
537eval { *myclass->squeak };
538like $@,
539 qr/^Can't call method "squeak" without a package or object reference/,
540 'method call on typeglob ignores package';
541eval { (\*myclass)->squeak };
542like $@,
543 qr/^Can't call method "squeak" on unblessed reference/,
544 'method call on \*typeglob';
545*stdout2 = *STDOUT; # stdout2 now stringifies as *main::STDOUT
624c42e2 546 sub IO::Handle::self { $_[0] }
a77c16f7
FC
547# This used to stringify the glob:
548is *stdout2->self, (\*stdout2)->self,
549 '*glob->method is equiv to (\*glob)->method';
550sub { $_[0] = *STDOUT; is $_[0]->self, \$::h{k}, '$pvlv_glob->method' }
551 ->($::h{k});
552
6f908f1b
NC
553# Test that PL_stashcache doesn't change the resolution behaviour for file
554# handles and package names.
555SKIP: {
b87eccc1 556 skip_if_miniperl('file handles as methods requires loading IO::File', 26);
6f908f1b
NC
557 require Fcntl;
558
559 foreach (qw (Count::DATA Count Colour::H1 Color::H1 C3::H1)) {
560 eval qq{
561 package $_;
562
563 sub getline {
564 return "method in $_";
565 }
566
567 1;
568 } or die $@;
569 }
570
571 BEGIN {
572 *The::Count:: = \*Count::;
573 }
574
575 is(Count::DATA->getline(), 'method in Count::DATA',
576 'initial resolution is a method');
577 is(The::Count::DATA->getline(), 'method in Count::DATA',
578 'initial resolution is a method in aliased classes');
579
580 require Count;
581
582 is(Count::DATA->getline(), "one! ha ha ha\n", 'file handles take priority');
583 is(The::Count::DATA->getline(), "two! ha ha ha\n",
584 'file handles take priority in aliased classes');
585
586 eval q{close Count::DATA} or die $!;
587
588 {
589 no warnings 'io';
590 is(Count::DATA->getline(), undef,
591 "closing a file handle doesn't change object resolution");
592 is(The::Count::DATA->getline(), undef,
593 "closing a file handle doesn't change object resolution in aliased classes");
594}
595
596 undef *Count::DATA;
597 is(Count::DATA->getline(), 'method in Count::DATA',
598 'undefining the typeglob does change object resolution');
599 is(The::Count::DATA->getline(), 'method in Count::DATA',
600 'undefining the typeglob does change object resolution in aliased classes');
601
602 is(Count->getline(), 'method in Count',
603 'initial resolution is a method');
604 is(The::Count->getline(), 'method in Count',
605 'initial resolution is a method in aliased classes');
606
607 eval q{
608 open Count, '<', $INC{'Count.pm'}
609 or die "Can't open $INC{'Count.pm'}: $!";
6101;
611 } or die $@;
612
613 is(Count->getline(), "# zero! ha ha ha\n", 'file handles take priority');
614 is(The::Count->getline(), 'method in Count', 'but not in an aliased class');
615
616 eval q{close Count} or die $!;
617
618 {
619 no warnings 'io';
620 is(Count->getline(), undef,
621 "closing a file handle doesn't change object resolution");
622 }
623
624 undef *Count;
625 is(Count->getline(), 'method in Count',
626 'undefining the typeglob does change object resolution');
627
628 open Colour::H1, 'op/method.t' or die $!;
629 while (<Colour::H1>) {
630 last if /^__END__/;
631 }
632 open CLOSED, 'TEST' or die $!;
633 close CLOSED or die $!;
634
635 my $fh_start = tell Colour::H1;
636 my $data_start = tell DATA;
637 is(Colour::H1->getline(), <DATA>, 'read from a file');
638 is(Color::H1->getline(), 'method in Color::H1',
639 'initial resolution is a method');
640
641 *Color::H1 = *Colour::H1{IO};
642
643 is(Colour::H1->getline(), <DATA>, 'read from a file');
644 is(Color::H1->getline(), <DATA>,
210fdecd 645 'file handles take priority after io-to-typeglob assignment');
6f908f1b
NC
646
647 *Color::H1 = *CLOSED{IO};
648 {
649 no warnings 'io';
650 is(Color::H1->getline(), undef,
651 "assigning a closed a file handle doesn't change object resolution");
652 }
653
654 undef *Color::H1;
655 is(Color::H1->getline(), 'method in Color::H1',
656 'undefining the typeglob does change object resolution');
657
210fdecd
FC
658 *Color::H1 = *Colour::H1;
659
660 is(Color::H1->getline(), <DATA>,
661 'file handles take priority after typeglob-to-typeglob assignment');
662
6f908f1b
NC
663 seek Colour::H1, $fh_start, Fcntl::SEEK_SET() or die $!;
664 seek DATA, $data_start, Fcntl::SEEK_SET() or die $!;
665
666 is(Colour::H1->getline(), <DATA>, 'read from a file');
5c25e937 667 is(C3::H1->getline(), 'method in C3::H1', 'initial resolution is a method');
6f908f1b
NC
668
669 *Copy:: = \*C3::;
670 *C3:: = \*Colour::;
671
672 is(Colour::H1->getline(), <DATA>, 'read from a file');
673 is(C3::H1->getline(), <DATA>,
674 'file handles take priority after stash aliasing');
675
676 *C3:: = \*Copy::;
677
678 is(C3::H1->getline(), 'method in C3::H1',
679 'restoring the stash returns to a method');
680}
681
b99dfd93
DM
682# RT #123619 constant class name should be read-only
683
684{
685 sub RT123619::f { chop $_[0] }
686 eval { 'RT123619'->f(); };
687 like ($@, qr/Modification of a read-only value attempted/, 'RT #123619');
688}
689
0cd52e23
TC
690{
691 # RT #126042 &{1==1} * &{1==1} would crash
692
693 # pp_entersub and pp_method_named cooperate to prevent calls to an
694 # undefined import() or unimport() method from croaking.
695 # If pp_method_named can't find the method it pushes &PL_sv_yes, and
696 # pp_entersub checks for that specific SV to avoid croaking.
697 # Ideally they wouldn't use that hack but...
698 # Unfortunately pp_entersub's handling of that case is broken in scalar context.
699
700 # Rather than using the test case from the ticket, since &{1==1}
701 # isn't documented (and may not be supported in future perls) test
702 # calls to undefined import method, which also crashes.
703 fresh_perl_is('Unknown->import() * Unknown->unimport(); print "ok\n"', "ok\n", {},
704 "check unknown import() methods don't corrupt the stack");
705}
706
6f908f1b
NC
707__END__
708#FF9900
709#F78C08
710#FFA500
711#FF4D00
712#FC5100
713#FF5D00