This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[MERGE] refactor pp_match(), pp_subst(), regexec()
[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
6f1b3ab0 15plan( tests => 250 );
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
1f730e6c
FC
640# [perl #71254] - Assigning a glob to a variable that has a current
641# match position. (We are testing that Perl_magic_setmglob respects globs'
642# special used of SvSCREAM.)
643{
644 $m = 2; $m=~s/./0/gems; $m= *STDERR;
645 is(
646 "$m", "*main::STDERR",
647 '[perl #71254] assignment of globs to vars with pos'
648 );
649}
650
2867cdbc
Z
651# [perl #72740] - indirect object syntax, heuristically imputed due to
652# the non-existence of a function, should not cause a stash entry to be
653# created for the non-existent function.
654{
655 package RT72740a;
656 my $f = bless({}, RT72740b);
657 sub s1 { s2 $f; }
658 our $s4;
659 sub s3 { s4 $f; }
660}
661{
662 package RT72740b;
663 sub s2 { "RT72740b::s2" }
664 sub s4 { "RT72740b::s4" }
665}
666ok(exists($RT72740a::{s1}), "RT72740a::s1 exists");
667ok(!exists($RT72740a::{s2}), "RT72740a::s2 does not exist");
668ok(exists($RT72740a::{s3}), "RT72740a::s3 exists");
669ok(exists($RT72740a::{s4}), "RT72740a::s4 exists");
670is(RT72740a::s1(), "RT72740b::s2", "RT72740::s1 parsed correctly");
671is(RT72740a::s3(), "RT72740b::s4", "RT72740::s3 parsed correctly");
672
b9e00b79
LRG
673# [perl #71686] Globs that are in symbol table can be un-globbed
674$sym = undef;
675$::{fake} = *sym;
676is (eval 'local *::fake = \"chuck"; $fake', 'chuck',
677 "Localized glob didn't coerce into a RV");
678is ($@, '', "Can localize FAKE glob that's present in stash");
679is (scalar $::{fake}, "*main::sym",
680 "Localized FAKE glob's value was correctly restored");
681
0fe688f5 682# [perl #1804] *$x assignment when $x is a copy of another glob
0095ccd4 683# And [perl #77508] (same thing with list assignment)
0fe688f5
FC
684{
685 no warnings 'once';
686 my $x = *_random::glob_that_is_not_used_elsewhere;
687 *$x = sub{};
688 is(
689 "$x", '*_random::glob_that_is_not_used_elsewhere',
690 '[perl #1804] *$x assignment when $x is FAKE',
691 );
0095ccd4
FC
692 $x = *_random::glob_that_is_not_used_elsewhere;
693 (my $dummy, *$x) = (undef,[]);
694 is(
695 "$x", '*_random::glob_that_is_not_used_elsewhere',
696 '[perl #77508] *$x list assignment when $x is FAKE',
697 ) or require Devel::Peek, Devel::Peek::Dump($x);
0fe688f5
FC
698}
699
cf203c62
FR
700# [perl #76540]
701# this caused panics or 'Attempt to free unreferenced scalar'
702# (its a compile-time issue, so the die lets us skip the prints)
703{
704 my @warnings;
705 local $SIG{__WARN__} = sub { push @warnings, @_ };
706
707 eval <<'EOF';
708BEGIN { $::{FOO} = \'bar' }
709die "made it";
710print FOO, "\n";
711print FOO, "\n";
712EOF
713
714 like($@, qr/made it/, "#76540 - no panic");
715 ok(!@warnings, "#76540 - no 'Attempt to free unreferenced scalar'");
716}
717
13be902c
FC
718# [perl #77362] various bugs related to globs as PVLVs
719{
720 no warnings qw 'once void';
721 my %h; # We pass a key of this hash to the subroutine to get a PVLV.
722 sub { for(shift) {
723 # Set up our glob-as-PVLV
724 $_ = *hon;
725
726 # Bad symbol for array
727 ok eval{ @$_; 1 }, 'PVLV glob slots can be autovivified' or diag $@;
728
729 # This should call TIEHANDLE, not TIESCALAR
730 *thext::TIEHANDLE = sub{};
731 ok eval{ tie *$_, 'thext'; 1 }, 'PVLV globs can be tied as handles'
732 or diag $@;
733
734 # Assigning undef to the glob should not overwrite it...
735 {
736 my $w;
737 local $SIG{__WARN__} = sub { $w = shift };
738 *$_ = undef;
739 is $_, "*main::hon", 'PVLV: assigning undef to the glob does nothing';
740 like $w, qr\Undefined value assigned to typeglob\,
741 'PVLV: assigning undef to the glob warns';
742 }
743
2acc3314 744 # Neither should reference assignment.
13be902c 745 *$_ = [];
2acc3314 746 is $_, "*main::hon", "PVLV: arrayref assignment assigns to the AV slot";
13be902c
FC
747
748 # Concatenation should still work.
749 ok eval { $_ .= 'thlew' }, 'PVLV concatenation does not die' or diag $@;
2acc3314 750 is $_, '*main::honthlew', 'PVLV concatenation works';
13be902c
FC
751
752 # And we should be able to overwrite it with a string, number, or refer-
753 # ence, too, if we omit the *.
754 $_ = *hon; $_ = 'tzor';
755 is $_, 'tzor', 'PVLV: assigning a string over a glob';
756 $_ = *hon; $_ = 23;
757 is $_, 23, 'PVLV: assigning an integer over a glob';
758 $_ = *hon; $_ = 23.23;
759 is $_, 23.23, 'PVLV: assigning a float over a glob';
760 $_ = *hon; $_ = \my $sthat;
761 is $_, \$sthat, 'PVLV: assigning a reference over a glob';
762
763 # This bug was found by code inspection. Could this ever happen in
764 # real life? :-)
765 # This duplicates a file handle, accessing it through a PVLV glob, the
766 # glob having been removed from the symbol table, so a stringified form
767 # of it does not work. This checks that sv_2io does not stringify a PVLV.
768 $_ = *quin;
769 open *quin, "test.pl"; # test.pl is as good a file as any
770 delete $::{quin};
771 ok eval { open my $zow, "<&", $_ }, 'PVLV: sv_2io stringifieth not'
772 or diag $@;
773
774 # Similar tests to make sure sv_2cv etc. do not stringify.
775 *$_ = sub { 1 };
776 ok eval { &$_ }, "PVLV glob can be called as a sub" or diag $@;
777 *flelp = sub { 2 };
778 $_ = 'flelp';
779 is eval { &$_ }, 2, 'PVLV holding a string can be called as a sub'
780 or diag $@;
781
782 # Coderef-to-glob assignment when the glob is no longer accessible
783 # under its name: These tests are to make sure the OPpASSIGN_CV_TO_GV
784 # optimisation takes PVLVs into account, which is why the RHSs have to be
785 # named subs.
786 use constant gheen => 'quare';
787 $_ = *ming;
788 delete $::{ming};
789 *$_ = \&gheen;
790 is eval { &$_ }, 'quare',
791 'PVLV: constant assignment when the glob is detached from the symtab'
792 or diag $@;
793 $_ = *bength;
794 delete $::{bength};
795 *gheck = sub { 'lon' };
796 *$_ = \&gheck;
797 is eval { &$_ }, 'lon',
798 'PVLV: coderef assignment when the glob is detached from the symtab'
799 or diag $@;
800
e77ae825
NC
801SKIP: {
802 skip_if_miniperl("no dynamic loading on miniperl, so can't load PerlIO::scalar", 1);
803 # open should accept a PVLV as its first argument
804 $_ = *hon;
805 ok eval { open $_,'<', \my $thlext }, 'PVLV can be the first arg to open'
806 or diag $@;
807 }
13be902c
FC
808
809 # -t should not stringify
810 $_ = *thlit; delete $::{thlit};
811 *$_ = *STDOUT{IO};
812 ok defined -t $_, 'PVLV: -t does not stringify';
813
814 # neither should -T
804401ea
FC
815 # but some systems don’t support this on file handles
816 my $pass;
817 ok
818 eval {
819 open my $quile, "<", 'test.pl';
820 $_ = *$quile;
821 $pass = -T $_;
822 1
823 } ? $pass : $@ =~ /not implemented on filehandles/,
824 "PVLV: -T does not stringify";
13be902c
FC
825
826 # Unopened file handle
827 {
828 my $w;
829 local $SIG{__WARN__} = sub { $w .= shift };
830 $_ = *vor;
831 close $_;
832 like $w, qr\unopened filehandle vor\,
833 'PVLV globs get their names reported in unopened error messages';
834 }
835
836 }}->($h{k});
837}
838
251c9b20
NC
839*aieee = 4;
840pass('Can assign integers to typeglobs');
841*aieee = 3.14;
842pass('Can assign floats to typeglobs');
843*aieee = 'pi';
844pass('Can assign strings to typeglobs');
845
0936ef8b
FC
846{
847 package thrext;
848 sub TIESCALAR{bless[]}
849 sub STORE{ die "No!"}
850 sub FETCH{ no warnings 'once'; *thrit }
851 tie my $a, "thrext";
852 () = "$a"; # do a fetch; now $a holds a glob
853 eval { *$a = sub{} };
854 untie $a;
855 eval { $a = "bar" };
856 ::is $a, "bar",
857 "[perl #77812] Globs in tied scalars can be reified if STORE dies"
858}
859
8d33f848
FC
860# These two crashed prior to 5.13.6. In 5.13.6 they were fatal errors. They
861# were fixed in 5.13.7.
862ok eval {
863 my $glob = \*heen::ISA;
864 delete $::{"heen::"};
865 *$glob = *bar;
866}, "glob-to-*ISA assignment works when *ISA has lost its stash";
867ok eval {
868 my $glob = \*slare::ISA;
869 delete $::{"slare::"};
870 *$glob = [];
871}, "array-to-*ISA assignment works when *ISA has lost its stash";
872# These two crashed in 5.13.6. They were likewise fixed in 5.13.7.
873ok eval {
874 sub greck;
4a68a320 875 my $glob = do { no warnings "once"; \*phing::foo};
8d33f848
FC
876 delete $::{"phing::"};
877 *$glob = *greck;
dc115b7c 878}, "Assigning a glob-with-sub to a glob that has lost its stash works";
8d33f848
FC
879ok eval {
880 sub pon::foo;
881 my $glob = \*pon::foo;
882 delete $::{"pon::"};
883 *$glob = *foo;
dc115b7c 884}, "Assigning a glob to a glob-with-sub that has lost its stash works";
8d33f848 885
d8906c05
FC
886{
887 package Tie::Alias;
888 sub TIESCALAR{ bless \\pop }
889 sub FETCH { $${$_[0]} }
890 sub STORE { $${$_[0]} = $_[1] }
891 package main;
892 tie my $alias, 'Tie::Alias', my $var;
893 no warnings 'once';
894 $var = *galobbe;
895 {
896 local *$alias = [];
897 $var = 3;
898 is $alias, 3, "[perl #77926] Glob reification during localisation";
899 }
900}
8d33f848 901
b0d55c99
FC
902# This code causes gp_free to call a destructor when a glob is being
903# restored on scope exit. The destructor used to see SVs with a refcount of
904# zero inside the glob, which could result in crashes (though not in this
905# test case, which just panics).
906{
907 no warnings 'once';
908 my $survived;
909 *Trit::DESTROY = sub {
910 $thwext = 42; # panic
911 $survived = 1;
912 };
913 {
914 local *thwext;
915 $thwext = bless[],'Trit';
916 ();
917 }
918 ok $survived,
919 'no error when gp_free calls a destructor that assigns to the gv';
920}
921
f132ae69
FC
922# *{undef}
923eval { *{my $undef} = 3 };
924like $@, qr/^Can't use an undefined value as a symbol reference at /,
925 '*{ $undef } assignment';
926eval { *{;undef} = 3 };
927like $@, qr/^Can't use an undefined value as a symbol reference at /,
928 '*{ ;undef } assignment';
929
914ecc63
FC
930# [perl #99142] defined &{"foo"} when there is a constant stub
931# If I break your module, you get to have it mentioned in Perl's tests. :-)
932package HTTP::MobileAttribute::Plugin::Locator {
933 use constant LOCATOR_GPS => 1;
934 ::ok defined &{__PACKAGE__."::LOCATOR_GPS"},
935 'defined &{"name of constant"}';
936 ::ok Internals::SvREFCNT(${__PACKAGE__."::"}{LOCATOR_GPS}),
937 "stash elem for slot is not freed prematurely";
938}
939
e38acfd7
FC
940# Check that constants promoted to CVs point to the right GVs when the name
941# contains a null.
942package lrcg {
943 use constant x => 3;
944 # These two lines abuse the optimisation that copies the scalar ref from
945 # one stash element to another, to get a constant with a null in its name
946 *{"yz\0a"} = \&{"x"};
947 my $ref = \&{"yz\0a"};
948 ::ok !exists $lrcg::{yz},
949 'constants w/nulls in their names point 2 the right GVs when promoted';
950}
951
f9509170
FC
952# Look away, please.
953# This violates perl's internal structures by fiddling with stashes in a
954# way that should never happen, but perl should not start trying to free
955# unallocated memory as a result. There is no ok() or is() because the
956# panic that used to occur only occurred during global destruction, and
957# only with PERL_DESTRUCT_LEVEL=2. (The panic itself was sufficient for
958# the harness to consider this test script to have failed.)
959$::{aoeuaoeuaoeaoeu} = __PACKAGE__; # cow
960() = *{"aoeuaoeuaoeaoeu"};
961
99491443 962__END__
98e007d4
NC
963Perl
964Rules
965perl
966rocks