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