You're opening and closing DATAOUT too often. How about loading an array and doing the write to file just once.
sub lookup {
my ($ldns, $fqdn) = @_;
my ($error, $error_a);
open DIG, "$dig \@$ldns $type $fqdn | " or die $!;
while (<DIG>) {
if (/;; ->>HEADER<<- opcode: QUERY\s|QUERY, status: NOERRO
+R, .*/) {
$error = 1;
next;
}
next unless /$fqdn\.\s+(\w+)(\s+A|\s+IN\sA)\s+(.*)/;
$error_a = 1;
my $time = lc($1);
my $ip = $3;
my $seconds = $time =~ /[dhms]/ ? string_to_time($time) :
+$time;
my $extended_time = time_to_string($seconds);
print "Request: $fqdn\tTTL: $extended_time\tIP: $ip\n" if
+$verbose;
push @data_out, "$ldns\t$fqdn\t$seconds\t$ip\n" if $outfil
+e;
}
close DIG;
if (!$error) {
print "Request: $fqdn\tERROR (No domain found)\n" if $verbose;
push @data_out, "$ldns\t$fqdn\tERROR\tNO DOMAIN\n" if $outfile
+;
} elsif (!$error_a) {
print "Request: $fqdn\tERROR (Domain found, no A record found)
+\n" if $verbose;
push @data_out, "$ldns\t$fqdn\tERROR\tNOT A RECORD\n" if $outf
+ile;
}
}
As you can see, the code is tighter and easier to read. But, we now must add a test for @data_out.
if (@data_out) {
open (DATAOUT, ">> $outfile") || die "can't open $outfile: $!";
print DATAOUT @data_out;
close DATAOUT;
}
This can be rewritten by taken advantage of the x operator.
foreach $thing (@ns){
push (@provide, "Unknown");
}
push @provide, ('Unknown') x @ns;
And, finally, all those ifs in the beginning could be combined.
if ( $help
|| !@ns && !$nsfile
|| !@addr && !$addrfile
|| !$verbose && !$outfile ) {
print <<END;
.
.
.
HTH,
Charles K. Clarkson
|