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