[inseparable changes from patch from perl5.003_10 to perl5.003_11]
[p5sagit/p5-mst-13.2.git] / lib / User / grent.pm
1 package User::grent;
2 use strict;
3
4 BEGIN { 
5     use Exporter   ();
6     use vars       qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
7     @ISA         = qw(Exporter);
8     @EXPORT      = qw(getgrent getgrgid getgrnam getgr);
9     @EXPORT_OK   = qw($gr_name $gr_gid $gr_passwd $gr_mem @gr_members);
10     %EXPORT_TAGS = ( FIELDS => [ @EXPORT_OK, @EXPORT ] );
11 }
12 use vars      @EXPORT_OK;
13
14 use Class::Template qw(struct);
15 struct 'User::grent' => [
16     name    => '$',
17     passwd  => '$',
18     gid     => '$',
19     members => '@',
20 ];
21
22 sub populate (@) {
23     return unless @_;
24     my $gob = new();
25     ($gr_name, $gr_passwd, $gr_gid) = @$gob[0,1,2] = @_[0,1,2];
26     @gr_members = @{$gob->[3]} = split ' ', $_[3];
27     return $gob;
28
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.pm - 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::Template
87 module to build a struct-like class, you shouldn't rely upon this.
88
89 =head1 AUTHOR
90
91 Tom Christiansen