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