简体   繁体   中英

How to test for undef when running whois in Perl?

In the following code example, I am trying to get a whois statement. Every now and then it throws a timeout exception. From PerlDoc it says that in cases the $Net::Whois::Raw::CHECK_FAIL = 1; is set to 1 then it should return undef .

I encapsulated the whois with eval so it will nto break the script, and then I added a sleep and then I attempt to get the whois again. But it throws a warning " ...isn't numeric in numeric eq (==) ".

So again, I un the run, and when it gets to the if it stills executes the code in there, and throws that error I mentioned above. How can I safetly evaluate the undef when it happens?

#!/usr/bin/perl
use strict;
use warnings;
use DBI;
use Net::Whois::Raw;
#use Net::Whois::Parser;
use Data::Dumper;

$Net::Whois::Raw::OMIT_MSG = 2; 
$Net::Whois::Raw::CHECK_FAIL = 1; 
$Net::Whois::Raw::TIMEOUT = 30;

my $domainName = "google.com";
my $domainInfo;

while (1) {
    eval {
        $domain_info = whois($domainName);
    };

    if (undef == $domain_info) { 
        sleep (10); 
        eval {
            $domain_info = whois($domainName);
        };
    }
}

== performs a numerical comparison. Its operands are coerced into numbers if they are not. It's not appropriate to use == here. To check if a scalar is defined or not, use defined .

my $domain_info;
while (1) {
    $domain_info = eval { whois($domainName) };
    last if defined($domain_info);
    sleep(10); 
}

Replace

 if (undef == $domain_info) { 

with

if (not defined $domain_info) { 

to check whether variable is not defined .

== is for comparing two numbers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM