This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[inseparable changes from patch from perl5.003_10 to perl5.003_11]
[perl5.git] / lib / User / pwent.pm
CommitLineData
36477c24
PP
1package User::pwent;
2use strict;
3
4BEGIN {
5 use Exporter ();
6 use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
7 @ISA = qw(Exporter);
8 @EXPORT = qw(getpwent getpwuid getpwnam getpw);
9 @EXPORT_OK = qw(
10 $pw_name $pw_passwd $pw_uid
11 $pw_gid $pw_quota $pw_comment
12 $pw_gecos $pw_dir $pw_shell
13 );
14 %EXPORT_TAGS = ( FIELDS => [ @EXPORT_OK, @EXPORT ] );
15}
16use vars @EXPORT_OK;
17
18use Class::Template qw(struct);
19struct 'User::pwent' => [
20 name => '$',
21 passwd => '$',
22 uid => '$',
23 gid => '$',
24 quota => '$',
25 comment => '$',
26 gcos => '$',
27 dir => '$',
28 shell => '$',
29];
30
31sub populate (@) {
32 return unless @_;
33 my $pwob = new();
34
35 ( $pw_name, $pw_passwd, $pw_uid,
36 $pw_gid, $pw_quota, $pw_comment,
37 $pw_gecos, $pw_dir, $pw_shell, ) = @$pwob = @_;
38
39 return $pwob;
40}
41
42sub getpwent ( ) { populate(CORE::getpwent()) }
43sub getpwnam ($) { populate(CORE::getpwnam(shift)) }
44sub getpwgid ($) { populate(CORE::getpwgid(shift)) }
45sub getpw ($) { ($_[0] =~ /^\d+/) ? &getpwgid : &getpwnam }
46
471;
48__END__
49
50=head1 NAME
51
52User::pwent.pm - by-name interface to Perl's built-in getpw*() functions
53
54=head1 SYNOPSIS
55
56 use User::pwent;
57 $pw = getpwnam('daemon') or die "No daemon user";
58 if ( $pw->uid == 1 && $pw->dir =~ m#^/(bin|tmp)?$# ) {
59 print "gid 1 on root dir";
60 }
61
62 use User::pwent qw(:FIELDS);
63 getpwnam('daemon') or die "No daemon user";
64 if ( $pw_uid == 1 && $pw_dir =~ m#^/(bin|tmp)?$# ) {
65 print "gid 1 on root dir";
66 }
67
68 $pw = getpw($whoever);
69
70=head1 DESCRIPTION
71
72This module's default exports override the core getpwent(), getpwuid(),
73and getpwnam() functions, replacing them with versions that return
74"User::pwent" objects. This object has methods that return the similarly
75named structure field name from the C's passwd structure from F<pwd.h>;
76namely name, passwd, uid, gid, quota, comment, gecos, dir, and shell.
77
78You may also import all the structure fields directly into your namespace
79as regular variables using the :FIELDS import tag. (Note that this still
80overrides your core functions.) Access these fields as
81variables named with a preceding C<pw_> in front their method names.
82Thus, C<$passwd_obj-E<gt>shell()> corresponds to $pw_shell if you import
83the fields.
84
85The getpw() funtion is a simple front-end that forwards
86a numeric argument to getpwuid() and the rest to getpwnam().
87
88To access this functionality without the core overrides,
89pass the C<use> an empty import list, and then access
90function functions with their full qualified names.
91On the other hand, the built-ins are still available
92via the C<CORE::> pseudo-package.
93
94=head1 NOTE
95
96While this class is currently implemented using the Class::Template
97module to build a struct-like class, you shouldn't rely upon this.
98
99=head1 AUTHOR
100
101Tom Christiansen