title: Perl Script for Email validation?
body: <p>I've created a perl script to validate email ids for my marketing team to send campaigns.</p>
  
  <p>The script is behaving  erratically.</p>
  
  <p>For Example, I had validated 135 email ids on various dates,</p>
  
  <pre><code>******************************************
  Date   |    Valid     | Invalid  | Total
  ******************************************
  23-Dec-13    45       |   90     | 135
  ******************************************
  24-Dec-13    90       |   45     | 135
  ******************************************
  25-Dec-13    133      |   02     | 135
  ******************************************
  </code></pre>
  
  <p>I'm unable to figure out where it went wrong,</p>
  
  <p><a href="https://github.com/bcrajkumar/techstuff/commit/8af92f1c60c7da6eddd4fa2f9705a87e52b25692" rel="nofollow">Link to code</a></p>
  
  <p>Code:</p>
  
  <pre><code> #!/usr/bin/perl
   use Data::Dumper;
  
   %lookup_cache = ();
  
   sub valid_address {
     my($addr) = @_;
     my($domain, $valid);
  
     # Lower-case address
     $addr = lc($addr);
  
     # Validate format of address
     return(0) unless ($addr =~ /^[^@]+@([-\w]+\.)+[a-z]{2,4}$/);
  
     # Grab domain
     $domain = (split(/@/, $addr))[1];
  
     # Lookup and return cached result if it exists
     $cached_result = $lookup_cache{$domain};
     if ($cached_result ne '')
     {
       #print "[cached_result] ";
       return $cached_result;
     }
  
     # Do domain lookup
     $valid = 0;
     if (open(DNS, "nslookup -q=any $domain |"))
     {
       while (&lt;DNS&gt;) {
         $valid = 1 if (/^$domain.*\s(mail exchanger|internet address)\s=/i);
       }
     }
  
     # Store cached result for later
     $lookup_cache{$domain} = $valid;
  
     return $valid;
   }
  
   while (&lt;&gt;) {
     $addy = $_;
     $addy =~ s/\s+$//;
     if ($addy)
     {
       print "$addy " . (valid_address($addy) ? 'valid' : 'invalid') . "\n";
     }
   }
  </code></pre>