Re: Perl IP to Domain Name converter.



RedGrittyBrick <RedGrittyBrick@xxxxxxxxxxxxxxxxx> wrote in
news:49870cbe$0$30301$da0feed9@xxxxxxxxxxxxxx:

http://www.osix.net/modules/article/?id=194

Ick!

Ick! is right. For many reasons. Here is one:

<quote src="http://www.osix.net/modules/article/?id=194";>
sub resolve_ip {
my($this_ip);
foreach $this_ip (keys %cached_ips) {
$_[0] eq $this_ip ? return($cached_ips{$this_ip}) :"";
}
return(cache_ip($_[0]));
}

sub cache_ip {
my $name;
if( ($name= gethostbyaddr(inet_aton($_[0]), AF_INET)) eq "" ){
return($cached_ips{$_[0]}=$_[0]);
} else {
return($cached_ips{$_[0]}=$name);
}
}
</quote>

This is insanely bad. Here is something less bad:

#!/usr/bin/perl

use strict;
use warnings;

use Memoize;
use Socket;

memoize( 'resolve_ip' );

while ( <DATA> ) {
chomp;
last unless length;
print "Looking up '$_'\n\t";
print resolve_ip( $_ ), "\n";
}

sub resolve_ip {
my ($ip) = @_;

my $name = gethostbyaddr inet_aton($ip), AF_INET;

return $name if defined $name;
return $ip;
}

__DATA__
74.125.45.100
68.180.206.184
207.46.197.32
74.125.45.100
68.180.206.184
207.46.197.32

--
A. Sinan Unur <1usa@xxxxxxxxxxxxxxxxxxx>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
.



Relevant Pages