作者liyih ()
看板Perl
标题Re: [问题] 在do while中使用last跳出无作用
时间Thu Nov 19 13:39:51 2009
※ 引述《windincloud (当你孤单会想起谁)》之铭言:
: 昨天测试各种无限回圈中使用last跳出的情况
: 意外发现若在do while 中使用last跳出会出现错误讯息
: 有人知道是啥原因造成的嘛?
: 测资
: ---
: #!/usr/bin/perl
: for(;;)
: {
: print "Hello\n";
: last;
: }
: #成功跳出
: ---
: #!/usr/bin/perl
: while(1)
: {
: print "Hello\n";
: last;
: }
: #成功跳出
: ---
: #!/usr/bin/perl
: do
: {
: print "Hello\n";
: last;
: }
: while(1)
: #失败
: # error msg: Can't "last" outside a loop block
: ---
: #!/usr/bin/perl
: do
: {
: print "Hello\n";
: last;
: }
: for(0..50)
: #成功但不是无穷回圈
: ---
: #!/usr/bin/perl
: do
: {
: print "Hello\n";
: last;
: }
: for(;;)
: #没这样写法
: 希望有人能解答一下这问题~
: 或是提供一下官方手册中是否有提到 last 在 do while回圈内会失效~
: 感谢罗~
参考文件
1.
http://perldoc.perl.org/functions/do.html
2.
http://perldoc.perl.org/perlsyn.html
3.
http://en.wikipedia.org/wiki/Perl_control_structures
...
do BLOCK does not count as a loop, so the loop control statements next,
last, or redo cannot be used to leave or restart the block. See perlsyn for
alternative strategies.
...
以下的写法或许可以,不过有明确的方式和条件来结束回圈结构会比较适当。
LOOP: {
do {
last LOOP;
} while (1);
}
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.114.64.130
1F:推 cutecpu:谢谢 liyih 11/19 22:13
2F:推 abliou:推一下! 11/20 00:49