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