2015-11-05 216 views
0

我的问题是,如何打印该消息,查看或查看我的代码。Perl打印消息

继承人的代码...

#!/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 { 
     print "- Server is not ok!\n"; 
     last; 
    } 
    sleep 1; 
} 

# How to show this message if "- Server is not ok!\n". 
# Example: perl test.pl 
# + Server is ok! 
# + Server is ok! 
# + Server is ok! 
# + Server is ok! 
# + Server is ok! 
# + Server is ok! 
# - Server is not ok! 
# Your server is not ok, please check! 
# 
print "Your server is not ok, please check!\n"; 

# And how to show this message if 10 reply is "+ Server is ok!\n". 
# Example: perl test.pl 
# + Server is ok! 
# + Server is ok! 
# + Server is ok! 
# + Server is ok! 
# + Server is ok! 
# + Server is ok! 
# + Server is ok! 
# + Server is ok! 
# + Server is ok! 
# + Server is ok! 
# Your server is ok, Congrats! 
# 
print "Your server is ok, Congrats!\n"; 

如何显示此消息如果- Server is not ok!\n

例如:perl的test.pl

  • 服务器就行了!

  • 服务器没问题!

  • 服务器没问题!

  • 服务器没问题!

  • 服务器没问题!

  • 服务器没问题!

  • 服务器不正常!

您的服务器不正常,请检查!

以及如果10回复是“+服务器正常!\ n”,如何显示此消息。

例如:perl的test.pl

  • 服务器就行了!

  • 服务器没问题!

  • 服务器没问题!

  • 服务器没问题!

  • 服务器没问题!

  • 服务器没问题!

  • 服务器没问题!

  • 服务器没问题!

  • 服务器没问题!

  • 服务器没问题!

您的服务器没问题,恭喜!

在此先感谢有人会解决我的问题在Perl脚本。

回答

2

您可以在跳出last之前输出消息。但在更复杂的情况,你可以引入一个“标志”变量:

my $ok = 1; 

last之前,将其设置为零:

$ok = 0; 

在最后,用它来确定消息:

if ($ok) { 
    print "Your server is ok, Congrats!\n"; 
} else { 
    print "Your server is not ok, please check!\n"; 
} 
+0

你能为我重新编码我的脚本吗? –

+3

不幸的是,我有我的脚本来编码... :) – choroba

+0

好吧,我明白了!感谢这个解决方案。我的脚本现在好了。 :) –