在Perl中,條件語句使用 if、elsif 和 else 關鍵字來實現。以下是一些示例:
my $num = 10;
if ($num > 5) {
print "Number is greater than 5\n";
}
my $num = 3;
if ($num > 5) {
print "Number is greater than 5\n";
} else {
print "Number is less than or equal to 5\n";
}
my $num = 5;
if ($num > 5) {
print "Number is greater than 5\n";
} elsif ($num < 5) {
print "Number is less than 5\n";
} else {
print "Number is equal to 5\n";
}
my $num = 6;
unless ($num > 5) {
print "Number is less than or equal to 5\n";
}
在這些示例中,根據條件的結果,會執行相應的代碼塊。可以根據具體的需求選擇合適的條件語句來實現邏輯控制。