NAME
Config::Model::DumpAsData - Dump configuration content as a perl data structure
VERSION
version 2.169
SYNOPSIS
use Config::Model ;
use Data::Dumper ;
# define configuration tree object
my $model = Config::Model->new ;
$model ->create_config_class (
name => "MyClass",
element => [
{
name => 'foo',
type => 'leaf',
value_type => 'string',
},
{
name => 'bar',
alias => 'foo',
},
{
name => 'baz',
type => 'hash',
index_type => 'string',
cargo => {
type => 'leaf',
value_type => 'string',
},
},
],
) ;
my $inst = $model->instance(root_class_name => 'MyClass' );
my $root = $inst->config_root ;
# put some data in config tree the hard way
$root->fetch_element('foo')->store('yada') ;
$root->fetch_element('bar')->store('bla bla') ;
$root->fetch_element('baz')->fetch_with_id('en')->store('hello') ;
# put more data the easy way
my $steps = 'baz:fr=bonjour baz:hr="dobar dan"';
$root->load( steps => $steps ) ;
print Dumper($root->dump_as_data);
# $VAR1 = {
# 'bar' => 'bla bla',
# 'baz' => {
# 'en' => 'hello',
# 'fr' => 'bonjour',
# 'hr' => 'dobar dan'
# },
# 'foo' => 'yada'
# };
DESCRIPTION
This module is used directly by Config::Model::Node to dump the content of a configuration tree in perl data structure.
The perl data structure is a hash of hash. Only CheckList content is stored in an array ref.
User can pass a sub reference to apply to values of boolean type. This sub can be used to convert the value to an object representing a boolean like boolean. (since 2.129)
Note that undefined values are skipped for list element. I.e. if a list element contains ('a',undef,'b'), the data structure then contains 'a','b'.
Dumping ordered hash
Dumping an ordered hash is a bit of a mess. Perl does not guarantee to preserve hash key order. So some tricks must be used to dump an ordered hash.
First, the ordered hash can be dumped as a list. In the examples below, the key order is foo, bar. So the hash:
{ foo => v1, bar => v2 }
is dumped as:
[ foo => v1, bar => v2 ]
Another trick is to convert the ordered hash in a list of hash when ordered_hash_as_list_of_hashes parameter is true. For instance, the ordered hash
{ foo => v1, bar => v2 }
can be converted in:
[
{ name => 'foo', value => 'v1'},
{ name => 'bar', value => 'v2'},
]
Another issue, arises for ordered hash of node. Lets start from this example written with a Config::Model::Loader string:
ordered_hash_of_node:z aa2=foo_x -
ordered_hash_of_node:y aa2=foo_y ab2=bar_y -
A Perl dump with the same technique as above would yield:
[
{
name => 'z', # hash key
value => { # another hash to contain the node attribute values
aa2 => 'foo_x'
}
},
{
name => 'y',
value => {
aa2 => 'foo_y',
ab2 => 'bar_y'
}
}
]
which may be confusing due to additional hash level.
So the Perl dump is done merging the content of the node in the first hash level:
[
{
name => 'z, # hash key
aa2 => 'foo_x, # node attribute
},
{
name => 'y, # hash key
aa2 => 'foo_y, # node attribute
ab2 => 'bar_y, # another node attribute
}
]
The parameter ordered_hash_key_name parameter can be used to avoid a collision between a node attribute and the hash key.
For instance, calling
$node->dump_as_data(
ordered_hash_as_list_of_hashes => 1,
ordered_hash_key_name => "my_unique_key"
);
would yield:
[
{
my_unique_key => 'z,
aa2 => 'foo_x,
},
{
my_unique_key => 'y,
aa2 => 'foo_y,
ab2 => 'bar_y,
}
]
CONSTRUCTOR
new
No parameter. The constructor should be used only by Config::Model::Node.
Methods
dump_as_data
Return a perl data structure
Parameters are:
- node
-
Reference to a Config::Model::Node object. Mandatory
- full_dump
-
Also dump default values in the data structure. Useful if the dumped configuration data is used by the application. This parameter is deprecated in favor of mode parameter.
- mode
-
Note that
modeparameter is also accepted and overridesfull_dumpparameter. See "fetch" in Config::Model::Value for details onmode. - ordered_hash_as_list_of_hashes
-
Boolean. Dump ordered hash as list of hashes. See "Dumping ordered hash" for details. Default is
false - ordered_hash_key_name
-
Key name to use when inserting original hash key in dumped hash value. Default is
name.For instance, if the original hash contains:
{ foo => v1, bar => v2 }The original hash is dumped in this list of hashes:
[ { name => 'foo', value => 'v1'}, { name => 'bar', value => 'v2'}, ] - skip_auto_write
-
Skip node that have a
perl writecapability in their model. See Config::Model::BackendMgr.This option must be used when using DumpAsData: to write back configuration data. When a configuration model contains several backends (one at the tree root and others in tree nodes), setting this option ensure that the "root" configuration file does not contain data duplicated in configuration file of others tree nodes.
- auto_vivify
-
Scan and create data for nodes elements even if no actual data was stored in them. This may be useful to trap missing mandatory values.
- ordered_hash_as_list
-
By default, ordered hash (i.e. the order of the keys are important) are dumped as Perl list. This is the faster way to dump such hashed while keeping the key order. But it's the less readable way.
When this parameter is 1 (default), the ordered hash is dumped as a list:
my_hash => [ A => 'foo', B => 'bar', C => 'baz' ]When this parameter is set as 0, the ordered hash is dumped with a special key that specifies the order of keys. E.g.:
my_hash => { __my_hash_order => [ 'A', 'B', 'C' ] , B => 'bar', A => 'foo', C => 'baz' } - to_boolean
-
Sub reference to map a value of type boolean to a boolean class (since 2.129). For instance:
to_boolean => sub { boolean($_[0]); }Default is
sub { return $_[0] }
AUTHOR
Dominique Dumont, (ddumont at cpan dot org)
SEE ALSO
Config::Model,Config::Model::Node,Config::Model::ObjTreeScanner
AUTHOR
Dominique Dumont
COPYRIGHT AND LICENSE
This software is Copyright (c) 2005-2022 by Dominique Dumont.
This is free software, licensed under:
The GNU Lesser General Public License, Version 2.1, February 1999