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
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
544303ee 11plan(228);
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
6e592b3a
BM
177
178for (
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 ],
544303ee
FC
188 [ 'scalar @array', SCALAR => \scalar @array ],
189 [ 'scalar %hash', SCALAR => \scalar %hash ],
6e592b3a
BM
190 [ 'vstring', VSTRING => \v1 ],
191 [ 'ref', REF => \\1 ],
7393165e
FC
192 [ 'substr lvalue', LVALUE => \substr($x, 0, 0) ],
193 [ 'pos lvalue', LVALUE => \pos ],
194 [ 'vec lvalue', LVALUE => \vec($x,0,1) ],
6e592b3a
BM
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
d963bf01
NC
209is (ref *STDOUT{IO}, 'IO::File', 'IO refs are blessed into IO::File');
210like (*STDOUT{IO}, qr/^IO::File=IO\(0x[0-9a-f]+\)$/,
6e592b3a 211 'stringify for IO refs');
79072805 212
b535e014
FC
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
79072805
LW
222# Test anonymous hash syntax.
223
224$anonhash = {};
1c509eb9 225is (ref $anonhash, 'HASH');
e24631be 226$anonhash2 = {FOO => 'BAR', ABC => 'XYZ',};
1c509eb9 227is (join('', sort values %$anonhash2), 'BARXYZ');
79072805
LW
228
229# Test bless operator.
230
231package MYHASH;
232
233$object = bless $main'anonhash2;
1c509eb9
NC
234main::is (ref $object, 'MYHASH');
235main::is ($object->{ABC}, 'XYZ');
79072805
LW
236
237$object2 = bless {};
1c509eb9 238main::is (ref $object2, 'MYHASH');
79072805
LW
239
240# Test ordinary call on object method.
241
1c509eb9 242&mymethod($object,"argument");
79072805
LW
243
244sub mymethod {
245 local($THIS, @ARGS) = @_;
ed6116ce 246 die 'Got a "' . ref($THIS). '" instead of a MYHASH'
e24631be 247 unless ref $THIS eq 'MYHASH';
1c509eb9
NC
248 main::is ($ARGS[0], "argument");
249 main::is ($THIS->{FOO}, 'BAR');
79072805
LW
250}
251
252# Test automatic destructor call.
253
1c509eb9 254$string = "bad";
79072805 255$object = "foo";
1c509eb9 256$string = "good";
79072805 257$main'anonhash2 = "foo";
8990e307 258$string = "";
79072805 259
ed6116ce 260DESTROY {
8990e307 261 return unless $string;
1c509eb9 262 main::is ($string, 'good');
79072805 263
a0d0e21e 264 # Test that the object has not already been "cursed".
1c509eb9 265 main::isnt (ref shift, 'HASH');
79072805
LW
266}
267
268# Now test inheritance of methods.
269
270package OBJ;
271
e24631be 272@ISA = ('BASEOBJ');
79072805 273
e24631be 274$main'object = bless {FOO => 'foo', BAR => 'bar'};
79072805
LW
275
276package main;
277
278# Test arrow-style method invocation.
279
e24631be 280is ($object->doit("BAR"), 'bar');
79072805
LW
281
282# Test indirect-object-style method invocation.
283
284$foo = doit $object "FOO";
e24631be 285main::is ($foo, 'foo');
79072805
LW
286
287sub BASEOBJ'doit {
288 local $ref = shift;
e24631be 289 die "Not an OBJ" unless ref $ref eq 'OBJ';
748a9306 290 $ref->{shift()};
79072805 291}
8990e307 292
a0d0e21e
LW
293package UNIVERSAL;
294@ISA = 'LASTCHANCE';
295
296package LASTCHANCE;
805232b4 297sub foo { main::is ($_[1], 'works') }
a0d0e21e
LW
298
299package WHATEVER;
805232b4 300foo WHATEVER "works";
a0d0e21e 301
58e0a6ae
GS
302#
303# test the \(@foo) construct
304#
305package main;
fb53bbb2 306@foo = \(1..3);
58e0a6ae
GS
307@bar = \(@foo);
308@baz = \(1,@foo,@bar);
805232b4
NC
309is (scalar (@bar), 3);
310is (scalar grep(ref($_), @bar), 3);
311is (scalar (@baz), 3);
58e0a6ae 312
fb53bbb2 313my(@fuu) = \(1..2,3);
58e0a6ae
GS
314my(@baa) = \(@fuu);
315my(@bzz) = \(1,@fuu,@baa);
805232b4
NC
316is (scalar (@baa), 3);
317is (scalar grep(ref($_), @baa), 3);
318is (scalar (@bzz), 3);
58e0a6ae 319
75ea820e
SM
320# also, it can't be an lvalue
321eval '\\($x, $y) = (1, 2);';
805232b4 322like ($@, qr/Can\'t modify.*ref.*in.*assignment/);
75ea820e 323
bc44cdaf 324# test for proper destruction of lexical objects
1c509eb9 325$test = curr_test();
805232b4
NC
326sub larry::DESTROY { print "# larry\nok $test\n"; }
327sub curly::DESTROY { print "# curly\nok ", $test + 1, "\n"; }
328sub moe::DESTROY { print "# moe\nok ", $test + 2, "\n"; }
bc44cdaf
GS
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
338print "# left block\n";
805232b4 339curr_test($test + 3);
bc44cdaf 340
fb73857a 341# another glob test
342
805232b4
NC
343
344$foo = "garbage";
fb73857a 345{ local(*bar) = "foo" }
805232b4 346$bar = "glob 3";
fb73857a 347local(*bar) = *bar;
805232b4 348is ($bar, "glob 3");
fb73857a 349
805232b4 350$var = "glob 4";
d4010388 351$_ = \$var;
805232b4 352is ($$_, 'glob 4');
d4010388 353
4e8e7886 354
805232b4
NC
355# test if reblessing during destruction results in more destruction
356$test = curr_test();
4e8e7886
GS
357{
358 package A;
359 sub new { bless {}, shift }
805232b4 360 DESTROY { print "# destroying 'A'\nok ", $test + 1, "\n" }
8bac7e00 361 package _B;
4e8e7886 362 sub new { bless {}, shift }
805232b4 363 DESTROY { print "# destroying '_B'\nok $test\n"; bless shift, 'A' }
4e8e7886 364 package main;
8bac7e00 365 my $b = _B->new;
4e8e7886 366}
805232b4 367curr_test($test + 2);
4e8e7886
GS
368
369# test if $_[0] is properly protected in DESTROY()
370
371{
805232b4 372 my $test = curr_test();
4e8e7886
GS
373 my $i = 0;
374 local $SIG{'__DIE__'} = sub {
375 my $m = shift;
376 if ($i++ > 4) {
805232b4 377 print "# infinite recursion, bailing\nnot ok $test\n";
4e8e7886
GS
378 exit 1;
379 }
805232b4 380 like ($m, qr/^Modification of a read-only/);
4e8e7886
GS
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
5e307270
FC
392# test that DESTROY is called on all objects during global destruction,
393# even those without hard references [perl #36347]
394
395is(
396 runperl(
c1b879e5 397 stderr => 1, prog => 'sub DESTROY { print qq-aaa\n- } bless \$a[0]'
5e307270 398 ),
c1b879e5 399 "aaa\n", 'DESTROY called on array elem'
5e307270
FC
400);
401is(
402 runperl(
403 stderr => 1,
c1b879e5 404 prog => '{ bless \my@x; *a=sub{@x}}sub DESTROY { print qq-aaa\n- }'
5e307270 405 ),
c1b879e5 406 "aaa\n",
5e307270
FC
407 'DESTROY called on closure variable'
408);
640c0c3e 409
5e307270 410
0dd88869 411# test if refgen behaves with autoviv magic
0dd88869
GS
412{
413 my @a;
805232b4
NC
414 $a[1] = "good";
415 my $got;
416 for (@a) {
417 $got .= ${\$_};
418 $got .= ';';
419 }
420 is ($got, ";good;");
0dd88869
GS
421}
422
840a7b70
IZ
423# This test is the reason for postponed destruction in sv_unref
424$a = [1,2,3];
425$a = $a->[1];
805232b4 426is ($a, 2);
840a7b70 427
04ca4930
NC
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
04ca4930
NC
433
434foreach 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
805232b4
NC
439 is ($?, 0);
440 is ($result, $expect);
840a7b70
IZ
441}
442
e24631be 443$test = curr_test();
04ca4930
NC
444sub 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}
805232b4
NC
452curr_test($test+4);
453
454is (runperl (switches=>['-l'],
455 prog=> 'print 1; print qq-*$\*-;print 1;'),
456 "1\n*\n*\n1\n");
b2ce0fda 457
39cff0d9
AE
458# bug #21347
459
460runperl(prog => 'sub UNIVERSAL::AUTOLOAD { qr// } a->p' );
805232b4 461is ($?, 0, 'UNIVERSAL::AUTOLOAD called when freeing qr//');
39cff0d9 462
7b102d90 463runperl(prog => 'sub UNIVERSAL::DESTROY { warn } bless \$a, A', stderr => 1);
805232b4 464is ($?, 0, 'warn called inside UNIVERSAL::DESTROY');
7b102d90 465
23bb1b96
DM
466
467# bug #22719
468
469runperl(prog => 'sub f { my $x = shift; *z = $x; } f({}); f();');
805232b4 470is ($?, 0, 'coredump on typeglob = (SvRV && !SvROK)');
23bb1b96 471
ec5f3c78
DM
472# bug #27268: freeing self-referential typeglobs could trigger
473# "Attempt to free unreferenced scalar" warnings
474
805232b4 475is (runperl(
3d7a9343 476 prog => 'use Symbol;my $x=bless \gensym,q{t}; print;*$$x=$x',
ec5f3c78 477 stderr => 1
805232b4 478), '', 'freeing self-referential typeglob');
23bb1b96 479
804ffa60
DM
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
ff26e4c8
CB
484TODO: {
485 local $TODO = "works but output through pipe is mangled" if $^O eq 'VMS';
486 like (runperl(
3d7a9343 487 prog => '$x=bless[]; sub IO::Handle::DESTROY{$_=q{bad};s/bad/ok/;print}',
ff26e4c8
CB
488 stderr => 1
489 ), qr/^(ok)+$/, 'STDOUT destructor');
490}
804ffa60 491
2e434a10 492{
512d1826
NC
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');
512d1826
NC
502 is ($$name_utf8, "Pound", 'Accessing via UTF8 symref works');
503}
504
2e434a10 505{
512d1826
NC
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');
512d1826
NC
517 is ($$name_utf8, undef,
518 'Accessing via the UTF8 byte sequence gives nothing');
519}
520
431529db 521{
512d1826
NC
522 no strict 'refs';
523 $name1 = "\0Chalk";
524 $name2 = "\0Cheese";
525
526 isnt ($name1, $name2, "They differ");
527
431529db 528 is ($$name1, undef, 'Nothing before we start (scalars)');
512d1826 529 is ($$name2, undef, 'Nothing before we start');
b3d904f3 530 $$name1 = "Yummy";
512d1826 531 is ($$name1, "Yummy", 'Accessing via the correct name works');
512d1826
NC
532 is ($$name2, undef,
533 'Accessing via a different NUL-containing name gives nothing');
fc4809d7
NC
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');
431529db
NC
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');
fc4809d7
NC
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');
431529db
NC
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');
fc4809d7
NC
559 ok (defined $one, 'defined via the correct name works');
560 ok (!defined $two,
561 'defined via a different NUL-containing name gives nothing');
431529db
NC
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');
fc4809d7
NC
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');
431529db
NC
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');
fc4809d7
NC
583 ok (defined $one, 'defined via the correct name works');
584 ok (!defined $two,
585 'defined via a different NUL-containing name gives nothing');
431529db
NC
586
587 $name1 = "Left"; $name2 = "Left\0Right";
588 my $glob2 = *{$name2};
589
88e5f542 590 is ($glob1, undef, "We get different typeglobs. In fact, undef");
780a5241
NC
591
592 *{$name1} = sub {"One"};
593 *{$name2} = sub {"Two"};
594
595 is (&{$name1}, "One");
596 is (&{$name2}, "Two");
512d1826
NC
597}
598
9a9798c2
YST
599# test derefs after list slice
600
601is ( ({foo => "bar"})[0]{foo}, "bar", 'hash deref from list slice w/o ->' );
602is ( ({foo => "bar"})[0]->{foo}, "bar", 'hash deref from list slice w/ ->' );
603is ( ([qw/foo bar/])[0][1], "bar", 'array deref from list slice w/o ->' );
604is ( ([qw/foo bar/])[0]->[1], "bar", 'array deref from list slice w/ ->' );
605is ( (sub {"bar"})[0](), "bar", 'code deref from list slice w/o ->' );
606is ( (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
cbae9b9f
YST
616# test dereferencing errors
617{
2d905216
NC
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");
cbae9b9f
YST
641}
642
6e592b3a
BM
643# these will segfault if they fail
644
645my $pvbm = PVBM;
646my $rpvbm = \$pvbm;
647
648ok (!eval { *$rpvbm }, 'PVBM ref is not a GLOB ref');
649ok (!eval { *$pvbm }, 'PVBM is not a GLOB ref');
650ok (!eval { $$pvbm }, 'PVBM is not a SCALAR ref');
651ok (!eval { @$pvbm }, 'PVBM is not an ARRAY ref');
652ok (!eval { %$pvbm }, 'PVBM is not a HASH ref');
653ok (!eval { $pvbm->() }, 'PVBM is not a CODE ref');
654ok (!eval { $rpvbm->foo }, 'PVBM is not an object');
655
fcf99ed4
B
656# bug 24254
657is( runperl(stderr => 1, prog => 'map eval qq(exit),1 for 1'), "");
658is( runperl(stderr => 1, prog => 'eval { for (1) { map { die } 2 } };'), "");
659is( runperl(stderr => 1, prog => 'for (125) { map { exit } (213)}'), "");
54c717c3
CB
660my $hushed = $^O eq 'VMS' ? 'use vmsish qw(hushed);' : '';
661is( runperl(stderr => 1, prog => $hushed . 'map die,4 for 3'), "Died at -e line 1.\n");
662is( runperl(stderr => 1, prog => $hushed . 'grep die,4 for 3'), "Died at -e line 1.\n");
663is( runperl(stderr => 1, prog => $hushed . 'for $a (3) {@b=sort {die} 4,5}'), "Died at -e line 1.\n");
fcf99ed4
B
664
665# bug 57564
666is( runperl(stderr => 1, prog => 'my $i;for $i (1) { for $i (2) { } }'), "");
667
57ef47cc
DM
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
93f09d7b 673# find anything nasty left by the previous GV processing.
57ef47cc
DM
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
678is (runperl(
679 prog => 'eval q[bless \@y; bless \$x; $y[0] = \*x; $z = \*y; ]; '
bf70f410 680 . 'delete $::{x}; delete $::{y}; print qq{ok\n};',
57ef47cc
DM
681 stderr => 1),
682 "ok\n", 'freeing freed glob in global destruction');
683
fcf99ed4 684
fd1d9b5c
FC
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
da0c0b27
DM
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
709SKIP: {
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');
718use Scalar::Util qw(weaken);
719my $r = [];
720Internals::SvREFCNT(@$r, 9);
721my $r1 = $r;
722weaken($r1);
723print "ok";
724EOF
725
726 fresh_perl_is(<<'EOF', 'ok', { stderr => 1 }, 'array with 2 weak refs');
727use Scalar::Util qw(weaken);
728my $r = [];
729Internals::SvREFCNT(@$r, 9);
730my $r1 = $r;
731weaken($r1);
732my $r2 = $r;
733weaken($r2);
734print "ok";
735EOF
736
737 fresh_perl_is(<<'EOF', 'ok', { stderr => 1 }, 'hash with 1 weak ref');
738use Scalar::Util qw(weaken);
739my $r = {};
740Internals::SvREFCNT(%$r, 9);
741my $r1 = $r;
742weaken($r1);
743print "ok";
744EOF
745
746 fresh_perl_is(<<'EOF', 'ok', { stderr => 1 }, 'hash with 2 weak refs');
747use Scalar::Util qw(weaken);
748my $r = {};
749Internals::SvREFCNT(%$r, 9);
750my $r1 = $r;
751weaken($r1);
752my $r2 = $r;
753weaken($r2);
754print "ok";
755EOF
756
757}
fd1d9b5c 758
5d4ff231
FC
759SKIP:{
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
a15456de
BF
780is ref( bless {}, "nul\0clean" ), "nul\0clean", "ref() is nul-clean";
781
805232b4
NC
782# Bit of a hack to make test.pl happy. There are 3 more tests after it leaves.
783$test = curr_test();
784curr_test($test + 3);
4e8e7886
GS
785# test global destruction
786
840a7b70
IZ
787my $test1 = $test + 1;
788my $test2 = $test + 2;
789
8990e307
LW
790package FINALE;
791
792{
840a7b70
IZ
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
8990e307
LW
796 1; # flush any temp values on stack
797}
798
799DESTROY {
800 print $_[0][0];
801}
804ffa60 802