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

My goal is to create a configuration file (or, at the extreme, a database) to hold project configuration data for a project that consists of various perl and shell (tcsh) scripts.

For perl, I would usually just create a module consisting of a hash - simple.

For tcsh, I would generally create a file with a bunch of setenvs that I would source - also simple.

So how to I make configuration file that works for both? I suppose I could just write a parser for both languages, but I was wondering if anyone has come up with a more elegant solution.

Replies are listed 'Best First'.
Re: Language independent configuration
by tilly (Archbishop) on Feb 21, 2008 at 18:04 UTC
    And a variation on kyle's suggestion is that from within Perl you could execute a trivial tcsh script that loads up the tcsh configuration and then prints out the environment. Parse that output from within Perl then load up a Perl configuration.

    RE (tilly) 3: Get default login environment demonstrates the basic technique. (That was for picking up a login environment.)

    This is convenient if your configuration has complex conditionals that you don't want to parse directly in Perl.

Re: Language independent configuration
by pc88mxer (Vicar) on Feb 21, 2008 at 17:00 UTC
    Having a single authority for your configuration data make things very convenient and side-steps a lot of problems.

    If possible, I would consider having the perl program parse the tcsh configuration file. This will be very easy if, for instance, your config data won't contain any spaces or characters that need to be escaped. If this is not the case, perl's parsing capabilities should be able to manage tcsh's escaping and quoting mechanisms.

Re: Language independent configuration
by dwm042 (Priest) on Feb 21, 2008 at 17:31 UTC
    I can't speak for tcsh, but I have written my share of Korn shell. It's not that hard to write a Korn shell program that could parse INI style configuration files, and in that case, the Perl is already written for me, in modules like Config::Simple and Config::Tiny.
Re: Language independent configuration
by kyle (Abbot) on Feb 21, 2008 at 17:35 UTC

    If you make the config that you'd make for tcsh, you could execute it before running any Perl and then access the environment variables via %ENV in Perl (see perlvar).

Re: Language independent configuration
by chromatic (Archbishop) on Feb 21, 2008 at 19:05 UTC

    If you store the information in the database, write a small program to generate the Perl and tcsh configuration programs.