This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Added porting tests for CUSTOMIZED files
[perl5.git] / t / op / ref.t
1 #!./perl
2
3 BEGIN {
4     chdir 't' if -d 't';
5     @INC = qw(. ../lib);
6     require 'test.pl';
7 }
8
9 use strict qw(refs subs);
10
11 plan(228);
12
13 # Test glob operations.
14
15 $bar = "one";
16 $foo = "two";
17 {
18     local(*foo) = *bar;
19     is($foo, 'one');
20 }
21 is ($foo, 'two');
22
23 $baz = "three";
24 $foo = "four";
25 {
26     local(*foo) = 'baz';
27     is ($foo, 'three');
28 }
29 is ($foo, 'four');
30
31 $foo = "global";
32 {
33     local(*foo);
34     is ($foo, undef);
35     $foo = "local";
36     is ($foo, 'local');
37 }
38 is ($foo, 'global');
39
40 {
41     no strict 'refs';
42 # Test fake references.
43
44     $baz = "valid";
45     $bar = 'baz';
46     $foo = 'bar';
47     is ($$$foo, 'valid');
48 }
49
50 # Test real references.
51
52 $FOO = \$BAR;
53 $BAR = \$BAZ;
54 $BAZ = "hit";
55 is ($$$FOO, 'hit');
56
57 # Test references to real arrays.
58
59 my $test = curr_test();
60 @ary = ($test,$test+1,$test+2,$test+3);
61 $ref[0] = \@a;
62 $ref[1] = \@b;
63 $ref[2] = \@c;
64 $ref[3] = \@d;
65 for $i (3,1,2,0) {
66     push(@{$ref[$i]}, "ok $ary[$i]\n");
67 }
68 print @a;
69 print ${$ref[1]}[0];
70 print @{$ref[2]}[0];
71 {
72     no strict 'refs';
73     print @{'d'};
74 }
75 curr_test($test+4);
76
77 # Test references to references.
78
79 $refref = \\$x;
80 $x = "Good";
81 is ($$$refref, 'Good');
82
83 # Test nested anonymous lists.
84
85 $ref = [[],2,[3,4,5,]];
86 is (scalar @$ref, 3);
87 is ($$ref[1], 2);
88 is (${$$ref[2]}[2], 5);
89 is (scalar @{$$ref[0]}, 0);
90
91 is ($ref->[1], 2);
92 is ($ref->[2]->[0], 3);
93
94 # Test references to hashes of references.
95
96 $refref = \%whatever;
97 $refref->{"key"} = $ref;
98 is ($refref->{"key"}->[2]->[0], 3);
99
100 # Test to see if anonymous subarrays spring into existence.
101
102 $spring[5]->[0] = 123;
103 $spring[5]->[1] = 456;
104 push(@{$spring[5]}, 789);
105 is (join(':',@{$spring[5]}), "123:456:789");
106
107 # Test to see if anonymous subhashes spring into existence.
108
109 @{$spring2{"foo"}} = (1,2,3);
110 $spring2{"foo"}->[3] = 4;
111 is (join(':',@{$spring2{"foo"}}), "1:2:3:4");
112
113 # Test references to subroutines.
114
115 {
116     my $called;
117     sub mysub { $called++; }
118     $subref = \&mysub;
119     &$subref;
120     is ($called, 1);
121 }
122
123 # Test references to return values of operators (TARGs/PADTMPs)
124 {
125     my @refs;
126     for("a", "b") {
127         push @refs, \"$_"
128     }
129     is join(" ", map $$_, @refs), "a b", 'refgen+PADTMP';
130 }
131
132 $subrefref = \\&mysub2;
133 is ($$subrefref->("GOOD"), "good");
134 sub mysub2 { lc shift }
135
136 # Test REGEXP assignment
137
138 SKIP: {
139     skip_if_miniperl("no dynamic loading on miniperl, so can't load re", 5);
140     require re;
141     my $x = qr/x/;
142     my $str = "$x"; # regex stringification may change
143
144     my $y = $$x;
145     is ($y, $str, "bare REGEXP stringifies correctly");
146     ok (eval { "x" =~ $y }, "bare REGEXP matches correctly");
147     
148     my $z = \$y;
149     ok (re::is_regexp($z), "new ref to REXEXP passes is_regexp");
150     is ($z, $str, "new ref to REGEXP stringifies correctly");
151     ok (eval { "x" =~ $z }, "new ref to REGEXP matches correctly");
152 }
153 {
154     my ($x, $str);
155     {
156         my $y = qr/x/;
157         $str = "$y";
158         $x = $$y;
159     }
160     is ($x, $str, "REGEXP keeps a ref to its mother_re");
161     ok (eval { "x" =~ $x }, "REGEXP with mother_re still matches");
162 }
163
164 # Test the ref operator.
165
166 sub PVBM () { 'foo' }
167 { my $dummy = index 'foo', PVBM }
168
169 my $pviv = 1; "$pviv";
170 my $pvnv = 1.0; "$pvnv";
171 my $x;
172
173 # we don't test
174 #   tied lvalue => SCALAR, as we haven't tested tie yet
175 #   BIND, 'cos we can't create them yet
176 #   REGEXP, 'cos that requires overload or Scalar::Util
177
178 for (
179     [ 'undef',          SCALAR  => \undef               ],
180     [ 'constant IV',    SCALAR  => \1                   ],
181     [ 'constant NV',    SCALAR  => \1.0                 ],
182     [ 'constant PV',    SCALAR  => \'f'                 ],
183     [ 'scalar',         SCALAR  => \$x                  ],
184     [ 'PVIV',           SCALAR  => \$pviv               ],
185     [ 'PVNV',           SCALAR  => \$pvnv               ],
186     [ 'PVMG',           SCALAR  => \$0                  ],
187     [ 'PVBM',           SCALAR  => \PVBM                ],
188     [ 'scalar @array',  SCALAR  => \scalar @array       ],
189     [ 'scalar %hash',   SCALAR  => \scalar %hash        ],
190     [ 'vstring',        VSTRING => \v1                  ],
191     [ 'ref',            REF     => \\1                  ],
192     [ 'substr lvalue',  LVALUE  => \substr($x, 0, 0)    ],
193     [ 'pos lvalue',     LVALUE  => \pos                 ],
194     [ 'vec lvalue',     LVALUE  => \vec($x,0,1)         ],     
195     [ 'named array',    ARRAY   => \@ary                ],
196     [ 'anon array',     ARRAY   => [ 1 ]                ],
197     [ 'named hash',     HASH    => \%whatever           ],
198     [ 'anon hash',      HASH    => { a => 1 }           ],
199     [ 'named sub',      CODE    => \&mysub,             ],
200     [ 'anon sub',       CODE    => sub { 1; }           ],
201     [ 'glob',           GLOB    => \*foo                ],
202     [ 'format',         FORMAT  => *STDERR{FORMAT}      ],
203 ) {
204     my ($desc, $type, $ref) = @$_;
205     is (ref $ref, $type, "ref() for ref to $desc");
206     like ("$ref", qr/^$type\(0x[0-9a-f]+\)$/, "stringify for ref to $desc");
207 }
208
209 is (ref *STDOUT{IO}, 'IO::File', 'IO refs are blessed into IO::File');
210 like (*STDOUT{IO}, qr/^IO::File=IO\(0x[0-9a-f]+\)$/,
211     'stringify for IO refs');
212
213 { # Test re-use of ref's TARG [perl #101738]
214   my $obj = bless [], '____';
215   my $uniobj = bless [], chr 256;
216   my $get_ref = sub { ref shift };
217   my $dummy = &$get_ref($uniobj);
218      $dummy = &$get_ref($obj);
219   ok exists { ____ => undef }->{$dummy}, 'ref sets UTF8 flag correctly';
220 }
221
222 # Test anonymous hash syntax.
223
224 $anonhash = {};
225 is (ref $anonhash, 'HASH');
226 $anonhash2 = {FOO => 'BAR', ABC => 'XYZ',};
227 is (join('', sort values %$anonhash2), 'BARXYZ');
228
229 # Test bless operator.
230
231 package MYHASH;
232
233 $object = bless $main'anonhash2;
234 main::is (ref $object, 'MYHASH');
235 main::is ($object->{ABC}, 'XYZ');
236
237 $object2 = bless {};
238 main::is (ref $object2, 'MYHASH');
239
240 # Test ordinary call on object method.
241
242 &mymethod($object,"argument");
243
244 sub mymethod {
245     local($THIS, @ARGS) = @_;
246     die 'Got a "' . ref($THIS). '" instead of a MYHASH'
247         unless ref $THIS eq 'MYHASH';
248     main::is ($ARGS[0], "argument");
249     main::is ($THIS->{FOO}, 'BAR');
250 }
251
252 # Test automatic destructor call.
253
254 $string = "bad";
255 $object = "foo";
256 $string = "good";
257 $main'anonhash2 = "foo";
258 $string = "";
259
260 DESTROY {
261     return unless $string;
262     main::is ($string, 'good');
263
264     # Test that the object has not already been "cursed".
265     main::isnt (ref shift, 'HASH');
266 }
267
268 # Now test inheritance of methods.
269
270 package OBJ;
271
272 @ISA = ('BASEOBJ');
273
274 $main'object = bless {FOO => 'foo', BAR => 'bar'};
275
276 package main;
277
278 # Test arrow-style method invocation.
279
280 is ($object->doit("BAR"), 'bar');
281
282 # Test indirect-object-style method invocation.
283
284 $foo = doit $object "FOO";
285 main::is ($foo, 'foo');
286
287 sub BASEOBJ'doit {
288     local $ref = shift;
289     die "Not an OBJ" unless ref $ref eq 'OBJ';
290     $ref->{shift()};
291 }
292
293 package UNIVERSAL;
294 @ISA = 'LASTCHANCE';
295
296 package LASTCHANCE;
297 sub foo { main::is ($_[1], 'works') }
298
299 package WHATEVER;
300 foo WHATEVER "works";
301
302 #
303 # test the \(@foo) construct
304 #
305 package main;
306 @foo = \(1..3);
307 @bar = \(@foo);
308 @baz = \(1,@foo,@bar);
309 is (scalar (@bar), 3);
310 is (scalar grep(ref($_), @bar), 3);
311 is (scalar (@baz), 3);
312
313 my(@fuu) = \(1..2,3);
314 my(@baa) = \(@fuu);
315 my(@bzz) = \(1,@fuu,@baa);
316 is (scalar (@baa), 3);
317 is (scalar grep(ref($_), @baa), 3);
318 is (scalar (@bzz), 3);
319
320 # also, it can't be an lvalue
321 eval '\\($x, $y) = (1, 2);';
322 like ($@, qr/Can\'t modify.*ref.*in.*assignment/);
323
324 # test for proper destruction of lexical objects
325 $test = curr_test();
326 sub larry::DESTROY { print "# larry\nok $test\n"; }
327 sub curly::DESTROY { print "# curly\nok ", $test + 1, "\n"; }
328 sub moe::DESTROY   { print "# moe\nok ", $test + 2, "\n"; }
329
330 {
331     my ($joe, @curly, %larry);
332     my $moe = bless \$joe, 'moe';
333     my $curly = bless \@curly, 'curly';
334     my $larry = bless \%larry, 'larry';
335     print "# leaving block\n";
336 }
337
338 print "# left block\n";
339 curr_test($test + 3);
340
341 # another glob test
342
343
344 $foo = "garbage";
345 { local(*bar) = "foo" }
346 $bar = "glob 3";
347 local(*bar) = *bar;
348 is ($bar, "glob 3");
349
350 $var = "glob 4";
351 $_   = \$var;
352 is ($$_, 'glob 4');
353
354
355 # test if reblessing during destruction results in more destruction
356 $test = curr_test();
357 {
358     package A;
359     sub new { bless {}, shift }
360     DESTROY { print "# destroying 'A'\nok ", $test + 1, "\n" }
361     package _B;
362     sub new { bless {}, shift }
363     DESTROY { print "# destroying '_B'\nok $test\n"; bless shift, 'A' }
364     package main;
365     my $b = _B->new;
366 }
367 curr_test($test + 2);
368
369 # test if $_[0] is properly protected in DESTROY()
370
371 {
372     my $test = curr_test();
373     my $i = 0;
374     local $SIG{'__DIE__'} = sub {
375         my $m = shift;
376         if ($i++ > 4) {
377             print "# infinite recursion, bailing\nnot ok $test\n";
378             exit 1;
379         }
380         like ($m, qr/^Modification of a read-only/);
381     };
382     package C;
383     sub new { bless {}, shift }
384     DESTROY { $_[0] = 'foo' }
385     {
386         print "# should generate an error...\n";
387         my $c = C->new;
388     }
389     print "# good, didn't recurse\n";
390 }
391
392 # test that DESTROY is called on all objects during global destruction,
393 # even those without hard references [perl #36347]
394
395 is(
396   runperl(
397    stderr => 1, prog => 'sub DESTROY { print qq-aaa\n- } bless \$a[0]'
398   ),
399  "aaa\n", 'DESTROY called on array elem'
400 );
401 is(
402   runperl(
403    stderr => 1,
404    prog => '{ bless \my@x; *a=sub{@x}}sub DESTROY { print qq-aaa\n- }'
405   ),
406  "aaa\n",
407  'DESTROY called on closure variable'
408 );
409
410
411 # test if refgen behaves with autoviv magic
412 {
413     my @a;
414     $a[1] = "good";
415     my $got;
416     for (@a) {
417         $got .= ${\$_};
418         $got .= ';';
419     }
420     is ($got, ";good;");
421 }
422
423 # This test is the reason for postponed destruction in sv_unref
424 $a = [1,2,3];
425 $a = $a->[1];
426 is ($a, 2);
427
428 # This test used to coredump. The BEGIN block is important as it causes the
429 # op that created the constant reference to be freed. Hence the only
430 # reference to the constant string "pass" is in $a. The hack that made
431 # sure $a = $a->[1] would work didn't work with references to constants.
432
433
434 foreach my $lexical ('', 'my $a; ') {
435   my $expect = "pass\n";
436   my $result = runperl (switches => ['-wl'], stderr => 1,
437     prog => $lexical . 'BEGIN {$a = \q{pass}}; $a = $$a; print $a');
438
439   is ($?, 0);
440   is ($result, $expect);
441 }
442
443 $test = curr_test();
444 sub x::DESTROY {print "ok ", $test + shift->[0], "\n"}
445 { my $a1 = bless [3],"x";
446   my $a2 = bless [2],"x";
447   { my $a3 = bless [1],"x";
448     my $a4 = bless [0],"x";
449     567;
450   }
451 }
452 curr_test($test+4);
453
454 is (runperl (switches=>['-l'],
455              prog=> 'print 1; print qq-*$\*-;print 1;'),
456     "1\n*\n*\n1\n");
457
458 # bug #21347
459
460 runperl(prog => 'sub UNIVERSAL::AUTOLOAD { qr// } a->p' );
461 is ($?, 0, 'UNIVERSAL::AUTOLOAD called when freeing qr//');
462
463 runperl(prog => 'sub UNIVERSAL::DESTROY { warn } bless \$a, A', stderr => 1);
464 is ($?, 0, 'warn called inside UNIVERSAL::DESTROY');
465
466
467 # bug #22719
468
469 runperl(prog => 'sub f { my $x = shift; *z = $x; } f({}); f();');
470 is ($?, 0, 'coredump on typeglob = (SvRV && !SvROK)');
471
472 # bug #27268: freeing self-referential typeglobs could trigger
473 # "Attempt to free unreferenced scalar" warnings
474
475 is (runperl(
476     prog => 'use Symbol;my $x=bless \gensym,q{t}; print;*$$x=$x',
477     stderr => 1
478 ), '', 'freeing self-referential typeglob');
479
480 # using a regex in the destructor for STDOUT segfaulted because the
481 # REGEX pad had already been freed (ithreads build only). The
482 # object is required to trigger the early freeing of GV refs to to STDOUT
483
484 TODO: {
485     local $TODO = "works but output through pipe is mangled" if $^O eq 'VMS';
486     like (runperl(
487         prog => '$x=bless[]; sub IO::Handle::DESTROY{$_=q{bad};s/bad/ok/;print}',
488         stderr => 1
489           ), qr/^(ok)+$/, 'STDOUT destructor');
490 }
491
492 {
493     no strict 'refs';
494     $name8 = chr 163;
495     $name_utf8 = $name8 . chr 256;
496     chop $name_utf8;
497
498     is ($$name8, undef, 'Nothing before we start');
499     is ($$name_utf8, undef, 'Nothing before we start');
500     $$name8 = "Pound";
501     is ($$name8, "Pound", 'Accessing via 8 bit symref works');
502     is ($$name_utf8, "Pound", 'Accessing via UTF8 symref works');
503 }
504
505 {
506     no strict 'refs';
507     $name_utf8 = $name = chr 9787;
508     utf8::encode $name_utf8;
509
510     is (length $name, 1, "Name is 1 char");
511     is (length $name_utf8, 3, "UTF8 representation is 3 chars");
512
513     is ($$name, undef, 'Nothing before we start');
514     is ($$name_utf8, undef, 'Nothing before we start');
515     $$name = "Face";
516     is ($$name, "Face", 'Accessing via Unicode symref works');
517     is ($$name_utf8, undef,
518         'Accessing via the UTF8 byte sequence gives nothing');
519 }
520
521 {
522     no strict 'refs';
523     $name1 = "\0Chalk";
524     $name2 = "\0Cheese";
525
526     isnt ($name1, $name2, "They differ");
527
528     is ($$name1, undef, 'Nothing before we start (scalars)');
529     is ($$name2, undef, 'Nothing before we start');
530     $$name1 = "Yummy";
531     is ($$name1, "Yummy", 'Accessing via the correct name works');
532     is ($$name2, undef,
533         'Accessing via a different NUL-containing name gives nothing');
534     # defined uses a different code path
535     ok (defined $$name1, 'defined via the correct name works');
536     ok (!defined $$name2,
537         'defined via a different NUL-containing name gives nothing');
538
539     is ($name1->[0], undef, 'Nothing before we start (arrays)');
540     is ($name2->[0], undef, 'Nothing before we start');
541     $name1->[0] = "Yummy";
542     is ($name1->[0], "Yummy", 'Accessing via the correct name works');
543     is ($name2->[0], undef,
544         'Accessing via a different NUL-containing name gives nothing');
545     ok (defined $name1->[0], 'defined via the correct name works');
546     ok (!defined$name2->[0],
547         'defined via a different NUL-containing name gives nothing');
548
549     my (undef, $one) = @{$name1}[2,3];
550     my (undef, $two) = @{$name2}[2,3];
551     is ($one, undef, 'Nothing before we start (array slices)');
552     is ($two, undef, 'Nothing before we start');
553     @{$name1}[2,3] = ("Very", "Yummy");
554     (undef, $one) = @{$name1}[2,3];
555     (undef, $two) = @{$name2}[2,3];
556     is ($one, "Yummy", 'Accessing via the correct name works');
557     is ($two, undef,
558         'Accessing via a different NUL-containing name gives nothing');
559     ok (defined $one, 'defined via the correct name works');
560     ok (!defined $two,
561         'defined via a different NUL-containing name gives nothing');
562
563     is ($name1->{PWOF}, undef, 'Nothing before we start (hashes)');
564     is ($name2->{PWOF}, undef, 'Nothing before we start');
565     $name1->{PWOF} = "Yummy";
566     is ($name1->{PWOF}, "Yummy", 'Accessing via the correct name works');
567     is ($name2->{PWOF}, undef,
568         'Accessing via a different NUL-containing name gives nothing');
569     ok (defined $name1->{PWOF}, 'defined via the correct name works');
570     ok (!defined $name2->{PWOF},
571         'defined via a different NUL-containing name gives nothing');
572
573     my (undef, $one) = @{$name1}{'SNIF', 'BEEYOOP'};
574     my (undef, $two) = @{$name2}{'SNIF', 'BEEYOOP'};
575     is ($one, undef, 'Nothing before we start (hash slices)');
576     is ($two, undef, 'Nothing before we start');
577     @{$name1}{'SNIF', 'BEEYOOP'} = ("Very", "Yummy");
578     (undef, $one) = @{$name1}{'SNIF', 'BEEYOOP'};
579     (undef, $two) = @{$name2}{'SNIF', 'BEEYOOP'};
580     is ($one, "Yummy", 'Accessing via the correct name works');
581     is ($two, undef,
582         'Accessing via a different NUL-containing name gives nothing');
583     ok (defined $one, 'defined via the correct name works');
584     ok (!defined $two,
585         'defined via a different NUL-containing name gives nothing');
586
587     $name1 = "Left"; $name2 = "Left\0Right";
588     my $glob2 = *{$name2};
589
590     is ($glob1, undef, "We get different typeglobs. In fact, undef");
591
592     *{$name1} = sub {"One"};
593     *{$name2} = sub {"Two"};
594
595     is (&{$name1}, "One");
596     is (&{$name2}, "Two");
597 }
598
599 # test derefs after list slice
600
601 is ( ({foo => "bar"})[0]{foo}, "bar", 'hash deref from list slice w/o ->' );
602 is ( ({foo => "bar"})[0]->{foo}, "bar", 'hash deref from list slice w/ ->' );
603 is ( ([qw/foo bar/])[0][1], "bar", 'array deref from list slice w/o ->' );
604 is ( ([qw/foo bar/])[0]->[1], "bar", 'array deref from list slice w/ ->' );
605 is ( (sub {"bar"})[0](), "bar", 'code deref from list slice w/o ->' );
606 is ( (sub {"bar"})[0]->(), "bar", 'code deref from list slice w/ ->' );
607
608 # deref on empty list shouldn't autovivify
609 {
610     local $@;
611     eval { ()[0]{foo} };
612     like ( "$@", "Can't use an undefined value as a HASH reference",
613            "deref of undef from list slice fails" );
614 }
615
616 # test dereferencing errors
617 {
618     format STDERR =
619 .
620     my $ref;
621     foreach $ref (*STDOUT{IO}, *STDERR{FORMAT}) {
622         eval q/ $$ref /;
623         like($@, qr/Not a SCALAR reference/, "Scalar dereference");
624         eval q/ @$ref /;
625         like($@, qr/Not an ARRAY reference/, "Array dereference");
626         eval q/ %$ref /;
627         like($@, qr/Not a HASH reference/, "Hash dereference");
628         eval q/ &$ref /;
629         like($@, qr/Not a CODE reference/, "Code dereference");
630     }
631
632     $ref = *STDERR{FORMAT};
633     eval q/ *$ref /;
634     like($@, qr/Not a GLOB reference/, "Glob dereference");
635
636     $ref = *STDOUT{IO};
637     eval q/ *$ref /;
638     is($@, '', "Glob dereference of PVIO is acceptable");
639
640     is($ref, *{$ref}{IO}, "IO slot of the temporary glob is set correctly");
641 }
642
643 # these will segfault if they fail
644
645 my $pvbm = PVBM;
646 my $rpvbm = \$pvbm;
647
648 ok (!eval { *$rpvbm }, 'PVBM ref is not a GLOB ref');
649 ok (!eval { *$pvbm }, 'PVBM is not a GLOB ref');
650 ok (!eval { $$pvbm }, 'PVBM is not a SCALAR ref');
651 ok (!eval { @$pvbm }, 'PVBM is not an ARRAY ref');
652 ok (!eval { %$pvbm }, 'PVBM is not a HASH ref');
653 ok (!eval { $pvbm->() }, 'PVBM is not a CODE ref');
654 ok (!eval { $rpvbm->foo }, 'PVBM is not an object');
655
656 # bug 24254
657 is( runperl(stderr => 1, prog => 'map eval qq(exit),1 for 1'), "");
658 is( runperl(stderr => 1, prog => 'eval { for (1) { map { die } 2 } };'), "");
659 is( runperl(stderr => 1, prog => 'for (125) { map { exit } (213)}'), "");
660 my $hushed = $^O eq 'VMS' ? 'use vmsish qw(hushed);' : '';
661 is( runperl(stderr => 1, prog => $hushed . 'map die,4 for 3'), "Died at -e line 1.\n");
662 is( runperl(stderr => 1, prog => $hushed . 'grep die,4 for 3'), "Died at -e line 1.\n");
663 is( runperl(stderr => 1, prog => $hushed . 'for $a (3) {@b=sort {die} 4,5}'), "Died at -e line 1.\n");
664
665 # bug 57564
666 is( runperl(stderr => 1, prog => 'my $i;for $i (1) { for $i (2) { } }'), "");
667
668 # The mechanism for freeing objects in globs used to leave dangling
669 # pointers to freed SVs. To test this, we construct this nested structure:
670 #    GV => blessed(AV) => RV => GV => blessed(SV)
671 # all with a refcnt of 1, and hope that the second GV gets processed first
672 # by do_clean_named_objs.  Then when the first GV is processed, it mustn't
673 # find anything nasty left by the previous GV processing.
674 # The eval is stop things in the main body of the code holding a reference
675 # to a GV, and the print at the end seems to bee necessary to ensure
676 # the correct freeing order of *x and *y (no, I don't know why - DAPM).
677
678 is (runperl(
679         prog => 'eval q[bless \@y; bless \$x; $y[0] = \*x; $z = \*y; ]; '
680                 . 'delete $::{x}; delete $::{y}; print qq{ok\n};',
681         stderr => 1),
682     "ok\n", 'freeing freed glob in global destruction');
683
684
685 # Test undefined hash references as arguments to %{} in boolean context
686 # [perl #81750]
687 {
688  no strict 'refs';
689  eval { my $foo; %$foo;             }; ok !$@, '%$undef';
690  eval { my $foo; scalar %$foo;      }; ok !$@, 'scalar %$undef';
691  eval { my $foo; !%$foo;            }; ok !$@, '!%$undef';
692  eval { my $foo; if ( %$foo) {}     }; ok !$@, 'if ( %$undef) {}';
693  eval { my $foo; if (!%$foo) {}     }; ok !$@, 'if (!%$undef) {}';
694  eval { my $foo; unless ( %$foo) {} }; ok !$@, 'unless ( %$undef) {}';
695  eval { my $foo; unless (!%$foo) {} }; ok !$@, 'unless (!%$undef) {}';
696  eval { my $foo; 1 if %$foo;        }; ok !$@, '1 if %$undef';
697  eval { my $foo; 1 if !%$foo;       }; ok !$@, '1 if !%$undef';
698  eval { my $foo; 1 unless %$foo;    }; ok !$@, '1 unless %$undef;';
699  eval { my $foo; 1 unless ! %$foo;  }; ok !$@, '1 unless ! %$undef';
700  eval { my $foo;  %$foo ? 1 : 0;    }; ok !$@, ' %$undef ? 1 : 0';
701  eval { my $foo; !%$foo ? 1 : 0;    }; ok !$@, '!%$undef ? 1 : 0';
702 }
703
704 # RT #88330
705 # Make sure that a leaked thinggy with multiple weak references to
706 # it doesn't trigger a panic with multiple rounds of global cleanup
707 # (Perl_sv_clean_all).
708
709 SKIP: {
710     skip_if_miniperl('no Scalar::Util under miniperl', 4);
711
712     local $ENV{PERL_DESTRUCT_LEVEL} = 2;
713
714     # we do all permutations of array/hash, 1ref/2ref, to account
715     # for the different way backref magic is stored
716
717     fresh_perl_is(<<'EOF', 'ok', { stderr => 1 }, 'array with 1 weak ref');
718 use Scalar::Util qw(weaken);
719 my $r = [];
720 Internals::SvREFCNT(@$r, 9);
721 my $r1 = $r;
722 weaken($r1);
723 print "ok";
724 EOF
725
726     fresh_perl_is(<<'EOF', 'ok', { stderr => 1 }, 'array with 2 weak refs');
727 use Scalar::Util qw(weaken);
728 my $r = [];
729 Internals::SvREFCNT(@$r, 9);
730 my $r1 = $r;
731 weaken($r1);
732 my $r2 = $r;
733 weaken($r2);
734 print "ok";
735 EOF
736
737     fresh_perl_is(<<'EOF', 'ok', { stderr => 1 }, 'hash with 1 weak ref');
738 use Scalar::Util qw(weaken);
739 my $r = {};
740 Internals::SvREFCNT(%$r, 9);
741 my $r1 = $r;
742 weaken($r1);
743 print "ok";
744 EOF
745
746     fresh_perl_is(<<'EOF', 'ok', { stderr => 1 }, 'hash with 2 weak refs');
747 use Scalar::Util qw(weaken);
748 my $r = {};
749 Internals::SvREFCNT(%$r, 9);
750 my $r1 = $r;
751 weaken($r1);
752 my $r2 = $r;
753 weaken($r2);
754 print "ok";
755 EOF
756
757 }
758
759 SKIP:{
760     skip_if_miniperl "no Scalar::Util on miniperl", 1;
761     my $error;
762     *hassgropper::DESTROY = sub {
763         require Scalar::Util;
764         eval { Scalar::Util::weaken($_[0]) };
765         $error = $@;
766         # This line caused a crash before weaken refused to weaken a
767         # read-only reference:
768         $do::not::overwrite::this = $_[0];
769     };
770     my $xs = bless [], "hassgropper";
771     undef $xs;
772     like $error, qr/^Modification of a read-only/,
773        'weaken refuses to weaken a read-only ref';
774     # Now that the test has passed, avoid sabotaging global destruction:
775     undef *hassgropper::DESTROY;
776     undef $do::not::overwrite::this;
777 }
778
779
780 is ref( bless {}, "nul\0clean" ), "nul\0clean", "ref() is nul-clean";
781
782 # Bit of a hack to make test.pl happy. There are 3 more tests after it leaves.
783 $test = curr_test();
784 curr_test($test + 3);
785 # test global destruction
786
787 my $test1 = $test + 1;
788 my $test2 = $test + 2;
789
790 package FINALE;
791
792 {
793     $ref3 = bless ["ok $test2\n"];      # package destruction
794     my $ref2 = bless ["ok $test1\n"];   # lexical destruction
795     local $ref1 = bless ["ok $test\n"]; # dynamic destruction
796     1;                                  # flush any temp values on stack
797 }
798
799 DESTROY {
800     print $_[0][0];
801 }
802