16.2.10. Alias Manipulation with Unix::AliasFile
While not a member of the Mail umbrella, Unix::AliasFile is an
important module since it implements a complete interface to the Unix
aliases text file that allows you to query, add, update, and delete
aliases. In addition, it has a cleaner interface than Mail::Alias.
Unix::AliasFile automatically handles file locking, comma and colon
placement, and any other detail that's related to
manipulating your Unix mail alias file. For example:
use Unix::AliasFile;
my $aliasfile = '/etc/mail/aliases';
my $uaf = Unix::AliasFile->new($aliasfile);
foreach my $alias $uaf->aliases( ) {
chomp($alias);
if($uaf->alias($alias) ne "$alias\@yourpopserver.your.domain") {
# Doesn't exist, so add alias of the form:
# alias: alias@yourpopserver.your.domain
$uaf->alias($alias, ("$alias\@yourpopserver.your.domain"));
} else {
print "I already have $alias that way.\n";
}
}
$uaf->commit( );
undef $uaf;
Unix::AliasFile implements the following methods.
new(filename)
Constructor. Creates a new Unix::AliasFile object.
add_user(alias, @users)
Adds @users to
alias. If a user in
@users already exists
in alias, duplicates will be ignored. If
alias is set to *,
@users will be added to
every alias.
alias(alias, @users)
Adds, modifies, or returns information about an alias. When only
alias is suppled, alias(
) returns a list of all members of an alias, or
undef if alias does not
exist. If you supply
@users,
alias will be modified or created if it
doesn't already exist.
aliases( )
Returns an alphabetized list of all existing aliases. In scalar
context, this method returns the total number of aliases.
comment(alias, comment)
Inserts a comment line before alias.
comment must begin with
#, but a newline will be appended to it. Returns
1 on success and 0 on failure.
commit( )
Writes the alias file.
delempty( )
Deletes all existing aliases that have no members, returning a count
of the number of aliases deleted.
delete(alias)
Deletes alias.
remove_user(alias, @users)
Removes the list of users from an existing alias. If a user in
@usersisn't a member of alias,
the removal attempt will be silently ignored. remove_user(
) returns 1 on success and
0 on failure.
rename_user(oldname, newname)
Changes one username to another in every alias. Returns the number of
aliases affected.
uncomment(comment)
Removes the comment from the file that is exactly matched to the
supplied text. Returns 1 on success and
0 on failure.