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