Leaked source code of windows server 2003
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.

112 lines
3.5 KiB

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