Perl没有继续

时间:2015-11-05 08:31:30

标签: perl

注意:这是一个测试perl代码,用于检查是否有效。

我的perl脚本有问题,我知道通过添加

可以解决这个问题
print "Your server is not ok, please check!\n";
die "- Server is not ok!\n";

但是在die "- Server is not ok!\n";停止后我的项目中继续编写脚本,我使用print来显示是否有效。

继承代码

#!/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

停止其他解决方案? Intead使用die继续perl脚本。

我希望有人能解决这个小问题,谢谢你的回复!

1 个答案:

答案 0 :(得分: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";