曲径通幽论坛

标题: last 操作符 [打印本页]

作者: beyes    时间: 2011-10-9 17:52
标题: last 操作符
在 Perl 中,last 操作符就如同 C 语言中的 break,它的作用就是立即终止循环。如下例子所示:
[Perl] 纯文本查看 复制代码
#!/usr/bin/perl

while (<STDIN>) {
        chomp($_);
        if ($_ eq "yes") {
                last;
        } else {
                print "reinput..\n";
        }
}

print "input end\n";

for (1..10) {
        print "$_ ";
        if ($_ eq 5) {
                last;
        }
}


运行输出:
# ./last.pl
hello
reinput..
yes
input end
1 2 3 4 5
在 Perl 中有 5 种循环块,for foreach while until 以及裸块。裸块是只执行一次的循环,它也称为“伪循环”。比如:
{
  loop1;
  loop2;
  loop3;
}




欢迎光临 曲径通幽论坛 (http://www.groad.net/bbs/) Powered by Discuz! X3.2