This is for a cPanel server, and basically it takes the argument $domain (which is passed from cPanel), and removes that line from the '/etc/localdomains' file and inserts it into '/etc/remotedomains'. At least, that's what it's supposed to do, and what it *appears* to do, but... Anyway, I've commented verbosely, so any input would be gratefully appreciated!
#!/usr/bin/perluse Tie::File;
# hooks provided by cPanel
my %OPTS = @ARGV;
my $domain = $OPTS{'domain'};
# the two text files, one domain per line
my $localDomains = '/etc/localdomains';
my $remoteDomains = '/etc/remotedomains';
# open the localDomains file with tie
tie @LFILE, 'Tie::File', $localDomains ¦¦ die "Can't open: $!\n";
# filter out lines starting with $domain
@LFILE = grep {!/^$domain/ } @LFILE;
# close the file with untie.
untie @LFILE or die "$!";
# add $domain to remotedomains
tie @RFILE, 'Tie::File', $remoteDomains ¦¦ die "Can't open: $!\n";
push @RFILE, $domain;
untie @RFILE;
use strict;
use warnings;
Tie::File basically allows you to treat a file like a perl array and edit the file using simple array commands, such as: "shift", "unshift", "last", etc etc etc. It is really best used on large files that would use too much memory if read into an array and then edited, but it will work for small files too.
The two text files are pretty small... they'll never have more than a coupla hundred lines in them. The only reason I went with Tie::File is because, frankly, I couldn't find any other concrete examples that would remove a line from one file and add it to another.
Besides, this script will only be executed when I add a new domain to the server, so as long as it works, that'll do :)
Thanks for your help!