2

我不得不表达:

%MON =    months => 1, end_of_month => 'limit';      # months => undef
%MON =  ( months => 1, end_of_month => 'limit' );

为什么第一个表达式只有一个monthsundef值的键?它们之间有什么区别?

4

2 回答 2

5

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")
于 2020-11-27T15:52:39.993 回答
2

这是 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

请注意,=的优先级高于,=>。因此,您需要括号来覆盖优先级。

于 2020-11-27T15:55:31.117 回答