8c0fc13890ae4add99d8dff6e52f6512180b80d6
[p5sagit/p5-mst-13.2.git] / lib / Net / servent.pm
1 package Net::servent;
2 use strict;
3
4 BEGIN {
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 }
12 use vars      @EXPORT_OK;
13
14 use Class::Template qw(struct);
15 struct 'Net::servent' => [
16    name         => '$',
17    aliases      => '@',
18    port         => '$',
19    proto        => '$',
20 ];
21
22 sub 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
32 sub getservent    (   ) { populate(CORE::getservent()) }
33 sub getservbyname ($;$) { populate(CORE::getservbyname(shift,shift||'tcp')) }
34 sub getservbyport ($;$) { populate(CORE::getservbyport(shift,shift||'tcp')) }
35
36 sub getserv ($;$) {
37     no strict 'refs';
38     return &{'getservby' . ($_[0]=~/^\d+$/ ? 'port' : 'name')}(@_);
39 }
40
41 1;
42
43 __END__
44
45 =head1 NAME
46
47 Net::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
62 This module's default exports override the core getservent(),
63 getservbyname(), and
64 getnetbyport() 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
66 named structure field name from the C's servent structure from F<netdb.h>;
67 namely name, aliases, port, and proto.  The aliases
68 method returns an array reference, the rest scalars.
69
70 You may also import all the structure fields directly into your namespace
71 as regular variables using the :FIELDS import tag.  (Note that this still
72 overrides your core functions.)  Access these fields as variables named
73 with 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
75 regular array variables, so for example C<@{ $serv_obj-E<gt>aliases()
76 }> would be simply @s_aliases.
77
78 The getserv() function is a simple front-end that forwards a numeric
79 argument to getservbyport(), and the rest to getservbyname().
80
81 To access this functionality without the core overrides,
82 pass the C<use> an empty import list, and then access
83 function functions with their full qualified names.
84 On the other hand, the built-ins are still available
85 via 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
104 While this class is currently implemented using the Class::Template
105 module to build a struct-like class, you shouldn't rely upon this.
106
107 =head1 AUTHOR
108
109 Tom Christiansen