注意:這是一個測試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"?
見圖片...
其他解決方案停止?使用die
繼續執行perl腳本。
我希望有人會解決這個小問題,謝謝你的回覆!
你究竟想要達到什麼目的?輸出看起來是正確的。 'die'終止程序,而不是循環。有什麼問題? – Arc676
停止'HTTP :: Request'並打印最後一個的其他方法。 –
如果你想跳出循環,使用'last;'。 – Ryan