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