如何判断 Perl 代码中的一组括号将充当分组括号还是形成列表?
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里三个关键原则很有用:
上下文为王。示例
(1 + 1)
的评估取决于上下文。在标量上下文中,不存在列表这样的东西。要看到这一点,请尝试将看似列表的内容分配给标量变量。思考这个问题的方法是关注逗号运算符的行为:在标量上下文中,它计算其左侧参数,丢弃该值,然后计算其右侧参数,并返回该值。在列表上下文中,逗号运算符将两个参数插入到列表中。
括号不会创建列表。逗号运算符创建列表(假设我们处于列表上下文中)。在 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.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.
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.
Parentheses are not needed to create a list with one element.
But parentheses are needed with multiple items -- for precedence reasons.
示例:
粗略地说,在列表上下文中,逗号运算符分隔列表中的项目;在标量上下文中,逗号运算符是 C“串行逗号”,它计算其左侧和右侧,并返回右侧的值。在标量上下文中,括号对表达式进行分组以覆盖操作顺序,而在列表上下文中,括号所做的...完全相同的事情,真的。它们与分配给数组相关的原因是这样的:
至于你的问题“它是标量还是单元素列表”,由于上下文的原因,单独询问表达式并不是一个有意义的问题。在列表上下文中,一切都是列表;在标量环境中,什么都不是。
推荐阅读:perlop、perldata,Perl 编程。
Examples:
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:
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.