1 | package Net::servent;
|
---|
2 | use strict;
|
---|
3 |
|
---|
4 | use 5.006_001;
|
---|
5 | our $VERSION = '1.01';
|
---|
6 | our(@EXPORT, @EXPORT_OK, %EXPORT_TAGS);
|
---|
7 | BEGIN {
|
---|
8 | use Exporter ();
|
---|
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 | }
|
---|
13 | use vars @EXPORT_OK;
|
---|
14 |
|
---|
15 | # Class::Struct forbids use of @ISA
|
---|
16 | sub import { goto &Exporter::import }
|
---|
17 |
|
---|
18 | use Class::Struct qw(struct);
|
---|
19 | struct 'Net::servent' => [
|
---|
20 | name => '$',
|
---|
21 | aliases => '@',
|
---|
22 | port => '$',
|
---|
23 | proto => '$',
|
---|
24 | ];
|
---|
25 |
|
---|
26 | sub 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 |
|
---|
36 | sub getservent ( ) { populate(CORE::getservent()) }
|
---|
37 | sub getservbyname ($;$) { populate(CORE::getservbyname(shift,shift||'tcp')) }
|
---|
38 | sub getservbyport ($;$) { populate(CORE::getservbyport(shift,shift||'tcp')) }
|
---|
39 |
|
---|
40 | sub getserv ($;$) {
|
---|
41 | no strict 'refs';
|
---|
42 | return &{'getservby' . ($_[0]=~/^\d+$/ ? 'port' : 'name')}(@_);
|
---|
43 | }
|
---|
44 |
|
---|
45 | 1;
|
---|
46 |
|
---|
47 | __END__
|
---|
48 |
|
---|
49 | =head1 NAME
|
---|
50 |
|
---|
51 | Net::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 |
|
---|
66 | This module's default exports override the core getservent(),
|
---|
67 | getservbyname(), and
|
---|
68 | getnetbyport() 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
|
---|
70 | named structure field name from the C's servent structure from F<netdb.h>;
|
---|
71 | namely name, aliases, port, and proto. The aliases
|
---|
72 | method returns an array reference, the rest scalars.
|
---|
73 |
|
---|
74 | You may also import all the structure fields directly into your namespace
|
---|
75 | as regular variables using the :FIELDS import tag. (Note that this still
|
---|
76 | overrides your core functions.) Access these fields as variables named
|
---|
77 | with a preceding C<s_>. Thus, C<$serv_obj-E<gt>name()> corresponds to
|
---|
78 | $s_name if you import the fields. Array references are available as
|
---|
79 | regular array variables, so for example C<@{ $serv_obj-E<gt>aliases()}>
|
---|
80 | would be simply @s_aliases.
|
---|
81 |
|
---|
82 | The getserv() function is a simple front-end that forwards a numeric
|
---|
83 | argument to getservbyport(), and the rest to getservbyname().
|
---|
84 |
|
---|
85 | To access this functionality without the core overrides,
|
---|
86 | pass the C<use> an empty import list, and then access
|
---|
87 | function functions with their full qualified names.
|
---|
88 | On the other hand, the built-ins are still available
|
---|
89 | via 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 |
|
---|
108 | While this class is currently implemented using the Class::Struct
|
---|
109 | module to build a struct-like class, you shouldn't rely upon this.
|
---|
110 |
|
---|
111 | =head1 AUTHOR
|
---|
112 |
|
---|
113 | Tom Christiansen
|
---|