You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Perl 6嵌套列表赋值结果不符合预期问题咨询

问题分析: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:

  1. 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 $a and $b (with (1,2) and 3 respectively) and leaves $c as Any.

  2. 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.

  3. 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 04:09:04