2015-11-05 68 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

是的,我的帖子是一个测试,检查是否工作。 –