xref: /onnv-gate/usr/src/cmd/perl/5.8.4/distrib/lib/Net/servent.pm (revision 0:68f95e015346)
1*0Sstevel@tonic-gatepackage Net::servent;
2*0Sstevel@tonic-gateuse strict;
3*0Sstevel@tonic-gate
4*0Sstevel@tonic-gateuse 5.006_001;
5*0Sstevel@tonic-gateour $VERSION = '1.01';
6*0Sstevel@tonic-gateour(@EXPORT, @EXPORT_OK, %EXPORT_TAGS);
7*0Sstevel@tonic-gateBEGIN {
8*0Sstevel@tonic-gate    use Exporter   ();
9*0Sstevel@tonic-gate    @EXPORT      = qw(getservbyname getservbyport getservent getserv);
10*0Sstevel@tonic-gate    @EXPORT_OK   = qw( $s_name @s_aliases $s_port $s_proto );
11*0Sstevel@tonic-gate    %EXPORT_TAGS = ( FIELDS => [ @EXPORT_OK, @EXPORT ] );
12*0Sstevel@tonic-gate}
13*0Sstevel@tonic-gateuse vars      @EXPORT_OK;
14*0Sstevel@tonic-gate
15*0Sstevel@tonic-gate# Class::Struct forbids use of @ISA
16*0Sstevel@tonic-gatesub import { goto &Exporter::import }
17*0Sstevel@tonic-gate
18*0Sstevel@tonic-gateuse Class::Struct qw(struct);
19*0Sstevel@tonic-gatestruct 'Net::servent' => [
20*0Sstevel@tonic-gate   name		=> '$',
21*0Sstevel@tonic-gate   aliases	=> '@',
22*0Sstevel@tonic-gate   port		=> '$',
23*0Sstevel@tonic-gate   proto	=> '$',
24*0Sstevel@tonic-gate];
25*0Sstevel@tonic-gate
26*0Sstevel@tonic-gatesub populate (@) {
27*0Sstevel@tonic-gate    return unless @_;
28*0Sstevel@tonic-gate    my $sob = new();
29*0Sstevel@tonic-gate    $s_name 	 =    $sob->[0]     	     = $_[0];
30*0Sstevel@tonic-gate    @s_aliases	 = @{ $sob->[1] } = split ' ', $_[1];
31*0Sstevel@tonic-gate    $s_port	 =    $sob->[2] 	     = $_[2];
32*0Sstevel@tonic-gate    $s_proto	 =    $sob->[3] 	     = $_[3];
33*0Sstevel@tonic-gate    return $sob;
34*0Sstevel@tonic-gate}
35*0Sstevel@tonic-gate
36*0Sstevel@tonic-gatesub getservent    (   ) { populate(CORE::getservent()) }
37*0Sstevel@tonic-gatesub getservbyname ($;$) { populate(CORE::getservbyname(shift,shift||'tcp')) }
38*0Sstevel@tonic-gatesub getservbyport ($;$) { populate(CORE::getservbyport(shift,shift||'tcp')) }
39*0Sstevel@tonic-gate
40*0Sstevel@tonic-gatesub getserv ($;$) {
41*0Sstevel@tonic-gate    no strict 'refs';
42*0Sstevel@tonic-gate    return &{'getservby' . ($_[0]=~/^\d+$/ ? 'port' : 'name')}(@_);
43*0Sstevel@tonic-gate}
44*0Sstevel@tonic-gate
45*0Sstevel@tonic-gate1;
46*0Sstevel@tonic-gate
47*0Sstevel@tonic-gate__END__
48*0Sstevel@tonic-gate
49*0Sstevel@tonic-gate=head1 NAME
50*0Sstevel@tonic-gate
51*0Sstevel@tonic-gateNet::servent - by-name interface to Perl's built-in getserv*() functions
52*0Sstevel@tonic-gate
53*0Sstevel@tonic-gate=head1 SYNOPSIS
54*0Sstevel@tonic-gate
55*0Sstevel@tonic-gate use Net::servent;
56*0Sstevel@tonic-gate $s = getservbyname(shift || 'ftp') || die "no service";
57*0Sstevel@tonic-gate printf "port for %s is %s, aliases are %s\n",
58*0Sstevel@tonic-gate    $s->name, $s->port, "@{$s->aliases}";
59*0Sstevel@tonic-gate
60*0Sstevel@tonic-gate use Net::servent qw(:FIELDS);
61*0Sstevel@tonic-gate getservbyname(shift || 'ftp') || die "no service";
62*0Sstevel@tonic-gate print "port for $s_name is $s_port, aliases are @s_aliases\n";
63*0Sstevel@tonic-gate
64*0Sstevel@tonic-gate=head1 DESCRIPTION
65*0Sstevel@tonic-gate
66*0Sstevel@tonic-gateThis module's default exports override the core getservent(),
67*0Sstevel@tonic-gategetservbyname(), and
68*0Sstevel@tonic-gategetnetbyport() functions, replacing them with versions that return
69*0Sstevel@tonic-gate"Net::servent" objects.  They take default second arguments of "tcp".  This object has methods that return the similarly
70*0Sstevel@tonic-gatenamed structure field name from the C's servent structure from F<netdb.h>;
71*0Sstevel@tonic-gatenamely name, aliases, port, and proto.  The aliases
72*0Sstevel@tonic-gatemethod returns an array reference, the rest scalars.
73*0Sstevel@tonic-gate
74*0Sstevel@tonic-gateYou may also import all the structure fields directly into your namespace
75*0Sstevel@tonic-gateas regular variables using the :FIELDS import tag.  (Note that this still
76*0Sstevel@tonic-gateoverrides your core functions.)  Access these fields as variables named
77*0Sstevel@tonic-gatewith a preceding C<s_>.  Thus, C<$serv_obj-E<gt>name()> corresponds to
78*0Sstevel@tonic-gate$s_name if you import the fields.  Array references are available as
79*0Sstevel@tonic-gateregular array variables, so for example C<@{ $serv_obj-E<gt>aliases()}>
80*0Sstevel@tonic-gatewould be simply @s_aliases.
81*0Sstevel@tonic-gate
82*0Sstevel@tonic-gateThe getserv() function is a simple front-end that forwards a numeric
83*0Sstevel@tonic-gateargument to getservbyport(), and the rest to getservbyname().
84*0Sstevel@tonic-gate
85*0Sstevel@tonic-gateTo access this functionality without the core overrides,
86*0Sstevel@tonic-gatepass the C<use> an empty import list, and then access
87*0Sstevel@tonic-gatefunction functions with their full qualified names.
88*0Sstevel@tonic-gateOn the other hand, the built-ins are still available
89*0Sstevel@tonic-gatevia the C<CORE::> pseudo-package.
90*0Sstevel@tonic-gate
91*0Sstevel@tonic-gate=head1 EXAMPLES
92*0Sstevel@tonic-gate
93*0Sstevel@tonic-gate use Net::servent qw(:FIELDS);
94*0Sstevel@tonic-gate
95*0Sstevel@tonic-gate while (@ARGV) {
96*0Sstevel@tonic-gate     my ($service, $proto) = ((split m!/!, shift), 'tcp');
97*0Sstevel@tonic-gate     my $valet = getserv($service, $proto);
98*0Sstevel@tonic-gate     unless ($valet) {
99*0Sstevel@tonic-gate         warn "$0: No service: $service/$proto\n"
100*0Sstevel@tonic-gate         next;
101*0Sstevel@tonic-gate     }
102*0Sstevel@tonic-gate     printf "service $service/$proto is port %d\n", $valet->port;
103*0Sstevel@tonic-gate     print "alias are @s_aliases\n" if @s_aliases;
104*0Sstevel@tonic-gate }
105*0Sstevel@tonic-gate
106*0Sstevel@tonic-gate=head1 NOTE
107*0Sstevel@tonic-gate
108*0Sstevel@tonic-gateWhile this class is currently implemented using the Class::Struct
109*0Sstevel@tonic-gatemodule to build a struct-like class, you shouldn't rely upon this.
110*0Sstevel@tonic-gate
111*0Sstevel@tonic-gate=head1 AUTHOR
112*0Sstevel@tonic-gate
113*0Sstevel@tonic-gateTom Christiansen
114