Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
3.4 KiB

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