This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Simple \@array and \%hash assignment
[perl5.git] / t / op / lvref.t
CommitLineData
72ed4618
FC
1BEGIN {
2 chdir 't';
3 require './test.pl';
4 set_up_inc("../lib");
5}
6
87da42eb 7plan 88;
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';
72ed4618
FC
76
77# Array Elements
78
0ca7b7f7
FC
79sub expect_scalar_cx { wantarray ? 0 : \$_ }
80sub expect_list_cx { wantarray ? (\$_,\$_) : 0 }
81\$a[0] = expect_scalar_cx;
6102323a 82is \$a[0], \$_, '\$array[0]';
0ca7b7f7 83\($a[1]) = expect_list_cx;
6102323a
FC
84is \$a[1], \$_, '\($array[0])';
85{
86 my @a;
0ca7b7f7 87 \$a[0] = expect_scalar_cx;
6102323a 88 is \$a[0], \$_, '\$lexical_array[0]';
0ca7b7f7 89 \($a[1]) = expect_list_cx;
6102323a 90 is \$a[1], \$_, '\($lexical_array[0])';
40d2b828
FC
91 my $tmp;
92 {
93 \local $a[0] = \$tmp;
94 is \$a[0], \$tmp, '\local $a[0]';
95 }
96 is \$a[0], \$_, '\local $a[0] unwound';
97 {
98 \local ($a[1]) = \$tmp;
99 is \$a[1], \$tmp, '\local ($a[0])';
100 }
101 is \$a[1], \$_, '\local $a[0] unwound';
6102323a 102}
0ca7b7f7
FC
103{
104 my @a;
105 \@a[0,1] = expect_list_cx;
106 is \$a[0].\$a[1], \$_.\$_, '\@array[indices]';
107 \(@a[2,3]) = expect_list_cx;
108 is \$a[0].\$a[1], \$_.\$_, '\(@array[indices])';
109 my $tmp;
110 {
111 \local @a[0,1] = (\$tmp)x2;
112 is \$a[0].\$a[1], \$tmp.\$tmp, '\local @a[indices]';
113 }
114 is \$a[0].\$a[1], \$_.\$_, '\local @a[indices] unwound';
115}
72ed4618
FC
116
117# Hash Elements
118
5f94141d
FC
119\$h{a} = expect_scalar_cx;
120is \$h{a}, \$_, '\$hash{a}';
121\($h{b}) = expect_list_cx;
122is \$h{b}, \$_, '\($hash{a})';
123{
124 my @h;
125 \$h{a} = expect_scalar_cx;
126 is \$h{a}, \$_, '\$lexical_array{a}';
127 \($h{b}) = expect_list_cx;
128 is \$h{b}, \$_, '\($lexical_array{a})';
129 my $tmp;
130 {
131 \local $h{a} = \$tmp;
132 is \$h{a}, \$tmp, '\local $h{a}';
133 }
134 is \$h{a}, \$_, '\local $h{a} unwound';
135 {
136 \local ($h{b}) = \$tmp;
137 is \$h{b}, \$tmp, '\local ($h{a})';
138 }
139 is \$h{b}, \$_, '\local $h{a} unwound';
140}
141{
142 my @h;
143 \@h{"a","b"} = expect_list_cx;
144 is \$h{a}.\$h{b}, \$_.\$_, '\@hash{indices}';
145 \(@h{2,3}) = expect_list_cx;
146 is \$h{a}.\$h{b}, \$_.\$_, '\(@hash{indices})';
147 my $tmp;
148 {
149 \local @h{"a","b"} = (\$tmp)x2;
150 is \$h{a}.\$h{b}, \$tmp.\$tmp, '\local @h{indices}';
151 }
152 is \$h{a}.\$h{b}, \$_.\$_, '\local @h{indices} unwound';
153}
72ed4618
FC
154
155# Arrays
156
4779e7f9
FC
157package ArrayTest {
158 BEGIN { *is = *main::is }
159 sub expect_scalar_cx { wantarray ? 0 : \@ThatArray }
160 sub expect_list_cx { wantarray ? (\$_,\$_) : 0 }
161 sub expect_list_cx_a { wantarray ? (\@ThatArray)x2 : 0 }
162 eval '\@a = expect_scalar_cx';
163 is \@a, \@ThatArray, '\@pkg';
164 my @a;
3f114923 165 \@a = expect_scalar_cx;
4779e7f9 166 is \@a, \@ThatArray, '\@lexical';
3f114923 167::on;
4779e7f9
FC
168 eval '(\@b) = expect_list_cx_a';
169 is \@b, \@ThatArray, '(\@pkg)';
170 my @b;
171 eval '(\@b) = expect_list_cx_a';
172 is \@b, \@ThatArray, '(\@lexical)';
3f114923
FC
173::off;
174 \my @c = expect_scalar_cx;
4779e7f9 175 is \@c, \@ThatArray, '\my @lexical';
3f114923 176::on;
4779e7f9
FC
177 eval '(\my @d) = expect_list_cx_a';
178 is \@d, \@ThatArray, '(\my @lexical)';
179 eval '\(@e) = expect_list_cx';
180 is \$e[0].$e[1], \$_.\$_, '\(@pkg)';
181 my @e;
182 eval '\(@e) = expect_list_cx';
183 is \$e[0].$e[1], \$_.\$_, '\(@lexical)';
184 eval '\(my @f) = expect_list_cx';
185 is \$f[0].$f[1], \$_.\$_, '\(my @lexical)';
186 eval '\my(@g) = expect_list_cx';
187 is \$g[0].$g[1], \$_.\$_, '\my(@lexical)';
188}
72ed4618
FC
189
190# Hashes
191
3f114923 192off;
87da42eb
FC
193package HashTest {
194 BEGIN { *is = *main::is }
195 sub expect_scalar_cx { wantarray ? 0 : \%ThatHash }
196 sub expect_list_cx { wantarray ? (\%ThatHash)x2 : 0 }
3f114923 197 \%a = expect_scalar_cx;
87da42eb
FC
198 is \%a, \%ThatHash, '\%pkg';
199 my %a;
3f114923 200 \%a = expect_scalar_cx;
87da42eb 201 is \%a, \%ThatHash, '\%lexical';
3f114923 202::on;
87da42eb
FC
203 eval '(\%b) = expect_list_cx';
204 is \%b, \%ThatArray, '(\%pkg)';
205 my %b;
206 eval '(\%b) = expect_list_cx';
207 is \%b, \%ThatHash, '(\%lexical)';
3f114923
FC
208::off;
209 \my %c = expect_scalar_cx;
87da42eb 210 is \%c, \%ThatHash, '\my %lexical';
3f114923 211::on;
87da42eb
FC
212 eval '(\my %d) = expect_list_cx';
213 is \%d, \%ThatHash, '(\my %lexical)';
214}
215off;
72ed4618
FC
216
217# Subroutines
218
219# ...
220
b7ae253e 221# Mixed List Assignments
72ed4618 222
26a50d99 223(\$tahi, $rua) = \(1,2);
9fc71ff4
FC
224is join(' ', $tahi, $$rua), '1 2',
225 'mixed scalar ref and scalar list assignment';
26a50d99 226on;
b7ae253e
FC
227
228# Conditional expressions
229
9fc71ff4
FC
230$_ = 3;
231eval '$_ == 3 ? \$tahi : $rua = \3';
232is $tahi, 3, 'cond assignment resolving to scalar ref';
233eval '$_ == 3 ? \$toru : $wha = \3';
234is $$wha, 3, 'cond assignment resolving to scalar';
b7ae253e
FC
235eval '$_ == 3 ? \$rima : \$ono = \5';
236is $$rima, 5, 'cond assignment with refgens on both branches';
72ed4618 237
096cc2cc
FC
238# Foreach
239
240eval '
241 for \my $a(\$for1, \$for2) {
242 push @for, \$a;
243 }
244';
245is "@for", \$for1 . ' ' . \$for2, 'foreach \my $a';
246
247@for = ();
248eval '
249 for \my @a([1,2], [3,4]) {
250 push @for, @a;
251 }
252';
253is "@for", "1 2 3 4", 'foreach \my @a [perl #22335]';
254
255@for = ();
256eval '
257 for \my %a({5,6}, {7,8}) {
258 push @for, %a;
259 }
260';
261is "@for", "5 6 7 8", 'foreach \my %a [perl #22335]';
262
263@for = ();
264eval '
265 for \my &a(sub {9}, sub {10}) {
266 push @for, &a;
267 }
268';
269is "@for", "9 10", 'foreach \my &a';
270
271
72ed4618
FC
272# Errors
273
b3717a0e
FC
274off;
275eval { my $x; \$x = 3 };
276like $@, qr/^Assigned value is not a reference at/, 'assigning non-ref';
277eval { my $x; \$x = [] };
278like $@, qr/^Assigned value is not a SCALAR reference at/,
279 'assigning non-scalar ref to scalar ref';
d8a875d9
FC
280eval { \$::x = [] };
281like $@, qr/^Assigned value is not a SCALAR reference at/,
282 'assigning non-scalar ref to package scalar ref';
d6378458
FC
283
284on;
285eval '(\do{}) = 42';
286like $@, qr/^Can't modify reference to do block in list assignment at /,
287 "Can't modify reference to do block in list assignment";
288off;
289eval '(\pos) = 42';
290like $@,
291 qr/^Can't modify reference to match position in list assignment at /,
292 "Can't modify ref to some scalar-returning op in list assignment";
d6378458
FC
293eval '(\glob) = 42';
294like $@,
295 qr/^Can't modify reference to glob in list assignment at /,
296 "Can't modify reference to some list-returning op in list assignment";
d6b7592f
FC
297eval '\pos = 42';
298like $@,
299 qr/^Can't modify reference to match position in scalar assignment at /,
300 "Can't modify ref to some scalar-returning op in scalar assignment";
301on;
87da42eb
FC
302eval '\(%b) = 42';
303like $@,
304 qr/^Can't modify reference to parenthesized hash in list assignment a/,
305 "Can't modify ref to parenthesized package hash in scalar assignment";
306eval '\(my %b) = 42';
307like $@,
308 qr/^Can't modify reference to parenthesized hash in list assignment a/,
309 "Can't modify ref to parenthesized hash (\(my %b)) in list assignment";
310eval '\my(%b) = 42';
311like $@,
312 qr/^Can't modify reference to parenthesized hash in list assignment a/,
313 "Can't modify ref to parenthesized hash (\my(%b)) in list assignment";
3f114923 314off;
87da42eb
FC
315eval '\%{"42"} = 42';
316like $@,
317 qr/^Can't modify reference to hash dereference in scalar assignment a/,
318 "Can't modify reference to hash dereference in scalar assignment";
3f114923 319on;
87da42eb 320
781ff25d
FC
321
322# Miscellaneous
323
324{
325 my($x,$y);
326 sub {
327 sub {
328 \$x = \$y;
329 }->();
330 is \$x, \$y, 'lexical alias affects outer closure';
331 }->();
332 is \$x, \$y, 'lexical alias affects outer sub where vars are declared';
333}
81cb1af6
FC
334
335{ # PADSTALE has a double meaning
336 use feature 'lexical_subs', 'signatures', 'state';
337 no warnings 'experimental';
338 my $c;
339 my sub s ($arg) {
340 state $x = ++$c;
341 if ($arg == 3) { return $c }
342 goto skip if $arg == 2;
343 my $y;
344 skip:
345 # $y is PADSTALE the 2nd time
346 \$x = \$y if $arg == 2;
347 }
348 s(1);
349 s(2);
350 is s(3), 1, 'padstale alias should not reset state'
351}
cf5d2d91
FC
352
353off;
354SKIP: {
355 skip_without_dynamic_extension('List/Util');
356 require Scalar::Util;
357 my $a;
358 Scalar::Util::weaken($r = \$a);
359 \$a = $r;
360 pass 'no crash when assigning \$lex = $weakref_to_lex'
361}