Perl打印消息

时间:2015-11-05 10:33:29

标签: 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脚本中解决我的问题。

1 个答案:

答案 0 :(得分: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";
}
相关问题