/
grent.pm6
111 lines (83 loc) · 3.08 KB
/
grent.pm6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
use v6.c;
our $gr_name is export(:FIELDS);
our $gr_passwd is export(:FIELDS);
our $gr_gid is export(:FIELDS);
our @gr_members is export(:FIELDS);
class User::grent:ver<0.0.1>:auth<cpan:ELIZABETH> {
has Str $.name;
has Str $.passwd;
has Int $.gid;
has @.members;
}
sub populate(@fields) {
if @fields {
User::grent.new(
name => ($gr_name = @fields[0]),
passwd => ($gr_passwd = @fields[1]),
gid => ($gr_gid = @fields[2]),
members => (@gr_members = @fields[3].split(" ")),
)
}
else {
$gr_name = Str;
$gr_passwd = Str;
$gr_gid = Int;
@gr_members = ();
Nil
}
}
my sub getgrnam(Str() $name) is export(:DEFAULT:FIELDS) {
use P5getgrnam; populate(getgrnam($name))
}
my sub getgrgid(Int() $gid) is export(:DEFAULT:FIELDS) {
use P5getgrnam; populate(getgrgid($gid))
}
my sub getgrent() is export(:DEFAULT:FIELDS) {
use P5getgrnam; populate(getgrent)
}
my proto sub getgr(|) is export(:DEFAULT:FIELDS) {*}
my multi sub getgr(Int:D $gid) is export(:DEFAULT:FIELDS) { getgrgid($gid) }
my multi sub getgr(Str:D $nam) is export(:DEFAULT:FIELDS) { getgrnam($nam) }
my constant &setgrent is export(:DEFAULT:FIELDS) = do {
use P5getgrnam; &setgrent
}
my constant &endgrent is export(:DEFAULT:FIELDS) = do {
use P5getgrnam; &endgrent
}
=begin pod
=head1 NAME
User::grent - Port of Perl 5's User::grent
=head1 SYNOPSIS
use User::grent;
$gr = getgrgid(0) or die "No group zero";
if $gr.name eq 'wheel' && $gr.members > 1 {
print "gid zero name wheel, with other members";
}
use User::grent qw(:FIELDS);
getgrgid(0) or die "No group zero";
if $gr_name eq 'wheel' && @gr_members > 1 {
print "gid zero name wheel, with other members";
}
$gr = getgr($whoever);
=head1 DESCRIPTION
This module's default exports C<getgrent>, C<getgrgid>, and C<getgrnam>
functions, replacing them with versions that return C<User::grent> objects.
This object has methods that return the similarly named structure field name
from the C's passwd structure from grp.h; namely name, passwd, gid, and members
(not mem). The first three return scalars, the last an array.
You may also import all the structure fields directly into your namespace as
regular variables using the :FIELDS import tag. (Note that this still exports
the functions.) Access these fields as variables named with a preceding gr_.
Thus, C<$group_obj.gid> corresponds to C<$gr_gid> if you import the fields.
The C<getgr> function is a simple front-end that forwards a numeric argumenti
to C<getgrgid> and the rest to C<getgrnam>.
=head1 AUTHOR
Elizabeth Mattijsen <liz@wenzperl.nl>
Source can be located at: https://github.com/lizmat/User-grent . Comments and
Pull Requests are welcome.
=head1 COPYRIGHT AND LICENSE
Copyright 2018 Elizabeth Mattijsen
Re-imagined from Perl 5 as part of the CPAN Butterfly Plan.
This library is free software; you can redistribute it and/or modify it under the Artistic License 2.0.
=end pod
# vim: ft=perl6 expandtab sw=4