8 BEGIN { require "./test.pl"; }
12 # Used to segfault (bug #15479)
15 qr/Odd number of elements in hash assignment at - line 1\./,
16 { switches => [ '-w' ] },
17 'delete $::{STDERR} and print a warning',
22 'BEGIN { $::{"X::"} = 2 }',
24 { switches => [ '-w' ] },
25 q(Insert a non-GV in a stash, under warnings 'once'),
29 no warnings 'deprecated';
30 ok( defined %oedipa::maas::, q(stashes happen to be defined if not used) );
31 ok( defined %{"oedipa::maas::"}, q(- work with hard refs too) );
33 ok( defined %tyrone::slothrop::, q(stashes are defined if seen at compile time) );
34 ok( defined %{"tyrone::slothrop::"}, q(- work with hard refs too) );
36 ok( defined %bongo::shaftsbury::, q(stashes are defined if a var is seen at compile time) );
37 ok( defined %{"bongo::shaftsbury::"}, q(- work with hard refs too) );
40 package tyrone::slothrop;
41 $bongo::shaftsbury::scalar = 1;
46 # Unbalanced string table refcount: (1) for "A::" during global destruction.
49 local $ENV{PERL_DESTRUCT_LEVEL} = 2;
51 'package A; sub a { // }; %::=""',
59 ok( eval { no warnings 'deprecated'; defined %achtfaden:: }, 'works in eval{}' );
60 ok( eval q{ no warnings 'deprecated'; defined %schoenmaker:: }, 'works in eval("")' );
62 # now tests with strictures
66 no warnings 'deprecated';
67 ok( defined %pig::, q(referencing a non-existent stash doesn't produce stricture errors) );
68 ok( !exists $pig::{bodine}, q(referencing a non-existent stash element doesn't produce stricture errors) );
72 eval { require B; 1 } or skip "no B", 29;
74 *b = \&B::svref_2object;
75 my $CVf_ANON = B::CVf_ANON();
84 isa_ok( $gv, "B::GV", "deleted stash entry leaves CV with valid GV");
85 is( b($sub)->CvFLAGS & $CVf_ANON, $CVf_ANON, "...and CVf_ANON set");
86 is( eval { $gv->NAME }, "__ANON__", "...and an __ANON__ name");
87 is( eval { $gv->STASH->NAME }, "one", "...but leaves stash intact");
96 isa_ok( $gv, "B::GV", "cleared stash leaves CV with valid GV");
97 is( b($sub)->CvFLAGS & $CVf_ANON, $CVf_ANON, "...and CVf_ANON set");
98 is( eval { $gv->NAME }, "__ANON__", "...and an __ANON__ name");
99 is( eval { $gv->STASH->NAME }, "two", "...but leaves stash intact");
108 isa_ok( $gv, "B::GV", "undefed stash leaves CV with valid GV");
109 is( b($sub)->CvFLAGS & $CVf_ANON, $CVf_ANON, "...and CVf_ANON set");
110 is( eval { $gv->NAME }, "__ANON__", "...and an __ANON__ name");
111 is( eval { $gv->STASH->NAME }, "__ANON__", "...and an __ANON__ stash");
119 my $gv = B::svref_2object($sub)->GV;
120 ok($gv->isa(q/B::GV/), "cleared stash leaves anon CV with valid GV");
122 my $st = eval { $gv->STASH->NAME };
123 is($st, q/four/, "...but leaves the stash intact");
131 $gv = B::svref_2object($sub)->GV;
132 ok($gv->isa(q/B::GV/), "undefed stash leaves anon CV with valid GV");
134 $st = eval { $gv->STASH->NAME };
135 { local $TODO = 'STASHES not anonymized';
136 is($st, q/__ANON__/, "...and an __ANON__ stash");
143 my $stash_glob = delete $::{"six::"};
144 # Now free the GV while the stash still exists (though detached)
145 delete $$stash_glob{"six"};
146 $gv = B::svref_2object($sub)->GV;
147 ok($gv->isa(q/B::GV/),
148 'anonymised CV whose stash is detached still has a GV');
149 is $gv->STASH->NAME, '__ANON__',
150 'CV anonymised when its stash is detached becomes __ANON__::__ANON__';
152 # CvSTASH should be null on a named sub if the stash has been deleted
159 my $cv = B::svref_2object($rfoo);
160 # (is there a better way of testing for NULL ?)
161 my $stash = $cv->STASH;
162 like($stash, qr/B::SPECIAL/, "NULL CvSTASH on named sub");
165 # on glob reassignment, orphaned CV should have anon CvGV
176 my $cv = B::svref_2object($r);
178 ok($gv->isa(q/B::GV/), "orphaned CV has valid GV");
179 is($gv->NAME, '__ANON__', "orphaned CV has anon GV");
182 # deleting __ANON__ glob shouldn't break things
188 my $named = eval q[\&named];
190 delete $FOO3::{named}; # make named anonymous
192 delete $FOO3::{__ANON__}; # whoops!
194 $cv = B::svref_2object($named);
196 ok($gv->isa(q/B::GV/), "ex-named CV has valid GV");
197 is($gv->NAME, '__ANON__', "ex-named CV has anon GV");
199 $cv = B::svref_2object($anon);
201 ok($gv->isa(q/B::GV/), "anon CV has valid GV");
202 is($gv->NAME, '__ANON__', "anon CV has anon GV");
215 my $br = B::svref_2object($r);
216 is ($br->STASH->NAME, 'bloop',
217 'stub records the package it was compiled in');
218 # Arguably this shouldn't quite be here, but it's easy to add it
219 # here, and tricky to figure out a different good place for it.
220 like ($br->FILE, qr/stash/i,
221 'stub records the file it was compiled in');
223 # We need to take this reference "late", after the subroutine is
225 $br = B::svref_2object(eval 'sub whack {}; \&whack');
228 is ($br->STASH->NAME, 'main',
229 'definition overrides the package it was compiled in');
230 like ($br->FILE, qr/eval/,
231 'definition overrides the file it was compiled in');
237 'sub foo { 1 }; use overload q/""/ => \&foo;' .
238 'delete $main::{foo}; bless []',
241 "no segfault with overload/deleted stash entry [#58530]",
244 # make sure having a sub called __ANON__ doesn't confuse perl.
248 sub __ANON__ { $c = (caller(0))[3]; }
250 is ($c, 'main::__ANON__', '__ANON__ sub called ok');
254 # Stashes that are effectively renamed
263 # effectively rename a stash
264 *slin:: = *rile::; *rile:: = *zor::;
266 ::is *$globref, "*rile::tat",
267 'globs stringify the same way when stashes are moved';
268 ::is ref $obj, "rile",
269 'ref() returns the same thing when an object’s stash is moved';
270 ::like "$obj", qr "^rile=ARRAY\(0x[\da-f]+\)\z",
271 'objects stringify the same way when their stashes are moved';
273 local $::TODO = $Config{useithreads} ? "fails under threads" : undef;
274 ::is eval '__PACKAGE__', 'rile',
275 '__PACKAGE__ returns the same when the current stash is moved';
278 # Now detach it completely from the symtab, making it effect-
280 my $life_raft = \%slin::;
283 ::is *$globref, "*rile::tat",
284 'globs stringify the same way when stashes are detached';
285 ::is ref $obj, "rile",
286 'ref() returns the same thing when an object’s stash is detached';
287 ::like "$obj", qr "^rile=ARRAY\(0x[\da-f]+\)\z",
288 'objects stringify the same way when their stashes are detached';
290 local $::TODO = $Config{useithreads} ? "fails under threads" : undef;
291 ::is eval '__PACKAGE__', 'rile',
292 '__PACKAGE__ returns the same when the current stash is detached';
296 # Setting the name during undef %stash:: should have no effect.
298 my $glob = \*Phoo::glob;
299 sub o::DESTROY { eval '++$Phoo::bar' }
301 ${"Phoo::thing1"} = bless [], "o";
303 is "$$glob", "*__ANON__::glob",
304 "setting stash name during undef has no effect";
307 # [perl #88134] incorrect package structure
312 ok eval { Bear::::baz() },
313 'packages ending with :: are self-consistent';
316 # [perl #88138] ' not equivalent to :: before a null
318 is ${"a::\0b"}, "c", "' is equivalent to :: before a null";