This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Make PL_DBgv and other db interp vars refcounted
[perl5.git] / t / op / gv.t
CommitLineData
b9894134
PP
1#!./perl
2
3#
4# various typeglob tests
5#
6
9f1b1f2d
GS
7BEGIN {
8 chdir 't' if -d 't';
20822f61 9 @INC = '../lib';
13be902c 10 require './test.pl';
98e007d4 11}
9f1b1f2d
GS
12
13use warnings;
14
3a6ce63a 15plan( tests => 254 );
b9894134 16
cfffe6dc 17# type coercion on assignment
b9894134
PP
18$foo = 'foo';
19$bar = *main::foo;
20$bar = $foo;
98e007d4 21is(ref(\$bar), 'SCALAR');
b9894134
PP
22$foo = *main::bar;
23
cfffe6dc 24# type coercion (not) on misc ops
b9894134 25
98e007d4
NC
26ok($foo);
27is(ref(\$foo), 'GLOB');
b9894134 28
98e007d4
NC
29unlike ($foo, qr/abcd/);
30is(ref(\$foo), 'GLOB');
b9894134 31
98e007d4
NC
32is($foo, '*main::bar');
33is(ref(\$foo), 'GLOB');
b9894134 34
2acc3314
FC
35{
36 no warnings;
37 ${\*$foo} = undef;
cfffe6dc 38 is(ref(\$foo), 'GLOB', 'no type coercion when assigning to *{} retval');
2acc3314
FC
39 $::{phake} = *bar;
40 is(
41 \$::{phake}, \*{"phake"},
42 'symbolic *{} returns symtab entry when FAKE'
43 );
44 ${\*{"phake"}} = undef;
45 is(
46 ref(\$::{phake}), 'GLOB',
cfffe6dc 47 'no type coercion when assigning to retval of symbolic *{}'
2acc3314
FC
48 );
49 $::{phaque} = *bar;
50 eval '
51 is(
52 \$::{phaque}, \*phaque,
53 "compile-time *{} returns symtab entry when FAKE"
54 );
55 ${\*phaque} = undef;
56 ';
57 is(
58 ref(\$::{phaque}), 'GLOB',
cfffe6dc 59 'no type coercion when assigning to retval of compile-time *{}'
2acc3314
FC
60 );
61}
62
cfffe6dc 63# type coercion on substitutions that match
b9894134
PP
64$a = *main::foo;
65$b = $a;
66$a =~ s/^X//;
98e007d4 67is(ref(\$a), 'GLOB');
b9894134 68$a =~ s/^\*//;
98e007d4
NC
69is($a, 'main::foo');
70is(ref(\$b), 'GLOB');
b9894134
PP
71
72# typeglobs as lvalues
73substr($foo, 0, 1) = "XXX";
98e007d4
NC
74is(ref(\$foo), 'SCALAR');
75is($foo, 'XXXmain::bar');
b9894134
PP
76
77# returning glob values
78sub foo {
79 local($bar) = *main::foo;
80 $foo = *main::bar;
81 return ($foo, $bar);
82}
83
84($fuu, $baa) = foo();
98e007d4
NC
85ok(defined $fuu);
86is(ref(\$fuu), 'GLOB');
b9894134 87
98e007d4
NC
88
89ok(defined $baa);
90is(ref(\$baa), 'GLOB');
b9894134 91
85aff577
CS
92# nested package globs
93# NOTE: It's probably OK if these semantics change, because the
94# fact that %X::Y:: is stored in %X:: isn't documented.
95# (I hope.)
96
9f1b1f2d 97{ package Foo::Bar; no warnings 'once'; $test=1; }
98e007d4
NC
98ok(exists $Foo::{'Bar::'});
99is($Foo::{'Bar::'}, '*Foo::Bar::');
100
20408e3c
GS
101
102# test undef operator clearing out entire glob
103$foo = 'stuff';
104@foo = qw(more stuff);
105%foo = qw(even more random stuff);
106undef *foo;
98e007d4
NC
107is ($foo, undef);
108is (scalar @foo, 0);
109is (scalar %foo, 0);
20408e3c 110
20408e3c 111{
98e007d4
NC
112 # test warnings from assignment of undef to glob
113 my $msg = '';
20408e3c 114 local $SIG{__WARN__} = sub { $msg = $_[0] };
9f1b1f2d 115 use warnings;
20408e3c 116 *foo = 'bar';
98e007d4 117 is($msg, '');
20408e3c 118 *foo = undef;
98e007d4 119 like($msg, qr/Undefined value assigned to typeglob/);
e36cc0fb
NC
120
121 no warnings 'once';
122 # test warnings for converting globs to other forms
123 my $copy = *PWOMPF;
124 foreach ($copy, *SKREEE) {
125 $msg = '';
126 my $victim = sprintf "%d", $_;
127 like($msg, qr/Argument "\*main::[A-Z]{6}" isn't numeric in sprintf/,
128 "Warning on conversion to IV");
129 is($victim, 0);
130
131 $msg = '';
132 $victim = sprintf "%u", $_;
133 like($msg, qr/Argument "\*main::[A-Z]{6}" isn't numeric in sprintf/,
134 "Warning on conversion to UV");
135 is($victim, 0);
136
137 $msg = '';
138 $victim = sprintf "%e", $_;
139 like($msg, qr/Argument "\*main::[A-Z]{6}" isn't numeric in sprintf/,
140 "Warning on conversion to NV");
141 like($victim, qr/^0\.0+E\+?00/i, "Expect floating point zero");
142
143 $msg = '';
144 $victim = sprintf "%s", $_;
145 is($msg, '', "No warning on stringification");
146 is($victim, '' . $_);
147 }
20408e3c 148}
640b9ef6 149
98e007d4 150my $test = curr_test();
640b9ef6 151# test *glob{THING} syntax
98e007d4
NC
152$x = "ok $test\n";
153++$test;
154@x = ("ok $test\n");
155++$test;
156%x = ("ok $test" => "\n");
157++$test;
158sub x { "ok $test\n" }
640b9ef6 159print ${*x{SCALAR}}, @{*x{ARRAY}}, %{*x{HASH}}, &{*x{CODE}};
98e007d4
NC
160# This needs to go here, after the print, as sub x will return the current
161# value of test
162++$test;
f4d13ee9 163format x =
98e007d4 164XXX This text isn't used. Should it be?
f4d13ee9 165.
98e007d4
NC
166curr_test($test);
167
168is (ref *x{FORMAT}, "FORMAT");
e14698d8
FC
169is ("@{sub { *_{ARRAY} }->(1..3)}", "1 2 3",
170 'returning *_{ARRAY} from sub');
640b9ef6 171*x = *STDOUT;
98e007d4 172is (*{*x{GLOB}}, "*main::STDOUT");
39b99f21 173
29a56bd6 174{
98e007d4
NC
175 my $test = curr_test();
176
177 print {*x{IO}} "ok $test\n";
178 ++$test;
179
180 my $warn;
181 local $SIG{__WARN__} = sub {
182 $warn .= $_[0];
183 };
184 my $val = *x{FILEHANDLE};
185 print {*x{IO}} ($warn =~ /is deprecated/
186 ? "ok $test\n" : "not ok $test\n");
187 curr_test(++$test);
29a56bd6
JH
188}
189
171e2879
FC
190is *x{NAME}, 'x', '*foo{NAME}';
191is *x{PACKAGE}, 'main', '*foo{PACKAGE}';
0787e289 192{ no warnings 'once'; *x = *Foo::y; }
4fbc7258
FC
193is *x, '*Foo::y', 'glob stringifies as assignee after glob-to-glob assign';
194is *x{NAME}, 'x', 'but *foo{NAME} still returns the original name';
195is *x{PACKAGE}, 'main', 'and *foo{PACKAGE} the original package';
35cd451c
GS
196
197{
98e007d4 198 # test if defined() doesn't create any new symbols
35cd451c
GS
199
200 my $a = "SYM000";
98e007d4 201 ok(!defined *{$a});
35cd451c 202
33f89799
NC
203 {
204 no warnings 'deprecated';
205 ok(!defined @{$a});
206 }
98e007d4 207 ok(!defined *{$a});
35cd451c 208
d47e1c27
NC
209 {
210 no warnings 'deprecated';
211 ok(!defined %{$a});
212 }
98e007d4 213 ok(!defined *{$a});
35cd451c 214
98e007d4
NC
215 ok(!defined ${$a});
216 ok(!defined *{$a});
35cd451c 217
98e007d4
NC
218 ok(!defined &{$a});
219 ok(!defined *{$a});
35cd451c 220
98e007d4
NC
221 my $state = "not";
222 *{$a} = sub { $state = "ok" };
223 ok(defined &{$a});
224 ok(defined *{$a});
225 &{$a};
226 is ($state, 'ok');
35cd451c 227}
640b9ef6 228
c9d5ac95 229{
98e007d4 230 # although it *should* if you're talking about magicals
c9d5ac95
GS
231
232 my $a = "]";
98e007d4 233 ok(defined *{$a});
82587197 234 ok(defined ${$a});
c9d5ac95
GS
235
236 $a = "1";
237 "o" =~ /(o)/;
98e007d4
NC
238 ok(${$a});
239 ok(defined *{$a});
c9d5ac95 240 $a = "2";
98e007d4
NC
241 ok(!${$a});
242 ok(defined *{$a});
c9d5ac95 243 $a = "1x";
98e007d4
NC
244 ok(!defined ${$a});
245 ok(!defined *{$a});
c9d5ac95
GS
246 $a = "11";
247 "o" =~ /(((((((((((o)))))))))))/;
98e007d4
NC
248 ok(${$a});
249 ok(defined *{$a});
c9d5ac95
GS
250}
251
bd2155e9
JH
252# [ID 20010526.001] localized glob loses value when assigned to
253
254$j=1; %j=(a=>1); @j=(1); local *j=*j; *j = sub{};
255
98e007d4
NC
256is($j, 1);
257is($j{a}, 1);
258is($j[0], 1);
99491443
GS
259
260{
98e007d4 261 # does pp_readline() handle glob-ness correctly?
99491443
GS
262 my $g = *foo;
263 $g = <DATA>;
98e007d4 264 is ($g, "Perl\n");
99491443
GS
265}
266
fb24441d
RGS
267{
268 my $w = '';
bb112e5a 269 local $SIG{__WARN__} = sub { $w = $_[0] };
fb24441d
RGS
270 sub abc1 ();
271 local *abc1 = sub { };
98e007d4 272 is ($w, '');
fb24441d
RGS
273 sub abc2 ();
274 local *abc2;
275 *abc2 = sub { };
98e007d4 276 is ($w, '');
fb24441d
RGS
277 sub abc3 ();
278 *abc3 = sub { };
98e007d4 279 like ($w, qr/Prototype mismatch/);
fb24441d
RGS
280}
281
2b5e58c4
AMS
282{
283 # [17375] rcatline to formerly-defined undef was broken. Fixed in
284 # do_readline by checking SvOK. AMS, 20020918
285 my $x = "not ";
286 $x = undef;
287 $x .= <DATA>;
98e007d4 288 is ($x, "Rules\n");
2b5e58c4
AMS
289}
290
4ce457a6
TP
291{
292 # test the assignment of a GLOB to an LVALUE
293 my $e = '';
294 local $SIG{__DIE__} = sub { $e = $_[0] };
13be902c 295 my %v;
4ce457a6 296 sub f { $_[0] = 0; $_[0] = "a"; $_[0] = *DATA }
13be902c
FC
297 f($v{v});
298 is ($v{v}, '*main::DATA');
299 is (ref\$v{v}, 'GLOB', 'lvalue assignment preserves globs');
300 my $x = readline $v{v};
98e007d4 301 is ($x, "perl\n");
34770c8c 302 is ($e, '', '__DIE__ handler never called');
4ce457a6
TP
303}
304
98e007d4 305{
34770c8c 306 my $e = '';
4ce457a6
TP
307 # GLOB assignment to tied element
308 local $SIG{__DIE__} = sub { $e = $_[0] };
98e007d4
NC
309 sub T::TIEARRAY { bless [] => "T" }
310 sub T::STORE { $_[0]->[ $_[1] ] = $_[2] }
311 sub T::FETCH { $_[0]->[ $_[1] ] }
312 sub T::FETCHSIZE { @{$_[0]} }
4ce457a6
TP
313 tie my @ary => "T";
314 $ary[0] = *DATA;
98e007d4 315 is ($ary[0], '*main::DATA');
13be902c
FC
316 is (
317 ref\tied(@ary)->[0], 'GLOB',
318 'tied elem assignment preserves globs'
319 );
34770c8c 320 is ($e, '', '__DIE__ handler not called');
4ce457a6 321 my $x = readline $ary[0];
98e007d4 322 is($x, "rocks\n");
34770c8c 323 is ($e, '', '__DIE__ handler never called');
4ce457a6
TP
324}
325
e15faf7d 326{
4184c77b
NC
327 # Need some sort of die or warn to get the global destruction text if the
328 # bug is still present
5c2a9b31 329 my $output = runperl(prog => <<'EOPROG');
e15faf7d 330package M;
5c2a9b31 331$| = 1;
4184c77b 332sub DESTROY {eval {die qq{Farewell $_[0]}}; print $@}
e15faf7d
NC
333package main;
334
3d7a9343 335bless \$A::B, q{M};
e15faf7d
NC
336*A:: = \*B::;
337EOPROG
338 like($output, qr/^Farewell M=SCALAR/, "DESTROY was called");
339 unlike($output, qr/global destruction/,
340 "unreferenced symbol tables should be cleaned up immediately");
341}
63fa9adc
NC
342
343# Possibly not the correct test file for these tests.
344# There are certain space optimisations implemented via promotion rules to
345# GVs
346
bb112e5a
NC
347foreach (qw (oonk ga_shloip)) {
348 ok(!exists $::{$_}, "no symbols of any sort to start with for $_");
349}
63fa9adc
NC
350
351# A string in place of the typeglob is promoted to the function prototype
352$::{oonk} = "pie";
353my $proto = eval 'prototype \&oonk';
354die if $@;
355is ($proto, "pie", "String is promoted to prototype");
356
357
358# A reference to a value is used to generate a constant subroutine
359foreach my $value (3, "Perl rules", \42, qr/whatever/, [1,2,3], {1=>2},
5c1f4d79 360 \*STDIN, \&ok, \undef, *STDOUT) {
63fa9adc
NC
361 delete $::{oonk};
362 $::{oonk} = \$value;
363 $proto = eval 'prototype \&oonk';
364 die if $@;
365 is ($proto, '', "Prototype for a constant subroutine is empty");
366
367 my $got = eval 'oonk';
368 die if $@;
5c1f4d79 369 is (ref $got, ref $value, "Correct type of value (" . ref($value) . ")");
63fa9adc
NC
370 is ($got, $value, "Value is correctly set");
371}
5c1f4d79 372
bb112e5a
NC
373delete $::{oonk};
374$::{oonk} = \"Value";
375
376*{"ga_shloip"} = \&{"oonk"};
377
378is (ref $::{ga_shloip}, 'SCALAR', "Export of proxy constant as is");
379is (ref $::{oonk}, 'SCALAR', "Export doesn't affect original");
380is (eval 'ga_shloip', "Value", "Constant has correct value");
381is (ref $::{ga_shloip}, 'SCALAR',
93f09d7b 382 "Inlining of constant doesn't change representation");
bb112e5a
NC
383
384delete $::{ga_shloip};
385
386eval 'sub ga_shloip (); 1' or die $@;
387is ($::{ga_shloip}, '', "Prototype is stored as an empty string");
388
389# Check that a prototype expands.
390*{"ga_shloip"} = \&{"oonk"};
391
392is (ref $::{oonk}, 'SCALAR', "Export doesn't affect original");
393is (eval 'ga_shloip', "Value", "Constant has correct value");
394is (ref \$::{ga_shloip}, 'GLOB', "Symbol table has full typeglob");
395
396
397@::zwot = ('Zwot!');
398
399# Check that assignment to an existing typeglob works
400{
401 my $w = '';
402 local $SIG{__WARN__} = sub { $w = $_[0] };
403 *{"zwot"} = \&{"oonk"};
404 is($w, '', "Should be no warning");
405}
406
407is (ref $::{oonk}, 'SCALAR', "Export doesn't affect original");
408is (eval 'zwot', "Value", "Constant has correct value");
409is (ref \$::{zwot}, 'GLOB', "Symbol table has full typeglob");
410is (join ('!', @::zwot), 'Zwot!', "Existing array still in typeglob");
411
412sub spritsits () {
413 "Traditional";
414}
415
416# Check that assignment to an existing subroutine works
417{
418 my $w = '';
419 local $SIG{__WARN__} = sub { $w = $_[0] };
420 *{"spritsits"} = \&{"oonk"};
421 like($w, qr/^Constant subroutine main::spritsits redefined/,
422 "Redefining a constant sub should warn");
423}
424
425is (ref $::{oonk}, 'SCALAR', "Export doesn't affect original");
426is (eval 'spritsits', "Value", "Constant has correct value");
427is (ref \$::{spritsits}, 'GLOB', "Symbol table has full typeglob");
428
bb112e5a
NC
429# Check that assignment to an existing typeglob works
430{
431 my $w = '';
432 local $SIG{__WARN__} = sub { $w = $_[0] };
50baa5ea
VP
433 *{"plunk"} = [];
434 *{"plunk"} = \&{"oonk"};
bb112e5a
NC
435 is($w, '', "Should be no warning");
436}
437
bb112e5a
NC
438is (ref $::{oonk}, 'SCALAR', "Export doesn't affect original");
439is (eval 'plunk', "Value", "Constant has correct value");
440is (ref \$::{plunk}, 'GLOB', "Symbol table has full typeglob");
441
442my $gr = eval '\*plunk' or die;
443
444{
445 my $w = '';
446 local $SIG{__WARN__} = sub { $w = $_[0] };
50baa5ea 447 *{$gr} = \&{"oonk"};
2111d928 448 is($w, '', "Redefining a constant sub to another constant sub with the same underlying value should not warn (It's just re-exporting, and that was always legal)");
bb112e5a
NC
449}
450
451is (ref $::{oonk}, 'SCALAR', "Export doesn't affect original");
452is (eval 'plunk', "Value", "Constant has correct value");
453is (ref \$::{plunk}, 'GLOB', "Symbol table has full typeglob");
454
50baa5ea
VP
455# Non-void context should defeat the optimisation, and will cause the original
456# to be promoted (what change 26482 intended)
457my $result;
458{
459 my $w = '';
460 local $SIG{__WARN__} = sub { $w = $_[0] };
461 $result = *{"awkkkkkk"} = \&{"oonk"};
462 is($w, '', "Should be no warning");
463}
464
465is (ref \$result, 'GLOB',
466 "Non void assignment should still return a typeglob");
467
468is (ref \$::{oonk}, 'GLOB', "This export does affect original");
469is (eval 'plunk', "Value", "Constant has correct value");
470is (ref \$::{plunk}, 'GLOB', "Symbol table has full typeglob");
471
472delete $::{oonk};
473$::{oonk} = \"Value";
474
475sub non_dangling {
476 my $w = '';
477 local $SIG{__WARN__} = sub { $w = $_[0] };
478 *{"zap"} = \&{"oonk"};
479 is($w, '', "Should be no warning");
480}
481
482non_dangling();
483is (ref $::{oonk}, 'SCALAR', "Export doesn't affect original");
484is (eval 'zap', "Value", "Constant has correct value");
485is (ref $::{zap}, 'SCALAR', "Exported target is also a PCS");
486
487sub dangling {
488 local $SIG{__WARN__} = sub { die $_[0] };
489 *{"biff"} = \&{"oonk"};
490}
491
492dangling();
493is (ref \$::{oonk}, 'GLOB', "This export does affect original");
494is (eval 'biff', "Value", "Constant has correct value");
495is (ref \$::{biff}, 'GLOB', "Symbol table has full typeglob");
496
6f1b3ab0
FC
497$::{yarrow} = [4,5,6];
498is join("-", eval "yarrow()"), '4-5-6', 'array ref as stash elem';
499is ref $::{yarrow}, "ARRAY", 'stash elem is still array ref after use';
500is join("-", eval "&yarrow"), '4-5-6', 'calling const list with &';
501is join("-", eval "&yarrow(1..10)"), '4-5-6', 'const list ignores & args';
502is prototype "yarrow", "", 'const list has "" prototype';
503is eval "yarrow", 3, 'const list in scalar cx returns length';
504
acaa9288
NC
505{
506 use vars qw($glook $smek $foof);
507 # Check reference assignment isn't affected by the SV type (bug #38439)
508 $glook = 3;
509 $smek = 4;
510 $foof = "halt and cool down";
511
512 my $rv = \*smek;
513 is($glook, 3);
514 *glook = $rv;
515 is($glook, 4);
516
517 my $pv = "";
518 $pv = \*smek;
519 is($foof, "halt and cool down");
520 *foof = $pv;
521 is($foof, 4);
522}
523
5c1f4d79
NC
524format =
525.
526
6f1b3ab0 527foreach my $value ({1=>2}, *STDOUT{IO}, \&ok, *STDOUT{FORMAT}) {
5c1f4d79
NC
528 # *STDOUT{IO} returns a reference to a PVIO. As it's blessed, ref returns
529 # IO::Handle, which isn't what we want.
530 my $type = $value;
531 $type =~ s/.*=//;
532 $type =~ s/\(.*//;
533 delete $::{oonk};
534 $::{oonk} = $value;
535 $proto = eval 'prototype \&oonk';
536 like ($@, qr/^Cannot convert a reference to $type to typeglob/,
537 "Cannot upgrade ref-to-$type to typeglob");
538}
f9d52e31
NC
539
540{
541 no warnings qw(once uninitialized);
542 my $g = \*clatter;
543 my $r = eval {no strict; ${*{$g}{SCALAR}}};
544 is ($@, '', "PERL_DONT_CREATE_GVSV shouldn't affect thingy syntax");
545
546 $g = \*vowm;
547 $r = eval {use strict; ${*{$g}{SCALAR}}};
548 is ($@, '',
549 "PERL_DONT_CREATE_GVSV shouldn't affect thingy syntax under strict");
550}
551
06be3b40
NC
552{
553 # Bug reported by broquaint on IRC
554 *slosh::{HASH}->{ISA}=[];
555 slosh->import;
556 pass("gv_fetchmeth coped with the unexpected");
9e0d86f8
NC
557
558 # An audit found these:
559 {
560 package slosh;
561 sub rip {
562 my $s = shift;
563 $s->SUPER::rip;
564 }
565 }
566 eval {slosh->rip;};
567 like ($@, qr/^Can't locate object method "rip"/, "Even with SUPER");
568
569 is(slosh->isa('swoosh'), '');
570
571 $CORE::GLOBAL::{"lock"}=[];
572 eval "no warnings; lock";
573 like($@, qr/^Not enough arguments for lock/,
574 "Can't trip up general keyword overloading");
575
576 $CORE::GLOBAL::{"readline"}=[];
b3c9268e 577 eval "<STDOUT> if 0";
9e0d86f8 578 is($@, '', "Can't trip up readline overloading");
d5e716f5
NC
579
580 $CORE::GLOBAL::{"readpipe"}=[];
581 eval "`` if 0";
582 is($@, '', "Can't trip up readpipe overloading");
06be3b40 583}
53a42478
NC
584
585{
586 die if exists $::{BONK};
587 $::{BONK} = \"powie";
588 *{"BONK"} = \&{"BONK"};
589 eval 'is(BONK(), "powie",
93f09d7b 590 "Assignment works when glob created midway (bug 45607)"); 1'
53a42478
NC
591 or die $@;
592}
1f257c95
NC
593
594# For now these tests are here, but they would probably be better in a file for
595# tests for croaks. (And in turn, that probably deserves to be in a different
596# directory. Gerard Goossen has a point about the layout being unclear
597
598sub coerce_integer {
599 no warnings 'numeric';
600 $_[0] |= 0;
601}
602sub coerce_number {
603 no warnings 'numeric';
604 $_[0] += 0;
605}
606sub coerce_string {
607 $_[0] .= '';
608}
609
610foreach my $type (qw(integer number string)) {
611 my $prog = "coerce_$type(*STDERR)";
612 is (scalar eval "$prog; 1", undef, "$prog failed...");
613 like ($@, qr/Can't coerce GLOB to $type in/,
614 "with the correct error message");
615}
616
e8986967 617# RT #65582 anonymous glob should be defined, and not coredump when
1809c940
DM
618# stringified. The behaviours are:
619#
52a6327b
FC
620# defined($glob) "$glob" $glob .= ...
621# 5.8.8 false "" with uninit warning "" with uninit warning
622# 5.10.0 true (coredump) (coredump)
623# 5.1[24] true "" "" with uninit warning
624# 5.16 true "*__ANON__::..." "*__ANON__::..."
1809c940
DM
625
626{
627 my $io_ref = *STDOUT{IO};
628 my $glob = *$io_ref;
e8986967 629 ok(defined $glob, "RT #65582 anon glob should be defined");
1809c940
DM
630
631 my $warn = '';
632 local $SIG{__WARN__} = sub { $warn = $_[0] };
633 use warnings;
634 my $str = "$glob";
e8986967 635 is($warn, '', "RT #65582 anon glob stringification shouldn't warn");
885f468a 636 is($str, '*__ANON__::__ANONIO__',
e8986967 637 "RT #65582/#96326 anon glob stringification");
1809c940
DM
638}
639
25451cef
FC
640# Another stringification bug: Test that recursion does not cause lexical
641# handles to lose their names.
642sub r {
643 my @output;
644 @output = r($_[0]-1) if $_[0];
645 open my $fh, "TEST";
646 push @output, $$fh;
647 close $fh;
648 @output;
649}
650is join(' ', r(4)),
651 '*main::$fh *main::$fh *main::$fh *main::$fh *main::$fh',
652 'recursion does not cause lex handles to lose their names';
653
3a6ce63a
FC
654# And sub cloning, too; not just recursion
655my $close_over_me;
656is join(' ', sub {
657 () = $close_over_me;
658 my @output;
659 @output = CORE::__SUB__->($_[0]-1) if $_[0];
660 open my $fh, "TEST";
661 push @output, $$fh;
662 close $fh;
663 @output;
664 }->(4)),
665 '*main::$fh *main::$fh *main::$fh *main::$fh *main::$fh',
666 'sub cloning does not cause lex handles to lose their names';
667
1f730e6c
FC
668# [perl #71254] - Assigning a glob to a variable that has a current
669# match position. (We are testing that Perl_magic_setmglob respects globs'
670# special used of SvSCREAM.)
671{
672 $m = 2; $m=~s/./0/gems; $m= *STDERR;
673 is(
674 "$m", "*main::STDERR",
675 '[perl #71254] assignment of globs to vars with pos'
676 );
677}
678
2867cdbc
Z
679# [perl #72740] - indirect object syntax, heuristically imputed due to
680# the non-existence of a function, should not cause a stash entry to be
681# created for the non-existent function.
682{
683 package RT72740a;
684 my $f = bless({}, RT72740b);
685 sub s1 { s2 $f; }
686 our $s4;
687 sub s3 { s4 $f; }
688}
689{
690 package RT72740b;
691 sub s2 { "RT72740b::s2" }
692 sub s4 { "RT72740b::s4" }
693}
694ok(exists($RT72740a::{s1}), "RT72740a::s1 exists");
695ok(!exists($RT72740a::{s2}), "RT72740a::s2 does not exist");
696ok(exists($RT72740a::{s3}), "RT72740a::s3 exists");
697ok(exists($RT72740a::{s4}), "RT72740a::s4 exists");
698is(RT72740a::s1(), "RT72740b::s2", "RT72740::s1 parsed correctly");
699is(RT72740a::s3(), "RT72740b::s4", "RT72740::s3 parsed correctly");
700
b9e00b79
LRG
701# [perl #71686] Globs that are in symbol table can be un-globbed
702$sym = undef;
703$::{fake} = *sym;
704is (eval 'local *::fake = \"chuck"; $fake', 'chuck',
705 "Localized glob didn't coerce into a RV");
706is ($@, '', "Can localize FAKE glob that's present in stash");
707is (scalar $::{fake}, "*main::sym",
708 "Localized FAKE glob's value was correctly restored");
709
0fe688f5 710# [perl #1804] *$x assignment when $x is a copy of another glob
0095ccd4 711# And [perl #77508] (same thing with list assignment)
0fe688f5
FC
712{
713 no warnings 'once';
714 my $x = *_random::glob_that_is_not_used_elsewhere;
715 *$x = sub{};
716 is(
717 "$x", '*_random::glob_that_is_not_used_elsewhere',
718 '[perl #1804] *$x assignment when $x is FAKE',
719 );
0095ccd4
FC
720 $x = *_random::glob_that_is_not_used_elsewhere;
721 (my $dummy, *$x) = (undef,[]);
722 is(
723 "$x", '*_random::glob_that_is_not_used_elsewhere',
724 '[perl #77508] *$x list assignment when $x is FAKE',
725 ) or require Devel::Peek, Devel::Peek::Dump($x);
0fe688f5
FC
726}
727
cf203c62
FR
728# [perl #76540]
729# this caused panics or 'Attempt to free unreferenced scalar'
730# (its a compile-time issue, so the die lets us skip the prints)
731{
732 my @warnings;
733 local $SIG{__WARN__} = sub { push @warnings, @_ };
734
735 eval <<'EOF';
736BEGIN { $::{FOO} = \'bar' }
737die "made it";
738print FOO, "\n";
739print FOO, "\n";
740EOF
741
742 like($@, qr/made it/, "#76540 - no panic");
743 ok(!@warnings, "#76540 - no 'Attempt to free unreferenced scalar'");
744}
745
13be902c
FC
746# [perl #77362] various bugs related to globs as PVLVs
747{
748 no warnings qw 'once void';
749 my %h; # We pass a key of this hash to the subroutine to get a PVLV.
750 sub { for(shift) {
751 # Set up our glob-as-PVLV
752 $_ = *hon;
753
754 # Bad symbol for array
755 ok eval{ @$_; 1 }, 'PVLV glob slots can be autovivified' or diag $@;
756
757 # This should call TIEHANDLE, not TIESCALAR
758 *thext::TIEHANDLE = sub{};
759 ok eval{ tie *$_, 'thext'; 1 }, 'PVLV globs can be tied as handles'
760 or diag $@;
761
762 # Assigning undef to the glob should not overwrite it...
763 {
764 my $w;
765 local $SIG{__WARN__} = sub { $w = shift };
766 *$_ = undef;
767 is $_, "*main::hon", 'PVLV: assigning undef to the glob does nothing';
768 like $w, qr\Undefined value assigned to typeglob\,
769 'PVLV: assigning undef to the glob warns';
770 }
771
2acc3314 772 # Neither should reference assignment.
13be902c 773 *$_ = [];
2acc3314 774 is $_, "*main::hon", "PVLV: arrayref assignment assigns to the AV slot";
13be902c
FC
775
776 # Concatenation should still work.
777 ok eval { $_ .= 'thlew' }, 'PVLV concatenation does not die' or diag $@;
2acc3314 778 is $_, '*main::honthlew', 'PVLV concatenation works';
13be902c
FC
779
780 # And we should be able to overwrite it with a string, number, or refer-
781 # ence, too, if we omit the *.
782 $_ = *hon; $_ = 'tzor';
783 is $_, 'tzor', 'PVLV: assigning a string over a glob';
784 $_ = *hon; $_ = 23;
785 is $_, 23, 'PVLV: assigning an integer over a glob';
786 $_ = *hon; $_ = 23.23;
787 is $_, 23.23, 'PVLV: assigning a float over a glob';
788 $_ = *hon; $_ = \my $sthat;
789 is $_, \$sthat, 'PVLV: assigning a reference over a glob';
790
791 # This bug was found by code inspection. Could this ever happen in
792 # real life? :-)
793 # This duplicates a file handle, accessing it through a PVLV glob, the
794 # glob having been removed from the symbol table, so a stringified form
795 # of it does not work. This checks that sv_2io does not stringify a PVLV.
796 $_ = *quin;
797 open *quin, "test.pl"; # test.pl is as good a file as any
798 delete $::{quin};
799 ok eval { open my $zow, "<&", $_ }, 'PVLV: sv_2io stringifieth not'
800 or diag $@;
801
802 # Similar tests to make sure sv_2cv etc. do not stringify.
803 *$_ = sub { 1 };
804 ok eval { &$_ }, "PVLV glob can be called as a sub" or diag $@;
805 *flelp = sub { 2 };
806 $_ = 'flelp';
807 is eval { &$_ }, 2, 'PVLV holding a string can be called as a sub'
808 or diag $@;
809
810 # Coderef-to-glob assignment when the glob is no longer accessible
811 # under its name: These tests are to make sure the OPpASSIGN_CV_TO_GV
812 # optimisation takes PVLVs into account, which is why the RHSs have to be
813 # named subs.
814 use constant gheen => 'quare';
815 $_ = *ming;
816 delete $::{ming};
817 *$_ = \&gheen;
818 is eval { &$_ }, 'quare',
819 'PVLV: constant assignment when the glob is detached from the symtab'
820 or diag $@;
821 $_ = *bength;
822 delete $::{bength};
823 *gheck = sub { 'lon' };
824 *$_ = \&gheck;
825 is eval { &$_ }, 'lon',
826 'PVLV: coderef assignment when the glob is detached from the symtab'
827 or diag $@;
828
e77ae825
NC
829SKIP: {
830 skip_if_miniperl("no dynamic loading on miniperl, so can't load PerlIO::scalar", 1);
831 # open should accept a PVLV as its first argument
832 $_ = *hon;
833 ok eval { open $_,'<', \my $thlext }, 'PVLV can be the first arg to open'
834 or diag $@;
835 }
13be902c
FC
836
837 # -t should not stringify
838 $_ = *thlit; delete $::{thlit};
839 *$_ = *STDOUT{IO};
840 ok defined -t $_, 'PVLV: -t does not stringify';
841
842 # neither should -T
804401ea
FC
843 # but some systems don’t support this on file handles
844 my $pass;
845 ok
846 eval {
847 open my $quile, "<", 'test.pl';
848 $_ = *$quile;
849 $pass = -T $_;
850 1
851 } ? $pass : $@ =~ /not implemented on filehandles/,
852 "PVLV: -T does not stringify";
13be902c
FC
853
854 # Unopened file handle
855 {
856 my $w;
857 local $SIG{__WARN__} = sub { $w .= shift };
858 $_ = *vor;
859 close $_;
860 like $w, qr\unopened filehandle vor\,
861 'PVLV globs get their names reported in unopened error messages';
862 }
863
864 }}->($h{k});
865}
866
251c9b20
NC
867*aieee = 4;
868pass('Can assign integers to typeglobs');
869*aieee = 3.14;
870pass('Can assign floats to typeglobs');
871*aieee = 'pi';
872pass('Can assign strings to typeglobs');
873
0936ef8b
FC
874{
875 package thrext;
876 sub TIESCALAR{bless[]}
877 sub STORE{ die "No!"}
878 sub FETCH{ no warnings 'once'; *thrit }
879 tie my $a, "thrext";
880 () = "$a"; # do a fetch; now $a holds a glob
881 eval { *$a = sub{} };
882 untie $a;
883 eval { $a = "bar" };
884 ::is $a, "bar",
885 "[perl #77812] Globs in tied scalars can be reified if STORE dies"
886}
887
8d33f848
FC
888# These two crashed prior to 5.13.6. In 5.13.6 they were fatal errors. They
889# were fixed in 5.13.7.
890ok eval {
891 my $glob = \*heen::ISA;
892 delete $::{"heen::"};
893 *$glob = *bar;
894}, "glob-to-*ISA assignment works when *ISA has lost its stash";
895ok eval {
896 my $glob = \*slare::ISA;
897 delete $::{"slare::"};
898 *$glob = [];
899}, "array-to-*ISA assignment works when *ISA has lost its stash";
900# These two crashed in 5.13.6. They were likewise fixed in 5.13.7.
901ok eval {
902 sub greck;
4a68a320 903 my $glob = do { no warnings "once"; \*phing::foo};
8d33f848
FC
904 delete $::{"phing::"};
905 *$glob = *greck;
dc115b7c 906}, "Assigning a glob-with-sub to a glob that has lost its stash works";
8d33f848
FC
907ok eval {
908 sub pon::foo;
909 my $glob = \*pon::foo;
910 delete $::{"pon::"};
911 *$glob = *foo;
dc115b7c 912}, "Assigning a glob to a glob-with-sub that has lost its stash works";
8d33f848 913
d8906c05
FC
914{
915 package Tie::Alias;
916 sub TIESCALAR{ bless \\pop }
917 sub FETCH { $${$_[0]} }
918 sub STORE { $${$_[0]} = $_[1] }
919 package main;
920 tie my $alias, 'Tie::Alias', my $var;
921 no warnings 'once';
922 $var = *galobbe;
923 {
924 local *$alias = [];
925 $var = 3;
926 is $alias, 3, "[perl #77926] Glob reification during localisation";
927 }
928}
8d33f848 929
b0d55c99
FC
930# This code causes gp_free to call a destructor when a glob is being
931# restored on scope exit. The destructor used to see SVs with a refcount of
932# zero inside the glob, which could result in crashes (though not in this
933# test case, which just panics).
934{
935 no warnings 'once';
936 my $survived;
937 *Trit::DESTROY = sub {
938 $thwext = 42; # panic
939 $survived = 1;
940 };
941 {
942 local *thwext;
943 $thwext = bless[],'Trit';
944 ();
945 }
946 ok $survived,
947 'no error when gp_free calls a destructor that assigns to the gv';
948}
949
f132ae69
FC
950# *{undef}
951eval { *{my $undef} = 3 };
952like $@, qr/^Can't use an undefined value as a symbol reference at /,
953 '*{ $undef } assignment';
954eval { *{;undef} = 3 };
955like $@, qr/^Can't use an undefined value as a symbol reference at /,
956 '*{ ;undef } assignment';
957
914ecc63
FC
958# [perl #99142] defined &{"foo"} when there is a constant stub
959# If I break your module, you get to have it mentioned in Perl's tests. :-)
960package HTTP::MobileAttribute::Plugin::Locator {
961 use constant LOCATOR_GPS => 1;
962 ::ok defined &{__PACKAGE__."::LOCATOR_GPS"},
963 'defined &{"name of constant"}';
964 ::ok Internals::SvREFCNT(${__PACKAGE__."::"}{LOCATOR_GPS}),
965 "stash elem for slot is not freed prematurely";
966}
967
e38acfd7
FC
968# Check that constants promoted to CVs point to the right GVs when the name
969# contains a null.
970package lrcg {
971 use constant x => 3;
972 # These two lines abuse the optimisation that copies the scalar ref from
973 # one stash element to another, to get a constant with a null in its name
974 *{"yz\0a"} = \&{"x"};
975 my $ref = \&{"yz\0a"};
976 ::ok !exists $lrcg::{yz},
977 'constants w/nulls in their names point 2 the right GVs when promoted';
978}
979
f9509170
FC
980# Look away, please.
981# This violates perl's internal structures by fiddling with stashes in a
982# way that should never happen, but perl should not start trying to free
983# unallocated memory as a result. There is no ok() or is() because the
984# panic that used to occur only occurred during global destruction, and
985# only with PERL_DESTRUCT_LEVEL=2. (The panic itself was sufficient for
986# the harness to consider this test script to have failed.)
987$::{aoeuaoeuaoeaoeu} = __PACKAGE__; # cow
988() = *{"aoeuaoeuaoeaoeu"};
989
2f222bbd
FC
990$x = *_119051;
991$y = \&$x;
992undef $x;
993eval { &$y };
994pass "No crash due to CvGV(vivified stub) pointing to flattened glob copy";
995# Not really supported, but this should not crash either:
996$x = *_119051again;
997delete $::{_119051again};
998$::{_119051again} = $x; # now we have a fake glob under the right name
999$y = \&$x; # so when this tries to look up the right GV for
1000undef $::{_119051again}; # CvGV, it still gets a fake one
1001eval { $y->() };
1002pass "No crash due to CvGV pointing to glob copy in the stash";
1003
99491443 1004__END__
98e007d4
NC
1005Perl
1006Rules
1007perl
1008rocks