在列表上下文中分配散列时有什么区别?
What is a difference while assigning hash in list context?
我要表达:
%MON = months => 1, end_of_month => 'limit'; # months => undef
%MON = ( months => 1, end_of_month => 'limit' );
为什么第一个表达式结果只有一个键 months
和 undef
值?
它们有什么区别?
参见perlop。 =
的优先级高于 =>
%MON = months => 1, end_of_month => 'limit';
相当于:
(%MON = "months"), 1, "end_of_month", "limit"
同时:
%MON = ( months => 1, end_of_month => 'limit' );
是:
%MON = ("months", 1, "end_of_month", "limit")
这是 Perl 的 table 运算符优先级(来自 perlop):
left terms and list operators (leftward)
left ->
nonassoc ++ --
right **
right ! ~ \ and unary + and -
left =~ !~
left * / % x
left + - .
left << >>
nonassoc named unary operators
nonassoc < > <= >= lt gt le ge
nonassoc == != <=> eq ne cmp ~~
left &
left | ^
left &&
left || //
nonassoc .. ...
right ?:
right = += -= *= etc.
left , =>
nonassoc list operators (rightward)
right not
left and
left or xor
请注意 =
的优先级高于 ,
或 =>
。因此,您需要括号来覆盖优先级。
我要表达:
%MON = months => 1, end_of_month => 'limit'; # months => undef
%MON = ( months => 1, end_of_month => 'limit' );
为什么第一个表达式结果只有一个键 months
和 undef
值?
它们有什么区别?
参见perlop。 =
的优先级高于 =>
%MON = months => 1, end_of_month => 'limit';
相当于:
(%MON = "months"), 1, "end_of_month", "limit"
同时:
%MON = ( months => 1, end_of_month => 'limit' );
是:
%MON = ("months", 1, "end_of_month", "limit")
这是 Perl 的 table 运算符优先级(来自 perlop):
left terms and list operators (leftward)
left ->
nonassoc ++ --
right **
right ! ~ \ and unary + and -
left =~ !~
left * / % x
left + - .
left << >>
nonassoc named unary operators
nonassoc < > <= >= lt gt le ge
nonassoc == != <=> eq ne cmp ~~
left &
left | ^
left &&
left || //
nonassoc .. ...
right ?:
right = += -= *= etc.
left , =>
nonassoc list operators (rightward)
right not
left and
left or xor
请注意 =
的优先级高于 ,
或 =>
。因此,您需要括号来覆盖优先级。