home *** CD-ROM | disk | FTP | other *** search
/ isnet Internet / Isnet Internet CD.iso / prog / hiz / 09 / 09.exe / adynware.exe / perl / lib / User / grent.pm next >
Encoding:
Perl POD Document  |  1999-12-28  |  2.9 KB  |  93 lines

  1. package User::grent;
  2. use strict;
  3.  
  4. BEGIN { 
  5.     use Exporter   ();
  6.     use vars       qw(@EXPORT @EXPORT_OK %EXPORT_TAGS);
  7.     @EXPORT      = qw(getgrent getgrgid getgrnam getgr);
  8.     @EXPORT_OK   = qw($gr_name $gr_gid $gr_passwd $gr_mem @gr_members);
  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 'User::grent' => [
  17.     name    => '$',
  18.     passwd  => '$',
  19.     gid        => '$',
  20.     members => '@',
  21. ];
  22.  
  23. sub populate (@) {
  24.     return unless @_;
  25.     my $gob = new();
  26.     ($gr_name, $gr_passwd, $gr_gid) = @$gob[0,1,2] = @_[0,1,2];
  27.     @gr_members = @{$gob->[3]} = split ' ', $_[3];
  28.     return $gob;
  29.  
  30. sub getgrent ( ) { populate(CORE::getgrent()) } 
  31. sub getgrnam ($) { populate(CORE::getgrnam(shift)) } 
  32. sub getgrgid ($) { populate(CORE::getgrgid(shift)) } 
  33. sub getgr    ($) { ($_[0] =~ /^\d+/) ? &getgrgid : &getgrnam } 
  34.  
  35. 1;
  36. __END__
  37.  
  38. =head1 NAME
  39.  
  40. User::grent - by-name interface to Perl's built-in getgr*() functions
  41.  
  42. =head1 SYNOPSIS
  43.  
  44.  use User::grent;
  45.  $gr = getgrgid(0) or die "No group zero";
  46.  if ( $gr->name eq 'wheel' && @{$gr->members} > 1 ) {
  47.      print "gid zero name wheel, with other members";
  48.  } 
  49.  
  50.  use User::grent qw(:FIELDS;
  51.  getgrgid(0) or die "No group zero";
  52.  if ( $gr_name eq 'wheel' && @gr_members > 1 ) {
  53.      print "gid zero name wheel, with other members";
  54.  } 
  55.  
  56.  $gr = getgr($whoever);
  57.  
  58. =head1 DESCRIPTION
  59.  
  60. This module's default exports override the core getgrent(), getgruid(),
  61. and getgrnam() functions, replacing them with versions that return
  62. "User::grent" objects.  This object has methods that return the similarly
  63. named structure field name from the C's passwd structure from F<grp.h>; 
  64. namely name, passwd, gid, and members (not mem).  The first three
  65. return scalars, the last an array reference.
  66.  
  67. You may also import all the structure fields directly into your namespace
  68. as regular variables using the :FIELDS import tag.  (Note that this still
  69. overrides your core functions.)  Access these fields as variables named
  70. with a preceding C<gr_>.  Thus, C<$group_obj-E<gt>gid()> corresponds
  71. to $gr_gid if you import the fields.  Array references are available as
  72. regular array variables, so C<@{ $group_obj-E<gt>members() }> would be
  73. simply @gr_members.
  74.  
  75. The getpw() funtion is a simple front-end that forwards
  76. a numeric argument to getpwuid() and the rest to getpwnam().
  77.  
  78. To access this functionality without the core overrides,
  79. pass the C<use> an empty import list, and then access
  80. function functions with their full qualified names.
  81. On the other hand, the built-ins are still available
  82. via the C<CORE::> pseudo-package.
  83.  
  84. =head1 NOTE
  85.  
  86. While this class is currently implemented using the Class::Struct
  87. module to build a struct-like class, you shouldn't rely upon this.
  88.  
  89. =head1 AUTHOR
  90.  
  91. Tom Christiansen
  92.