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