为什么(至少)在perl 5.010中,//的优先级低于==

例如这个

use 5.010;
my $may_be_undefined = 1;
my $is_equal_to_two = ($may_be_undefined//0 == 2);
say $is_equal_to_two;

(对我来说)打印出非常意外的结果。

最佳答案

这是因为//以及==属于运算符的类别。
==是“相等运算符”,尽管//属于“C样式逻辑运算符”类别。

举个例子; &&//处于同一“类别”中,当涉及到运算符优先级时,以下两个语句是等效的。这样可能更容易理解?

  print "hello world" if $may_be_undefined && 0 == 2;
  print "hello world" if $may_be_undefined // 0 == 2;

Documentation of C-style Logical Defined-Or ( // )



Documentation of Operator Precedence and Associativity

关于perl - 为什么//在perl中优先级比平等低?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8469273/

10-14 16:20
查看更多