Re: [問題] 在do while中使用last跳出無作用

看板Perl作者時間15年前 (2009/11/19 13:39), 編輯推噓2(200)
留言2則, 2人參與, 最新討論串2/3 (看更多)
※ 引述《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
謝謝 liyih
11/19 22:13, 1F

11/20 00:49, , 2F
推一下!
11/20 00:49, 2F
文章代碼(AID): #1B1DegVx (Perl)
文章代碼(AID): #1B1DegVx (Perl)