Code Comments
Programming Forum and web based access to our favorite programming groups.
Can anyone point out what is uninitialized in line 162?
my @resolved = ();
foreach (@dns){
my $ip = gethostbyname($_);
my ($a,$b,$c,$d);
my $x = join (".", (unpack('C4',$ip ))); # line 162
$x ||= "????";
push (@resolved, join(" => ", $_,$x));
}
Thanks
Post Follow-up to this message
perl@fongo.de wrote:
> Can anyone point out what is uninitialized in line 162?
>
> my @resolved = ();
> foreach (@dns){
> my $ip = gethostbyname($_);
> my ($a,$b,$c,$d);
> my $x = join (".", (unpack('C4',$ip ))); # line 162
> $x ||= "????";
> push (@resolved, join(" => ", $_,$x));
>
> }
lets narrow it down a bit:
#!/usr/bin/perl
use strict;
use warnings;
for(@ARGV) {
my $ip = gethostbyname($_);
print "Host -$_-\nResolved: ";
print join ('.', unpack('C4',$ip)) || '????';
print "\n";
}
perl dns.pl is ok (is ok)
perl dns.pl google.com search.cpan.org (is ok)
perl dns.pl google.com search.cpan.org fake.fake (uninitialized value)
I imagine the join is only given one thing and not a list when the
hostname lookup fails:
IE like me saying to you "please tell me the sum of 8 +"
then you'd say "er, 8 + what? the second value you have not initialized?"
The solution would be to check for that before using it:
#!/usr/bin/perl
use strict;
use warnings;
for(@ARGV) {
print "Host -$_-\nResolved: ";
my $ip = gethostbyname($_);
if($ip) {
print join ('.', unpack('C4',$ip)),"\n";
} else {
print "????\n";
}
}
Now try it, no more warning :)
HTH :)
Lee.M - JupiterHhost.Net
Post Follow-up to this messageperl@fongo.de wrote:
> Can anyone point out what is uninitialized in line 162?
You don't say which line is number 162.
> my @resolved = ();
> foreach (@dns){
> my $ip = gethostbyname($_);
gethostbyname() will return undef if $_ does not contain a valid host name.
defined( my $ip = gethostbyname($_) ) or do {
warn "Host '$_' not valid.\n";
next;
};
> my ($a,$b,$c,$d);
> my $x = join (".", (unpack('C4',$ip ))); # line 162
> $x ||= "????";
> push (@resolved, join(" => ", $_,$x));
>
> }
John
--
use Perl;
program
fulfillment
Post Follow-up to this messageOn Thu, 23 Dec 2004 14:34:02 +0100, perl@fongo.de <perl@fongo.de> wrote:
>
> Can anyone point out what is uninitialized in line 162?
>
> my @resolved = ();
> foreach (@dns){
> my $ip = gethostbyname($_);
If the gethostbyname() call fails (the name can not be resolved), then you
get undef. Line 162 is the first time you use $ip.
> my ($a,$b,$c,$d);
Are these for the future? Not used anywhere.
> my $x = join (".", (unpack('C4',$ip ))); # line 162
> $x ||= "????";
You would be better having $x undefined if the resolve
fails.
> push (@resolved, join(" => ", $_,$x));
>
> }
Since I have made several suggestions, here is what I
would write:
my %resolved;
for my $name (@dns) {
my $ip = gethostbyname $name;
if (defined $ip) {
$ip = join ".", unpack('C4', $ip);
}
$resolved{$name} = $ip;
}
Notice I have introduced a more meaningful loop variable,
removed "????" as the default and used a hash instead.
I suggest you have the code reviewed.
Jonathan Paton
--
#!perl
$J=' 'x25 ;for (qq< 1+10 9+14 5-10 50-9 7+13 2-18 6+13
17+6 02+1 2-10 00+4 00+8 3-13 3+12 01-5 2-10 01+1 03+4
00+4 00+8 1-21 01+1 00+5 01-7 >=~/ \S\S \S\S /gx) {m/(
\d+) (.+) /x,, vec$ J,$p +=$2 ,8,= $c+= +$1} warn $J,,
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.