Perl 6嵌套列表赋值结果不符合预期问题咨询
Hey there! Let's break down why you're getting that unexpected output.
First, let's recap what you expected vs what you got:
- Expected:
$a=1,$b=2,$c=3 - Actual:
$a=(1,2),$b=3,$c=Any
What's causing this?
Your output tells us that the right-hand side list ((1,2), 3) is being split into two elements and assigned directly to $a and $b, leaving $c untouched (still its initial Any value). This means the left-hand side of your assignment wasn't parsed as the nested structure (($a, $b), $c) you intended. Here are the most likely reasons:
Accidental missing parentheses or wrong grouping
If you actually wrote($a, $b, $c) = ((1,2), 3)instead of(($a, $b), $c) = ((1,2), 3), this would explain the result perfectly. The right-hand side has only 2 elements, so it fills$aand$b(with(1,2)and3respectively) and leaves$casAny.REPL input formatting issues
If you split the assignment across lines in the REPL (like starting with(($a, $b),on one line and$c) = ...on the next), older REPL versions might misparse the incomplete parentheses, treating it as two separate expressions instead of one nested assignment.Outdated Rakudo version
Very early versions of Rakudo (pre-2018 or so) had bugs with nested destructuring assignments, where the parser didn't correctly recognize nested parentheses on the left-hand side of an assignment.
The Fix: Correct Nested Destructuring
To get your expected result (1, 2, 3 assigned to $a, $b, $c respectively), your original syntax is actually correct—if it's parsed properly. Here's how to verify it works in a modern Rakudo environment:
my ($a, $b, $c); (($a, $b), $c) = ((1, 2), 3); say "$a, $b, $c"; # Outputs: 1, 2, 3
If you're working with a flat list instead of a nested one (e.g., (1,2,3)), you can simplify to a straightforward assignment:
my ($a, $b, $c) = (1, 2, 3);
Quick Check
Double-check your code for typos in parentheses, make sure you're running a recent version of Rakudo, and if using the REPL, enter the entire assignment in one line to avoid parsing issues.
内容的提问来源于stack exchange,提问作者Eugene Barsky

