A json based config file system.
version 1.5100
use Config::JSON; my $config = Config::JSON->create($pathToFile); my $config = Config::JSON->new($pathToFile); my $config = Config::JSON->new(pathToFile=>$pathToFile); my $element = $config->get($directive); $config->set($directive,$value); $config->delete($directive); $config->deleteFromHash($directive, $key); $config->deleteFromArray($directive, $value); $config->addToHash($directive, $key, $value); $config->addToArray($directive, $value); my $path = $config->pathToFile; my $filename = $config->getFilename;
# config-file-type: JSON 1 { "dsn" : "DBI:mysql:test", "user" : "tester", "password" : "xxxxxx",
# some colors to choose from "colors" : [ "red", "green", "blue" ],
# some statistics "stats" : { "health" : 32, "vitality" : 11 },
# including another file "includes" : ["macros.conf"] }
This package parses the config files written in \s-1JSON\s0. It also does some non-JSON stuff, like allowing for comments in the files.
If you want to see it in action, it is used as the config file system in WebGUI <http://www.webgui.org/>.
Why build yet another config file system? Well there are a number of reasons: We used to use other config file parsers, but we kept running into limitations. We already use \s-1JSON\s0 in our app, so using \s-1JSON\s0 to store config files means using less memory because we already have the \s-1JSON\s0 parser in memory. In addition, with \s-1JSON\s0 we can have any number of hierarchcal data structures represented in the config file, whereas most config files will give you only one level of hierarchy, if any at all. \s-1JSON\s0 parses faster than \s-1XML\s0 and \s-1YAML\s0. \s-1JSON\s0 is easier to read and edit than \s-1XML\s0. Many other config file systems allow you to read a config file, but they don't provide any mechanism or utilities to write back to it. \s-1JSON\s0 is taint safe. \s-1JSON\s0 is easily parsed by languages other than Perl when we need to do that.
You may of course access a directive called \*(L"foo\*(R", but since the config is basically a hash you can traverse multiple elements of the hash when specifying a directive name by simply delimiting each level with a slash, like \*(L"foo/bar\*(R". For example you may:
my $vitality = $config->get("stats/vitality"); $config->set("stats/vitality", 15);
You may do this wherever you specify a directive name.
You can put comments in the config file as long as # is the first non-space character on the line. However, if you use this \s-1API\s0 to write to the config file, your comments will be eliminated.
There is a special directive called \*(L"includes\*(R", which is an array of include files that may be brought in to the config. Even the files you include can have an \*(L"includes\*(R" directive, so you can do hierarchical includes.
Any directive in the main file will take precedence over the directives in the includes. Likewise the files listed first in the \*(L"includes\*(R" directive will have precedence over the files that come after it. When writing to the files, the same precedence is followed.
If you're setting a new directive that doesn't currently exist, it will only be written to the main file.
If a directive is deleted, it will be deleted from all files, including the includes.
Adds a value to an array directive in the config file.
directive
The name of the array.
value
The value to add.
Inserts a value into an array immediately before another item. If that item can't be found, inserts at the beginning on the array.
directive
The name of the array.
insertBefore
The value to search for and base the positioning on.
value
The value to insert.
Inserts a value into an array immediately after another item. If that item can't be found, inserts at the end on the array.
directive
The name of the array.
insertAfter
The value to search for and base the positioning on.
value
The value to insert.
Adds a value to a hash directive in the config file. \s-1NOTE:\s0 This is really the same as $config->set(\*(L"directive/key\*(R", $value);
directive
The name of the hash.
key
The key to add.
value
The value to add.
Constructor. Creates a new empty config file.
pathToFile
The path and filename of the file to create.
Deletes a key from the config file.
directive
The name of the directive to delete.
Deletes a value from an array directive in the config file.
directive
The name of the array.
value
The value to delete.
Delete a key from a hash directive in the config file. \s-1NOTE:\s0 This is really just the same as doing $config->delete(\*(L"directive/key\*(R");
directive
The name of the hash.
key
The key to delete.
Returns the value of a particular directive from the config file.
directive
The name of the directive to return.
Returns the filename for this config.
Returns the filename and path for this config. May also be called as \*(C`getFilePath\*(C' for backward campatibility sake.
Returns an array reference of Config::JSON objects that are files included by this config. May also be called as \*(C`getIncludes\*(C' for backward compatibility sake.
Constructor. Builds an object around a config file.
pathToFile
A string representing a path such as \*(L"/etc/my-cool-config.conf\*(R".
Creates a new or updates an existing directive in the config file.
directive
A directive name.
value
The value to set the paraemter to. Can be a scalar, hash reference, or array reference.
Returns an array of key parts.
key
A key string. Could be 'foo' (simple key), 'foo/bar' (a multilevel key referring to the bar key as a child of foo), or 'foo\/bar' (a simple key that contains a slash in the key). Don't forget to double escape in your perl code if you have a slash in your key parts like this:
$config->get('foo\\/bar');
Writes the file to the filesystem. Normally you'd never need to call this as it's called automatically by the other methods when a change occurs.
This means that the config file does not appear to be formatted properly as a \s-1JSON\s0 file. Common mistakes are missing commas or trailing commas on the end of a list. We couldn't read the config file. This usually means that the path specified in the constructor is incorrect. We couldn't write to the config file. This usually means that the file system is full, or the that the file is write protected.
\s-1JSON\s0 Moose List::Util Test::More Test::Deep
<http://github.com/plainblack/Config-JSON>
<http://rt.cpan.org/Public/Dist/Display.html?Name=Config-JSON>
\s-1JT\s0 Smith <jt-at-plainblack-dot-com>
Config::JSON is Copyright 2009 Plain Black Corporation (<http://www.plainblack.com/>) and is licensed under the same terms as Perl itself.