Net::hostent(3perl)
π Net::hostent β the by-name interface to Perl's built-in gethost*() functions β¨
okay bestie, buckle up, we're doing Net::hostent from the Perl Programmers Reference Guide (that's section 3perl, for the record) and honestly? it's giving β¨legacy networking realnessβ¨. let's go π¦
β¨ SYNOPSIS β¨
use Net::hostent;
that's it. that's the whole vibe. one line and you're in π
πΈ DESCRIPTION πΈ
so here's the tea: this module's default exports straight-up override your core gethostbyname() and gethostbyaddr() functions. not "add a wrapper", not "suggest nicely" β override, no cap. what you get back instead is a "Net::hostent" object π
that object has methods, and each one hands you a field name that mirrors the correspondingly named field in C's hostent structure from netdb.h. the whole lineup:
namealiasesaddrtypelengthaddr_list
now, aliases and addr_list return an array reference (so many pointers, so little time), and the rest return scalars. and addr? bestie, addr is literally just the zeroth element in that addr_list array reference. same thing, cuter name π
π«Ά importing the fields directly (the :FIELDS tag)
you can also import all the structure fields directly into your namespace as regular variables using the :FIELDS import tag. heads up tho β (and this is important, don't skip it) β this still overrides your core functions. no escape, sorry π
access these fields as variables named with a preceding "h_". so like, $host_obj->name() corresponds to $h_name if you import the fields. love that for you.
array references become available as regular array variables, so for example:
@{ $host_obj->aliases() }
would simply be @h_aliases. iconic, honestly π
π§ the gethost() front-end
there's also a gethost() function which is just a simple little front-end doing forwarding duties: if you give it a numeric argument it sends it off to gethostbyaddr() by way of Socket::inet_aton, and everything else goes to gethostbyname(). one function, two lanes, no drama π
π¦ escaping the overrides
not feeling the core overrides today? valid. to get at this functionality without them, pass the "use" an empty import list, and then call the function functions with their full qualified names. main character boundary-setting energy π
on the other hand, the built-ins are still available via the "CORE::" pseudo-package. they never left you, they're just behind a door now πͺβ¨
π₯ EXAMPLES π₯
use Net::hostent;
use Socket;
@ARGV = ('netscape.com') unless @ARGV;
for $host ( @ARGV ) {
unless ($h = gethost($host)) {
warn "$0: no such host: $host\n";
next;
}
printf "\n%s is %s%s\n",
$host,
lc($h->name) eq lc($host) ? "" : "*really* ",
$h->name;
print "\taliases are ", join(", ", @{$h->aliases}), "\n"
if @{$h->aliases};
if ( @{$h->addr_list} > 1 ) {
my $i;
for $addr ( @{$h->addr_list} ) {
printf "\taddr #%d is [%s]\n", $i++, inet_ntoa($addr);
}
} else {
printf "\taddress is [%s]\n", inet_ntoa($h->addr);
}
if ($h = gethostbyaddr($h->addr)) {
if (lc($h->name) ne lc($host)) {
printf "\tThat addr reverses to host %s!\n", $h->name;
$host = $h->name;
redo;
}
}
}
π NOTE π
okay so, technical honesty moment: while this class is currently implemented using the Class::Struct module to build a struct-like class, you shouldn't rely upon this. it's an implementation detail, not a promise. do not build your whole personality on it, bestie π
π AUTHOR π
Tom Christiansen β yes, that Tom Christiansen. legend behaviour, we stan π«Ά
and that's the whole page, fr fr! this was perl v5.44.0, dated 2026-10-17, living its best life in section 3perl. go forth, resolve some hostnames, and remember: addr is just addr_list[0] in a little outfit πβ¨ bye bestie!! ππ¦πΈ
π¦ where this page comes from
- arch rolling: perl 5.44.0-1 Β· Artistic-1.0-Perl AND GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms