逻辑运算符 #
一、逻辑运算符概述 #
Perl提供两组逻辑运算符:
| 高优先级 | 低优先级 | 说明 |
|---|---|---|
| && | and | 逻辑与 |
| || | or | 逻辑或 |
| ! | not | 逻辑非 |
二、逻辑与 #
2.1 && 运算符 #
perl
my $a = 1;
my $b = 1;
if ($a && $b) {
print "Both are true\n";
}
2.2 and 运算符 #
perl
my $a = 1;
my $b = 1;
if ($a and $b) {
print "Both are true\n";
}
2.3 短路求值 #
perl
my $x = 0;
my $y = 1;
if ($x && $y++) {
print "True\n";
}
print "y = $y\n";
2.4 实际应用 #
perl
my $file = "test.txt";
if (-e $file && -r $file) {
print "File exists and is readable\n";
}
三、逻辑或 #
3.1 || 运算符 #
perl
my $a = 0;
my $b = 1;
if ($a || $b) {
print "At least one is true\n";
}
3.2 or 运算符 #
perl
my $a = 0;
my $b = 1;
if ($a or $b) {
print "At least one is true\n";
}
3.3 短路求值 #
perl
my $x = 1;
my $y = 0;
if ($x || $y++) {
print "True\n";
}
print "y = $y\n";
3.4 实际应用 #
perl
open my $fh, "<", "file.txt"
or die "Cannot open file: $!";
my $value = $config{setting} // "default";
四、逻辑非 #
4.1 ! 运算符 #
perl
my $a = 0;
if (!$a) {
print "a is false\n";
}
4.2 not 运算符 #
perl
my $a = 0;
if (not $a) {
print "a is false\n";
}
4.3 实际应用 #
perl
my $logged_in = 0;
if (!$logged_in) {
print "Please log in\n";
}
五、优先级差异 #
5.1 高优先级 vs 低优先级 #
perl
my $a = 0;
my $b = 1;
my $result1 = $a && $b || 1;
my $result2 = $a and $b or 1;
print "result1 = $result1\n";
print "result2 = $result2\n";
5.2 推荐用法 #
- 使用
&&,||,!用于表达式 - 使用
and,or,not用于流程控制
perl
if ($condition1 && $condition2) {
do_something();
}
open my $fh, "<", $file or die "Cannot open: $!";
六、定义或运算符 // #
6.1 基本用法 #
perl
my $name;
my $display = $name // "Anonymous";
print $display;
6.2 vs || #
perl
my $value = 0;
my $result1 = $value || "default";
my $result2 = $value // "default";
print "||: $result1\n";
print "//: $result2\n";
6.3 实际应用 #
perl
my $config = {
timeout => 30,
retries => 0,
};
my $timeout = $config->{timeout} // 60;
my $retries = $config->{retries} // 3;
my $debug = $config->{debug} // 0;
print "timeout: $timeout\n";
print "retries: $retries\n";
print "debug: $debug\n";
七、异或运算符 #
Perl没有内置的异或运算符,但可以这样实现:
7.1 使用 xor #
perl
my $a = 1;
my $b = 0;
if ($a xor $b) {
print "Exactly one is true\n";
}
7.2 自定义异或 #
perl
sub xor_custom {
my ($a, $b) = @_;
return ($a && !$b) || (!$a && $b);
}
八、三元运算符 #
8.1 基本语法 #
perl
my $condition = 1;
my $result = $condition ? "true" : "false";
print $result;
8.2 实际应用 #
perl
my $age = 20;
my $status = $age >= 18 ? "adult" : "minor";
print $status;
8.3 嵌套三元 #
perl
my $score = 85;
my $grade = $score >= 90 ? "A" :
$score >= 80 ? "B" :
$score >= 70 ? "C" :
$score >= 60 ? "D" : "F";
print $grade;
九、布尔上下文 #
9.1 假值 #
以下值在布尔上下文中为假:
perl
my @false_values = (0, "0", "", undef, ());
foreach my $val (@false_values) {
if ($val) {
print "true\n";
} else {
print "false\n";
}
}
9.2 真值 #
其他所有值为真:
perl
my @true_values = (1, -1, "hello", " ", "0.0", []);
foreach my $val (@true_values) {
if ($val) {
print "true\n";
}
}
十、实践练习 #
练习1:用户验证 #
perl
#!/usr/bin/perl
use strict;
use warnings;
use v5.10;
my ($username, $password, $is_admin) = ("admin", "secret", 1);
if ($username eq "admin" && $password eq "secret") {
say "Login successful";
if ($is_admin) {
say "Welcome, Administrator!";
}
} else {
say "Login failed";
}
练习2:配置检查 #
perl
#!/usr/bin/perl
use strict;
use warnings;
use v5.10;
my %config = (
host => "localhost",
port => 3306,
);
my $host = $config{host} // "127.0.0.1";
my $port = $config{port} // 80;
my $user = $config{user} // "root";
my $pass = $config{pass} // "";
say "Host: $host";
say "Port: $port";
say "User: $user";
say "Pass: " . ($pass ? "****" : "(empty)");
练习3:条件输出 #
perl
#!/usr/bin/perl
use strict;
use warnings;
use v5.10;
my @items = (1, 2, 3, 4, 5);
my $count = scalar @items;
my $message = $count == 0 ? "No items" :
$count == 1 ? "One item" :
"$count items";
say $message;
my @empty = ();
$count = scalar @empty;
$message = $count == 0 ? "No items" :
$count == 1 ? "One item" :
"$count items";
say $message;
十一、总结 #
本章学习了:
- 逻辑与运算符
&&和and - 逻辑或运算符
||和or - 逻辑非运算符
!和not - 运算符优先级差异
- 定义或运算符
// - 三元运算符
- 布尔上下文
下一章将学习赋值运算符。
最后更新:2026-03-27