循环语句 #
一、while循环 #
1.1 基本while #
perl
my $count = 1;
while ($count <= 5) {
print "Count: $count\n";
$count++;
}
1.2 读取文件 #
perl
open my $fh, "<", "file.txt" or die $!;
while (my $line = <$fh>) {
chomp $line;
print $line . "\n";
}
close $fh;
1.3 无限循环 #
perl
while (1) {
print "Running...\n";
last;
}
1.4 语句修饰符 #
perl
my $count = 0;
print $count++, "\n" while $count < 5;
二、until循环 #
2.1 基本until #
until 是 while not 的简写:
perl
my $count = 1;
until ($count > 5) {
print "Count: $count\n";
$count++;
}
等价于:
perl
while (!($count > 5)) {
print "Count: $count\n";
$count++;
}
2.2 语句修饰符 #
perl
my $done = 0;
do_something() until $done;
三、for循环 #
3.1 C风格for #
perl
for (my $i = 1; $i <= 5; $i++) {
print "i = $i\n";
}
3.2 逆序循环 #
perl
for (my $i = 5; $i >= 1; $i--) {
print "i = $i\n";
}
3.3 步进循环 #
perl
for (my $i = 0; $i <= 10; $i += 2) {
print "i = $i\n";
}
3.4 多变量 #
perl
for (my ($i, $j) = (1, 10); $i <= $j; $i++, $j--) {
print "i=$i, j=$j\n";
}
四、foreach循环 #
4.1 遍历数组 #
perl
my @colors = ("red", "green", "blue");
foreach my $color (@colors) {
print $color . "\n";
}
4.2 默认变量$_ #
perl
my @colors = ("red", "green", "blue");
foreach (@colors) {
print $_ . "\n";
}
4.3 范围遍历 #
perl
foreach my $i (1..10) {
print $i . "\n";
}
4.4 修改元素 #
perl
my @numbers = (1, 2, 3, 4, 5);
foreach my $num (@numbers) {
$num *= 2;
}
print "@numbers\n";
4.5 for和foreach等价 #
perl
for my $item (@array) { }
foreach my $item (@array) { }
五、循环控制 #
5.1 last #
last 跳出循环(类似break):
perl
my @numbers = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
foreach my $num (@numbers) {
last if $num > 5;
print $num . "\n";
}
5.2 next #
next 跳过当前迭代(类似continue):
perl
my @numbers = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
foreach my $num (@numbers) {
next if $num % 2 == 0;
print $num . "\n";
}
5.3 redo #
redo 重新执行当前迭代:
perl
my $count = 0;
while ($count < 5) {
$count++;
print "Count: $count\n";
redo if $count == 3;
}
5.4 循环标签 #
perl
OUTER: for my $i (1..3) {
INNER: for my $j (1..3) {
if ($i == 2 && $j == 2) {
last OUTER;
}
print "i=$i, j=$j\n";
}
}
5.5 continue块 #
perl
my $count = 0;
while ($count < 5) {
$count++;
next if $count == 3;
print "Count: $count\n";
}
continue {
print " (iteration done)\n";
}
六、循环技巧 #
6.1 读取输入 #
perl
print "Enter lines (Ctrl+D to end):\n";
while (my $line = <STDIN>) {
chomp $line;
last if $line eq "quit";
print "You entered: $line\n";
}
6.2 遍历哈希 #
perl
my %person = (
name => "Tom",
age => 25,
city => "Beijing",
);
while (my ($key, $value) = each %person) {
print "$key: $value\n";
}
6.3 无限循环惯用法 #
perl
{
print "Processing...\n";
last if done();
redo;
}
while (1) {
my $input = get_input();
last if $input eq "exit";
process($input);
}
6.4 列表生成 #
perl
my @squares = map { $_ ** 2 } 1..10;
my @evens = grep { $_ % 2 == 0 } 1..20;
七、嵌套循环 #
7.1 基本嵌套 #
perl
for my $i (1..3) {
for my $j (1..3) {
print "($i, $j) ";
}
print "\n";
}
7.2 使用标签控制 #
perl
OUTER: for my $i (1..3) {
for my $j (1..3) {
if ($i + $j == 4) {
next OUTER;
}
print "($i, $j) ";
}
print "\n";
}
八、实践练习 #
练习1:乘法表 #
perl
#!/usr/bin/perl
use strict;
use warnings;
use v5.10;
for my $i (1..9) {
for my $j (1..$i) {
printf "%d×%d=%-4d", $j, $i, $i * $j;
}
print "\n";
}
练习2:猜数字游戏 #
perl
#!/usr/bin/perl
use strict;
use warnings;
use v5.10;
my $target = int(rand(100)) + 1;
my $attempts = 0;
say "Guess a number between 1 and 100!";
while (1) {
print "Your guess: ";
my $guess = <STDIN>;
chomp $guess;
$attempts++;
if ($guess < $target) {
say "Too low!";
} elsif ($guess > $target) {
say "Too high!";
} else {
say "Correct! You got it in $attempts attempts!";
last;
}
}
练习3:文件处理 #
perl
#!/usr/bin/perl
use strict;
use warnings;
use v5.10;
my $file = "data.txt";
open my $fh, "<", $file or die "Cannot open $file: $!";
my ($line_count, $word_count, $char_count) = (0, 0, 0);
while (my $line = <$fh>) {
$line_count++;
$char_count += length($line);
my @words = split /\s+/, $line;
$word_count += scalar @words;
}
close $fh;
say "Lines: $line_count";
say "Words: $word_count";
say "Characters: $char_count";
练习4:过滤处理 #
perl
#!/usr/bin/perl
use strict;
use warnings;
use v5.10;
my @data = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
my @evens = grep { $_ % 2 == 0 } @data;
my @squares = map { $_ ** 2 } @evens;
my $sum = 0;
$sum += $_ foreach @squares;
say "Original: @data";
say "Evens: @evens";
say "Squares of evens: @squares";
say "Sum of squares: $sum";
九、总结 #
本章学习了:
- while循环
- until循环
- for循环
- foreach循环
- 循环控制(last、next、redo)
- 循环标签
- 嵌套循环
下一章将学习子程序。
最后更新:2026-03-27