fmg*_*fmg 1 perl scalar list operator-precedence comma-operator
Having some significant confusion about what it means to "evaluate in list context", specifically related to the comma operator. In the linked perlop doc, it says: In list context, it's just the list argument separator, and inserts both its arguments into the list. However, the code
@y = 9, 8, 7, 6;
say @y, ', ', scalar @y;
Run Code Online (Sandbox Code Playgroud)
gives output 9, 1. This respects the fact that, when used as a binary operator on scalar values (?), the comma operator , has lower precedence that the assignment = operator. But since I'm assigning to the list @y, shouldn't the right hand side of the assignment be evaluated in list context and, by the above quote, treat the comma simply as a separator?
I suspect that I just don't truly understand what it "evaluate in list context" means, precisely...
这个问题与上下文无关。[1]这是一个优先级问题。
赋值的优先级高于逗号,所以
my @y = 9, 8, 7, 6;
Run Code Online (Sandbox Code Playgroud)
方法
( my @y = 9 ), 8, 7, 6;
Run Code Online (Sandbox Code Playgroud)
但你想要
my @y = ( 9, 8, 7, 6 );
Run Code Online (Sandbox Code Playgroud)
请注意,括号除了覆盖优先级之外什么也不做。
警告会发现这一点。始终使用use strict; use warnings;或等效!
我怀疑我只是没有真正理解“在列表上下文中评估”的含义,确切地说......
每个运算符根据评估的上下文决定要做什么和返回什么。
但是,它不会改变代码的解析方式。
my @y左边的足以导致使用列表分配,因此足以导致在列表上下文中评估 RHS。请参阅标量与列表赋值运算符。| 归档时间: |
|
| 查看次数: |
143 次 |
| 最近记录: |