Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Config file recommendations?

by Spidy (Chaplain)
on Mar 19, 2008 at 02:59 UTC ( #674926=perlquestion: print w/ replies, xml ) Need Help??
Spidy has asked for the wisdom of the Perl Monks concerning the following question:

Greetings, fellow monks.

I am currently working on a project that, like every other project, needs a config file.

I've read about a few options for handling the need for config files - from building a parse for name=value pairs to just having a config.pm that has all my configuration stuff inside it, to using Config::foo, and so on.

Long story short, I'm no longer certain which format to use(or how to accomplish it).

I need the config file to be able to contain Perl code, which means I can't use the first method I mentioned.

However, I'd also like it to insinuate its data into a global %config hash - so that I can refer to it from anywhere within my collection of scripts by simple doing this:

use config; print $config{foo};

Does anyone have any recommendations on how to implement this in Perl, or a module I could use that would help me accomplish this?

Thanks,

Spidy

Comment on Config file recommendations?
Download Code
Re: Config file recommendations?
by samtregar (Abbot) on Mar 19, 2008 at 03:40 UTC
    There's a ton of Config:: modules to choose from on CPAN. You should pick one that suites your tastes, it's a simple enough job that it's hard to pick badly. To me the key questions are 1) who is going to edit your config file and 2) how complicated is that configuration data.

    When my answer to #1 was admins who knew Apache and the answer to #2 was Apache-style config blocks I created Config::ApacheFormat.

    If you're feeling particularly ambivalent you might use Data::PhraseBook which will let you pick one format today and switch tomorrow without changing the code that uses the configs.

    Another option for the undecided is Config::Auto. It may well be too clever for its own good but at least it tries hard!

    Once you've picked a parser writing some glue code to populate a global %CONFIG (don't use %Config since that's what the core Config module uses and you'll probably need it someday) should be easy. If it's not, pick another module!

    -sam

Re: Config file recommendations?
by kyle (Abbot) on Mar 19, 2008 at 03:57 UTC
      That does seem to be what I'm looking for - if I had a config file like this:
      %config = ( foo => bar, bat => baz, );
      How would I use that inside my Perl script, and have it globally available? Would require config; automatically setup the %config hash for the script that required it?

        I think maybe you're looking for Exporter. With it, you can have a module that will place a variable into the namespace of a program that uses it.

Re: Config file recommendations?
by hangon (Deacon) on Mar 19, 2008 at 14:50 UTC

    You might want to look at YAML::Tiny. This module uses a subset of YAML that is easily understood by a reasonably intelligent non-geek and can handle name/values, lists, text blocks and can be commented.

    You can retreive the data as a hash as you require, and although I havn't tried it, you should be able to include blocks of Perl code that you can eval.

Re: Config file recommendations?
by dwhite20899 (Friar) on Mar 19, 2008 at 16:53 UTC
    For something that just plain works, we use Config::Tiny.

    Drop dead simple use, and the config file format is dead simple too.

    The config file format supports a cascading flow, so we put config keys and values that apply to every implementaion in the "master" section, and Win, OSX, Linux etc. specific keys in those separate sections, and use the file everywhere.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://674926]
Approved by xdg
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (11)
As of 2014-11-28 19:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (200 votes), past polls