如何判断 Perl 代码中的一组括号将充当分组括号还是形成列表?

发布于 2024-09-02 04:05:31 字数 153 浏览 4 评论 0原文

在 Perl 中,括号用于覆盖优先级(与大多数编程语言一样)以及创建列表。如何判断一对特定的括号是否将被视为分组结构或单元素列表?

例如,我很确定这是一个标量而不是单元素列表:(1 + 1)
但是更复杂的表达式呢?有没有简单的方法可以辨别呢?

In perl, parentheses are used for overriding precedence (as in most programming languages) as well as for creating lists. How can I tell if a particular pair of parens will be treated as a grouping construct or a one-element list?

For example, I'm pretty sure this is a scalar and not a one-element list: (1 + 1)
But what about more complex expressions? Is there an easy way to tell?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

那请放手 2024-09-09 04:05:31

这里三个关键原则很有用:

上下文为王。示例 (1 + 1) 的评估取决于上下文。

$x = (1 + 1); # Scalar context. $x will equal 2. Parentheses do nothing here.
@y = (1 + 1); # List context. @y will contain one element: (2).
              # Parens do nothing (see below), aside from following 
              # syntax conventions.

在标量上下文中,不存在列表这样的东西。要看到这一点,请尝试将看似列表的内容分配给标量变量。思考这个问题的方法是关注逗号运算符的行为:在标量上下文中,它计算其左侧参数,丢弃该值,然后计算其右侧参数,并返回该值。在列表上下文中,逗号运算符将两个参数插入到列表中。

@arr  = (12, 34, 56); # Right side returns a list.

$x    = (12, 34, 56); # Right side returns 56. Also, we get warnings
                      # about 12 and 34 being used in void context.

$x = (@arr, 7);       # Right side returns 7. And we get a warning
                      # about using an array in a void context.

括号不会创建列表。逗号运算符创建列表(假设我们处于列表上下文中)。在 Perl 代码中键入列表时,需要括号是出于优先级原因,而不是出于列表创建原因。几个例子:

  • 括号不起作用:我们正在评估标量数组
    上下文,因此右侧返回数组大小。

    <前><代码>$x = (@arr);

  • 创建包含一个元素的列表不需要括号。

    <前><代码>@arr = 33; # 工作正常,@arr 等于 (33)。

  • 但是出于优先顺序的原因,多个项目需要括号。

    <前><代码>@arr = 12, 34, 56; # @arr 等于 (12)。我们收到有关使用的警告
    # 34 和 56 在无效上下文中。

Three key principles are useful here:

Context is king. The evaluation of your example (1 + 1) depends on the context.

$x = (1 + 1); # Scalar context. $x will equal 2. Parentheses do nothing here.
@y = (1 + 1); # List context. @y will contain one element: (2).
              # Parens do nothing (see below), aside from following 
              # syntax conventions.

In a scalar context, there is no such thing as a list. To see this, try to assign what appears to be a list to a scalar variable. The way to think about this is to focus on the behavior of the comma operator: in scalar context it evaluates its left argument, throws that value away, then evaluates its right argument, and returns that value. In list context, the comma operator inserts both arguments into the list.

@arr  = (12, 34, 56); # Right side returns a list.

$x    = (12, 34, 56); # Right side returns 56. Also, we get warnings
                      # about 12 and 34 being used in void context.

$x = (@arr, 7);       # Right side returns 7. And we get a warning
                      # about using an array in a void context.

Parentheses do not create lists. The comma operator creates the list (provided that we are in list context). When typing lists in Perl code, the parentheses are needed for precedence reasons -- not for list-creation reasons. A few examples:

  • The parentheses have no effect: we are evaluating an array in scalar
    context, so the right side returns the array size.

    $x = (@arr);
    
  • Parentheses are not needed to create a list with one element.

    @arr = 33;         # Works fine, with @arr equal to (33).
    
  • But parentheses are needed with multiple items -- for precedence reasons.

    @arr = 12, 34, 56; # @arr equals (12). And we get warnings about using
                       # 34 and 56 in void context.
    
太阳公公是暖光 2024-09-09 04:05:31
  1. 语境。
  2. 括号在创建列表时没有您认为的作用。

示例:

$x = 1 + 1;   # $x is 2.
$x = (1 + 1); # $x is 2.
@x = 1 + 1;   # @x is (2).
@x = (1 + 1); # @x is (2).

$x = (1 + 1, 1 + 2); # $x is 3.
@x = (1 + 1, 1 + 2); # @x is (2, 3).

粗略地说,在列表上下文中,逗号运算符分隔列表中的项目;在标量上下文中,逗号运算符是 C“串行逗号”,它计算其左侧和右侧,并返回右侧的值。在标量上下文中,括号对表达式进行分组以覆盖操作顺序,而在列表上下文中,括号所做的...完全相同的事情,真的。它们与分配给数组相关的原因是这样的:

# Comma has precedence below assignment. 
# @a is assigned (1), 2 and 3 are discarded.
@a = 1, 2, 3; 

# @a is (1, 2, 3).
@a = (1, 2, 3);

至于你的问题“它是标量还是单元素列表”,由于上下文的原因,单独询问表达式并不是一个有意义的问题。在列表上下文中,一切都是列表;在标量环境中,什么都不是。

推荐阅读:perlopperldataPerl 编程

  1. Context.
  2. Parentheses don't have the role you think they have in creating a list.

Examples:

$x = 1 + 1;   # $x is 2.
$x = (1 + 1); # $x is 2.
@x = 1 + 1;   # @x is (2).
@x = (1 + 1); # @x is (2).

$x = (1 + 1, 1 + 2); # $x is 3.
@x = (1 + 1, 1 + 2); # @x is (2, 3).

Roughly speaking, in list context the comma operator separates items of a list; in scalar context the comma operator is the C "serial comma", which evaluates its left and right sides, and returns the value of the right side. In scalar context, parentheses group expressions to override the order of operations, and in list context, parentheses do... the exact same thing, really. The reason they're relevant in assigning to arrays is this:

# Comma has precedence below assignment. 
# @a is assigned (1), 2 and 3 are discarded.
@a = 1, 2, 3; 

# @a is (1, 2, 3).
@a = (1, 2, 3);

As for your question "is it a scalar or a one-element list", it's just not a meaningful question to ask of an expression in isolation, because of context. In list context, everything is a list; in scalar context, nothing is.

Recommended reading: perlop, perldata, Programming Perl.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文