in reply to Config file

This is a rather old thread but I stumbled across it looking for ideas for a config file format so I thought I would update it for future journeymen. For my config files I have settled on JSON, which I guess was not around when this thread was hewn from stone tablets. To make it easier for humans I have used relaxed (allow comments and terminal commas) and pretty options for when I output them.

use strict; use warnings; use JSON; use Data::Dumper; # Get a JSON object to handle our config file and data stores my $json = JSON->new->relaxed->pretty; # Read configuration my $cfg; # open my $config, '<', $conf_file or die "Can not read $conf_file: $! +\n"; local $/; $cfg = $json->decode (<DATA>); close $config; print Dumper $cfg; __DATA__ # Here is our config # There are many like it but this one is ours { ##################################################### # # # Data Base connections strings and active option # # # ##################################################### "DB_Connections" : { "foo" : { "conx" : [ "dbi:Oracle:FOO.world", "foo_user", "foo_pswd" ], "active" : 1 }, "bar" : { "conx" : [ "dbi:DB2:bar", "bar_user", "bar_pass", ], "active" : 0 } }, "hosts" : ( "host1" : ["readme", "andme"], "host2" : ["readthis", "that", "and another"], # comma allowed b +y relaxed ) }

Cheers,
R.

Pereant, qui ante nos nostra dixerunt!

Replies are listed 'Best First'.
Re^2: Config file
by scott8035 (Novice) on Oct 30, 2014 at 22:25 UTC

    Seriously, everyone? More than 90% of config files are simple and can be processed in two lines of code (not counting formatting). Like this...open a file, and parse the lines with a map:

    open( my $config_fh, '< :encoding(UTF-8)', '/some/path/.tofile' ) or die "$0: Can't open config file. $!\n"; my %creds = map { $_ =~ /(\S+)\s*?:\s*?(\S+)/ } <$config_fh>;

    This will hande those simple key/value config lines:

    key: value

    But at the end of the day, I would also say "standardize on a common CPAN module..."

      I like that this was the issue that fired you up enough to create your first node, just now, from an account created in 2008. (credit to LanX for noticing)
Re^2: Config file
by scott8035 (Novice) on Oct 30, 2014 at 22:49 UTC

    ...and in my mind, this is the one to use for simple configs:

        Config::Simple->import_from('myconf.cfg', \%Config);