今天发现Perl中原来还有个while-continue这样的循环。其功能是除了在while中进行last语句外,每走一次while循环,再执行
紧接其后的continue语句,甚至包括next语句。并且在while(),括号中的变量是可以在continue中继续使用的,当然也包括$_。比如:
#!/usr/bin/perl
my $temp_file = "tmp.$$";#后缀是进城ID
open (TMP,"> $temp_file") or die("die:$!");
open (INPUT,"< megaliths.dat") or die("die:$!");
while (my $line = <INPUT>) {
#在continue中是undef
my $while = "above";
#根据预设的正则进行过滤,以缩小进一步修改的范围
next unless $line =~ //Q$some_pattern/;#/Q把特殊的正则元字符一般化
$line .= " append";
}
continue{
#即使在next语句后,continue仍然执行
print TMP "In continue $line/n";
print "[$while]/n";
}
close(TMP);
close(INPUT);
open A ,"<w.txt" or die $!;
while (<A>){
next if /5/;
print "1--$_ is $_
";
}
continue {print "2--$_ is $_
";}
zjzc01:/root/dbi# perl a3.pl
1--$_ is 1
2--$_ is 1
1--$_ is 2
2--$_ is 2
1--$_ is 3
2--$_ is 3
1--$_ is 4
2--$_ is 4
2--$_ is 5
1--$_ is 6
2--$_ is 6
1--$_ is 7
2--$_ is 7
1--$_ is 8
2--$_ is 8
1--$_ is 9
2--$_ is 9
1--$_ is 10
2--$_ is 10