Skip to Content
Programming Perl, 3rd Edition
book

Programming Perl, 3rd Edition

by Larry Wall, Tom Christiansen, Jon Orwant
July 2000
Intermediate to advanced
1104 pages
35h 1m
English
O'Reilly Media, Inc.
Content preview from Programming Perl, 3rd Edition

Net::hostent

use Socket;
use Net::hostent;

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

print gethost("127.0.0.1")->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.

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Programming Perl, 4th Edition

Programming Perl, 4th Edition

Tom Christiansen, brian d foy, Larry Wall, Jon Orwant
Learning Perl, 7th Edition

Learning Perl, 7th Edition

Randal L. Schwartz, brian d foy, Tom Phoenix
Programming the Perl DBI

Programming the Perl DBI

Tim Bunce, Alligator Descartes

Publisher Resources

ISBN: 0596000278Supplemental ContentErrata