NAME
Config::Neat::Render - Render configs in Config::Neat format
SYNOPSIS
use Config::Neat::Render;
my $r = Config::Neat::Render->new();
my $data = {
'foo' => 'Hello, World!',
'bar' => [1, 2, 3],
'baz' => {
'etc' => ['foo bar', 'baz', '', 1]
}
};
print $r->render($data);
The output will be:
bar 1 2 3
baz
{
etc `foo bar` baz `` 1
}
foo Hello, World!
DESCRIPTION
This module allows you to render Config::Neat-compatible structures from your data (but read below for limitations). See https://github.com/iafan/Config-Neat/blob/master/sample/readme.nconf for the detailed file syntax specification. For parsing, use Config::Neat.
METHODS
- Config::Neat::Render->new([$options])
-
Constructs a new renderer object. $options is a reference to a hash containing rendering options' overrides (see the RENDERING OPTIONS section below).
- Config::Neat::Render->render($data[, $options])
-
Renders $data into a string and returns it. $options is a reference to a hash containing rendering options' overrides (see the RENDERING OPTIONS section below).
RENDERING OPTIONS
- indentation
-
A number of spaces to indent each nested block contents with.
Default value:
4
- key_spacing
-
A number of spaces between a key and and a value.
Default value:
4
- wrap_width
-
A suggested maximum width of each line in a multiline string or array.
Default value:
60
- brace_under
-
If true, put the opening brace under the key name, not on the same line
Default value:
1
(true) - separate_blocks
-
If true, surrond blocks with empty lines for better readability.
Default value:
1
(true) - align_all
-
If true, align all values in the configuration file (otherwise the values are aligned only within current block).
Default value:
1
(true) - sort
-
Note that hashes in Perl do not guarantee the correct order, so blocks may have individual parameters shuffled randomly. Set this option to a true value if you want to sort keys alphabetically, or to a reference to an array holding an ordered list of key names
Default value:
undef
(false)Example:
my $data = { 'bar' => [1, 2, 3], 'baz' => { 'etc' => ['foo bar', 'baz', '', 1] } 'foo' => 'Hello, World!', }; my @order = qw(foo bar baz); print $r->render($data, {sort => \@order});
The output will be:
foo Hello, World! bar 1 2 3 baz { etc `foo bar` baz `` 1 }
- undefined_value
-
A string representation of the value to emit for undefined values
Default value:
'NO'
LIMITATIONS
Do not use Config::Neat::Render in conjunction with Config::Neat for arbitrary data serialization/desrialization. JSON and YAML will work better for this kind of task.
Why? Because Config::Neat was primarily designed to allow easier configuration file authoring and reading, and uses relaxed syntax where strings are treated like space-separated arrays (and vice versa), and where there's no strict definition for boolean types, no null values, etc.
It's the developer's responsibility to treat any given parameter as a boolean, or string, or an array. This means that once you serialize your string into Config::Neat format and parse it back, it will be converted to an array, and you will need to use `->as_string` method to get the value as string.
In other words, when doing this:
my $c = Config::Neat->new();
my $r = Config::Neat::Render->new();
my $parsed_data = $c->parse($r->render($arbitrary_data));
$parsed_data will almost always be different from $arbitrary_data.
However, doing this immediately after:
my $parsed_data_2 = $c->parse($r->render($parsed_data));
Should produce the same data structure again.
COPYRIGHT
Copyright (C) 2012-2015 Igor Afanasyev <igor.afanasyev@gmail.com>