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