This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
double-double needs the first mantissa bits of doubles.
[perl5.git] / lib / Net / servent.pm
CommitLineData
36477c24 1package Net::servent;
2use strict;
3
3b825e41 4use 5.006_001;
2af1ab88 5our $VERSION = '1.01';
17f410f9 6our(@EXPORT, @EXPORT_OK, %EXPORT_TAGS);
36477c24 7BEGIN {
8 use Exporter ();
36477c24 9 @EXPORT = qw(getservbyname getservbyport getservent getserv);
10 @EXPORT_OK = qw( $s_name @s_aliases $s_port $s_proto );
11 %EXPORT_TAGS = ( FIELDS => [ @EXPORT_OK, @EXPORT ] );
12}
13use vars @EXPORT_OK;
14
8cc95fdb 15# Class::Struct forbids use of @ISA
16sub import { goto &Exporter::import }
17
18use Class::Struct qw(struct);
36477c24 19struct 'Net::servent' => [
20 name => '$',
21 aliases => '@',
22 port => '$',
23 proto => '$',
24];
25
26sub populate (@) {
27 return unless @_;
28 my $sob = new();
29 $s_name = $sob->[0] = $_[0];
30 @s_aliases = @{ $sob->[1] } = split ' ', $_[1];
31 $s_port = $sob->[2] = $_[2];
32 $s_proto = $sob->[3] = $_[3];
33 return $sob;
34}
35
36sub getservent ( ) { populate(CORE::getservent()) }
37sub getservbyname ($;$) { populate(CORE::getservbyname(shift,shift||'tcp')) }
38sub getservbyport ($;$) { populate(CORE::getservbyport(shift,shift||'tcp')) }
39
40sub getserv ($;$) {
41 no strict 'refs';
42 return &{'getservby' . ($_[0]=~/^\d+$/ ? 'port' : 'name')}(@_);
43}
44
451;
46
47__END__
48
49=head1 NAME
50
51Net::servent - by-name interface to Perl's built-in getserv*() functions
52
53=head1 SYNOPSIS
54
55 use Net::servent;
56 $s = getservbyname(shift || 'ftp') || die "no service";
57 printf "port for %s is %s, aliases are %s\n",
58 $s->name, $s->port, "@{$s->aliases}";
59
60 use Net::servent qw(:FIELDS);
61 getservbyname(shift || 'ftp') || die "no service";
62 print "port for $s_name is $s_port, aliases are @s_aliases\n";
63
64=head1 DESCRIPTION
65
66This module's default exports override the core getservent(),
67getservbyname(), and
68getnetbyport() functions, replacing them with versions that return
69"Net::servent" objects. They take default second arguments of "tcp". This object has methods that return the similarly
70named structure field name from the C's servent structure from F<netdb.h>;
71namely name, aliases, port, and proto. The aliases
72method returns an array reference, the rest scalars.
73
74You may also import all the structure fields directly into your namespace
75as regular variables using the :FIELDS import tag. (Note that this still
76overrides your core functions.) Access these fields as variables named
09e36754 77with a preceding C<s_>. Thus, C<$serv_obj-E<gt>name()> corresponds to
36477c24 78$s_name if you import the fields. Array references are available as
09e36754
AL
79regular array variables, so for example C<@{ $serv_obj-E<gt>aliases()}>
80would be simply @s_aliases.
36477c24 81
82The getserv() function is a simple front-end that forwards a numeric
83argument to getservbyport(), and the rest to getservbyname().
84
85To access this functionality without the core overrides,
86pass the C<use> an empty import list, and then access
87function functions with their full qualified names.
88On the other hand, the built-ins are still available
89via the C<CORE::> pseudo-package.
90
91=head1 EXAMPLES
92
93 use Net::servent qw(:FIELDS);
94
95 while (@ARGV) {
96 my ($service, $proto) = ((split m!/!, shift), 'tcp');
97 my $valet = getserv($service, $proto);
98 unless ($valet) {
99 warn "$0: No service: $service/$proto\n"
100 next;
101 }
102 printf "service $service/$proto is port %d\n", $valet->port;
103 print "alias are @s_aliases\n" if @s_aliases;
104 }
105
106=head1 NOTE
107
8cc95fdb 108While this class is currently implemented using the Class::Struct
36477c24 109module to build a struct-like class, you shouldn't rely upon this.
110
111=head1 AUTHOR
112
113Tom Christiansen