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