3 # Some miscellaneous checks for the list assignment operator, OP_AASSIGN.
5 # This file was only added in 2015; before then, such tests were
6 # typically in various other random places like op/array.t. This test file
7 # doesn't therefore attempt to be comprehensive; it merely provides a
8 # central place to new put additional tests, especially those related to
9 # the trickiness of commonality, e.g. ($a,$b) = ($b,$a).
11 # In particular, it's testing the flags
12 # OPpASSIGN_COMMON_SCALAR
13 # OPpASSIGN_COMMON_RC1
14 # OPpASSIGN_COMMON_AGG
25 # general purpose package vars
31 sub f_ret_14 { return 1..4 }
33 # stringify a hash ref
37 join ',', map "$_:$rh->{$_}", sort keys %$rh;
41 # where the RHS has surplus elements
46 is("$a:$b", "1:2", "surplus");
54 is("$a[0]:$a[1]", "2:1", "lex array slice");
61 for $pkg_scalar ($a) {
62 ($pkg_scalar, $b) = (3, $a);
63 is($pkg_scalar, 3, "package alias pkg");
64 is("$a:$b", "3:1", "package alias a:b");
68 # my array/hash populated via closure
73 sub f1 { $x = 1; @a = 2..4; \@a }
74 is($x, 2, "my: array closure x");
75 is("@a", "3 4", "my: array closure a");
78 my ($k, $v, %h) = (d => 4, %$rh, e => 6);
79 sub f2 { $k = 'a'; $v = 1; %h = qw(b 2 c 3); \%h }
80 is("$k:$v", "d:4", "my: hash closure k:v");
81 is(sh(\%h), "b:2,c:3,e:6", "my: hash closure h");
85 # various shared element scenarios within a my (...)
88 my ($x,$y) = f3(); # $x and $y on both sides
89 sub f3 : lvalue { ($x,$y) = (1,2); $y, $x }
90 is ("$x:$y", "2:1", "my: scalar and lvalue sub");
95 my @a = @$ra; # elements of @a on both sides
96 sub f4 { @a = 1..4; \@a }
97 is("@a", "1 2 3 4", "my: array and elements");
102 my %h = %$rh; # elements of %h on both sides
103 sub f5 { %h = qw(a 1 b 2 c 3); \%h }
104 is(sh(\%h), "a:1,b:2,c:3", "my: hash and elements");
110 my ($x, $y) = (2, $xalias6);
111 sub f6 { $x = 1; *xalias6 = \$x; }
112 is ("$x:$y", "2:1", "my: pkg var aliased to lexical");
120 is ("$x:$y", "2:1", "my: lex array elements aliased");
124 use feature 'refaliasing';
125 no warnings 'experimental';
126 \($a[0], $a[1]) = \($y,$x);
133 my ($x,$y) = @pkg_array;
134 is ("$x:$y", "2:1", "my: pkg array elements aliased");
138 use feature 'refaliasing';
139 no warnings 'experimental';
140 \($pkg_array[0], $pkg_array[1]) = \($y,$x);
147 is ("$x:$y", "2:1", "my: pkg scalar alias");
158 use feature 'refaliasing';
159 no warnings 'experimental';
163 \(my $lex) = \$pkg10;
164 my @a = ($lex,3); # equivalent to ($a[0],3)
165 is("@a", "1 3", "my: lex alias of array alement");
175 use feature 'refaliasing';
176 no warnings 'experimental';
181 is("@a", "2 1", "my: lex alias of array alements");
195 for $pkg_scalar ($x) {
196 ($pkg_scalar, $y) = (3, $x);
197 is("$pkg_scalar,$y", "3,1", "package scalar aliased");
205 sub f12 : lvalue { @a }
207 is("@a", "1 2 3", "lvalue sub on RHS returns array");
212 sub f13 : lvalue { $x,$y }
214 is("$x:$y", "1:2", "lvalue sub on RHS returns scalars");
218 # package shared scalar vars
223 ($pkg14a,$pkg14b) = ($pkg14b,$pkg14a);
224 is("$pkg14a:$pkg14b", "2:1", "shared package scalars");
227 # lexical shared scalar vars
233 is("$a:$b", "2:1", "shared lexical scalars");
237 # lexical nested array elem swap
243 ($a[0][0],$a[0][1]) = ($a[0][1],$a[0][0]);
244 is("$a[0][0]:$a[0][1]", "2:1", "lexical nested array elem swap");
247 # package nested array elem swap
253 ($a15[0][0],$a15[0][1]) = ($a15[0][1],$a15[0][0]);
254 is("$a15[0][0]:$a15[0][1]", "2:1", "package nested array elem swap");
261 ($a16, undef, $b16) = 1..30;
262 is("$a16:$b16", "1:3", "surplus RHS junk");
265 # my ($scalar,....) = @_
267 # technically this is an unsafe usage commonality-wise, but
268 # a) you have to try really hard to break it, as this test shows;
269 # b) it's such an important usage that for performance reasons we
270 # mark it as safe even though it isn't really. Hence it's a TODO.
274 # debugging builds will detect this failure and panic
275 skip "DEBUGGING build" if $::Config{ccflags} =~ /DEBUGGING/
276 or $^O eq 'VMS' && $::Config{usedebugging_perl} eq 'Y';
277 local $::TODO = 'cheat and optimise my (....) = @_';
281 is("($a)(@b)", "(3)(2 1)", 'my (....) = @_');
284 use feature 'refaliasing';
285 no warnings 'experimental';
287 \($_[2], $_[1], $_[0]) = \($a, $b[0], $b[1]);
291 # single scalar on RHS that's in an aggregate on LHS
297 is ("(@a)", "(1)", 'single scalar on RHS, agg');
301 # TEMP buffer stealing.
304 # the same TEMP RHS element may be used more than once, so when copying
305 # it, we mustn't steal its buffer.
308 # a string long enough for COW and buffer stealing to be enabled
309 my $long = 'def' . ('x' x 2000);
311 # a sub that is intended to return a TEMP string that isn't COW
312 # the concat returns a non-COW PADTMP; pp_leavesub sees a long
313 # stealable string, so creates a TEMP with the stolen buffer from the
314 # PADTMP - hence it returns a non-COW string
322 # with @a initially empty,the code path creates a new copy of each
323 # RHS element to store in the array
326 is (substr($a[0], 0, 7), "abcdefx", 'NOSTEAL empty $a[0]');
327 is (substr($a[1], 0, 7), "abcdefx", 'NOSTEAL empty $a[1]');
329 # with @a initially non-empty, it takes a different code path that
330 # makes a mortal copy of each RHS element
333 is (substr($a[0], 0, 7), "abcdefx", 'NOSTEAL non-empty $a[0]');
334 is (substr($a[1], 0, 7), "abcdefx", 'NOSTEAL non-empty $a[1]');
341 ($x,$y) = (undef, $x);
342 is($x, undef, 'single scalar on RHS, but two on LHS: x');
343 is($y, 1, 'single scalar on RHS, but two on LHS: y');
346 { # magic handling, see #126633
350 sub TIEARRAY { bless [ $_[1] ] }
351 sub STORE { $_[0][0]->[$_[1]] = $_[2]; $set = 1 }
352 sub FETCH { $_[0][0]->[$_[1]] }
353 sub CLEAR { @{$_[0][0]} = () }
356 my @base = ( "a", "b" );
360 tie @proxy, "ArrayProxy", \@real;
361 @proxy[0, 1] = @real[1, 0];
362 is($real[0], "b", "tied left first");
363 is($real[1], "a", "tied left second");
365 @real[0, 1] = @proxy[1, 0];
366 is($real[0], "b", "tied right first");
367 is($real[1], "a", "tied right second");
369 @proxy[0, 1] = @proxy[1, 0];
370 is($real[0], "b", "tied both first");
371 is($real[1], "a", "tied both second");
373 ($temp, @real) = @proxy[1, 0];
374 is($real[0], "a", "scalar/array tied right");
376 ($temp, @proxy) = @real[1, 0];
377 is($real[0], "a", "scalar/array tied left");
379 ($temp, @proxy) = @proxy[1, 0];
380 is($real[0], "a", "scalar/array tied both");
383 ($proxy[0], $orig) = (1, $set);
384 is($orig, 0, 'previous value of $set');
388 skip "no List::Util::min on miniperl", 2, if is_miniperl;
392 ( $x, $y ) = ( List::Util::min($y), List::Util::min($x) );
393 is($x, 2, "check swap for \$x");
394 is($y, 1, "check swap for \$y");