6 use vars qw(@EXPORT @EXPORT_OK %EXPORT_TAGS);
7 @EXPORT = qw(gethostbyname gethostbyaddr gethost);
13 %EXPORT_TAGS = ( FIELDS => [ @EXPORT_OK, @EXPORT ] );
17 # Class::Struct forbids use of @ISA
18 sub import { goto &Exporter::import }
20 use Class::Struct qw(struct);
21 struct 'Net::hostent' => [
29 sub addr { shift->addr_list->[0] }
34 $h_name = $hob->[0] = $_[0];
35 @h_aliases = @{ $hob->[1] } = split ' ', $_[1];
36 $h_addrtype = $hob->[2] = $_[2];
37 $h_length = $hob->[3] = $_[3];
39 @h_addr_list = @{ $hob->[4] } = @_[ (4 .. $#_) ];
43 sub gethostbyname ($) { populate(CORE::gethostbyname(shift)) }
45 sub gethostbyaddr ($;$) {
46 my ($addr, $addrtype);
48 require Socket unless @_;
49 $addrtype = @_ ? shift : Socket::AF_INET();
50 populate(CORE::gethostbyaddr($addr, $addrtype))
54 if ($_[0] =~ /^\d+(?:\.\d+(?:\.\d+(?:\.\d+)?)?)?$/) {
56 &gethostbyaddr(Socket::inet_aton(shift));
67 Net::hostent - by-name interface to Perl's built-in gethost*() functions
75 This module's default exports override the core gethostbyname() and
76 gethostbyaddr() functions, replacing them with versions that return
77 "Net::hostent" objects. This object has methods that return the similarly
78 named structure field name from the C's hostent structure from F<netdb.h>;
79 namely name, aliases, addrtype, length, and addr_list. The aliases and
80 addr_list methods return array reference, the rest scalars. The addr
81 method is equivalent to the zeroth element in the addr_list array
84 You may also import all the structure fields directly into your namespace
85 as regular variables using the :FIELDS import tag. (Note that this still
86 overrides your core functions.) Access these fields as variables named
87 with a preceding C<h_>. Thus, C<$host_obj-E<gt>name()> corresponds to
88 $h_name if you import the fields. Array references are available as
89 regular array variables, so for example C<@{ $host_obj-E<gt>aliases()
90 }> would be simply @h_aliases.
92 The gethost() funtion is a simple front-end that forwards a numeric
93 argument to gethostbyaddr() by way of Socket::inet_aton, and the rest
96 To access this functionality without the core overrides,
97 pass the C<use> an empty import list, and then access
98 function functions with their full qualified names.
99 On the other hand, the built-ins are still available
100 via the C<CORE::> pseudo-package.
107 @ARGV = ('netscape.com') unless @ARGV;
109 for $host ( @ARGV ) {
111 unless ($h = gethost($host)) {
112 warn "$0: no such host: $host\n";
116 printf "\n%s is %s%s\n",
118 lc($h->name) eq lc($host) ? "" : "*really* ",
121 print "\taliases are ", join(", ", @{$h->aliases}), "\n"
124 if ( @{$h->addr_list} > 1 ) {
126 for $addr ( @{$h->addr_list} ) {
127 printf "\taddr #%d is [%s]\n", $i++, inet_ntoa($addr);
130 printf "\taddress is [%s]\n", inet_ntoa($h->addr);
133 if ($h = gethostbyaddr($h->addr)) {
134 if (lc($h->name) ne lc($host)) {
135 printf "\tThat addr reverses to host %s!\n", $h->name;
144 While this class is currently implemented using the Class::Struct
145 module to build a struct-like class, you shouldn't rely upon this.