Parse::JCONF - Parse JCONF (JSON optimized for configs)
use strict; use Parse::JCONF; my $raw_cfg = do { local $/; <DATA> }; my $parser = Parse::JCONF->new(autodie => 1); $cfg = $parser->parse($raw_cfg); $cfg->{modules}{Mo}[1]; # 0.08 $cfg->{enabled}; # Parse::JCONF::Boolean::TRUE or "1" in string context $cfg->{enabled} == Parse::JCONF::Boolean::TRUE; # yes $cfg->{enabled} == 1; # no if ($cfg->{enabled}) { 1 }; # yes $cfg->{data}[0]; # Test data $cfg->{query}; # SELECT * from pkg # LEFT JOIN ver ON pkg.id=ver.pkg_id # WHERE pkg.name IN ("Moose", "Mouse", "Moo", "Mo") __DATA__ modules = { Moose: 1, Mouse: 0.91, Moo: 0.05, # some comment here about version Mo: [0.01, 0.08], } enabled = true data = ["Test data", "Production data"] # some comment about data query = "SELECT * from pkg LEFT JOIN ver ON pkg.id=ver.pkg_id WHERE pkg.name IN (\"Moose\", \"Mouse\", \"Moo\", \"Mo\")"
JSON is good, but not very handy for configuration files. JCONF intended to fix this.
It has several differences with JSON format:
some_word # valid some word # invalid "some_word" # invalid
{test: 1} # valid {"test": 1} # valid
value1 = [1,2] # root trine: root key (bareword), equals sign (=), any valid JCONF value
val = [1,2,3,4] # with comma val = [ # with new line 1 2 3 4 ] val = { # several newlines are ok a: 1 b: 2 } val = { # comma and newlines are ok a: 1, b: 2 } val = { # invalid, several commas is not ok a: 1,,b:2 }
[1,2,3,4,] # ok {a:1,b:2,} # ok
str = "This is valid multiline JCONF string"
obj = { bool: false # this is comment }
This is parser object constructor. Available parameters are:
Throw exception on any error if true, default is false (in this case parser methods will return undef on error and error may be found with "last_error" method)
Store key/value pairs in the hash which keeps order if true, default is false. This is useful when you need to store your configuration back to the file (for example with JCONF::Writer) and want to save same order as it was before. You must have $Parse::JCONF::HashClass installed which default value is Tie::IxHash.
JCONF::Writer
Parses string provided as parameter. Expected string encoding is utf8. On success returns reference to hash. On fail returns undef/throws exception (according to autodie option in the constructor). Exception will be of type Parse::JCONF::Error::Parser.
autodie
Parse::JCONF::Error::Parser
Parses content of the file which path provided as parameter. Expected file content encoding is utf8. On success returns reference to hash. On fail returns undef/throws exception (according to autodie option in the constructor). Exception will be of type Parse::JCONF::Error::IO or Parse::JCONF::Error::Parser.
Parse::JCONF::Error::IO
Returns error occured for last parse() or parse_file() call. Error will be one of Parse::JCONF::Error subclass or undef (if there was no error).
Parse::JCONF::Error
Parse::JCONF::Error, Parse::JCONF::Boolean, JCONF::Writer
Copyright Oleg G <oleg@cpan.org>.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Parse::JCONF, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Parse::JCONF
CPAN shell
perl -MCPAN -e shell install Parse::JCONF
For more information on module installation, please visit the detailed CPAN module installation guide.