📜  Perl运算符(1)

📅  最后修改于: 2023-12-03 15:03:33.606000             🧑  作者: Mango

Perl运算符

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运算符的介绍。希望这些内容对你有所帮助!