Jerome Laliag
Jerome Laliag

Reputation: 47

Perl not continuing

Note: This is a test perl code to check if works.

I have problem with my perl script, I know there's a solution for that by adding

print "Your server is not ok, please check!\n";
die "- Server is not ok!\n";

But in my project after stop in die "- Server is not ok!\n"; continue the script, I use print to show if works.

Heres the code

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

See image...

enter image description here

Other solution to stop? Intead using die to continue the perl script.

I hope someone will fixed this little problem, thanks for all reply!

Upvotes: 0

Views: 92

Answers (1)

lordkain
lordkain

Reputation: 3109

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";

Upvotes: 1

Related Questions