This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Problems with SKIP in makemaker
[perl5.git] / lib / Net / hostent.pm
CommitLineData
36477c24 1package Net::hostent;
2use strict;
3
4BEGIN {
5 use Exporter ();
6 use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
7 @ISA = qw(Exporter);
8 @EXPORT = qw(gethostbyname gethostbyaddr gethost);
9 @EXPORT_OK = qw(
10 $h_name @h_aliases
11 $h_addrtype $h_length
12 @h_addr_list $h_addr
13 );
14 %EXPORT_TAGS = ( FIELDS => [ @EXPORT_OK, @EXPORT ] );
15}
16use vars @EXPORT_OK;
17
18use Class::Template qw(struct);
19struct 'Net::hostent' => [
20 name => '$',
21 aliases => '@',
22 addrtype => '$',
23 'length' => '$',
24 addr_list => '@',
25];
26
27sub addr { shift->addr_list->[0] }
28
29sub populate (@) {
30 return unless @_;
31 my $hob = new();
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];
36 $h_addr = $_[4];
37 @h_addr_list = @{ $hob->[4] } = @_[ (4 .. $#_) ];
38 return $hob;
39}
40
41sub gethostbyname ($) { populate(CORE::gethostbyname(shift)) }
42
43sub gethostbyaddr ($;$) {
44 my ($addr, $addrtype);
45 $addr = shift;
46 require Socket unless @_;
47 $addrtype = @_ ? shift : Socket::AF_INET();
48 populate(CORE::gethostbyaddr($addr, $addrtype))
49}
50
51sub gethost($) {
52 if ($_[0] =~ /^\d+(?:\.\d+(?:\.\d+(?:\.\d+)?)?)?$/) {
53 require Socket;
54 &gethostbyaddr(Socket::inet_aton(shift));
55 } else {
56 &gethostbyname;
57 }
58}
59
601;
61__END__
62
63=head1 NAME
64
65Net::hostent - by-name interface to Perl's built-in gethost*() functions
66
67=head1 SYNOPSIS
68
69 use Net::hostnet;
70
71=head1 DESCRIPTION
72
73This module's default exports override the core gethostbyname() and
74gethostbyaddr() functions, replacing them with versions that return
75"Net::hostent" objects. This object has methods that return the similarly
76named structure field name from the C's hostent structure from F<netdb.h>;
77namely name, aliases, addrtype, length, and addresses. The aliases and
78addresses methods return array reference, the rest scalars. The addr
79method is equivalent to the zeroth element in the addresses array
80reference.
81
82You may also import all the structure fields directly into your namespace
83as regular variables using the :FIELDS import tag. (Note that this still
84overrides your core functions.) Access these fields as variables named
85with 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
87regular array variables, so for example C<@{ $host_obj-E<gt>aliases()
88}> would be simply @h_aliases.
89
90The gethost() funtion is a simple front-end that forwards a numeric
91argument to gethostbyaddr() by way of Socket::inet_aton, and the rest
92to gethostbyname().
93
94To access this functionality without the core overrides,
95pass the C<use> an empty import list, and then access
96function functions with their full qualified names.
97On the other hand, the built-ins are still available
98via the C<CORE::> pseudo-package.
99
100=head1 EXAMPLES
101
102 use Net::hostent;
103 use Socket;
104
105 @ARGV = ('netscape.com') unless @ARGV;
106
107 for $host ( @ARGV ) {
108
109 unless ($h = gethost($host)) {
110 warn "$0: no such host: $host\n";
111 next;
112 }
113
114 printf "\n%s is %s%s\n",
115 $host,
116 lc($h->name) eq lc($host) ? "" : "*really* ",
117 $h->name;
118
119 print "\taliases are ", join(", ", @{$h->aliases}), "\n"
120 if @{$h->aliases};
121
122 if ( @{$h->addr_list} > 1 ) {
123 my $i;
124 for $addr ( @{$h->addr_list} ) {
125 printf "\taddr #%d is [%s]\n", $i++, inet_ntoa($addr);
126 }
127 } else {
128 printf "\taddress is [%s]\n", inet_ntoa($h->addr);
129 }
130
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;
134 $host = $h->name;
135 redo;
136 }
137 }
138 }
139
140=head1 NOTE
141
142While this class is currently implemented using the Class::Template
143module to build a struct-like class, you shouldn't rely upon this.
144
145=head1 AUTHOR
146
147Tom Christiansen