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