欢迎使用Perl。
您的代码的问题是您无法从外部循环中逃脱。
<>
将返回
undef
当它到达文件末尾时。此时,内部循环结束,外部循环将其送回。强制进一步读取原因
<>
开始看
STDIN
它从不发送EOF,所以您的循环将永远持续下去。
由于这是您的第一个Perl脚本,我将用一些注释为您重写它。Perl是一种非常好的语言,您可以编写一些很好的代码,但是大多数情况下,由于它的年代久远,有些旧的样式不再被建议使用。
use warnings; # Warn about coding errors
use strict; # Enforce good style
use 5.010; # Enable modernish (10 year old) features
# Another option which mostly does the same as above.
# I normally do this, but it does require a non-standard CPAN library
# use Modern::Perl;
# Much better style to have the condition in the while loop
# Much clearer than having an infinite loop with break/last statements
# Also avoid $l as a variable name, it looks too much like $1
my $count = 0; # Note variable declaration, enforced by strict
while(my $line = <>) {
if ($line =~ /^>/) {
# End of input block, output and reset
say $count;
$count = 0;
} else {
$count += length($line);
}
}
# Have reached the end of the input files
say $count;