This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[Merge] minitest fixes
[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';
13be902c 9 require './test.pl';
43ece5b1 10 set_up_inc('../lib');
98e007d4 11}
9f1b1f2d
GS
12
13use warnings;
14
acb187b4 15plan( tests => 269 );
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
98e007d4
NC
203 ok(!defined ${$a});
204 ok(!defined *{$a});
35cd451c 205
98e007d4
NC
206 ok(!defined &{$a});
207 ok(!defined *{$a});
35cd451c 208
98e007d4
NC
209 my $state = "not";
210 *{$a} = sub { $state = "ok" };
211 ok(defined &{$a});
212 ok(defined *{$a});
213 &{$a};
214 is ($state, 'ok');
35cd451c 215}
640b9ef6 216
c9d5ac95 217{
98e007d4 218 # although it *should* if you're talking about magicals
c9d5ac95
GS
219
220 my $a = "]";
98e007d4 221 ok(defined *{$a});
82587197 222 ok(defined ${$a});
c9d5ac95
GS
223
224 $a = "1";
225 "o" =~ /(o)/;
98e007d4
NC
226 ok(${$a});
227 ok(defined *{$a});
c9d5ac95 228 $a = "2";
98e007d4
NC
229 ok(!${$a});
230 ok(defined *{$a});
c9d5ac95 231 $a = "1x";
98e007d4
NC
232 ok(!defined ${$a});
233 ok(!defined *{$a});
c9d5ac95
GS
234 $a = "11";
235 "o" =~ /(((((((((((o)))))))))))/;
98e007d4
NC
236 ok(${$a});
237 ok(defined *{$a});
c9d5ac95
GS
238}
239
bd2155e9
JH
240# [ID 20010526.001] localized glob loses value when assigned to
241
242$j=1; %j=(a=>1); @j=(1); local *j=*j; *j = sub{};
243
98e007d4
NC
244is($j, 1);
245is($j{a}, 1);
246is($j[0], 1);
99491443
GS
247
248{
98e007d4 249 # does pp_readline() handle glob-ness correctly?
99491443
GS
250 my $g = *foo;
251 $g = <DATA>;
98e007d4 252 is ($g, "Perl\n");
99491443
GS
253}
254
fb24441d
RGS
255{
256 my $w = '';
bb112e5a 257 local $SIG{__WARN__} = sub { $w = $_[0] };
fb24441d
RGS
258 sub abc1 ();
259 local *abc1 = sub { };
98e007d4 260 is ($w, '');
fb24441d
RGS
261 sub abc2 ();
262 local *abc2;
263 *abc2 = sub { };
98e007d4 264 is ($w, '');
fb24441d
RGS
265 sub abc3 ();
266 *abc3 = sub { };
98e007d4 267 like ($w, qr/Prototype mismatch/);
fb24441d
RGS
268}
269
2b5e58c4
AMS
270{
271 # [17375] rcatline to formerly-defined undef was broken. Fixed in
272 # do_readline by checking SvOK. AMS, 20020918
273 my $x = "not ";
274 $x = undef;
275 $x .= <DATA>;
98e007d4 276 is ($x, "Rules\n");
2b5e58c4
AMS
277}
278
4ce457a6
TP
279{
280 # test the assignment of a GLOB to an LVALUE
281 my $e = '';
282 local $SIG{__DIE__} = sub { $e = $_[0] };
13be902c 283 my %v;
4ce457a6 284 sub f { $_[0] = 0; $_[0] = "a"; $_[0] = *DATA }
13be902c
FC
285 f($v{v});
286 is ($v{v}, '*main::DATA');
287 is (ref\$v{v}, 'GLOB', 'lvalue assignment preserves globs');
288 my $x = readline $v{v};
98e007d4 289 is ($x, "perl\n");
34770c8c 290 is ($e, '', '__DIE__ handler never called');
4ce457a6
TP
291}
292
98e007d4 293{
34770c8c 294 my $e = '';
4ce457a6
TP
295 # GLOB assignment to tied element
296 local $SIG{__DIE__} = sub { $e = $_[0] };
98e007d4
NC
297 sub T::TIEARRAY { bless [] => "T" }
298 sub T::STORE { $_[0]->[ $_[1] ] = $_[2] }
299 sub T::FETCH { $_[0]->[ $_[1] ] }
300 sub T::FETCHSIZE { @{$_[0]} }
4ce457a6
TP
301 tie my @ary => "T";
302 $ary[0] = *DATA;
98e007d4 303 is ($ary[0], '*main::DATA');
13be902c
FC
304 is (
305 ref\tied(@ary)->[0], 'GLOB',
306 'tied elem assignment preserves globs'
307 );
34770c8c 308 is ($e, '', '__DIE__ handler not called');
4ce457a6 309 my $x = readline $ary[0];
98e007d4 310 is($x, "rocks\n");
34770c8c 311 is ($e, '', '__DIE__ handler never called');
4ce457a6
TP
312}
313
e15faf7d 314{
4184c77b
NC
315 # Need some sort of die or warn to get the global destruction text if the
316 # bug is still present
5c2a9b31 317 my $output = runperl(prog => <<'EOPROG');
e15faf7d 318package M;
5c2a9b31 319$| = 1;
4184c77b 320sub DESTROY {eval {die qq{Farewell $_[0]}}; print $@}
e15faf7d
NC
321package main;
322
3d7a9343 323bless \$A::B, q{M};
e15faf7d
NC
324*A:: = \*B::;
325EOPROG
326 like($output, qr/^Farewell M=SCALAR/, "DESTROY was called");
327 unlike($output, qr/global destruction/,
328 "unreferenced symbol tables should be cleaned up immediately");
329}
63fa9adc
NC
330
331# Possibly not the correct test file for these tests.
332# There are certain space optimisations implemented via promotion rules to
333# GVs
334
bb112e5a
NC
335foreach (qw (oonk ga_shloip)) {
336 ok(!exists $::{$_}, "no symbols of any sort to start with for $_");
337}
63fa9adc
NC
338
339# A string in place of the typeglob is promoted to the function prototype
340$::{oonk} = "pie";
341my $proto = eval 'prototype \&oonk';
342die if $@;
343is ($proto, "pie", "String is promoted to prototype");
344
345
346# A reference to a value is used to generate a constant subroutine
347foreach my $value (3, "Perl rules", \42, qr/whatever/, [1,2,3], {1=>2},
5c1f4d79 348 \*STDIN, \&ok, \undef, *STDOUT) {
63fa9adc
NC
349 delete $::{oonk};
350 $::{oonk} = \$value;
351 $proto = eval 'prototype \&oonk';
352 die if $@;
353 is ($proto, '', "Prototype for a constant subroutine is empty");
354
355 my $got = eval 'oonk';
356 die if $@;
5c1f4d79 357 is (ref $got, ref $value, "Correct type of value (" . ref($value) . ")");
63fa9adc
NC
358 is ($got, $value, "Value is correctly set");
359}
5c1f4d79 360
bb112e5a
NC
361delete $::{oonk};
362$::{oonk} = \"Value";
363
364*{"ga_shloip"} = \&{"oonk"};
365
366is (ref $::{ga_shloip}, 'SCALAR', "Export of proxy constant as is");
367is (ref $::{oonk}, 'SCALAR', "Export doesn't affect original");
368is (eval 'ga_shloip', "Value", "Constant has correct value");
369is (ref $::{ga_shloip}, 'SCALAR',
93f09d7b 370 "Inlining of constant doesn't change representation");
bb112e5a
NC
371
372delete $::{ga_shloip};
373
374eval 'sub ga_shloip (); 1' or die $@;
375is ($::{ga_shloip}, '', "Prototype is stored as an empty string");
376
377# Check that a prototype expands.
378*{"ga_shloip"} = \&{"oonk"};
379
380is (ref $::{oonk}, 'SCALAR', "Export doesn't affect original");
381is (eval 'ga_shloip', "Value", "Constant has correct value");
382is (ref \$::{ga_shloip}, 'GLOB', "Symbol table has full typeglob");
383
384
385@::zwot = ('Zwot!');
386
387# Check that assignment to an existing typeglob works
388{
389 my $w = '';
390 local $SIG{__WARN__} = sub { $w = $_[0] };
391 *{"zwot"} = \&{"oonk"};
392 is($w, '', "Should be no warning");
393}
394
395is (ref $::{oonk}, 'SCALAR', "Export doesn't affect original");
396is (eval 'zwot', "Value", "Constant has correct value");
397is (ref \$::{zwot}, 'GLOB', "Symbol table has full typeglob");
398is (join ('!', @::zwot), 'Zwot!', "Existing array still in typeglob");
399
400sub spritsits () {
401 "Traditional";
402}
403
404# Check that assignment to an existing subroutine works
405{
406 my $w = '';
407 local $SIG{__WARN__} = sub { $w = $_[0] };
408 *{"spritsits"} = \&{"oonk"};
409 like($w, qr/^Constant subroutine main::spritsits redefined/,
410 "Redefining a constant sub should warn");
411}
412
413is (ref $::{oonk}, 'SCALAR', "Export doesn't affect original");
414is (eval 'spritsits', "Value", "Constant has correct value");
415is (ref \$::{spritsits}, 'GLOB', "Symbol table has full typeglob");
416
bb112e5a
NC
417# Check that assignment to an existing typeglob works
418{
419 my $w = '';
420 local $SIG{__WARN__} = sub { $w = $_[0] };
50baa5ea
VP
421 *{"plunk"} = [];
422 *{"plunk"} = \&{"oonk"};
bb112e5a
NC
423 is($w, '', "Should be no warning");
424}
425
bb112e5a
NC
426is (ref $::{oonk}, 'SCALAR', "Export doesn't affect original");
427is (eval 'plunk', "Value", "Constant has correct value");
428is (ref \$::{plunk}, 'GLOB', "Symbol table has full typeglob");
429
430my $gr = eval '\*plunk' or die;
431
432{
433 my $w = '';
434 local $SIG{__WARN__} = sub { $w = $_[0] };
50baa5ea 435 *{$gr} = \&{"oonk"};
2111d928 436 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
437}
438
439is (ref $::{oonk}, 'SCALAR', "Export doesn't affect original");
440is (eval 'plunk', "Value", "Constant has correct value");
441is (ref \$::{plunk}, 'GLOB', "Symbol table has full typeglob");
442
50baa5ea
VP
443# Non-void context should defeat the optimisation, and will cause the original
444# to be promoted (what change 26482 intended)
445my $result;
446{
447 my $w = '';
448 local $SIG{__WARN__} = sub { $w = $_[0] };
449 $result = *{"awkkkkkk"} = \&{"oonk"};
450 is($w, '', "Should be no warning");
451}
452
453is (ref \$result, 'GLOB',
454 "Non void assignment should still return a typeglob");
455
456is (ref \$::{oonk}, 'GLOB', "This export does affect original");
457is (eval 'plunk', "Value", "Constant has correct value");
458is (ref \$::{plunk}, 'GLOB', "Symbol table has full typeglob");
459
460delete $::{oonk};
461$::{oonk} = \"Value";
462
463sub non_dangling {
464 my $w = '';
465 local $SIG{__WARN__} = sub { $w = $_[0] };
466 *{"zap"} = \&{"oonk"};
467 is($w, '', "Should be no warning");
468}
469
470non_dangling();
471is (ref $::{oonk}, 'SCALAR', "Export doesn't affect original");
472is (eval 'zap', "Value", "Constant has correct value");
473is (ref $::{zap}, 'SCALAR', "Exported target is also a PCS");
474
475sub dangling {
476 local $SIG{__WARN__} = sub { die $_[0] };
477 *{"biff"} = \&{"oonk"};
478}
479
480dangling();
481is (ref \$::{oonk}, 'GLOB', "This export does affect original");
482is (eval 'biff', "Value", "Constant has correct value");
483is (ref \$::{biff}, 'GLOB', "Symbol table has full typeglob");
484
6f1b3ab0
FC
485$::{yarrow} = [4,5,6];
486is join("-", eval "yarrow()"), '4-5-6', 'array ref as stash elem';
487is ref $::{yarrow}, "ARRAY", 'stash elem is still array ref after use';
488is join("-", eval "&yarrow"), '4-5-6', 'calling const list with &';
489is join("-", eval "&yarrow(1..10)"), '4-5-6', 'const list ignores & args';
490is prototype "yarrow", "", 'const list has "" prototype';
491is eval "yarrow", 3, 'const list in scalar cx returns length';
492
acaa9288
NC
493{
494 use vars qw($glook $smek $foof);
495 # Check reference assignment isn't affected by the SV type (bug #38439)
496 $glook = 3;
497 $smek = 4;
498 $foof = "halt and cool down";
499
500 my $rv = \*smek;
501 is($glook, 3);
502 *glook = $rv;
503 is($glook, 4);
504
505 my $pv = "";
506 $pv = \*smek;
507 is($foof, "halt and cool down");
508 *foof = $pv;
509 is($foof, 4);
510}
511
5c1f4d79
NC
512format =
513.
514
6f1b3ab0 515foreach my $value ({1=>2}, *STDOUT{IO}, \&ok, *STDOUT{FORMAT}) {
5c1f4d79
NC
516 # *STDOUT{IO} returns a reference to a PVIO. As it's blessed, ref returns
517 # IO::Handle, which isn't what we want.
518 my $type = $value;
519 $type =~ s/.*=//;
520 $type =~ s/\(.*//;
521 delete $::{oonk};
522 $::{oonk} = $value;
523 $proto = eval 'prototype \&oonk';
524 like ($@, qr/^Cannot convert a reference to $type to typeglob/,
525 "Cannot upgrade ref-to-$type to typeglob");
526}
f9d52e31
NC
527
528{
529 no warnings qw(once uninitialized);
530 my $g = \*clatter;
531 my $r = eval {no strict; ${*{$g}{SCALAR}}};
532 is ($@, '', "PERL_DONT_CREATE_GVSV shouldn't affect thingy syntax");
533
534 $g = \*vowm;
535 $r = eval {use strict; ${*{$g}{SCALAR}}};
536 is ($@, '',
537 "PERL_DONT_CREATE_GVSV shouldn't affect thingy syntax under strict");
538}
539
06be3b40
NC
540{
541 # Bug reported by broquaint on IRC
542 *slosh::{HASH}->{ISA}=[];
543 slosh->import;
544 pass("gv_fetchmeth coped with the unexpected");
9e0d86f8
NC
545
546 # An audit found these:
547 {
548 package slosh;
549 sub rip {
550 my $s = shift;
551 $s->SUPER::rip;
552 }
553 }
554 eval {slosh->rip;};
555 like ($@, qr/^Can't locate object method "rip"/, "Even with SUPER");
556
557 is(slosh->isa('swoosh'), '');
558
559 $CORE::GLOBAL::{"lock"}=[];
560 eval "no warnings; lock";
561 like($@, qr/^Not enough arguments for lock/,
562 "Can't trip up general keyword overloading");
563
564 $CORE::GLOBAL::{"readline"}=[];
b3c9268e 565 eval "<STDOUT> if 0";
9e0d86f8 566 is($@, '', "Can't trip up readline overloading");
d5e716f5
NC
567
568 $CORE::GLOBAL::{"readpipe"}=[];
569 eval "`` if 0";
570 is($@, '', "Can't trip up readpipe overloading");
06be3b40 571}
53a42478
NC
572
573{
574 die if exists $::{BONK};
575 $::{BONK} = \"powie";
576 *{"BONK"} = \&{"BONK"};
577 eval 'is(BONK(), "powie",
93f09d7b 578 "Assignment works when glob created midway (bug 45607)"); 1'
53a42478
NC
579 or die $@;
580}
1f257c95
NC
581
582# For now these tests are here, but they would probably be better in a file for
583# tests for croaks. (And in turn, that probably deserves to be in a different
584# directory. Gerard Goossen has a point about the layout being unclear
585
586sub coerce_integer {
587 no warnings 'numeric';
588 $_[0] |= 0;
589}
590sub coerce_number {
591 no warnings 'numeric';
592 $_[0] += 0;
593}
594sub coerce_string {
595 $_[0] .= '';
596}
597
598foreach my $type (qw(integer number string)) {
599 my $prog = "coerce_$type(*STDERR)";
600 is (scalar eval "$prog; 1", undef, "$prog failed...");
601 like ($@, qr/Can't coerce GLOB to $type in/,
602 "with the correct error message");
603}
604
e8986967 605# RT #65582 anonymous glob should be defined, and not coredump when
1809c940
DM
606# stringified. The behaviours are:
607#
52a6327b
FC
608# defined($glob) "$glob" $glob .= ...
609# 5.8.8 false "" with uninit warning "" with uninit warning
610# 5.10.0 true (coredump) (coredump)
611# 5.1[24] true "" "" with uninit warning
612# 5.16 true "*__ANON__::..." "*__ANON__::..."
1809c940
DM
613
614{
615 my $io_ref = *STDOUT{IO};
616 my $glob = *$io_ref;
e8986967 617 ok(defined $glob, "RT #65582 anon glob should be defined");
1809c940
DM
618
619 my $warn = '';
620 local $SIG{__WARN__} = sub { $warn = $_[0] };
621 use warnings;
622 my $str = "$glob";
e8986967 623 is($warn, '', "RT #65582 anon glob stringification shouldn't warn");
885f468a 624 is($str, '*__ANON__::__ANONIO__',
e8986967 625 "RT #65582/#96326 anon glob stringification");
1809c940
DM
626}
627
25451cef
FC
628# Another stringification bug: Test that recursion does not cause lexical
629# handles to lose their names.
630sub r {
631 my @output;
632 @output = r($_[0]-1) if $_[0];
633 open my $fh, "TEST";
634 push @output, $$fh;
635 close $fh;
636 @output;
637}
638is join(' ', r(4)),
639 '*main::$fh *main::$fh *main::$fh *main::$fh *main::$fh',
640 'recursion does not cause lex handles to lose their names';
641
3a6ce63a
FC
642# And sub cloning, too; not just recursion
643my $close_over_me;
644is join(' ', sub {
645 () = $close_over_me;
646 my @output;
647 @output = CORE::__SUB__->($_[0]-1) if $_[0];
648 open my $fh, "TEST";
649 push @output, $$fh;
650 close $fh;
651 @output;
652 }->(4)),
653 '*main::$fh *main::$fh *main::$fh *main::$fh *main::$fh',
654 'sub cloning does not cause lex handles to lose their names';
655
1f730e6c
FC
656# [perl #71254] - Assigning a glob to a variable that has a current
657# match position. (We are testing that Perl_magic_setmglob respects globs'
658# special used of SvSCREAM.)
659{
660 $m = 2; $m=~s/./0/gems; $m= *STDERR;
661 is(
662 "$m", "*main::STDERR",
663 '[perl #71254] assignment of globs to vars with pos'
664 );
665}
666
2867cdbc
Z
667# [perl #72740] - indirect object syntax, heuristically imputed due to
668# the non-existence of a function, should not cause a stash entry to be
669# created for the non-existent function.
670{
671 package RT72740a;
672 my $f = bless({}, RT72740b);
673 sub s1 { s2 $f; }
674 our $s4;
675 sub s3 { s4 $f; }
676}
677{
678 package RT72740b;
679 sub s2 { "RT72740b::s2" }
680 sub s4 { "RT72740b::s4" }
681}
682ok(exists($RT72740a::{s1}), "RT72740a::s1 exists");
683ok(!exists($RT72740a::{s2}), "RT72740a::s2 does not exist");
684ok(exists($RT72740a::{s3}), "RT72740a::s3 exists");
685ok(exists($RT72740a::{s4}), "RT72740a::s4 exists");
686is(RT72740a::s1(), "RT72740b::s2", "RT72740::s1 parsed correctly");
687is(RT72740a::s3(), "RT72740b::s4", "RT72740::s3 parsed correctly");
688
b9e00b79
LRG
689# [perl #71686] Globs that are in symbol table can be un-globbed
690$sym = undef;
691$::{fake} = *sym;
692is (eval 'local *::fake = \"chuck"; $fake', 'chuck',
693 "Localized glob didn't coerce into a RV");
694is ($@, '', "Can localize FAKE glob that's present in stash");
695is (scalar $::{fake}, "*main::sym",
696 "Localized FAKE glob's value was correctly restored");
697
0fe688f5 698# [perl #1804] *$x assignment when $x is a copy of another glob
0095ccd4 699# And [perl #77508] (same thing with list assignment)
0fe688f5
FC
700{
701 no warnings 'once';
702 my $x = *_random::glob_that_is_not_used_elsewhere;
703 *$x = sub{};
704 is(
705 "$x", '*_random::glob_that_is_not_used_elsewhere',
706 '[perl #1804] *$x assignment when $x is FAKE',
707 );
0095ccd4
FC
708 $x = *_random::glob_that_is_not_used_elsewhere;
709 (my $dummy, *$x) = (undef,[]);
710 is(
711 "$x", '*_random::glob_that_is_not_used_elsewhere',
712 '[perl #77508] *$x list assignment when $x is FAKE',
713 ) or require Devel::Peek, Devel::Peek::Dump($x);
0fe688f5
FC
714}
715
cf203c62
FR
716# [perl #76540]
717# this caused panics or 'Attempt to free unreferenced scalar'
718# (its a compile-time issue, so the die lets us skip the prints)
719{
720 my @warnings;
721 local $SIG{__WARN__} = sub { push @warnings, @_ };
722
723 eval <<'EOF';
724BEGIN { $::{FOO} = \'bar' }
725die "made it";
726print FOO, "\n";
727print FOO, "\n";
728EOF
729
730 like($@, qr/made it/, "#76540 - no panic");
731 ok(!@warnings, "#76540 - no 'Attempt to free unreferenced scalar'");
732}
733
13be902c
FC
734# [perl #77362] various bugs related to globs as PVLVs
735{
736 no warnings qw 'once void';
737 my %h; # We pass a key of this hash to the subroutine to get a PVLV.
738 sub { for(shift) {
739 # Set up our glob-as-PVLV
740 $_ = *hon;
741
742 # Bad symbol for array
743 ok eval{ @$_; 1 }, 'PVLV glob slots can be autovivified' or diag $@;
744
745 # This should call TIEHANDLE, not TIESCALAR
746 *thext::TIEHANDLE = sub{};
747 ok eval{ tie *$_, 'thext'; 1 }, 'PVLV globs can be tied as handles'
748 or diag $@;
749
750 # Assigning undef to the glob should not overwrite it...
751 {
752 my $w;
753 local $SIG{__WARN__} = sub { $w = shift };
754 *$_ = undef;
755 is $_, "*main::hon", 'PVLV: assigning undef to the glob does nothing';
756 like $w, qr\Undefined value assigned to typeglob\,
757 'PVLV: assigning undef to the glob warns';
758 }
759
2acc3314 760 # Neither should reference assignment.
13be902c 761 *$_ = [];
2acc3314 762 is $_, "*main::hon", "PVLV: arrayref assignment assigns to the AV slot";
13be902c
FC
763
764 # Concatenation should still work.
765 ok eval { $_ .= 'thlew' }, 'PVLV concatenation does not die' or diag $@;
2acc3314 766 is $_, '*main::honthlew', 'PVLV concatenation works';
13be902c
FC
767
768 # And we should be able to overwrite it with a string, number, or refer-
769 # ence, too, if we omit the *.
770 $_ = *hon; $_ = 'tzor';
771 is $_, 'tzor', 'PVLV: assigning a string over a glob';
772 $_ = *hon; $_ = 23;
773 is $_, 23, 'PVLV: assigning an integer over a glob';
774 $_ = *hon; $_ = 23.23;
775 is $_, 23.23, 'PVLV: assigning a float over a glob';
776 $_ = *hon; $_ = \my $sthat;
777 is $_, \$sthat, 'PVLV: assigning a reference over a glob';
778
779 # This bug was found by code inspection. Could this ever happen in
780 # real life? :-)
781 # This duplicates a file handle, accessing it through a PVLV glob, the
782 # glob having been removed from the symbol table, so a stringified form
783 # of it does not work. This checks that sv_2io does not stringify a PVLV.
784 $_ = *quin;
785 open *quin, "test.pl"; # test.pl is as good a file as any
786 delete $::{quin};
787 ok eval { open my $zow, "<&", $_ }, 'PVLV: sv_2io stringifieth not'
788 or diag $@;
789
790 # Similar tests to make sure sv_2cv etc. do not stringify.
791 *$_ = sub { 1 };
792 ok eval { &$_ }, "PVLV glob can be called as a sub" or diag $@;
793 *flelp = sub { 2 };
794 $_ = 'flelp';
795 is eval { &$_ }, 2, 'PVLV holding a string can be called as a sub'
796 or diag $@;
797
798 # Coderef-to-glob assignment when the glob is no longer accessible
799 # under its name: These tests are to make sure the OPpASSIGN_CV_TO_GV
800 # optimisation takes PVLVs into account, which is why the RHSs have to be
801 # named subs.
802 use constant gheen => 'quare';
803 $_ = *ming;
804 delete $::{ming};
805 *$_ = \&gheen;
806 is eval { &$_ }, 'quare',
807 'PVLV: constant assignment when the glob is detached from the symtab'
808 or diag $@;
809 $_ = *bength;
810 delete $::{bength};
811 *gheck = sub { 'lon' };
812 *$_ = \&gheck;
813 is eval { &$_ }, 'lon',
814 'PVLV: coderef assignment when the glob is detached from the symtab'
815 or diag $@;
816
e77ae825
NC
817SKIP: {
818 skip_if_miniperl("no dynamic loading on miniperl, so can't load PerlIO::scalar", 1);
819 # open should accept a PVLV as its first argument
820 $_ = *hon;
821 ok eval { open $_,'<', \my $thlext }, 'PVLV can be the first arg to open'
822 or diag $@;
823 }
13be902c
FC
824
825 # -t should not stringify
826 $_ = *thlit; delete $::{thlit};
827 *$_ = *STDOUT{IO};
828 ok defined -t $_, 'PVLV: -t does not stringify';
829
830 # neither should -T
804401ea
FC
831 # but some systems don’t support this on file handles
832 my $pass;
833 ok
834 eval {
835 open my $quile, "<", 'test.pl';
836 $_ = *$quile;
837 $pass = -T $_;
838 1
839 } ? $pass : $@ =~ /not implemented on filehandles/,
840 "PVLV: -T does not stringify";
13be902c
FC
841
842 # Unopened file handle
843 {
844 my $w;
845 local $SIG{__WARN__} = sub { $w .= shift };
846 $_ = *vor;
847 close $_;
848 like $w, qr\unopened filehandle vor\,
849 'PVLV globs get their names reported in unopened error messages';
850 }
851
852 }}->($h{k});
853}
854
251c9b20
NC
855*aieee = 4;
856pass('Can assign integers to typeglobs');
857*aieee = 3.14;
858pass('Can assign floats to typeglobs');
859*aieee = 'pi';
860pass('Can assign strings to typeglobs');
861
0936ef8b
FC
862{
863 package thrext;
864 sub TIESCALAR{bless[]}
865 sub STORE{ die "No!"}
866 sub FETCH{ no warnings 'once'; *thrit }
867 tie my $a, "thrext";
868 () = "$a"; # do a fetch; now $a holds a glob
869 eval { *$a = sub{} };
870 untie $a;
871 eval { $a = "bar" };
872 ::is $a, "bar",
873 "[perl #77812] Globs in tied scalars can be reified if STORE dies"
874}
875
8d33f848
FC
876# These two crashed prior to 5.13.6. In 5.13.6 they were fatal errors. They
877# were fixed in 5.13.7.
878ok eval {
879 my $glob = \*heen::ISA;
880 delete $::{"heen::"};
881 *$glob = *bar;
882}, "glob-to-*ISA assignment works when *ISA has lost its stash";
883ok eval {
884 my $glob = \*slare::ISA;
885 delete $::{"slare::"};
886 *$glob = [];
887}, "array-to-*ISA assignment works when *ISA has lost its stash";
888# These two crashed in 5.13.6. They were likewise fixed in 5.13.7.
889ok eval {
890 sub greck;
4a68a320 891 my $glob = do { no warnings "once"; \*phing::foo};
8d33f848
FC
892 delete $::{"phing::"};
893 *$glob = *greck;
dc115b7c 894}, "Assigning a glob-with-sub to a glob that has lost its stash works";
8d33f848
FC
895ok eval {
896 sub pon::foo;
897 my $glob = \*pon::foo;
898 delete $::{"pon::"};
899 *$glob = *foo;
dc115b7c 900}, "Assigning a glob to a glob-with-sub that has lost its stash works";
8d33f848 901
d8906c05
FC
902{
903 package Tie::Alias;
904 sub TIESCALAR{ bless \\pop }
905 sub FETCH { $${$_[0]} }
906 sub STORE { $${$_[0]} = $_[1] }
907 package main;
908 tie my $alias, 'Tie::Alias', my $var;
909 no warnings 'once';
910 $var = *galobbe;
911 {
912 local *$alias = [];
913 $var = 3;
914 is $alias, 3, "[perl #77926] Glob reification during localisation";
915 }
916}
8d33f848 917
b0d55c99
FC
918# This code causes gp_free to call a destructor when a glob is being
919# restored on scope exit. The destructor used to see SVs with a refcount of
920# zero inside the glob, which could result in crashes (though not in this
921# test case, which just panics).
922{
923 no warnings 'once';
924 my $survived;
925 *Trit::DESTROY = sub {
926 $thwext = 42; # panic
927 $survived = 1;
928 };
929 {
930 local *thwext;
931 $thwext = bless[],'Trit';
932 ();
933 }
934 ok $survived,
935 'no error when gp_free calls a destructor that assigns to the gv';
936}
937
4571f4a7
FC
938# This is a similar test, for destructors seeing a GV without a reference
939# count on its gp.
940sub undefine_me_if_you_dare {}
941bless \&undefine_me_if_you_dare, "Undefiner";
942sub Undefiner::DESTROY {
943 undef *undefine_me_if_you_dare;
944}
945{
946 my $w;
947 local $SIG{__WARN__} = sub { $w .= shift };
948 undef *undefine_me_if_you_dare;
949 is $w, undef,
950 'undeffing a gv in DESTROY triggered by undeffing the same gv'
951}
952
795eb8c8
FC
953# [perl #121242]
954# More gp_free madness. gp_free could call a destructor that frees the gv
955# whose gp is being freed.
956sub Fred::AUTOLOAD { $Fred::AUTOLOAD }
957undef *{"Fred::AUTOLOAD"};
958pass 'no crash from gp_free triggering gv_try_downgrade';
959sub _121242::DESTROY { delete $_121242::{$_[0][0]} };
960${"_121242::foo"} = bless ["foo"], _121242::;
961undef *{"_121242::foo"};
962pass 'no crash from pp_undef/gp_free freeing the gv';
963${"_121242::bar"} = bless ["bar"], _121242::;
964*{"_121242::bar"} = "bar";
965pass 'no crash from sv_setsv/gp_free freeing the gv';
966${"_121242::baz"} = bless ["baz"], _121242::;
967*{"_121242::baz"} = *foo;
968pass 'no crash from glob_assign_glob/gp_free freeing the gv';
969{
970 my $foo;
971 undef *_121242::DESTROY;
972 *_121242::DESTROY = sub { undef $foo };
973 my $set_up_foo = sub {
974 # Make $$foo into a fake glob whose array slot holds a blessed
975 # array that undefines $foo, freeing the fake glob.
976 $foo = undef;
977 $$foo = do {local *bar};
978 *$$foo = bless [], _121242::;
979 };
980 &$set_up_foo;
981 $$foo = 3;
982 pass 'no crash from sv_setsv/sv_unglob/gp_free freeing the gv';
983 &$set_up_foo;
984 utf8::encode $$foo;
985 pass 'no crash from sv_utf8_encode/sv_unglob/gp_free freeing the gv';
986 &$set_up_foo;
987 open BAR, "TEST";
988 $$foo .= <BAR>;
989 pass 'no crash from do_readline/sv_unglob/gp_free freeing the gv';
990 close BAR;
991 &$set_up_foo;
992 $$foo .= 3;
993 pass 'no crash from pp_concat/sv_unglob/gp_free freeing the gv';
994 &$set_up_foo;
995 no warnings;
996 $$foo++;
997 pass 'no crash from sv_inc/sv_unglob/gp_free freeing the gv';
998 &$set_up_foo;
999 $$foo--;
1000 pass 'no crash from sv_dec/sv_unglob/gp_free freeing the gv';
1001 &$set_up_foo;
1002 undef $$foo;
1003 pass 'no crash from pp_undef/sv_unglob/gp_free freeing the gv';
1004 $foo = undef;
1005 $$foo = 3;
1006 $$foo =~ s/3/$$foo = do {local *bar}; *$$foo = bless [],_121242::; 4/e;
1007 pass 'no crash from pp_substcont/sv_unglob/gp_free freeing the gv';
1008}
1009
f132ae69
FC
1010# *{undef}
1011eval { *{my $undef} = 3 };
1012like $@, qr/^Can't use an undefined value as a symbol reference at /,
1013 '*{ $undef } assignment';
1014eval { *{;undef} = 3 };
1015like $@, qr/^Can't use an undefined value as a symbol reference at /,
1016 '*{ ;undef } assignment';
1017
914ecc63
FC
1018# [perl #99142] defined &{"foo"} when there is a constant stub
1019# If I break your module, you get to have it mentioned in Perl's tests. :-)
1020package HTTP::MobileAttribute::Plugin::Locator {
1021 use constant LOCATOR_GPS => 1;
1022 ::ok defined &{__PACKAGE__."::LOCATOR_GPS"},
1023 'defined &{"name of constant"}';
1024 ::ok Internals::SvREFCNT(${__PACKAGE__."::"}{LOCATOR_GPS}),
1025 "stash elem for slot is not freed prematurely";
1026}
1027
e38acfd7
FC
1028# Check that constants promoted to CVs point to the right GVs when the name
1029# contains a null.
1030package lrcg {
1031 use constant x => 3;
1032 # These two lines abuse the optimisation that copies the scalar ref from
1033 # one stash element to another, to get a constant with a null in its name
1034 *{"yz\0a"} = \&{"x"};
1035 my $ref = \&{"yz\0a"};
1036 ::ok !exists $lrcg::{yz},
1037 'constants w/nulls in their names point 2 the right GVs when promoted';
1038}
1039
af230ad6
FC
1040{
1041 no warnings 'io';
1042 stat *{"try_downgrade"};
1043 -T _;
1044 $bang = $!;
1045 eval "*try_downgrade if 0";
1046 -T _;
1047 is "$!",$bang,
1048 'try_downgrade does not touch PL_statgv (last stat handle)';
2be08ad1
FC
1049 readline *{"try_downgrade2"};
1050 my $lastfh = "${^LAST_FH}";
1051 eval "*try_downgrade2 if 0";
1052 is ${^LAST_FH}, $lastfh, 'try_downgrade does not touch PL_last_in_gv';
af230ad6
FC
1053}
1054
0fbaa0e5
FC
1055is runperl(prog => '$s = STDERR; close $s; undef *$s;'
1056 .'eval q-*STDERR if 0-; *$s = *STDOUT{IO}; warn'),
1057 "Warning: something's wrong at -e line 1.\n",
1058 "try_downgrade does not touch PL_stderrgv";
1059
8941bf97
FC
1060is runperl(prog =>
1061 'use constant foo=>1; BEGIN { $x = \&foo } undef &$x; $x->()',
1062 stderr=>1),
1063 "Undefined subroutine &main::foo called at -e line 1.\n",
1064 "gv_try_downgrade does not anonymise CVs referenced elsewhere";
1065
acb187b4
FC
1066package glob_constant_test {
1067 sub foo { 42 }
1068 use constant bar => *foo;
1069 BEGIN { undef *foo }
1070 ::is eval { bar->() }, eval { &{+bar} },
1071 'glob_constant->() is not mangled at compile time';
1072 ::is "$@", "", 'no error from eval { &{+glob_constant} }';
1073}
1074
f9509170
FC
1075# Look away, please.
1076# This violates perl's internal structures by fiddling with stashes in a
1077# way that should never happen, but perl should not start trying to free
1078# unallocated memory as a result. There is no ok() or is() because the
1079# panic that used to occur only occurred during global destruction, and
1080# only with PERL_DESTRUCT_LEVEL=2. (The panic itself was sufficient for
1081# the harness to consider this test script to have failed.)
1082$::{aoeuaoeuaoeaoeu} = __PACKAGE__; # cow
1083() = *{"aoeuaoeuaoeaoeu"};
1084
2f222bbd
FC
1085$x = *_119051;
1086$y = \&$x;
1087undef $x;
1088eval { &$y };
1089pass "No crash due to CvGV(vivified stub) pointing to flattened glob copy";
1090# Not really supported, but this should not crash either:
1091$x = *_119051again;
1092delete $::{_119051again};
1093$::{_119051again} = $x; # now we have a fake glob under the right name
1094$y = \&$x; # so when this tries to look up the right GV for
1095undef $::{_119051again}; # CvGV, it still gets a fake one
1096eval { $y->() };
1097pass "No crash due to CvGV pointing to glob copy in the stash";
1098
99491443 1099__END__
98e007d4
NC
1100Perl
1101Rules
1102perl
1103rocks