我不得不表达:
%MON = months => 1, end_of_month => 'limit'; # months => undef
%MON = ( months => 1, end_of_month => 'limit' );
为什么第一个表达式只有一个months
带undef
值的键?它们之间有什么区别?
我不得不表达:
%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 的运算符优先级表(来自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
请注意,=
的优先级高于,
或=>
。因此,您需要括号来覆盖优先级。