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