1N/A# Class::Struct forbids use of @ISA 1N/Asub getservbyname ($;$) {
populate(
CORE::
getservbyname(
shift,
shift||
'tcp')) }
1N/Asub getservbyport ($;$) {
populate(
CORE::
getservbyport(
shift,
shift||
'tcp')) }
1N/A return &{
'getservby' . ($_[
0]=~/^\d+$/ ?
'port' :
'name')}(@_);
1N/ANet::servent - by-name interface to Perl's built-in getserv*() functions 1N/A $s = getservbyname(shift || 'ftp') || die "no service"; 1N/A printf "port for %s is %s, aliases are %s\n", 1N/A $s->name, $s->port, "@{$s->aliases}"; 1N/A use Net::servent qw(:FIELDS); 1N/A getservbyname(shift || 'ftp') || die "no service"; 1N/A print "port for $s_name is $s_port, aliases are @s_aliases\n"; 1N/AThis module's default exports override the core getservent(), 1N/Agetnetbyport() functions, replacing them with versions that return 1N/A"Net::servent" objects. They take default second arguments of "tcp". This object has methods that return the similarly 1N/Anamed structure field name from the C's servent structure from F<netdb.h>; 1N/Anamely name, aliases, port, and proto. The aliases 1N/Amethod returns an array reference, the rest scalars. 1N/AYou may also import all the structure fields directly into your namespace 1N/Aas regular variables using the :FIELDS import tag. (Note that this still 1N/Aoverrides your core functions.) Access these fields as variables named 1N/Awith a preceding C<s_>. Thus, C<$serv_obj-E<gt>name()> corresponds to 1N/A$s_name if you import the fields. Array references are available as 1N/Aregular array variables, so for example C<@{ $serv_obj-E<gt>aliases()}> 1N/Awould be simply @s_aliases. 1N/AThe getserv() function is a simple front-end that forwards a numeric 1N/Aargument to getservbyport(), and the rest to getservbyname(). 1N/ATo access this functionality without the core overrides, 1N/Apass the C<use> an empty import list, and then access 1N/Afunction functions with their full qualified names. 1N/AOn the other hand, the built-ins are still available 1N/Avia the C<CORE::> pseudo-package. 1N/A use Net::servent qw(:FIELDS); 1N/A my ($service, $proto) = ((split m!/!, shift), 'tcp'); 1N/A my $valet = getserv($service, $proto); 1N/A warn "$0: No service: $service/$proto\n" 1N/A printf "service $service/$proto is port %d\n", $valet->port; 1N/A print "alias are @s_aliases\n" if @s_aliases; 1N/AWhile this class is currently implemented using the Class::Struct 1N/Amodule to build a struct-like class, you shouldn't rely upon this.