1

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);
        };
    }
}
KingsInnerSoul
  • 1,373
  • 4
  • 20
  • 49

2 Answers2

6

== 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); 
}
ikegami
  • 367,544
  • 15
  • 269
  • 518
4

Replace

 if (undef == $domain_info) { 

with

if (not defined $domain_info) { 

to check whether variable is not defined.

== is for comparing two numbers.

melpomene
  • 84,125
  • 8
  • 85
  • 148
mpapec
  • 50,217
  • 8
  • 67
  • 127