(e-mail address removed) (Ali Ataman) wrote in
I have a script that should read a file line by line and make a querry
for each of these domains in the file but it won't work. Can anyone
tell me where the problem might be?
Thanx.
#!/usr/bin/perl -w
use warnings;
is preferable.
Also, you are missing
use strict;
Please read the posting guidelines for this group before going any
further.
$file = '/domlist'; # Name the file
Useless comments like this are neither necessary nor helpful.
open(INFO, $file); # Open the file
Always check if open succeeded. Also, I prefer to use the 3-argument form
of open and lexical filehandles:
open my $INFO, '<', $file or die "Cannot open $file: $!";
However, for the purposes of posting in this group, it would be better if
you put some sample data in the __DATA__ section of your script.
Better to process the file one line at a time:
while( said:
use Net:
NS;
There is no point in putting this inside your loop.
my $res = Net:
NS::Resolver->new;
my @mx = mx($res,"$_");
Useless use of quotes. But more importantly, $_ probably has a newline at
the end.
if (@mx) {
foreach $rr (@mx) {
print $rr->preference, " ", $rr->exchange, "\n";
}
} else {
warn "Can't find MX records for $_: ", $res->errorstring, "\n";
}
close(INFO);
}
Always reduce your scripts to the smallest possible program that still
displays the problem. So, for example, if you had tried:
#! /usr/bin/perl
use strict;
use warnings;
use Data:
umper;
use Net:
NS;
my @mx = mx 'unur.com';
print Dumper \@mx;
__END__
C:\Dload> perl dns.pl
$VAR1 = [
bless( {
'preference' => 5,
'rdlength' => 9,
'ttl' => 86400,
'name' => 'unur.com',
'class' => 'IN',
'type' => 'MX',
'rdata' => ' ??root+?',
'exchange' => 'root.unur.com'
}, 'Net:
NS::RR::MX' )
];
and seen that it produces the output you wanted, you would have known
that the problem lay in the reading of domain names from the file as the
following version demonstrates:
#! /usr/bin/perl
use strict;
use warnings;
use Data:
umper;
use Net:
NS;
while(<DATA>) {
my @mx = mx $_;
print Dumper \@mx;
}
__DATA__
unur.com
C:\Dload> perl dns.pl
$VAR1 = [];
$VAR1 = [];
A few seconds of head scratching would then have enabled you to realize
that you forgot to chomp the data you read:
#! /usr/bin/perl
use strict;
use warnings;
use Data:
umper;
use Net:
NS;
while(<DATA>) {
chomp;
next unless $_;
my @mx = mx $_;
print Dumper \@mx;
}
__DATA__
unur.com
C:\Dload> perl dns.pl
$VAR1 = [
bless( {
'preference' => 5,
'rdlength' => 9,
'ttl' => 86194,
'name' => 'unur.com',
'class' => 'IN',
'type' => 'MX',
'rdata' => ' ??root+?',
'exchange' => 'root.unur.com'
}, 'Net:
NS::RR::MX' )
];
Now, reading the posting guidelines and following the suggestions in that
document would have enabled you to solve the problem on your own. So,
please go ahead, and read that document.
Sinan