Tintin has asked for the wisdom of the Perl Monks concerning the following question:

I'm developing a Perl CGI admin script that allows a user to edit a configuration file. The config file is along the lines of:

$base='/home/foo';<BR> $logdir="$base/logs";<BR> $admindir="$base/logs";<BR>

etc. Obviously there are a lot more entries in the real config file.

At present, I'm in two minds as to how to handle editing of the file via a form.

1.) I can just do a 'require config'; and then use the values of the various variables set in the file, eg: $admindir. The only problem with this approach is if someone changes $base, then they will need to manually change all the other variablesas their values will be expanded.

2.) Change the format of the config file and read it in by splitting the variable name and value, eg:

while (<CONFIG>) { next if (/^#/); ($var,$value) = split(/=/); $value =~ s/[\$,",',;]//g; $configvar{$var}=$value; }

Obviously approach one is much simplier, but flawed (for my purposes) and method two seems very messy.

I suspect I need to look at options 3, 4 and beyond. Opinions welcomed.

Replies are listed 'Best First'.
Re: Editing configuration file with a CGI Perl script
by jepri (Parson) on Jun 04, 2001 at 17:19 UTC
    The other options are to use one of CPANs marvellous Tie modules to load all the options from a configuration file into a hash. Much nicer for dealing with, and much lower maintenance. You have your choice of formats, from Windows to Apache style config files, and they are loaded in to a hash, so you could just iterate over the hash and create a textbox on the HTML page for each variable. This is very easy using HTML::Template.

    In fact, you could probably do the whole routine in about ten lines.

    ____________________
    Jeremy
    I didn't believe in evil until I dated it.

Re: Editing configuration file with a CGI Perl script
by wardk (Deacon) on Jun 04, 2001 at 18:37 UTC