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