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