This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
dumb bug in User::pwent.pm
[perl5.git] / lib / User / grent.pm
CommitLineData
36477c24 1package User::grent;
2use strict;
3
4BEGIN {
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}
12use vars @EXPORT_OK;
13
14use Class::Template qw(struct);
15struct 'User::grent' => [
16 name => '$',
17 passwd => '$',
18 gid => '$',
19 members => '@',
20];
21
22sub 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
30sub getgrent ( ) { populate(CORE::getgrent()) }
31sub getgrnam ($) { populate(CORE::getgrnam(shift)) }
32sub getgrgid ($) { populate(CORE::getgrgid(shift)) }
33sub getgr ($) { ($_[0] =~ /^\d+/) ? &getgrgid : &getgrnam }
34
351;
36__END__
37
38=head1 NAME
39
40User::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
60This module's default exports override the core getgrent(), getgruid(),
61and getgrnam() functions, replacing them with versions that return
62"User::grent" objects. This object has methods that return the similarly
63named structure field name from the C's passwd structure from F<grp.h>;
64namely name, passwd, gid, and members (not mem). The first three
65return scalars, the last an array reference.
66
67You may also import all the structure fields directly into your namespace
68as regular variables using the :FIELDS import tag. (Note that this still
69overrides your core functions.) Access these fields as variables named
70with a preceding C<gr_>. Thus, C<$group_obj-E<gt>gid()> corresponds
71to $gr_gid if you import the fields. Array references are available as
72regular array variables, so C<@{ $group_obj-E<gt>members() }> would be
73simply @gr_members.
74
75The getpw() funtion is a simple front-end that forwards
76a numeric argument to getpwuid() and the rest to getpwnam().
77
78To access this functionality without the core overrides,
79pass the C<use> an empty import list, and then access
80function functions with their full qualified names.
81On the other hand, the built-ins are still available
82via the C<CORE::> pseudo-package.
83
84=head1 NOTE
85
86While this class is currently implemented using the Class::Template
87module to build a struct-like class, you shouldn't rely upon this.
88
89=head1 AUTHOR
90
91Tom Christiansen