#Perl 条件语句
#if 语句
#基本 if 语句
my $age = 18;
if ($age >= 18) {
print "你是成年人\n";
}#if-else 语句
my $age = 15;
if ($age >= 18) {
print "你是成年人\n";
} else {
print "你是未成年人\n";
}#if-elsif-else 语句
my $score = 85;
if ($score >= 90) {
print "优秀\n";
} elsif ($score >= 80) {
print "良好\n";
} elsif ($score >= 70) {
print "中等\n";
} elsif ($score >= 60) {
print "及格\n";
} else {
print "不及格\n";
}#unless 语句
unless 是 if not 的简写,当条件为假时执行代码块。
#基本 unless 语句
my $is_raining = 0;
unless ($is_raining) {
print "我们可以出去玩\n";
}#unless-else 语句
my $has_ticket = 0;
unless ($has_ticket) {
print "请先买票\n";
} else {
print "请进\n";
}#嵌套使用
my $weather = "sunny";
my $has_money = 1;
unless ($weather eq "rainy") {
print "天气不错\n";
unless ($has_money) {
print "但是没有钱\n";
} else {
print "可以去购物\n";
}
}#三元运算符
#基本用法
my $age = 18;
my $status = $age >= 18 ? "成年人" : "未成年人";
print $status; # 成年人#嵌套三元运算符
my $score = 75;
my $grade = $score >= 90 ? "A" :
$score >= 80 ? "B" :
$score >= 70 ? "C" :
$score >= 60 ? "D" : "F";
print $grade; # C#赋值和函数调用
my $user = defined($input_user) ? $input_user : "Guest";
print defined($file) ? $file : "default.txt";#given-when 语句(Perl 5.10+)
#基本 given-when
use v5.10;
my $day = "Monday";
given ($day) {
when ("Monday") { print "星期一\n"; }
when ("Tuesday") { print "星期二\n"; }
when ("Wednesday") { print "星期三\n"; }
when ("Thursday") { print "星期四\n"; }
when ("Friday") { print "星期五\n"; }
default { print "周末\n"; }
}#使用正则表达式
use v5.10;
my $input = "hello world";
given ($input) {
when (/^hello/) { print "以 hello 开头\n"; }
when (/world$/) { print "以 world 结尾\n"; }
when (/hello/) { print "包含 hello\n"; }
default { print "其他\n"; }
}#使用多个条件
use v5.10;
my $number = 5;
given ($number) {
when ([1, 2, 3]) { print "1, 2, 或 3\n"; }
when (4) { print "4\n"; }
when ([5, 6, 7]) { print "5, 6, 或 7\n"; }
default { print "其他数字\n"; }
}#使用 continue
use v5.10;
my $day = "Wednesday";
given ($day) {
when ("Monday") { print "工作日\n"; continue; }
when ("Tuesday") { print "工作日\n"; continue; }
when ("Wednesday") { print "工作日\n"; continue; }
when ("Thursday") { print "工作日\n"; continue; }
when ("Friday") { print "工作日\n"; continue; }
}
print "继续执行\n";#逻辑运算符与条件
#使用 && 和 ||
my $age = 25;
my $has_license = 1;
# 逻辑与
if ($age >= 18 && $has_license) {
print "可以开车\n";
}
# 逻辑或
my $weather = "sunny";
my $has_umbrella = 0;
if ($weather eq "sunny" || $has_umbrella) {
print "可以出门\n";
}#使用 and 和 or
# 低优先级逻辑运算符
if ($age >= 18 and $has_license) {
print "可以开车\n";
}
if ($weather eq "sunny" or $has_umbrella) {
print "可以出门\n";
}#短路求值
# 如果第一个条件为真,不会执行第二个
my $x = 1;
my $y = 0;
if ($x || ($y = 10)) {
print "条件为真\n";
}
print "$y\n"; # $y 仍然是 0
# 如果第一个条件为假,不会执行第二个
my $a = 0;
my $b = 0;
if ($a && ($b = 10)) {
print "条件为真\n";
}
print "$b\n"; # $b 仍然是 0#字符串比较
#字符串相等和不相等
my $str1 = "hello";
my $str2 = "world";
if ($str1 eq $str2) {
print "字符串相等\n";
} else {
print "字符串不相等\n";
}
unless ($str1 eq $str2) {
print "字符串不相等\n";
}#字符串大小比较
my $str1 = "apple";
my $str2 = "banana";
if ($str1 lt $str2) {
print "'$str1' 在字典序中位于 '$str2' 之前\n";
}
if ($str1 le $str2) {
print "'$str1' 在字典序中位于 '$str2' 之前或相等\n";
}#定义检查
#使用 defined
my $value;
if (defined($value)) {
print "值已定义\n";
} else {
print "值未定义\n";
}
my $num = 42;
if (defined($num)) {
print "数值: $num\n";
}#使用定义-or 运算符
my $input;
# 如果 $input 未定义,使用默认值
my $value = $input // "default";
print $value; # default
$input = "user input";
$value = $input // "default";
print $value; # user input#使用 exists 检查哈希键
my %hash = (name => "Alice", age => 25);
if (exists $hash{name}) {
print "键 'name' 存在\n";
}
unless (exists $hash{city}) {
print "键 'city' 不存在\n";
}#复杂条件
#组合多个条件
my $age = 25;
my $has_job = 1;
my $has_car = 0;
my $has_license = 1;
if ($age >= 18 && $has_license && ($has_car || $has_job)) {
print "符合条件\n";
} else {
print "不符合条件\n";
}#使用括号明确优先级
# 清晰的条件组合
if (($age >= 18 and $has_license) or ($age >= 21 and $has_job)) {
print "可以申请\n";
}#德摩根定律
# 原始条件
if ($age < 18 or $income < 1000) {
print "不符合资格\n";
}
# 使用德摩根定律
unless ($age >= 18 and $income >= 1000) {
print "不符合资格\n";
}#实践示例
#示例 1:成绩评级系统
#!/usr/bin/perl
use strict;
use warnings;
print "请输入分数 (0-100): ";
chomp(my $score = <STDIN>);
if ($score < 0 || $score > 100) {
print "无效的分数\n";
} elsif ($score >= 90) {
print "A (优秀)\n";
} elsif ($score >= 80) {
print "B (良好)\n";
} elsif ($score >= 70) {
print "C (中等)\n";
} elsif ($score >= 60) {
print "D (及格)\n";
} else {
print "F (不及格)\n";
}#示例 2:登录验证
#!/usr/bin/perl
use strict;
use warnings;
my %users = (
"alice" => "password123",
"bob" => "secure456",
"charlie" => "mypass789"
);
print "用户名: ";
chomp(my $username = <STDIN>);
print "密码: ";
chomp(my $password = <STDIN>);
if (exists $users{$username} && $users{$username} eq $password) {
print "登录成功!欢迎,$username!\n";
} elsif (!exists $users{$username}) {
print "用户不存在\n";
} else {
print "密码错误\n";
}#示例 3:菜单选择系统
#!/usr/bin/perl
use strict;
use warnings;
use v5.10;
print "=== 菜单 ===\n";
print "1. 显示信息\n";
print "2. 计算器\n";
print "3. 退出\n";
print "请选择 (1-3): ";
chomp(my $choice = <STDIN>);
given ($choice) {
when ("1") {
print "=== 信息 ===\n";
print "版本: 1.0\n";
print "作者: Your Name\n";
}
when ("2") {
print "=== 计算器 ===\n";
print "输入第一个数字: ";
chomp(my $a = <STDIN>);
print "输入第二个数字: ";
chomp(my $b = <STDIN>);
print "和: " . ($a + $b) . "\n";
}
when ("3") {
print "再见!\n";
exit;
}
default {
print "无效的选择\n";
}
}#示例 4:闰年判断
#!/usr/bin/perl
use strict;
use warnings;
print "请输入年份: ";
chomp(my $year = <STDIN>);
my $is_leap = 0;
if ($year % 4 == 0) {
if ($year % 100 == 0) {
if ($year % 400 == 0) {
$is_leap = 1;
}
} else {
$is_leap = 1;
}
}
if ($is_leap) {
print "$year 是闰年\n";
} else {
print "$year 不是闰年\n";
}
# 简化版本
my $is_leap_simple = ($year % 4 == 0 and $year % 100 != 0) or ($year % 400 == 0);
print "简化判断: $is_leap_simple\n";#小结
本章节学习了 Perl 的条件语句:
- ✅ if/elsif/else 语句
- ✅ unless 语句
- ✅ 三元运算符
- ✅ given-when 语句
- ✅ 逻辑运算符与条件
- ✅ 字符串比较
- ✅ 定义检查
- ✅ 复杂条件组合
接下来,我们将学习 Perl 循环。