This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
lvref.t: Tests for \my assignment and scope exit
[perl5.git] / t / op / lvref.t
CommitLineData
72ed4618
FC
1BEGIN {
2 chdir 't';
3 require './test.pl';
4 set_up_inc("../lib");
5}
6
30bccb25 7plan 135;
72ed4618 8
53abf431
FC
9sub on { $::TODO = ' ' }
10sub off{ $::TODO = '' }
72ed4618
FC
11
12eval '\$x = \$y';
13like $@, qr/^Experimental lvalue references not enabled/,
14 'error when feature is disabled';
26a50d99
FC
15eval '\($x) = \$y';
16like $@, qr/^Experimental lvalue references not enabled/,
17 'error when feature is disabled (aassign)';
72ed4618
FC
18
19use feature 'lvalue_refs';
20
21{
22 my($w,$c);
23 local $SIG{__WARN__} = sub { $c++; $w = shift };
24 eval '\$x = \$y';
25 is $c, 1, 'one warning from lv ref assignment';
26 like $w, qr/^Lvalue references are experimental/,
27 'experimental warning';
26a50d99
FC
28 undef $c;
29 eval '\($x) = \$y';
30 is $c, 1, 'one warning from lv ref list assignment';
31 like $w, qr/^Lvalue references are experimental/,
32 'experimental warning';
72ed4618
FC
33}
34
35no warnings 'experimental::lvalue_refs';
36
37# Scalars
38
39eval '\$x = \$y';
40is \$x, \$y, '\$pkg_scalar = ...';
41my $m;
53abf431 42\$m = \$y;
72ed4618 43is \$m, \$y, '\$lexical = ...';
fc048fcf 44\my $n = \$y;
72ed4618
FC
45is \$n, \$y, '\my $lexical = ...';
46@_ = \$_;
26a50d99 47\($x) = @_;
72ed4618 48is \$x, \$_, '\($pkgvar) = ... gives list context';
238ef7dc 49undef *x;
26a50d99 50(\$x) = @_;
238ef7dc 51is \$x, \$_, '(\$pkgvar) = ... gives list context';
72ed4618 52my $o;
c146a62a 53\($o) = @_;
72ed4618 54is \$o, \$_, '\($lexical) = ... gives list cx';
26a50d99 55my $q;
c146a62a 56(\$q) = @_;
238ef7dc 57is \$q, \$_, '(\$lexical) = ... gives list cx';
c146a62a 58\(my $p) = @_;
72ed4618 59is \$p, \$_, '\(my $lexical) = ... gives list cx';
c146a62a 60(\my $r) = @_;
238ef7dc 61is \$r, \$_, '(\my $lexical) = ... gives list cx';
c146a62a 62\my($s) = @_;
238ef7dc 63is \$s, \$_, '\my($lexical) = ... gives list cx';
217e3565 64\($_a, my $a) = @{[\$b, \$c]};
72ed4618
FC
65is \$_a, \$b, 'package scalar in \(...)';
66is \$a, \$c, 'lex scalar in \(...)';
c146a62a 67(\$_b, \my $b) = @{[\$b, \$c]};
72ed4618
FC
68is \$_b, \$::b, 'package scalar in (\$foo, \$bar)';
69is \$b, \$c, 'lex scalar in (\$foo, \$bar)';
2a57afb1
FC
70is do { \local $l = \3; $l }, 3, '\local $scalar assignment';
71is $l, undef, 'localisation unwound';
72is do { \(local $l) = \4; $l }, 4, '\(local $scalar) assignment';
11ea28ee 73is $l, undef, 'localisation unwound';
29a3d628
FC
74\$foo = \*bar;
75is *foo{SCALAR}, *bar{GLOB}, 'globref-to-scalarref assignment';
30bccb25
FC
76for (1,2) {
77 \my $x = \3 if $_ == 1;
78 \my($y) = \3 if $_ == 1;
79 if ($_ == 2) {
80 is $x, undef, '\my $x = ... clears $x on scope exit';
81 is $y, undef, '\my($x) = ... clears $x on scope exit';
82 }
83}
72ed4618
FC
84
85# Array Elements
86
0ca7b7f7
FC
87sub expect_scalar_cx { wantarray ? 0 : \$_ }
88sub expect_list_cx { wantarray ? (\$_,\$_) : 0 }
89\$a[0] = expect_scalar_cx;
6102323a 90is \$a[0], \$_, '\$array[0]';
0ca7b7f7 91\($a[1]) = expect_list_cx;
6102323a
FC
92is \$a[1], \$_, '\($array[0])';
93{
94 my @a;
0ca7b7f7 95 \$a[0] = expect_scalar_cx;
6102323a 96 is \$a[0], \$_, '\$lexical_array[0]';
0ca7b7f7 97 \($a[1]) = expect_list_cx;
6102323a 98 is \$a[1], \$_, '\($lexical_array[0])';
40d2b828
FC
99 my $tmp;
100 {
101 \local $a[0] = \$tmp;
102 is \$a[0], \$tmp, '\local $a[0]';
103 }
104 is \$a[0], \$_, '\local $a[0] unwound';
105 {
106 \local ($a[1]) = \$tmp;
107 is \$a[1], \$tmp, '\local ($a[0])';
108 }
109 is \$a[1], \$_, '\local $a[0] unwound';
6102323a 110}
0ca7b7f7
FC
111{
112 my @a;
113 \@a[0,1] = expect_list_cx;
114 is \$a[0].\$a[1], \$_.\$_, '\@array[indices]';
115 \(@a[2,3]) = expect_list_cx;
116 is \$a[0].\$a[1], \$_.\$_, '\(@array[indices])';
117 my $tmp;
118 {
119 \local @a[0,1] = (\$tmp)x2;
120 is \$a[0].\$a[1], \$tmp.\$tmp, '\local @a[indices]';
121 }
122 is \$a[0].\$a[1], \$_.\$_, '\local @a[indices] unwound';
123}
72ed4618
FC
124
125# Hash Elements
126
5f94141d
FC
127\$h{a} = expect_scalar_cx;
128is \$h{a}, \$_, '\$hash{a}';
129\($h{b}) = expect_list_cx;
130is \$h{b}, \$_, '\($hash{a})';
131{
132 my @h;
133 \$h{a} = expect_scalar_cx;
134 is \$h{a}, \$_, '\$lexical_array{a}';
135 \($h{b}) = expect_list_cx;
136 is \$h{b}, \$_, '\($lexical_array{a})';
137 my $tmp;
138 {
139 \local $h{a} = \$tmp;
140 is \$h{a}, \$tmp, '\local $h{a}';
141 }
142 is \$h{a}, \$_, '\local $h{a} unwound';
143 {
144 \local ($h{b}) = \$tmp;
145 is \$h{b}, \$tmp, '\local ($h{a})';
146 }
147 is \$h{b}, \$_, '\local $h{a} unwound';
148}
149{
150 my @h;
151 \@h{"a","b"} = expect_list_cx;
152 is \$h{a}.\$h{b}, \$_.\$_, '\@hash{indices}';
153 \(@h{2,3}) = expect_list_cx;
154 is \$h{a}.\$h{b}, \$_.\$_, '\(@hash{indices})';
155 my $tmp;
156 {
157 \local @h{"a","b"} = (\$tmp)x2;
158 is \$h{a}.\$h{b}, \$tmp.\$tmp, '\local @h{indices}';
159 }
160 is \$h{a}.\$h{b}, \$_.\$_, '\local @h{indices} unwound';
161}
72ed4618
FC
162
163# Arrays
164
4779e7f9
FC
165package ArrayTest {
166 BEGIN { *is = *main::is }
167 sub expect_scalar_cx { wantarray ? 0 : \@ThatArray }
168 sub expect_list_cx { wantarray ? (\$_,\$_) : 0 }
169 sub expect_list_cx_a { wantarray ? (\@ThatArray)x2 : 0 }
170 eval '\@a = expect_scalar_cx';
171 is \@a, \@ThatArray, '\@pkg';
172 my @a;
3f114923 173 \@a = expect_scalar_cx;
4779e7f9 174 is \@a, \@ThatArray, '\@lexical';
9782ce69 175 (\@b) = expect_list_cx_a;
4779e7f9
FC
176 is \@b, \@ThatArray, '(\@pkg)';
177 my @b;
9782ce69 178 (\@b) = expect_list_cx_a;
4779e7f9 179 is \@b, \@ThatArray, '(\@lexical)';
3f114923 180 \my @c = expect_scalar_cx;
4779e7f9 181 is \@c, \@ThatArray, '\my @lexical';
bdaf10a5 182 (\my @d) = expect_list_cx_a;
4779e7f9 183 is \@d, \@ThatArray, '(\my @lexical)';
bdaf10a5
FC
184 \(@e) = expect_list_cx;
185 is \$e[0].\$e[1], \$_.\$_, '\(@pkg)';
4779e7f9 186 my @e;
bdaf10a5
FC
187 \(@e) = expect_list_cx;
188 is \$e[0].\$e[1], \$_.\$_, '\(@lexical)';
189 \(my @f) = expect_list_cx;
190 is \$f[0].\$f[1], \$_.\$_, '\(my @lexical)';
191 \my(@g) = expect_list_cx;
192 is \$g[0].\$g[1], \$_.\$_, '\my(@lexical)';
30494daf 193 my $old = \@h;
30494daf
FC
194 {
195 \local @h = \@ThatArray;
196 is \@h, \@ThatArray, '\local @a';
197 }
30494daf
FC
198 is \@h, $old, '\local @a unwound';
199 $old = \@i;
30494daf
FC
200 eval q{
201 (\local @i) = \@ThatArray;
202 is \@i, \@ThatArray, '(\local @a)';
203 } or do { SKIP: { ::skip 'unimplemented' } };
204 is \@i, $old, '(\local @a) unwound';
4779e7f9 205}
30bccb25
FC
206for (1,2) {
207 \my @x = [1..3] if $_ == 1;
208 \my(@y) = \3 if $_ == 1;
209 if ($_ == 2) {
210 is @x, 0, '\my @x = ... clears @x on scope exit';
211 is @y, 0, '\my(@x) = ... clears @x on scope exit';
212 }
213}
72ed4618
FC
214
215# Hashes
216
87da42eb
FC
217package HashTest {
218 BEGIN { *is = *main::is }
219 sub expect_scalar_cx { wantarray ? 0 : \%ThatHash }
220 sub expect_list_cx { wantarray ? (\%ThatHash)x2 : 0 }
3f114923 221 \%a = expect_scalar_cx;
87da42eb
FC
222 is \%a, \%ThatHash, '\%pkg';
223 my %a;
3f114923 224 \%a = expect_scalar_cx;
87da42eb 225 is \%a, \%ThatHash, '\%lexical';
9782ce69
FC
226 (\%b) = expect_list_cx;
227 is \%b, \%ThatHash, '(\%pkg)';
87da42eb 228 my %b;
9782ce69 229 (\%b) = expect_list_cx;
87da42eb 230 is \%b, \%ThatHash, '(\%lexical)';
3f114923 231 \my %c = expect_scalar_cx;
87da42eb 232 is \%c, \%ThatHash, '\my %lexical';
9782ce69 233 (\my %d) = expect_list_cx;
87da42eb 234 is \%d, \%ThatHash, '(\my %lexical)';
30494daf 235 my $old = \%h;
30494daf
FC
236 {
237 \local %h = \%ThatHash;
238 is \%h, \%ThatHash, '\local %a';
239 }
30494daf
FC
240 is \%h, $old, '\local %a unwound';
241 $old = \%i;
30494daf
FC
242 eval q{
243 (\local %i) = \%ThatHash;
244 is \%i, \%ThatHash, '(\local %a)';
245 } or do { SKIP: { ::skip 'unimplemented' } };
246 is \%i, $old, '(\local %a) unwound';
87da42eb 247}
30bccb25
FC
248for (1,2) {
249 \my %x = {1,2} if $_ == 1;
250 if ($_ == 2) {
251 is %x, 0, '\my %x = ... clears %x on scope exit';
252 }
253}
72ed4618
FC
254
255# Subroutines
256
408e9044
FC
257package CodeTest {
258 BEGIN { *is = *main::is; }
259 use feature 'lexical_subs', 'state';
260 no warnings 'experimental::lexical_subs';
261 sub expect_scalar_cx { wantarray ? 0 : \&ThatSub }
262 sub expect_list_cx { wantarray ? (\&ThatSub)x2 : 0 }
263 \&a = expect_scalar_cx;
264 is \&a, \&ThatSub, '\&pkg';
265 my sub a;
266 \&a = expect_scalar_cx;
267 is \&a, \&ThatSub, '\&mysub';
268 state sub as;
269 \&as = expect_scalar_cx;
270 is \&as, \&ThatSub, '\&statesub';
271 (\&b) = expect_list_cx;
272 is \&b, \&ThatSub, '(\&pkg)';
273 my sub b;
274 (\&b) = expect_list_cx;
275 is \&b, \&ThatSub, '(\&mysub)';
276 my sub bs;
277 (\&bs) = expect_list_cx;
278 is \&bs, \&ThatSub, '(\&statesub)';
279 \(&c) = expect_list_cx;
280 is \&c, \&ThatSub, '\(&pkg)';
281 my sub b;
282 \(&c) = expect_list_cx;
283 is \&c, \&ThatSub, '\(&mysub)';
284 my sub bs;
285 \(&cs) = expect_list_cx;
286 is \&cs, \&ThatSub, '\(&statesub)';
287}
72ed4618 288
b7ae253e 289# Mixed List Assignments
72ed4618 290
26a50d99 291(\$tahi, $rua) = \(1,2);
9fc71ff4
FC
292is join(' ', $tahi, $$rua), '1 2',
293 'mixed scalar ref and scalar list assignment';
b7ae253e
FC
294
295# Conditional expressions
296
9fc71ff4 297$_ = 3;
d1094c5b 298$_ == 3 ? \$tahi : $rua = \3;
9fc71ff4 299is $tahi, 3, 'cond assignment resolving to scalar ref';
d1094c5b 300$_ == 0 ? \$toru : $wha = \3;
9fc71ff4 301is $$wha, 3, 'cond assignment resolving to scalar';
d1094c5b 302$_ == 3 ? \$rima : \$ono = \5;
9e592f5a 303is $rima, 5, 'cond assignment with refgens on both branches';
7664512e
FC
304\($_ == 3 ? $whitu : $waru) = \5;
305is $whitu, 5, '\( ?: ) assignment';
72ed4618 306
096cc2cc
FC
307# Foreach
308
362e758e
FC
309for \my $topic (\$for1, \$for2) {
310 push @for, \$topic;
311}
096cc2cc 312is "@for", \$for1 . ' ' . \$for2, 'foreach \my $a';
362e758e
FC
313is \$topic, \$::topic, 'for \my scoping';
314
315@for = ();
316for \$::a(\$for1, \$for2) {
317 push @for, \$::a;
318}
319is "@for", \$for1 . ' ' . \$for2, 'foreach \$::a';
096cc2cc
FC
320
321@for = ();
362e758e 322for \my @a([1,2], [3,4]) {
096cc2cc 323 push @for, @a;
362e758e 324}
096cc2cc
FC
325is "@for", "1 2 3 4", 'foreach \my @a [perl #22335]';
326
327@for = ();
362e758e
FC
328for \@::a([1,2], [3,4]) {
329 push @for, @::a;
330}
331is "@for", "1 2 3 4", 'foreach \@::a [perl #22335]';
332
333@for = ();
334for \my %a({5,6}, {7,8}) {
096cc2cc 335 push @for, %a;
362e758e 336}
096cc2cc
FC
337is "@for", "5 6 7 8", 'foreach \my %a [perl #22335]';
338
339@for = ();
362e758e
FC
340for \%::a({5,6}, {7,8}) {
341 push @for, %::a;
342}
343is "@for", "5 6 7 8", 'foreach \%::a [perl #22335]';
344
345@for = ();
346{
347 use feature 'lexical_subs';
348 no warnings 'experimental::lexical_subs';
349 my sub a;
350 for \&a(sub {9}, sub {10}) {
096cc2cc
FC
351 push @for, &a;
352 }
362e758e
FC
353}
354is "@for", "9 10", 'foreach \&padcv';
096cc2cc 355
362e758e
FC
356@for = ();
357for \&::a(sub {9}, sub {10}) {
358 push @for, &::a;
359}
360is "@for", "9 10", 'foreach \&rv2cv';
096cc2cc 361
72ed4618
FC
362# Errors
363
b3717a0e
FC
364eval { my $x; \$x = 3 };
365like $@, qr/^Assigned value is not a reference at/, 'assigning non-ref';
366eval { my $x; \$x = [] };
367like $@, qr/^Assigned value is not a SCALAR reference at/,
368 'assigning non-scalar ref to scalar ref';
d8a875d9
FC
369eval { \$::x = [] };
370like $@, qr/^Assigned value is not a SCALAR reference at/,
371 'assigning non-scalar ref to package scalar ref';
0719038d
FC
372eval { my @x; \@x = {} };
373like $@, qr/^Assigned value is not an ARRAY reference at/,
374 'assigning non-array ref to array ref';
375eval { \@::x = {} };
376like $@, qr/^Assigned value is not an ARRAY reference at/,
377 'assigning non-array ref to package array ref';
378eval { my %x; \%x = [] };
379like $@, qr/^Assigned value is not a HASH reference at/,
380 'assigning non-hash ref to hash ref';
381eval { \%::x = [] };
382like $@, qr/^Assigned value is not a HASH reference at/,
383 'assigning non-hash ref to package hash ref';
408e9044
FC
384eval { use feature 'lexical_subs';
385 no warnings 'experimental::lexical_subs';
386 my sub x; \&x = [] };
387like $@, qr/^Assigned value is not a CODE reference at/,
388 'assigning non-code ref to lexical code ref';
389eval { \&::x = [] };
390like $@, qr/^Assigned value is not a CODE reference at/,
391 'assigning non-code ref to package code ref';
d6378458 392
bd9bf01b
FC
393eval { my $x; (\$x) = 3 };
394like $@, qr/^Assigned value is not a reference at/,
395 'list-assigning non-ref';
396eval { my $x; (\$x) = [] };
397like $@, qr/^Assigned value is not a SCALAR reference at/,
398 'list-assigning non-scalar ref to scalar ref';
399eval { (\$::x = []) };
400like $@, qr/^Assigned value is not a SCALAR reference at/,
401 'list-assigning non-scalar ref to package scalar ref';
402eval { my @x; (\@x) = {} };
403like $@, qr/^Assigned value is not an ARRAY reference at/,
404 'list-assigning non-array ref to array ref';
405eval { (\@::x) = {} };
406like $@, qr/^Assigned value is not an ARRAY reference at/,
407 'list-assigning non-array ref to package array ref';
408eval { my %x; (\%x) = [] };
409like $@, qr/^Assigned value is not a HASH reference at/,
410 'list-assigning non-hash ref to hash ref';
411eval { (\%::x) = [] };
412like $@, qr/^Assigned value is not a HASH reference at/,
413 'list-assigning non-hash ref to package hash ref';
408e9044
FC
414eval { use feature 'lexical_subs';
415 no warnings 'experimental::lexical_subs';
416 my sub x; (\&x) = [] };
417like $@, qr/^Assigned value is not a CODE reference at/,
418 'list-assigning non-code ref to lexical code ref';
419eval { (\&::x) = [] };
420like $@, qr/^Assigned value is not a CODE reference at/,
421 'list-assigning non-code ref to package code ref';
bd9bf01b 422
d6378458
FC
423eval '(\do{}) = 42';
424like $@, qr/^Can't modify reference to do block in list assignment at /,
425 "Can't modify reference to do block in list assignment";
d6378458
FC
426eval '(\pos) = 42';
427like $@,
428 qr/^Can't modify reference to match position in list assignment at /,
429 "Can't modify ref to some scalar-returning op in list assignment";
d6378458
FC
430eval '(\glob) = 42';
431like $@,
432 qr/^Can't modify reference to glob in list assignment at /,
433 "Can't modify reference to some list-returning op in list assignment";
d6b7592f
FC
434eval '\pos = 42';
435like $@,
436 qr/^Can't modify reference to match position in scalar assignment at /,
437 "Can't modify ref to some scalar-returning op in scalar assignment";
30494daf
FC
438eval '\(local @b) = 42';
439like $@,
bdaf10a5 440 qr/^Can't modify reference to localized parenthesized array in list(?x:
30494daf
FC
441 ) assignment at /,
442 q"Can't modify \(local @array) in list assignment";
443eval '\local(@b) = 42';
444like $@,
bdaf10a5 445 qr/^Can't modify reference to localized parenthesized array in list(?x:
30494daf
FC
446 ) assignment at /,
447 q"Can't modify \local(@array) in list assignment";
bdaf10a5
FC
448eval '\local(@{foo()}) = 42';
449like $@,
450 qr/^Can't modify reference to array dereference in list assignment at/,
451 q"'Array deref' error takes prec. over 'local paren' error";
87da42eb
FC
452eval '\(%b) = 42';
453like $@,
454 qr/^Can't modify reference to parenthesized hash in list assignment a/,
455 "Can't modify ref to parenthesized package hash in scalar assignment";
456eval '\(my %b) = 42';
457like $@,
458 qr/^Can't modify reference to parenthesized hash in list assignment a/,
459 "Can't modify ref to parenthesized hash (\(my %b)) in list assignment";
460eval '\my(%b) = 42';
461like $@,
462 qr/^Can't modify reference to parenthesized hash in list assignment a/,
463 "Can't modify ref to parenthesized hash (\my(%b)) in list assignment";
464eval '\%{"42"} = 42';
465like $@,
466 qr/^Can't modify reference to hash dereference in scalar assignment a/,
467 "Can't modify reference to hash dereference in scalar assignment";
63702de8
FC
468eval '$foo ? \%{"42"} : \%43 = 42';
469like $@,
470 qr/^Can't modify reference to hash dereference in scalar assignment a/,
471 "Can't modify ref to whatever in scalar assignment via cond expr";
3f114923 472on;
87da42eb 473
781ff25d
FC
474
475# Miscellaneous
476
477{
478 my($x,$y);
479 sub {
480 sub {
481 \$x = \$y;
482 }->();
483 is \$x, \$y, 'lexical alias affects outer closure';
484 }->();
485 is \$x, \$y, 'lexical alias affects outer sub where vars are declared';
486}
81cb1af6
FC
487
488{ # PADSTALE has a double meaning
489 use feature 'lexical_subs', 'signatures', 'state';
490 no warnings 'experimental';
491 my $c;
492 my sub s ($arg) {
493 state $x = ++$c;
494 if ($arg == 3) { return $c }
495 goto skip if $arg == 2;
496 my $y;
497 skip:
498 # $y is PADSTALE the 2nd time
499 \$x = \$y if $arg == 2;
500 }
501 s(1);
502 s(2);
503 is s(3), 1, 'padstale alias should not reset state'
504}
cf5d2d91
FC
505
506off;
507SKIP: {
508 skip_without_dynamic_extension('List/Util');
509 require Scalar::Util;
510 my $a;
511 Scalar::Util::weaken($r = \$a);
512 \$a = $r;
513 pass 'no crash when assigning \$lex = $weakref_to_lex'
514}