home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  

Book Home Programming PerlSearch this book

32.35. Net::hostent

use Socket;
use Net::hostent;

print inet_ntoa(gethost("www.perl.com")->addr);  # prints
printf "%vd", gethost("www.perl.com")->addr;     # same thing

print gethost("")->name;                # prints localhost

use Net::hostent ':FIELDS';
if (gethost($name_or_number)) {
    print "name is $h_name\n";
    print "aliases are $h_aliases\n";
    print "addrs are ",
        join ", " => map { inet_ntoa($_) } @h_addr_list;
This module's default exports override the core gethostbyname and gethostbyaddr functions, replacing them with versions that return a Net::hostent object (or undef on failure). This object has attribute accessor methods that return the like-named structure field name from the C library's struct hostent from netdb.h: name, aliases, addrtype, length, or addr_list. The aliases and addr_list methods return array references; the rest return scalars. The addr method is equivalent to the initial element in the addr_list array reference. The gethost function is a frontend that forwards a numeric argument to gethostbyaddr by way of the Socket::inet_aton function and the rest to gethostbyname. As with the other semipragmatic modules that override list-returning built-ins, if you import the ":FIELDS" tag, you can access scalar or array package variables by the same names as the method calls by using a leading "h_". This still overrides your core functions, though.

Library Navigation Links

Copyright © 2001 O'Reilly & Associates. All rights reserved.