2015-11-05 64 views
0

注意:這是一個測試Perl代碼來檢查是否有效。Perl不會繼續

我有問題,我的Perl腳本,我知道有加入

print "Your server is not ok, please check!\n"; 
die "- Server is not ok!\n"; 

但停在die "- Server is not ok!\n";後,我的項目是爲一個解決方案繼續腳本,我使用的打印顯示,如果作品。

繼承人的代碼

#!/usr/bin/perl 

use strict; 
use warnings; 
use LWP::UserAgent; 

my $ua = LWP::UserAgent->new(timeout => 1); 
$ua->agent("007"); 

my $req = HTTP::Request->new(GET => 'http://www.google.com.ph/'); 

my $res; 
for (1 .. 10) { 
    $res = $ua->request($req); 
    if ($res->is_success) { 
     print "+ Server is ok!\n"; 
    } 
    else { 
     die "- Server is not ok!\n"; # I want to stop if server is not ok and print last code. Or other solution to stop instead of using die. 
    } 
    sleep 1; 
} 
print "Your server is not ok, please check!\n"; # Why this print not showing if stop in "- Server is not ok!\n"? 

見圖片...

enter image description here

其他解決方案停止?使用die繼續執行perl腳本。

我希望有人會解決這個小問題,謝謝你的回覆!

+2

你究竟想要達到什麼目的?輸出看起來是正確的。 'die'終止程序,而不是循環。有什麼問題? – Arc676

+0

停止'HTTP :: Request'並打印最後一個的其他方法。 –

+2

如果你想跳出循環,使用'last;'。 – Ryan

回答

1
for (1 .. 10) { 
    $res = $ua->request($req); 
    if ($res->is_success) { 
     print "+ Server is ok!\n"; 
    } 
    else { 
     print "- Server is not ok!\n"; 
     last; #this statement causes to exit the loop 
    } 
    sleep 1; 
} 

# at this point everything is oke 
print "Your server is ok!\n"; 
+0

我想停止' - 服務器不正常!\ n'並繼續或打印最後的代碼。 –

+0

想繼續for循環? – lordkain

+0

是的,我的帖子是一個測試,檢查是否工作。 –