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