1Net::hostent(3pm) Perl Programmers Reference Guide Net::hostent(3pm)
2
3
4
6 Net::hostent - by-name interface to Perl's built-in gethost*()
7 functions
8
10 use Net::hostent;
11
13 This module's default exports override the core gethostbyname() and
14 gethostbyaddr() functions, replacing them with versions that return
15 "Net::hostent" objects. This object has methods that return the
16 similarly named structure field name from the C's hostent structure
17 from netdb.h; namely name, aliases, addrtype, length, and addr_list.
18 The aliases and addr_list methods return array reference, the rest
19 scalars. The addr method is equivalent to the zeroth element in the
20 addr_list array reference.
21
22 You may also import all the structure fields directly into your
23 namespace as regular variables using the :FIELDS import tag. (Note
24 that this still overrides your core functions.) Access these fields as
25 variables named with a preceding "h_". Thus, "$host_obj->name()"
26 corresponds to $h_name if you import the fields. Array references are
27 available as regular array variables, so for example "@{
28 $host_obj->aliases() }" would be simply @h_aliases.
29
30 The gethost() function is a simple front-end that forwards a numeric
31 argument to gethostbyaddr() by way of Socket::inet_aton, and the rest
32 to gethostbyname().
33
34 To access this functionality without the core overrides, pass the "use"
35 an empty import list, and then access function functions with their
36 full qualified names. On the other hand, the built-ins are still
37 available via the "CORE::" pseudo-package.
38
40 use Net::hostent;
41 use Socket;
42
43 @ARGV = ('netscape.com') unless @ARGV;
44
45 for $host ( @ARGV ) {
46
47 unless ($h = gethost($host)) {
48 warn "$0: no such host: $host\n";
49 next;
50 }
51
52 printf "\n%s is %s%s\n",
53 $host,
54 lc($h->name) eq lc($host) ? "" : "*really* ",
55 $h->name;
56
57 print "\taliases are ", join(", ", @{$h->aliases}), "\n"
58 if @{$h->aliases};
59
60 if ( @{$h->addr_list} > 1 ) {
61 my $i;
62 for $addr ( @{$h->addr_list} ) {
63 printf "\taddr #%d is [%s]\n", $i++, inet_ntoa($addr);
64 }
65 } else {
66 printf "\taddress is [%s]\n", inet_ntoa($h->addr);
67 }
68
69 if ($h = gethostbyaddr($h->addr)) {
70 if (lc($h->name) ne lc($host)) {
71 printf "\tThat addr reverses to host %s!\n", $h->name;
72 $host = $h->name;
73 redo;
74 }
75 }
76 }
77
79 While this class is currently implemented using the Class::Struct
80 module to build a struct-like class, you shouldn't rely upon this.
81
83 Tom Christiansen
84
85
86
87perl v5.28.2 2018-11-01 Net::hostent(3pm)