File Coverage

blib/lib/Unix/AliasFile.pm
Criterion Covered Total %
statement 95 108 87.9
branch 32 46 69.5
condition 4 6 66.6
subroutine 14 15 93.3
pod 9 11 81.8
total 154 186 82.8


line stmt bran cond sub pod time code
1             package Unix::AliasFile;
2              
3             # $Id: AliasFile.pm,v 1.5 2000/05/02 15:50:11 ssnodgra Exp $
4              
5 1     1   748 use strict;
  1         2  
  1         38  
6 1     1   5 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
  1         1  
  1         80  
7 1     1   619 use Unix::ConfigFile;
  1         3  
  1         52  
8 1     1   984 use Text::ParseWords;
  1         1478  
  1         1618  
9              
10             require Exporter;
11              
12             @ISA = qw(Unix::ConfigFile Exporter);
13             # Items to export into callers namespace by default. Note: do not export
14             # names by default without a very good reason. Use EXPORT_OK instead.
15             # Do not simply export all your public functions/methods/constants.
16             @EXPORT = qw(
17            
18             );
19             $VERSION = '0.06';
20              
21             # Implementation Notes
22             #
23             # This module adds one field to the basic ConfigFile object. The field
24             # is called 'alias' and is a hash of hashes. The key is the alias name and
25             # the subhash contains members of the alias as keys. The values of those
26             # keys are normally just '1', but may be object references in the case of
27             # :include: aliases. The module also makes use of the file sequencing
28             # facility provided by ConfigFile to preserve comments and keep the file in
29             # its original order.
30              
31             # Preloaded methods go here.
32              
33             # Read in the data structures from the supplied file
34             sub read {
35 1     1 0 17 my ($this, $fh) = @_;
36              
37 1         5 my $alias = ""; # Current alias being processed
38 1         40 while (<$fh>) {
39 66 100 100     468 if (/^#/ || /^$/) { # Comments/Blank Lines
    100 33        
    50          
    50          
40 44         159 $this->seq_append($_);
41             }
42             elsif (/^[^\s]/) { # Alias start
43 20         241 s/,?\s*$//;
44 20         308 ($alias, my $rhs) = split /:\s*/, $_, 2;
45              
46             # I use the parse_line routine from Text::ParseWords here because
47             # a simple split would hose program aliases with embedded commas.
48             # Note that this routine does not exist prior to 5.005, so older
49             # perl versions will have the comma bug.
50 20         37 my @members;
51 20 50       46 if ($] >= 5.005) {
52 20         64 @members = parse_line(',\s*', 1, $rhs);
53             }
54             else {
55 0         0 @members = split /,\s*/, $rhs;
56             }
57              
58             # This weird little hack fixes a bug that caused empty aliases
59             # to be deleted when the file was read, since the alias method
60             # won't actually create an empty alias.
61 20         1478 $this->alias($alias, "empty");
62 20         49 $this->remove_user($alias, "empty");
63 20         39 $this->alias($alias, @members);
64             }
65             elsif (/^\s+$/) { # Junk whitespace
66 0         0 $this->seq_append("\n");
67             }
68             elsif (/^\s+/ && $alias) { # Alias continuation
69 2         26 s/,?\s*$//;
70 2         11 s/^\s+//;
71 2 50       8 if ($] >= 5.005) {
72 2         6 $this->add_user($alias, parse_line(',\s*', 1, $_));
73             }
74             else {
75 0         0 $this->add_user($alias, split /,\s*/);
76             }
77             }
78             else { # What's this?
79 0         0 die "Bogus line: $_";
80             }
81             }
82 1         7 return 1;
83             }
84              
85              
86             # Add, modify or get an alias
87             sub alias {
88 70     70 1 432 my $this = shift;
89 70         93 my $name = shift;
90              
91             # If no more parameters, we return alias members
92 70 100       152 unless (@_) {
93 28 100       87 return undef unless defined $this->{alias}{$name};
94 26 100       63 return keys %{$this->{alias}{$name}} unless wantarray;
  6         28  
95 20         22 return sort keys %{$this->{alias}{$name}};
  20         128  
96             }
97              
98             # Create or modify an alias
99 42 100       179 $this->seq_append("_ALIAS_ $name") unless defined $this->{alias}{$name};
100 42         120 $this->{alias}{$name} = {};
101 42         123 $this->add_user($name, @_);
102 42 50       98 return keys %{$this->{alias}{$name}} unless wantarray;
  42         214  
103 0         0 return sort keys %{$this->{alias}{$name}};
  0         0  
104             }
105              
106              
107             # Delete an alias
108             sub delete {
109 1     1 1 39 my ($this, $name) = @_;
110              
111 1 50       7 return 0 unless defined $this->{alias}{$name};
112 1         19 $this->seq_remove("_ALIAS_ $name");
113 1         4 delete $this->{alias}{$name};
114 1         2 return 1;
115             }
116              
117              
118             # Delete aliases with no members
119             sub delempty {
120 0     0 1 0 my $this = shift;
121              
122 0         0 my $count = 0;
123 0         0 foreach my $name ($this->aliases) {
124 0 0       0 unless ($this->alias($name)) {
125 0         0 $this->delete($name);
126 0         0 $count++;
127             }
128             }
129 0         0 return $count;
130             }
131              
132              
133             # Add users to an existing alias
134             sub add_user {
135 45     45 1 256 my $this = shift;
136 45         60 my $name = shift;
137 45 50       123 my @aliases = ($name eq "*") ? $this->aliases : ($name);
138              
139 45         73 foreach (@aliases) {
140 45 50       104 return 0 unless defined $this->{alias}{$_};
141 45         79 foreach my $user (@_) {
142 68         259 $this->{alias}{$_}{$user} = 1;
143             }
144             }
145 45         101 return 1;
146             }
147              
148              
149             # Remove users from an existing alias
150             sub remove_user {
151 22     22 1 100 my $this = shift;
152 22         37 my $name = shift;
153 22 100       74 my @aliases = ($name eq "*") ? $this->aliases : ($name);
154              
155 22         39 foreach (@aliases) {
156 42 50       99 return 0 unless defined $this->{alias}{$_};
157 42         58 foreach my $user (@_) {
158 45         139 delete $this->{alias}{$_}{$user};
159             }
160             }
161 22         47 return 1;
162             }
163              
164              
165             # Rename a user
166             sub rename_user {
167 1     1 1 45 my ($this, $oldname, $newname) = @_;
168              
169 1         3 my $count = 0;
170 1         6 foreach ($this->aliases) {
171 20 100       52 if (exists $this->{alias}{$_}{$oldname}) {
172 1         4 delete $this->{alias}{$_}{$oldname};
173 1         4 $this->{alias}{$_}{$newname} = 1;
174 1         2 $count++;
175             }
176             }
177 1         7 return $count;
178             }
179              
180              
181             # Return the list of aliases
182             sub aliases {
183 3     3 1 33 my $this = shift;
184 3 100       9 wantarray ? sort keys %{$this->{alias}} : keys %{$this->{alias}};
  2         45  
  1         4  
185             }
186              
187              
188             # Add a comment before an alias
189             sub comment {
190 1     1 1 32 my ($this, $name, @cmnt) = @_;
191 1         5 grep { chomp; s/$/\n/; } @cmnt;
  1         5  
  1         11  
192 1         11 return $this->seq_insert("_ALIAS_ $name", @cmnt);
193             }
194              
195              
196             # Remove a comment
197             sub uncomment {
198 1     1 1 76 my ($this, $cmnt) = @_;
199 1         5 chomp $cmnt;
200 1         12 $cmnt =~ s/$/\n/;
201 1         6 return $this->seq_remove($cmnt);
202             }
203              
204              
205             # Output file to disk
206             sub write {
207 1     1 0 4 my ($this, $fh) = @_;
208              
209 1         18 foreach my $seq ($this->sequence) {
210 64 100       171 unless ($seq =~ /^_ALIAS_ ([^\s]+)$/) {
211 44 50       120 print $fh $seq or return 0;
212 44         57 next;
213             }
214 20         41 my $name = $1;
215 20         48 my @users = $this->alias($name);
216 20 50       49 next if !defined @users;
217 20 50       90 print $fh $this->joinwrap(80, "$name: ", "\t", ",", ",", @users), "\n"
218             or return 0;
219             }
220 1         11 return 1;
221             }
222              
223             # Autoload methods go after =cut, and are processed by the autosplit program.
224              
225             1;
226             __END__