6 use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
8 @EXPORT = qw(gethostbyname gethostbyaddr gethost);
14 %EXPORT_TAGS = ( FIELDS => [ @EXPORT_OK, @EXPORT ] );
18 use Class::Template qw(struct);
19 struct 'Net::hostent' => [
27 sub addr { shift->addr_list->[0] }
32 $h_name = $hob->[0] = $_[0];
33 @h_aliases = @{ $hob->[1] } = split ' ', $_[1];
34 $h_addrtype = $hob->[2] = $_[2];
35 $h_length = $hob->[3] = $_[3];
37 @h_addr_list = @{ $hob->[4] } = @_[ (4 .. $#_) ];
41 sub gethostbyname ($) { populate(CORE::gethostbyname(shift)) }
43 sub gethostbyaddr ($;$) {
44 my ($addr, $addrtype);
46 require Socket unless @_;
47 $addrtype = @_ ? shift : Socket::AF_INET();
48 populate(CORE::gethostbyaddr($addr, $addrtype))
52 if ($_[0] =~ /^\d+(?:\.\d+(?:\.\d+(?:\.\d+)?)?)?$/) {
54 &gethostbyaddr(Socket::inet_aton(shift));
65 Net::hostent - by-name interface to Perl's built-in gethost*() functions
73 This module's default exports override the core gethostbyname() and
74 gethostbyaddr() functions, replacing them with versions that return
75 "Net::hostent" objects. This object has methods that return the similarly
76 named structure field name from the C's hostent structure from F<netdb.h>;
77 namely name, aliases, addrtype, length, and addresses. The aliases and
78 addresses methods return array reference, the rest scalars. The addr
79 method is equivalent to the zeroth element in the addresses array
82 You may also import all the structure fields directly into your namespace
83 as regular variables using the :FIELDS import tag. (Note that this still
84 overrides your core functions.) Access these fields as variables named
85 with a preceding C<h_>. Thus, C<$host_obj-E<gt>name()> corresponds to
86 $h_name if you import the fields. Array references are available as
87 regular array variables, so for example C<@{ $host_obj-E<gt>aliases()
88 }> would be simply @h_aliases.
90 The gethost() funtion is a simple front-end that forwards a numeric
91 argument to gethostbyaddr() by way of Socket::inet_aton, and the rest
94 To access this functionality without the core overrides,
95 pass the C<use> an empty import list, and then access
96 function functions with their full qualified names.
97 On the other hand, the built-ins are still available
98 via the C<CORE::> pseudo-package.
105 @ARGV = ('netscape.com') unless @ARGV;
107 for $host ( @ARGV ) {
109 unless ($h = gethost($host)) {
110 warn "$0: no such host: $host\n";
114 printf "\n%s is %s%s\n",
116 lc($h->name) eq lc($host) ? "" : "*really* ",
119 print "\taliases are ", join(", ", @{$h->aliases}), "\n"
122 if ( @{$h->addr_list} > 1 ) {
124 for $addr ( @{$h->addr_list} ) {
125 printf "\taddr #%d is [%s]\n", $i++, inet_ntoa($addr);
128 printf "\taddress is [%s]\n", inet_ntoa($h->addr);
131 if ($h = gethostbyaddr($h->addr)) {
132 if (lc($h->name) ne lc($host)) {
133 printf "\tThat addr reverses to host %s!\n", $h->name;
142 While this class is currently implemented using the Class::Template
143 module to build a struct-like class, you shouldn't rely upon this.