Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k & phranque

Message Too Old, No Replies

cPanel script using Tie::File



1:56 pm on Aug 20, 2007 (gmt 0)

10+ Year Member

I know the C variants, PHP, and some other languages, but I've never had occasion to use Perl before. I have kludged together a small Perl script from various snippets around the web, and it seems to work correctly, but I would feel a lot better if someone could glance at it to make sure it's actually doing what I intend :)

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!


use 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;


5:39 pm on Aug 20, 2007 (gmt 0)

5+ Year Member

If that is all the code does and will ever do, then the script is fine. If it does more or will be expanded upon, then you should use the "stict" and "warnings" pragmas:

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.


5:59 pm on Aug 20, 2007 (gmt 0)

10+ Year Member

I don't expect the script will ever grow, it's just a one trick pony. I'm actually hoping the functionality will eventually be rolled into cPanel itself. If this had been a more complicated task, I would have written it in a language I actually know. <g> I think I'll add those pragmas just in case, though.

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!


8:20 pm on Aug 26, 2007 (gmt 0)

10+ Year Member

File::Slurp does a nice job. It allows reading and writing to an array in one function call. Lots of useful options as well, such as safe overwriting of an output file.


7:47 pm on Aug 27, 2007 (gmt 0)

5+ Year Member

Fle::Slurp is a good option. The only problem would be to install it since it is not a core module.

Featured Threads

Hot Threads This Week

Hot Threads This Month