Perl内置操作符
Perl的运算符有很多,但这里有几个最常见的:
算术运算符
+ addition - subtraction * multiplication / division
数字比较运算符
== equality != inequality < less than > greater than <= less than or equal >= greater than or equal
字符串比较运算符
eq equality ne inequality lt less than gt greater than le less than or equal ge greater than or equal
(为什么我们有独立的数字和字符串比较?因为我们并没有特殊的变量类型,及Perl需要知道是否数值进行排序(其中99是小于100)或按字母顺序排列(100前99)。
布尔逻辑运算符
&& and || or ! not
(与,或和不只是在上述操作的说明表 - 他们还支持运算符在他们自己权利,他们是比C风格的运算符更可读,但有不同的优先级,比&&友好。
杂项运算符
= assignment . string concatenation x string multiplication .. range operator (creates a list of numbers - by www.yiibai.com)
许多运算符可以结合=如下:
$a += 1; # same as $a = $a + 1 $a -= 1; # same as $a = $a - 1 $a .= "\n"; # same as $a = $a . "\n";
运算符优先级与关联性
Perl的运营商有以下的关联性和优先顺序,列出从最高优先级到最低。运算符借从C保持与对方相同的优先级关系,即使在C的优先级是轻微不同。(这使得用于C语言学习的Perl更容易。)除了极少数例外,所有这些操作只标量值,而不是数组中的值。
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