home *** CD-ROM | disk | FTP | other *** search
/ Acorn User 10 / AU_CD10.iso / Updates / Perl / Non-RPC / !Perl / lib / zip / User / pwent.pm < prev   
Text File  |  1999-01-08  |  3KB  |  104 lines

  1. package User::pwent;
  2. use strict;
  3.  
  4. BEGIN { 
  5.     use Exporter   ();
  6.     use vars       qw(@EXPORT @EXPORT_OK %EXPORT_TAGS);
  7.     @EXPORT      = qw(getpwent getpwuid getpwnam getpw);
  8.     @EXPORT_OK   = qw(
  9.             $pw_name   $pw_passwd     $pw_uid     
  10.             $pw_gid       $pw_quota    $pw_comment
  11.             $pw_gecos  $pw_dir    $pw_shell
  12.            );
  13.     %EXPORT_TAGS = ( FIELDS => [ @EXPORT_OK, @EXPORT ] );
  14. }
  15. use vars      @EXPORT_OK;
  16.  
  17. # Class::Struct forbids use of @ISA
  18. sub import { goto &Exporter::import }
  19.  
  20. use Class::Struct qw(struct);
  21. struct 'User::pwent' => [
  22.     name    => '$',
  23.     passwd  => '$',
  24.     uid        => '$',
  25.     gid        => '$',
  26.     quota   => '$',
  27.     comment => '$',
  28.     gecos   => '$',
  29.     dir        => '$',
  30.     shell   => '$',
  31. ];
  32.  
  33. sub populate (@) {
  34.     return unless @_;
  35.     my $pwob = new();
  36.  
  37.     ( $pw_name,   $pw_passwd,   $pw_uid,  
  38.       $pw_gid,    $pw_quota,    $pw_comment,
  39.       $pw_gecos,  $pw_dir,      $pw_shell,   )     = @$pwob = @_;
  40.  
  41.     return $pwob;
  42.  
  43. sub getpwent ( ) { populate(CORE::getpwent()) } 
  44. sub getpwnam ($) { populate(CORE::getpwnam(shift)) } 
  45. sub getpwuid ($) { populate(CORE::getpwuid(shift)) } 
  46. sub getpw    ($) { ($_[0] =~ /^\d+/) ? &getpwuid : &getpwnam } 
  47.  
  48. 1;
  49. __END__
  50.  
  51. =head1 NAME
  52.  
  53. User::pwent - by-name interface to Perl's built-in getpw*() functions
  54.  
  55. =head1 SYNOPSIS
  56.  
  57.  use User::pwent;
  58.  $pw = getpwnam('daemon') or die "No daemon user";
  59.  if ( $pw->uid == 1 && $pw->dir =~ m#^/(bin|tmp)?$# ) {
  60.      print "gid 1 on root dir";
  61.  } 
  62.  
  63.  use User::pwent qw(:FIELDS);
  64.  getpwnam('daemon') or die "No daemon user";
  65.  if ( $pw_uid == 1 && $pw_dir =~ m#^/(bin|tmp)?$# ) {
  66.      print "gid 1 on root dir";
  67.  } 
  68.  
  69.  $pw = getpw($whoever);
  70.  
  71. =head1 DESCRIPTION
  72.  
  73. This module's default exports override the core getpwent(), getpwuid(),
  74. and getpwnam() functions, replacing them with versions that return
  75. "User::pwent" objects.  This object has methods that return the similarly
  76. named structure field name from the C's passwd structure from F<pwd.h>; 
  77. namely name, passwd, uid, gid, quota, comment, gecos, dir, and shell.
  78.  
  79. You may also import all the structure fields directly into your namespace
  80. as regular variables using the :FIELDS import tag.  (Note that this still
  81. overrides your core functions.)  Access these fields as
  82. variables named with a preceding C<pw_> in front their method names.
  83. Thus, C<$passwd_obj-E<gt>shell()> corresponds to $pw_shell if you import
  84. the fields.
  85.  
  86. The getpw() function is a simple front-end that forwards
  87. a numeric argument to getpwuid() and the rest to getpwnam().
  88.  
  89. To access this functionality without the core overrides,
  90. pass the C<use> an empty import list, and then access
  91. function functions with their full qualified names.
  92. On the other hand, the built-ins are still available
  93. via the C<CORE::> pseudo-package.
  94.  
  95. =head1 NOTE
  96.  
  97. While this class is currently implemented using the Class::Struct
  98. module to build a struct-like class, you shouldn't rely upon this.
  99.  
  100. =head1 AUTHOR
  101.  
  102. Tom Christiansen
  103.