Perl学习笔记

perl中的循环控制结构

2019-06-15  本文已影响0人  dming1024

摘自:Perl 语言入门(Learning Perl)
last: 终止循环的执行,结束了!类似C语言中的break。
next:结束当前这次循环的迭代,重新再来一次新的循环。
redo:回到循环模块内的顶端,重新做执行一次命令,但不计入循环。

可以通过此段代码对这三组关键词进一步理解:

vim x197.pl

#!/usr/bin/perl -w
foreach(1..10){
    print "iternation number $_.\n\n";
    print "Please choose:last, next,redo,or none of the above?";
    chomp(my $choice =<STDIN>);
    print "\n";
    last if $choice =~ /last/i;
    next if $choice =~ /next/i;
    redo if $choice =~ /redo/i;
    print "that wasn't any of the choices...onward!\n\n";
}

在shell下运行此段脚本

perl x197.pl 
iternation number 1.

Please choose:last, next,redo,or none of the above?

that wasn't any of the choices...onward!

iternation number 2.

Please choose:last, next,redo,or none of the above?next #输入next继续运行循环

iternation number 3.

Please choose:last, next,redo,or none of the above?redo #输入redo在循环内继续运行

iternation number 3.

Please choose:last, next,redo,or none of the above?last #输入last直接结束循环的运行
上一篇 下一篇

猜你喜欢

热点阅读