home *** CD-ROM | disk | FTP | other *** search
/ isnet Internet / Isnet Internet CD.iso / prog / hiz / 09 / 09.exe / adynware.exe / perl / lib / Net / servent.pm < prev   
Encoding:
Perl POD Document  |  1999-12-28  |  3.4 KB  |  111 lines

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