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