9 sub on { $::TODO = ' ' }
10 sub off{ $::TODO = '' }
13 like $@, qr/^Experimental lvalue references not enabled/,
14 'error when feature is disabled';
16 like $@, qr/^Experimental lvalue references not enabled/,
17 'error when feature is disabled (aassign)';
19 use feature 'lvalue_refs';
23 local $SIG{__WARN__} = sub { $c++; $w = shift };
25 is $c, 1, 'one warning from lv ref assignment';
26 like $w, qr/^Lvalue references are experimental/,
27 'experimental warning';
30 is $c, 1, 'one warning from lv ref list assignment';
31 like $w, qr/^Lvalue references are experimental/,
32 'experimental warning';
35 no warnings 'experimental::lvalue_refs';
40 is \$x, \$y, '\$pkg_scalar = ...';
43 is \$m, \$y, '\$lexical = ...';
45 is \$n, \$y, '\my $lexical = ...';
48 is \$x, \$_, '\($pkgvar) = ... gives list context';
51 is \$x, \$_, '(\$pkgvar) = ... gives list context';
54 is \$o, \$_, '\($lexical) = ... gives list cx';
57 is \$q, \$_, '(\$lexical) = ... gives list cx';
59 is \$p, \$_, '\(my $lexical) = ... gives list cx';
61 is \$r, \$_, '(\my $lexical) = ... gives list cx';
63 is \$s, \$_, '\my($lexical) = ... gives list cx';
64 \($_a, my $a) = @{[\$b, \$c]};
65 is \$_a, \$b, 'package scalar in \(...)';
66 is \$a, \$c, 'lex scalar in \(...)';
67 (\$_b, \my $b) = @{[\$b, \$c]};
68 is \$_b, \$::b, 'package scalar in (\$foo, \$bar)';
69 is \$b, \$c, 'lex scalar in (\$foo, \$bar)';
70 is do { \local $l = \3; $l }, 3, '\local $scalar assignment';
71 is $l, undef, 'localisation unwound';
72 is do { \(local $l) = \4; $l }, 4, '\(local $scalar) assignment';
73 is $l, undef, 'localisation unwound';
75 is *foo{SCALAR}, *bar{GLOB}, 'globref-to-scalarref assignment';
79 sub expect_scalar_cx { wantarray ? 0 : \$_ }
80 sub expect_list_cx { wantarray ? (\$_,\$_) : 0 }
81 \$a[0] = expect_scalar_cx;
82 is \$a[0], \$_, '\$array[0]';
83 \($a[1]) = expect_list_cx;
84 is \$a[1], \$_, '\($array[0])';
87 \$a[0] = expect_scalar_cx;
88 is \$a[0], \$_, '\$lexical_array[0]';
89 \($a[1]) = expect_list_cx;
90 is \$a[1], \$_, '\($lexical_array[0])';
94 is \$a[0], \$tmp, '\local $a[0]';
96 is \$a[0], \$_, '\local $a[0] unwound';
98 \local ($a[1]) = \$tmp;
99 is \$a[1], \$tmp, '\local ($a[0])';
101 is \$a[1], \$_, '\local $a[0] unwound';
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])';
111 \local @a[0,1] = (\$tmp)x2;
112 is \$a[0].\$a[1], \$tmp.\$tmp, '\local @a[indices]';
114 is \$a[0].\$a[1], \$_.\$_, '\local @a[indices] unwound';
119 \$h{a} = expect_scalar_cx;
120 is \$h{a}, \$_, '\$hash{a}';
121 \($h{b}) = expect_list_cx;
122 is \$h{b}, \$_, '\($hash{a})';
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})';
131 \local $h{a} = \$tmp;
132 is \$h{a}, \$tmp, '\local $h{a}';
134 is \$h{a}, \$_, '\local $h{a} unwound';
136 \local ($h{b}) = \$tmp;
137 is \$h{b}, \$tmp, '\local ($h{a})';
139 is \$h{b}, \$_, '\local $h{a} unwound';
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})';
149 \local @h{"a","b"} = (\$tmp)x2;
150 is \$h{a}.\$h{b}, \$tmp.\$tmp, '\local @h{indices}';
152 is \$h{a}.\$h{b}, \$_.\$_, '\local @h{indices} unwound';
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';
165 \@a = expect_scalar_cx;
166 is \@a, \@ThatArray, '\@lexical';
167 (\@b) = expect_list_cx_a;
168 is \@b, \@ThatArray, '(\@pkg)';
170 (\@b) = expect_list_cx_a;
171 is \@b, \@ThatArray, '(\@lexical)';
172 \my @c = expect_scalar_cx;
173 is \@c, \@ThatArray, '\my @lexical';
175 eval '(\my @d) = expect_list_cx_a';
176 is \@d, \@ThatArray, '(\my @lexical)';
177 eval '\(@e) = expect_list_cx';
178 is \$e[0].$e[1], \$_.\$_, '\(@pkg)';
180 eval '\(@e) = expect_list_cx';
181 is \$e[0].$e[1], \$_.\$_, '\(@lexical)';
182 eval '\(my @f) = expect_list_cx';
183 is \$f[0].$f[1], \$_.\$_, '\(my @lexical)';
184 eval '\my(@g) = expect_list_cx';
185 is \$g[0].$g[1], \$_.\$_, '\my(@lexical)';
189 \local @h = \@ThatArray;
190 is \@h, \@ThatArray, '\local @a';
192 is \@h, $old, '\local @a unwound';
195 (\local @i) = \@ThatArray;
196 is \@i, \@ThatArray, '(\local @a)';
197 } or do { SKIP: { ::skip 'unimplemented' } };
198 is \@i, $old, '(\local @a) unwound';
204 BEGIN { *is = *main::is }
205 sub expect_scalar_cx { wantarray ? 0 : \%ThatHash }
206 sub expect_list_cx { wantarray ? (\%ThatHash)x2 : 0 }
207 \%a = expect_scalar_cx;
208 is \%a, \%ThatHash, '\%pkg';
210 \%a = expect_scalar_cx;
211 is \%a, \%ThatHash, '\%lexical';
212 (\%b) = expect_list_cx;
213 is \%b, \%ThatHash, '(\%pkg)';
215 (\%b) = expect_list_cx;
216 is \%b, \%ThatHash, '(\%lexical)';
217 \my %c = expect_scalar_cx;
218 is \%c, \%ThatHash, '\my %lexical';
219 (\my %d) = expect_list_cx;
220 is \%d, \%ThatHash, '(\my %lexical)';
223 \local %h = \%ThatHash;
224 is \%h, \%ThatHash, '\local %a';
226 is \%h, $old, '\local %a unwound';
229 (\local %i) = \%ThatHash;
230 is \%i, \%ThatHash, '(\local %a)';
231 } or do { SKIP: { ::skip 'unimplemented' } };
232 is \%i, $old, '(\local %a) unwound';
239 # Mixed List Assignments
241 (\$tahi, $rua) = \(1,2);
242 is join(' ', $tahi, $$rua), '1 2',
243 'mixed scalar ref and scalar list assignment';
246 # Conditional expressions
249 eval '$_ == 3 ? \$tahi : $rua = \3';
250 is $tahi, 3, 'cond assignment resolving to scalar ref';
251 eval '$_ == 3 ? \$toru : $wha = \3';
252 is $$wha, 3, 'cond assignment resolving to scalar';
253 eval '$_ == 3 ? \$rima : \$ono = \5';
254 is $$rima, 5, 'cond assignment with refgens on both branches';
259 for \my $a(\$for1, \$for2) {
263 is "@for", \$for1 . ' ' . \$for2, 'foreach \my $a';
267 for \my @a([1,2], [3,4]) {
271 is "@for", "1 2 3 4", 'foreach \my @a [perl #22335]';
275 for \my %a({5,6}, {7,8}) {
279 is "@for", "5 6 7 8", 'foreach \my %a [perl #22335]';
283 for \my &a(sub {9}, sub {10}) {
287 is "@for", "9 10", 'foreach \my &a';
293 eval { my $x; \$x = 3 };
294 like $@, qr/^Assigned value is not a reference at/, 'assigning non-ref';
295 eval { my $x; \$x = [] };
296 like $@, qr/^Assigned value is not a SCALAR reference at/,
297 'assigning non-scalar ref to scalar ref';
299 like $@, qr/^Assigned value is not a SCALAR reference at/,
300 'assigning non-scalar ref to package scalar ref';
301 eval { my @x; \@x = {} };
302 like $@, qr/^Assigned value is not an ARRAY reference at/,
303 'assigning non-array ref to array ref';
305 like $@, qr/^Assigned value is not an ARRAY reference at/,
306 'assigning non-array ref to package array ref';
307 eval { my %x; \%x = [] };
308 like $@, qr/^Assigned value is not a HASH reference at/,
309 'assigning non-hash ref to hash ref';
311 like $@, qr/^Assigned value is not a HASH reference at/,
312 'assigning non-hash ref to package hash ref';
316 like $@, qr/^Can't modify reference to do block in list assignment at /,
317 "Can't modify reference to do block in list assignment";
321 qr/^Can't modify reference to match position in list assignment at /,
322 "Can't modify ref to some scalar-returning op in list assignment";
325 qr/^Can't modify reference to glob in list assignment at /,
326 "Can't modify reference to some list-returning op in list assignment";
329 qr/^Can't modify reference to match position in scalar assignment at /,
330 "Can't modify ref to some scalar-returning op in scalar assignment";
332 eval '\(local @b) = 42';
334 qr/^Can't modify reference to parenthesized localized array in list(?x:
336 q"Can't modify \(local @array) in list assignment";
337 eval '\local(@b) = 42';
339 qr/^Can't modify reference to parenthesized localized array in list(?x:
341 q"Can't modify \local(@array) in list assignment";
345 qr/^Can't modify reference to parenthesized hash in list assignment a/,
346 "Can't modify ref to parenthesized package hash in scalar assignment";
347 eval '\(my %b) = 42';
349 qr/^Can't modify reference to parenthesized hash in list assignment a/,
350 "Can't modify ref to parenthesized hash (\(my %b)) in list assignment";
353 qr/^Can't modify reference to parenthesized hash in list assignment a/,
354 "Can't modify ref to parenthesized hash (\my(%b)) in list assignment";
355 eval '\%{"42"} = 42';
357 qr/^Can't modify reference to hash dereference in scalar assignment a/,
358 "Can't modify reference to hash dereference in scalar assignment";
370 is \$x, \$y, 'lexical alias affects outer closure';
372 is \$x, \$y, 'lexical alias affects outer sub where vars are declared';
375 { # PADSTALE has a double meaning
376 use feature 'lexical_subs', 'signatures', 'state';
377 no warnings 'experimental';
381 if ($arg == 3) { return $c }
382 goto skip if $arg == 2;
385 # $y is PADSTALE the 2nd time
386 \$x = \$y if $arg == 2;
390 is s(3), 1, 'padstale alias should not reset state'
395 skip_without_dynamic_extension('List/Util');
396 require Scalar::Util;
398 Scalar::Util::weaken($r = \$a);
400 pass 'no crash when assigning \$lex = $weakref_to_lex'