📅  最后修改于: 2023-12-03 15:03:33.606000             🧑  作者: Mango
Perl是一种强大的编程语言。它包含了许多运算符用于完成不同的任务。以下是一些Perl运算符的介绍。
Perl支持以下算数运算符:
+
加-
减*
乘/
除%
取模my $a = 10;
my $b = 3;
my $sum = $a + $b;
my $sub = $a - $b;
my $mul = $a * $b;
my $div = $a / $b;
my $mod = $a % $b;
print "Sum is $sum\n";
print "Sub is $sub\n";
print "Mul is $mul\n";
print "Div is $div\n";
print "Mod is $mod\n";
Perl支持以下比较运算符:
==
等于!=
不等于>
大于<
小于>=
大于等于<=
小于等于my $a = 10;
my $b = 3;
if ($a == $b) {
print "a is equal to b\n";
} else {
print "a is not equal to b\n";
}
if ($a > $b) {
print "a is greater than b\n";
} else {
print "a is less than or equal to b\n";
}
Perl支持以下逻辑运算符:
&&
逻辑与||
逻辑或!
逻辑非my $a = 10;
my $b = 3;
my $c = 5;
if ($a > $b && $b > $c) {
print "a is the largest\n";
} elsif ($b > $a && $b > $c) {
print "b is the largest\n";
} else {
print "c is the largest\n";
}
Perl支持以下位运算符:
&
按位与|
按位或^
按位异或~
按位取反<<
左移>>
右移my $a = 10;
my $b = 3;
print "a & b = ", $a & $b, "\n";
print "a | b = ", $a | $b, "\n";
print "a ^ b = ", $a ^ $b, "\n";
print "~a = ", ~$a, "\n";
print "a << 2 = ", $a << 2, "\n";
print "a >> 2 = ", $a >> 2, "\n";
Perl支持以下赋值运算符:
=
简单赋值+=
加且赋值-=
减且赋值*=
乘且赋值/=
除且赋值%=
取模且赋值my $a = 10;
my $b = 3;
$a += $b;
print "a = ", $a, "\n";
$a -= $b;
print "a = ", $a, "\n";
$a *= $b;
print "a = ", $a, "\n";
$a /= $b;
print "a = ", $a, "\n";
$a %= $b;
print "a = ", $a, "\n";
以上就是Perl运算符的介绍。希望这些内容对你有所帮助!