24 our @ISA = qw/MRO_A MRO_B MRO_C/;
26 our @ISA = qw/MRO_A MRO_B MRO_C/;
28 our @ISA = qw/MRO_D MRO_E/;
31 my @MFO_F_DFS = qw/MRO_F MRO_D MRO_A MRO_B MRO_C MRO_E/;
32 my @MFO_F_C3 = qw/MRO_F MRO_D MRO_E MRO_A MRO_B MRO_C/;
33 is(mro::get_mro('MRO_F'), 'dfs');
35 mro::get_linear_isa('MRO_F'), \@MFO_F_DFS
38 ok(eq_array(mro::get_linear_isa('MRO_F', 'dfs'), \@MFO_F_DFS));
39 ok(eq_array(mro::get_linear_isa('MRO_F', 'c3'), \@MFO_F_C3));
40 eval{mro::get_linear_isa('MRO_F', 'C3')};
41 like($@, qr/^Invalid mro name: 'C3'/);
43 mro::set_mro('MRO_F', 'c3');
44 is(mro::get_mro('MRO_F'), 'c3');
46 mro::get_linear_isa('MRO_F'), \@MFO_F_C3
49 ok(eq_array(mro::get_linear_isa('MRO_F', 'dfs'), \@MFO_F_DFS));
50 ok(eq_array(mro::get_linear_isa('MRO_F', 'c3'), \@MFO_F_C3));
51 eval{mro::get_linear_isa('MRO_F', 'C3')};
52 like($@, qr/^Invalid mro name: 'C3'/);
54 my @isarev = sort { $a cmp $b } @{mro::get_isarev('MRO_B')};
57 [qw/MRO_D MRO_E MRO_F/]
60 ok(!mro::is_universal('MRO_B'));
62 @UNIVERSAL::ISA = qw/MRO_F/;
63 ok(mro::is_universal('MRO_B'));
66 ok(!mro::is_universal('MRO_B'));
68 # is_universal, get_mro, and get_linear_isa should
69 # handle non-existent packages sanely
70 ok(!mro::is_universal('Does_Not_Exist'));
71 is(mro::get_mro('Also_Does_Not_Exist'), 'dfs');
73 mro::get_linear_isa('Does_Not_Exist_Three'),
74 [qw/Does_Not_Exist_Three/]
77 # Assigning @ISA via globref
80 sub testfunc { return 123 }
81 package MRO_TestOtherBase;
82 sub testfunctwo { return 321 }
83 package MRO_M; our @ISA = qw/MRO_TestBase/;
85 *MRO_N::ISA = *MRO_M::ISA;
86 is(eval { MRO_N->testfunc() }, 123);
88 # XXX TODO (when there's a way to backtrack through a glob's aliases)
89 # push(@MRO_M::ISA, 'MRO_TestOtherBase');
90 # is(eval { MRO_N->testfunctwo() }, 321);
92 # Simple DESTROY Baseline
98 package DESTROY_MRO_Baseline;
99 sub new { bless {} => shift }
102 package DESTROY_MRO_Baseline_Child;
103 our @ISA = qw/DESTROY_MRO_Baseline/;
106 $obj = DESTROY_MRO_Baseline->new();
110 $obj = DESTROY_MRO_Baseline_Child->new();
121 package DESTROY_MRO_Dynamic;
122 sub new { bless {} => shift }
124 package DESTROY_MRO_Dynamic_Child;
125 our @ISA = qw/DESTROY_MRO_Dynamic/;
128 $obj = DESTROY_MRO_Dynamic->new();
132 $obj = DESTROY_MRO_Dynamic_Child->new();
137 *DESTROY_MRO_Dynamic::DESTROY = sub { $x++ };
139 $obj = DESTROY_MRO_Dynamic->new();
143 $obj = DESTROY_MRO_Dynamic_Child->new();
148 # clearing @ISA in different ways
149 # some are destructive to the package, hence the new
150 # package name each time
152 no warnings 'uninitialized';
155 our @ISA = qw/XX YY ZZ/;
158 ok(eq_array(mro::get_linear_isa('ISACLEAR'),[qw/ISACLEAR XX YY ZZ/]));
160 # this looks dumb, but it preserves existing behavior for compatibility
161 # (undefined @ISA elements treated as "main")
162 $ISACLEAR::ISA[1] = undef;
163 ok(eq_array(mro::get_linear_isa('ISACLEAR'),[qw/ISACLEAR XX main ZZ/]));
165 # undef the array itself
166 undef @ISACLEAR::ISA;
167 ok(eq_array(mro::get_linear_isa('ISACLEAR'),[qw/ISACLEAR/]));
169 # Now, clear more than one package's @ISA at once
172 our @ISA = qw/WW XX/;
175 our @ISA = qw/YY ZZ/;
178 ok(eq_array(mro::get_linear_isa('ISACLEAR1'),[qw/ISACLEAR1 WW XX/]));
179 ok(eq_array(mro::get_linear_isa('ISACLEAR2'),[qw/ISACLEAR2 YY ZZ/]));
180 (@ISACLEAR1::ISA, @ISACLEAR2::ISA) = ();
182 ok(eq_array(mro::get_linear_isa('ISACLEAR1'),[qw/ISACLEAR1/]));
183 ok(eq_array(mro::get_linear_isa('ISACLEAR2'),[qw/ISACLEAR2/]));
185 # [perl #49564] This is a pretty obscure way of clearing @ISA but
186 # it tests a regression that affects XS code calling av_clear too.
189 our @ISA = qw/WW XX/;
191 ok(eq_array(mro::get_linear_isa('ISACLEAR3'),[qw/ISACLEAR3 WW XX/]));
196 ok(eq_array(mro::get_linear_isa('ISACLEAR3'),[qw/ISACLEAR3/]));
199 # Check that recursion bails out "cleanly" in a variety of cases
200 # (as opposed to say, bombing the interpreter or something)
202 my @recurse_codes = (
203 '@MRO_R1::ISA = "MRO_R2"; @MRO_R2::ISA = "MRO_R1";',
204 '@MRO_R3::ISA = "MRO_R4"; push(@MRO_R4::ISA, "MRO_R3");',
205 '@MRO_R5::ISA = "MRO_R6"; @MRO_R6::ISA = qw/XX MRO_R5 YY/;',
206 '@MRO_R7::ISA = "MRO_R8"; push(@MRO_R8::ISA, qw/XX MRO_R7 YY/)',
208 foreach my $code (@recurse_codes) {
210 ok($@ =~ /Recursive inheritance detected/);
214 # Check that SUPER caches get invalidated correctly
218 sub new { bless {} => shift }
221 package SUPERTEST::MID;
222 our @ISA = 'SUPERTEST';
224 package SUPERTEST::KID;
225 our @ISA = 'SUPERTEST::MID';
226 sub foo { my $s = shift; $s->SUPER::foo(@_) }
228 package SUPERTEST::REBASE;
232 my $stk_obj = SUPERTEST::KID->new();
233 is($stk_obj->foo(1), 2);
234 { no warnings 'redefine';
235 *SUPERTEST::foo = sub { $_[1]+2 };
237 is($stk_obj->foo(2), 4);
238 @SUPERTEST::MID::ISA = 'SUPERTEST::REBASE';
239 is($stk_obj->foo(3), 6);
244 # assigning @ISA via arrayref to globref RT 60220
246 sub new { bless {}, shift }
250 *{P2::ISA} = [ 'P1' ];
252 ok(!eval { $foo->bark }, "no bark method");
253 no warnings 'once'; # otherwise it'll bark about P1::bark used only once
254 *{P1::bark} = sub { "[bark]" };
255 is(scalar eval { $foo->bark }, "[bark]", "can bark now");
259 # assigning @ISA via arrayref then modifying it RT 72866
277 ok(!Q3->can("foo"), "can't call foo method any longer");
281 # test mro::method_changed_in
282 my $count = mro::get_pkg_gen("MRO_A");
283 mro::method_changed_in("MRO_A");
284 my $count_new = mro::get_pkg_gen("MRO_A");
286 is($count_new, $count + 1);
290 # test if we can call mro::invalidate_all_method_caches;
292 mro::invalidate_all_method_caches();
300 @main::ISA = 'parent';
302 *parent::do = sub { $output .= 'parent' };
303 *parent2::do = sub { $output .= 'parent2' };
305 @main::ISA = 'parent2';
307 is $output, 'parentparent2', '@main::ISA is magical';
311 # Undefining *ISA, then modifying @ISA
312 # This broke Class::Trait. See [perl #79024].
313 {package Class::Trait::Base}
315 undef *{"Extra::TSpouse::ISA"};
316 'Extra::TSpouse'->isa('Class::Trait::Base'); # cache the mro
317 unshift @{"Extra::TSpouse::ISA"}, 'Class::Trait::Base';
318 ok 'Extra::TSpouse'->isa('Class::Trait::Base'),
319 'a isa b after undef *a::ISA and @a::ISA modification';
323 # Deleting $package::{ISA}
324 # Broken in 5.10.0; fixed in 5.13.7
325 @Blength::ISA = 'Bladd';
326 delete $Blength::{ISA};
327 ok !Blength->isa("Bladd"), 'delete $package::{ISA}';
332 @Thrext::ISA = "Thwit";
333 @Thwit::ISA = "Sile";
335 ok !Thrext->isa('Sile'), 'undef %package:: updates subclasses';
339 # Obliterating @ISA via glob assignment
340 # Broken in 5.14.0; fixed in 5.17.2
341 @Gwythaint::ISA = "Fantastic::Creature";
342 undef *This_glob_haD_better_not_exist; # paranoia; must have no array
343 *Gwythaint::ISA = *This_glob_haD_better_not_exist;
344 ok !Gwythaint->isa("Fantastic::Creature"),
345 'obliterating @ISA via glob assignment';
349 # Autovivifying @ISA via @{*ISA}
352 @{*fednu::ISA} = "pyfg";
353 ok +fednu->isa("pyfg"), 'autovivifying @ISA via *{@ISA}';
357 sub Detached::method;
358 my $h = delete $::{"Detached::"};
359 eval { local *Detached::method };
360 is $@, "", 'localising gv-with-cv belonging to detached package';
365 @il::ISA = "ilsuper";
366 sub ilsuper::can { "puree" }
370 is +il->can("tomatoes"), \&il::tomatoes, 'local *ISA';
372 is "il"->can("tomatoes"), "puree", 'local *ISA unwinding';
375 is +il->can("tomatoes"), \&il::tomatoes, 'local *ISA = []';
377 is "il"->can("tomatoes"), "puree", 'local *ISA=[] unwinding';
380 # Changes to UNIVERSAL::DESTROY should not leave stale DESTROY caches
383 sub UNIVERSAL::DESTROY { $destroy_output = "old" }
385 undef $x; # cache the DESTROY method
386 undef *UNIVERSAL::DESTROY;
387 *UNIVERSAL::DESTROY = sub { $destroy_output = "new" };
389 undef $x; # should use the new DESTROY
390 is $destroy_output, "new",
391 'Changes to UNIVERSAL::DESTROY invalidate DESTROY caches';
392 undef *UNIVERSAL::DESTROY;
395 no warnings 'uninitialized';
397 pass "no crash when ISA contains nonexistent elements";
401 fresh_perl_is(<<'PROG', "ok", {}, "don't crash when deleting ISA");
402 $x = \@{q(Foo::ISA)};
408 # when there are multiple references to an ISA array, the mg_obj
409 # turns into an AV of globs, which is a different code path
410 # this test only crashes on -DDEBUGGING builds
411 fresh_perl_is(<<'PROG', "ok", {}, "a case with multiple refs to ISA");
412 @Foo::ISA = qw(Abc Def);
413 $x = \@{q(Foo::ISA)};
423 # reverse order of delete to exercise removing from the other end
425 # again, may only crash on -DDEBUGGING builds
426 fresh_perl_is(<<'PROG', "ok", {}, "a case with multiple refs to ISA");
427 $x = \@{q(Foo::ISA)};