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