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