6 our (@EXPORT, @EXPORT_OK, %EXPORT_TAGS);
9 $h_addrtype, $h_length,
15 @EXPORT = qw(gethostbyname gethostbyaddr gethost);
21 %EXPORT_TAGS = ( FIELDS => [ @EXPORT_OK, @EXPORT ] );
24 # Class::Struct forbids use of @ISA
25 sub import { goto &Exporter::import }
27 use Class::Struct qw(struct);
28 struct 'Net::hostent' => [
36 sub addr { shift->addr_list->[0] }
41 $h_name = $hob->[0] = $_[0];
42 @h_aliases = @{ $hob->[1] } = split ' ', $_[1];
43 $h_addrtype = $hob->[2] = $_[2];
44 $h_length = $hob->[3] = $_[3];
46 @h_addr_list = @{ $hob->[4] } = @_[ (4 .. $#_) ];
50 sub gethostbyname ($) { populate(CORE::gethostbyname(shift)) }
52 sub gethostbyaddr ($;$) {
53 my ($addr, $addrtype);
55 require Socket unless @_;
56 $addrtype = @_ ? shift : Socket::AF_INET();
57 populate(CORE::gethostbyaddr($addr, $addrtype))
61 if ($_[0] =~ /^\d+(?:\.\d+(?:\.\d+(?:\.\d+)?)?)?$/) {
63 &gethostbyaddr(Socket::inet_aton(shift));
74 Net::hostent - by-name interface to Perl's built-in gethost*() functions
82 This module's default exports override the core gethostbyname() and
83 gethostbyaddr() functions, replacing them with versions that return
84 "Net::hostent" objects. This object has methods that return the similarly
85 named structure field name from the C's hostent structure from F<netdb.h>;
86 namely name, aliases, addrtype, length, and addr_list. The aliases and
87 addr_list methods return array reference, the rest scalars. The addr
88 method is equivalent to the zeroth element in the addr_list array
91 You may also import all the structure fields directly into your namespace
92 as regular variables using the :FIELDS import tag. (Note that this still
93 overrides your core functions.) Access these fields as variables named
94 with a preceding C<h_>. Thus, C<$host_obj-E<gt>name()> corresponds to
95 $h_name if you import the fields. Array references are available as
96 regular array variables, so for example C<@{ $host_obj-E<gt>aliases()
97 }> would be simply @h_aliases.
99 The gethost() function is a simple front-end that forwards a numeric
100 argument to gethostbyaddr() by way of Socket::inet_aton, and the rest
103 To access this functionality without the core overrides,
104 pass the C<use> an empty import list, and then access
105 function functions with their full qualified names.
106 On the other hand, the built-ins are still available
107 via the C<CORE::> pseudo-package.
114 @ARGV = ('netscape.com') unless @ARGV;
116 for $host ( @ARGV ) {
118 unless ($h = gethost($host)) {
119 warn "$0: no such host: $host\n";
123 printf "\n%s is %s%s\n",
125 lc($h->name) eq lc($host) ? "" : "*really* ",
128 print "\taliases are ", join(", ", @{$h->aliases}), "\n"
131 if ( @{$h->addr_list} > 1 ) {
133 for $addr ( @{$h->addr_list} ) {
134 printf "\taddr #%d is [%s]\n", $i++, inet_ntoa($addr);
137 printf "\taddress is [%s]\n", inet_ntoa($h->addr);
140 if ($h = gethostbyaddr($h->addr)) {
141 if (lc($h->name) ne lc($host)) {
142 printf "\tThat addr reverses to host %s!\n", $h->name;
151 While this class is currently implemented using the Class::Struct
152 module to build a struct-like class, you shouldn't rely upon this.