Re: [問題] 在do while中使用last跳出無作用
※ 引述《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
推
11/19 22:13, , 1F
11/19 22:13, 1F
推
11/20 00:49, , 2F
11/20 00:49, 2F
討論串 (同標題文章)
Perl 近期熱門文章
PTT數位生活區 即時熱門文章