有没有人知道什么解析或优先级决定导致警告'使用'shift“没有括号是不明确的'为代码发出:
shift . 'some string';
# and not
(shift) . 'some string'; # or
shift() . 'some string';
这是否有意使某些句法结构更容易?或者它只是perl解析器工作方式的工件?
注意:这是关于语言设计的讨论,而不是建议的地方
"@{[shift]}some string"
答案 0 :(得分:26)
使用use diagnostics
,您会收到有用的消息:
Warning: Use of "shift" without parentheses is ambiguous at (eval 9)[/usr/lib/perl5/5.8/perl5db.pl:628] line 2 (#1) (S ambiguous) You wrote a unary operator followed by something that looks like a binary operator that could also have been interpreted as a term or unary operator. For instance, if you know that the rand function has a default argument of 1.0, and you write rand + 5; you may THINK you wrote the same thing as rand() + 5; but in actual fact, you got rand(+5); So put in parentheses to say what you really mean.
担心你可以写shift .5
之类的内容,它会被解析为shift(0.5)
。
答案 1 :(得分:8)
歧义并不意味着真正含糊不清,只是解析器已经确定的含糊不清。
shift .
特别“含糊不清”,因为.
可以开始一个术语(例如.123
)或运算符,
所以它不足以决定接下来是shift的操作数还是shift()是操作数的运算符(并且解析器不够聪明,不知道:a).
不是这个术语的开头或b).123
不是转换的有效操作数。