Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Uninitialized value in unpack
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

Report this thread to moderator Post Follow-up to this message
Old Post
perl@fongo.de
12-23-04 08:58 PM


Re: Uninitialized value in unpack

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

Report this thread to moderator Post Follow-up to this message
Old Post
JupiterHost.Net
12-23-04 08:58 PM


Re: Uninitialized value in unpack
perl@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

Report this thread to moderator Post Follow-up to this message
Old Post
John W. Krahn
12-23-04 08:58 PM


Re: Uninitialized value in unpack
On 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,,

Report this thread to moderator Post Follow-up to this message
Old Post
Jonathan Paton
12-23-04 08:58 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PERL Beginners archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 07:59 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.