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.gitbook.net)
許多運算符可以結合=如下:
$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