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