条件语句 #

一、if语句 #

1.1 基本if #

perl
my $score = 85;

if ($score >= 60) {
    print "Passed!\n";
}

1.2 if-else #

perl
my $score = 55;

if ($score >= 60) {
    print "Passed!\n";
} else {
    print "Failed!\n";
}

1.3 if-elsif-else #

perl
my $score = 85;

if ($score >= 90) {
    print "Grade: A\n";
} elsif ($score >= 80) {
    print "Grade: B\n";
} elsif ($score >= 70) {
    print "Grade: C\n";
} elsif ($score >= 60) {
    print "Grade: D\n";
} else {
    print "Grade: F\n";
}

1.4 语句修饰符 #

单行if语句:

perl
my $debug = 1;

print "Debug mode\n" if $debug;

二、unless语句 #

2.1 基本unless #

unlessif not 的简写:

perl
my $logged_in = 0;

unless ($logged_in) {
    print "Please log in\n";
}

等价于:

perl
if (!$logged_in) {
    print "Please log in\n";
}

2.2 unless-else #

perl
my $has_permission = 0;

unless ($has_permission) {
    print "Access denied\n";
} else {
    print "Access granted\n";
}

2.3 语句修饰符 #

perl
my $file = "test.txt";

die "File not found\n" unless -e $file;

三、given-when语句 #

3.1 基本语法 #

given-when 类似于其他语言的 switch-case(Perl 5.10+):

perl
use v5.10;

my $day = "Monday";

given ($day) {
    when ("Monday")    { say "Start of week"; }
    when ("Friday")    { say "End of week"; }
    when ("Saturday")  { say "Weekend!"; }
    when ("Sunday")    { say "Weekend!"; }
    default            { say "Midweek"; }
}

3.2 使用正则表达式 #

perl
use v5.10;

my $input = "hello123";

given ($input) {
    when (/^\d+$/)     { say "All digits"; }
    when (/^[a-z]+$/i) { say "All letters"; }
    when (/^\w+$/)     { say "Alphanumeric"; }
    default            { say "Other"; }
}

3.3 使用数组 #

perl
use v5.10;

my $fruit = "apple";
my @favorites = qw(apple banana cherry);

given ($fruit) {
    when (@favorites)  { say "Favorite!"; }
    when (/^a/)        { say "Starts with 'a'"; }
    default            { say "Other fruit"; }
}

3.4 continue #

使用 continue 继续匹配:

perl
use v5.10;

my $num = 10;

given ($num) {
    when ($_ > 5)  { say "Greater than 5"; continue; }
    when ($_ > 8)  { say "Greater than 8"; continue; }
    when ($_ < 20) { say "Less than 20"; }
}

四、三元运算符 #

4.1 基本语法 #

perl
my $age = 20;
my $status = $age >= 18 ? "adult" : "minor";
print $status;

4.2 嵌套使用 #

perl
my $score = 85;
my $grade = $score >= 90 ? "A" :
            $score >= 80 ? "B" :
            $score >= 70 ? "C" :
            $score >= 60 ? "D" : "F";
print $grade;

4.3 实际应用 #

perl
my @items = (1, 2, 3);
my $message = @items ? scalar(@items) . " items" : "Empty";
print $message;

五、条件表达式 #

5.1 文件测试 #

perl
my $file = "test.txt";

if (-e $file) {
    print "File exists\n";
}

if (-f $file && -r $file) {
    print "Regular file and readable\n";
}

常用文件测试符:

测试符 说明
-e 存在
-f 普通文件
-d 目录
-r 可读
-w 可写
-x 可执行
-s 非空(返回大小)

5.2 数字比较 #

perl
my $a = 10;
my $b = 20;

if ($a < $b) {
    print "$a is less than $b\n";
}

if ($a == 10) {
    print "a equals 10\n";
}

5.3 字符串比较 #

perl
my $name = "Perl";

if ($name eq "Perl") {
    print "Match!\n";
}

if ($name =~ /^P/) {
    print "Starts with P\n";
}

六、逻辑组合 #

6.1 与条件 #

perl
my $age = 25;
my $has_license = 1;

if ($age >= 18 && $has_license) {
    print "Can drive\n";
}

6.2 或条件 #

perl
my $role = "admin";

if ($role eq "admin" || $role eq "superuser") {
    print "Has full access\n";
}

6.3 复杂条件 #

perl
my $age = 25;
my $role = "user";
my $verified = 1;

if (($age >= 18 && $role eq "user") || $role eq "admin") {
    print "Access granted\n";
}

七、实践练习 #

练习1:成绩等级判断 #

perl
#!/usr/bin/perl
use strict;
use warnings;
use v5.10;

print "Enter score: ";
my $score = <STDIN>;
chomp $score;

my $grade;
if ($score >= 90) {
    $grade = "A";
} elsif ($score >= 80) {
    $grade = "B";
} elsif ($score >= 70) {
    $grade = "C";
} elsif ($score >= 60) {
    $grade = "D";
} else {
    $grade = "F";
}

say "Score: $score, Grade: $grade";

练习2:简单计算器 #

perl
#!/usr/bin/perl
use strict;
use warnings;
use v5.10;

my ($a, $op, $b) = (10, '+', 5);

my $result = $op eq '+' ? $a + $b :
             $op eq '-' ? $a - $b :
             $op eq '*' ? $a * $b :
             $op eq '/' ? $a / $b :
             "Unknown operator";

say "$a $op $b = $result";

练习3:文件类型判断 #

perl
#!/usr/bin/perl
use strict;
use warnings;
use v5.10;

my $path = ".";

if (-d $path) {
    say "$path is a directory";
    my @files = glob("$path/*");
    say "Contains " . scalar(@files) . " items";
} elsif (-f $path) {
    say "$path is a file";
    say "Size: " . (-s $path) . " bytes";
    say "Readable" if -r $path;
    say "Writable" if -w $path;
} else {
    say "$path does not exist";
}

八、总结 #

本章学习了:

  • if、elsif、else语句
  • unless语句
  • given-when语句
  • 三元运算符
  • 条件表达式
  • 逻辑组合

下一章将学习循环语句。

最后更新:2026-03-27