homepage Welcome to WebmasterWorld Guest from 54.237.213.31
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
Forum Library, Charter, Moderators: coopster & jatar k & phranque

Perl Server Side CGI Scripting Forum

    
cPanel script using Tie::File
Bluesplinter

10+ Year Member



 
Msg#: 3426827 posted 1:56 pm on Aug 20, 2007 (gmt 0)

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!

#!/usr/bin/perl

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;

 

perl_diver

5+ Year Member



 
Msg#: 3426827 posted 5:39 pm on Aug 20, 2007 (gmt 0)

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.

Bluesplinter

10+ Year Member



 
Msg#: 3426827 posted 5:59 pm on Aug 20, 2007 (gmt 0)

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!

wruppert

10+ Year Member



 
Msg#: 3426827 posted 8:20 pm on Aug 26, 2007 (gmt 0)

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.

perl_diver

5+ Year Member



 
Msg#: 3426827 posted 7:47 pm on Aug 27, 2007 (gmt 0)

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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved