Config...l::Node(3pm)
πΈ NAME πΈ
Config::Model::Node β the class that builds the nodes of a configuration tree. This is its perl manual page (that (3pm) bit = User Contributed Perl Documentation, bestie, you're in the right place π
).
β¨ VERSION β¨
version 2.166 π«Ά
π« SYNOPSIS π«
Okay so here's the whole vibe, in code:
use Config::Model;
# define configuration tree object
my $model = Config::Model->new;
$model->create_config_class(
name => 'OneConfigClass',
class_description => "OneConfigClass detailed description",
element => [ # use array ref to keep element ordering
X => {
type => 'leaf',
value_type => 'enum',
choice => [qw/Av Bv Cv/]
status => 'deprecated',
description => 'A description (can be long)',
summary => 'A summary',
},
Y => '*X', # same properties as X element
Z => '*X', # same properties as X element
],
accept => [
'ip.*' => {
type => 'leaf',
value_type => 'uniline',
summary => 'ip address',
}
]
);
my $instance = $model->instance (root_class_name => 'OneConfigClass');
my $root = $instance->config_root ;
# X is not shown below because of its deprecated status
print $root->describe,"\n" ;
# name value type comment
# Y [undef] enum choice: Av Bv Cv
# Z [undef] enum choice: Av Bv Cv
# add some data
$root->load( steps => 'Y=Av' );
# add some accepted element, ipA and ipB are created on the fly
$root->load( steps => q!ipA=192.168.1.0 ipB=192.168.1.1"! );
# show also ip* element created in the last "load" call
print $root->describe,"\n" ;
# name value type comment
# Y Av enum choice: Av Bv Cv
# Z [undef] enum choice: Av Bv Cv
# ipA 192.168.1.0 uniline
# ipB 192.168.1.1 uniline
Likeβ¦ read those comments in the code, they're literally telling you the whole story π
π¦ DESCRIPTION π¦
This class hands you the nodes of a configuration tree. When a node object gets created, it receives a set of rules that defines its properties inside the configuration tree. Rules first, chaos never β¨
Every node holds a set of elements. And an element can contain any of these:
- A leaf element, implemented with
Config::Model::Value. A leaf can be plain (aka a totally unconstrained value) or it can be strongly typed (values get checked against a set of rules) π - Another node.
- A collection of items: a list element, implemented with
Config::Model::ListId. Each item can be another node or a leaf. - A collection of identified items: a hash element, implemented with
Config::Model::HashId. Each item can also be another node or a leaf.
π CONFIGURATION CLASS DECLARATION π
A class declaration is made of these parameters, bestie, take notes π
nameβ Mandatory"string"parameter. This config class name can be used by a node element inside another configuration class.class_descriptionβ Optional"string"parameter. This description gets used when generating user interfaces.classβ Optional"string"that lets you specify a Perl class to override the default implementation (Config::Model::Node). That Perl class must inheritConfig::Model::Node. Use with care, fr fr.copyrightβ Optional array of"strings".licenseβ Optional string.elementβ Mandatory"array ref"of elements of the configuration class:
element => [ foo => { type = 'leaf', ... },
bar => { type = 'leaf', ... }
]
See below for details on element declaration π
gistβ A string used to construct a summary of the content of a node. This parameter is used by the user interface to show users the gist of the content of this node. This parameter has no other effect. This string may contain element values in the form"{foo}and@{a_hash_or_list}". When constructing the gist,"{foo}"is replaced by the value of element"foo", and"@{a_hash_of_list}"is replaced by the number of elements of the hash or list element.
Resulting gist is trimmed of extra spaces βοΈ
rw_configβ Parameter used to load configuration data on demand. SeeConfig::Model::BackendMgrfor details.config_dirβ Configuration directory. SeeConfig::Model::BackendMgrfor details.acceptβ Optional list of criteria (i.e. a regular expression to match) to accept unknown elements. Each criteria has a list of specification that enableConfig::Modelto create a model snippet for the unknown element.
Example:
accept => [
'list.*' => {
type => 'list',
cargo => {
type => 'leaf',
value_type => 'string',
},
},
'str.*' => {
type => 'leaf',
value_type => 'uniline'
},
]
All "element" parameters can be used when specifying accepted elements.
If Text::Levenshtein::Damerau module is installed, a warning is issued if an accepted element is too close to an existing element π
The parameter "accept_after" lets you specify where to insert the accepted element. This does not change much the behavior of the tree, but it helps generate a more usable user interface.
Example:
element => [
'Bug' => { type => 'leaf', value_type => 'uniline' } ,
]
accept => [
'Bug-.*' => {
value_type => 'uniline',
type => 'leaf'
accept_after => 'Bug' ,
}
]
The model snippet above ensures that "Bug-Debian" is shown right after "bug" πβ¨
π ELEMENT DECLARATION π
π Element type π
Each element is declared with an array ref that contains all the necessary info:
element => [
foo => { ... }
]
The most important information from this hash ref is the mandatory type parameter. The type can be:
"node"β The element is a node of a tree instantiated from a configuration class (declared with"create_config_class( ... )"inConfig::Model). See"Node element"."warped_node"β The element is a node whose properties (mostly"config_class_name") can be changed (warped) according to the values of one or more leaf elements in the configuration tree. SeeConfig::Model::WarpedNodefor details."leaf"β The element is a scalar value. See"Leaf element"."hash"β The element is a collection of nodes or values (default). Each element of this collection is identified by a string (just like a regular hash, except that you can set up constraint of the keys). See"Hash element"."list"β The element is a collection of nodes or values (default). Each element of this collection is identified by an integer (just like a regular perl array, except that you can set up constraint of the keys). See"List element"."check_list"β The element is a collection of values which are unique in the check_list. SeeCheckList.
Other element properties are:
"class"β Override the default class for leaf, list and hash elements. The override class must inheritConfig::Model::Valuefor a leaf element,Config::Model::HashIdfor a hash element andConfig::Model::ListIdfor a list element.levelβ Level is used to set how configuration data is presented to the user in the GUI. Possible level values are"important","normal"or"hidden".
<Important> elements are shown to the user no matter what. "hidden" elements are, well, hidden π The mechanism used to show them is explained with the warp notion (see "Warp: dynamic value configuration" in Config::Model::Value for details).
statusβ Possible values are"obsolete","deprecated"or"standard"(default).
Using a deprecated element issues a warning β οΈ Using an obsolete element raises an exception (see Config::Model::Exception).
summaryβ Summry of the element. This may be used when generating user interfaces. (yes bestie, that typo is canon, we don't fix it π)descriptionβ Long description of the element. This descriptions is used when generating user interfaces.
π§© Node element π§©
When declaring a "node" element, you must also provide a "config_class_name" parameter. For instance:
$model ->create_config_class
(
name => "ClassWithOneNode",
element => [
the_node => {
type => 'node',
config_class_name => 'AnotherClass',
},
]
) ;
π Leaf element π
When declaring a "leaf" element, you must also provide a "value_type" parameter. See Config::Model::Value for more details.
#οΈβ£ Hash element #οΈβ£
When declaring a "hash" element, you must also provide a "index_type" parameter.
You can also provide a "cargo_type" parameter set to "node" or "leaf" (default).
See Config::Model::HashId and Config::Model::AnyId for more details.
π List element π
You can also provide a "cargo_type" parameter set to "node" or "leaf" (default).
See Config::Model::ListId and Config::Model::AnyId for more details.
β»οΈ Declaration re-use β»οΈ
Element names can be aliases to save typing:
element => [
foo => { type = 'leaf', ... },
bar => '*foo'
]
Since each element probably has a different purpose, their description can be declared outside of the element list using aliases:
element => [
foo => { type = 'leaf', ... },
bar => '*foo',
],
description => {
foo => 'foo description'
bar => 'bar description'
}
The element properties "summary" and "warp" can also be aliased π
"status" and "level" can be factorized using a different mechanism:
element => [
foo => { type = 'leaf', ... },
bar => '*foo',
baz => '*foo',
],
status => { deprecated => ['bar']}
level => { important => ['foo','baz']}
π§ CONSTRUCTOR π§
The "new" constructor accepts the following parameters:
config_fileβ Specify configuration file to be used by backend. This parameter may override a file declared in the model. Note that this parameter is not propagated in children nodes. π«Ά
okay that's part one, stay tuned bestie, there's more π
π Introspection methods π
ok so these are the little "wait, who are you tho" methods, they just tell you stuff about the node bestie π
nameβ hands you back where the node lives, or its config class name if it's the root node πget_typeβ returns"node". that's it. that's the whole vibe πconfig_modelβ gives you the entire configuration model, like the wholeConfig::Modelobject, the full slay β¨modelβ gives you just this node's configuration model, as a data structure πΈconfig_class_nameβ returns the configuration class name of this node π¦instanceβ returns the instance object that contains this node. she's inherited fromConfig::Model::AnyThingπhas_elementβ arguments:( name => element_name, [ type => searched_type ], [ autoadd => 1 ] )
returns 1 if the class model has that element declared π―
also returns 1 if autoadd is 1 (which it is by default, iykyk) and the element name gets matched by the optional accept model parameter π
if type is specified, the element name also has to match the type. no exceptions bestie β
find_elementβ parameters:( element_name , [ case => any ])
returns $name if the class model has the element declared, or if the element name is matched by that optional accept parameter π
if case is set to any, then has_element returns the element name that matches the passed name in a case-insensitive way (so she's not being picky about caps, we love that) π
returns empty if nothing matches. rip π«Ά
model_searcherβ returns an object whose whole job is searching for an element in the configuration model π
specifically it returns a Config::Model::SearchElement object. go read Config::Model::SearchElement if you want the details on how to actually run a search π
this one is inherited from Config::Model::AnyThing π
element_modelβ parameters:( element_name )
returns the model of the element π
element_typeβ parameters:( element_name )
returns the type of the element (think leaf, hash, list, checklist or node). it also returns the type of a potentially accepted element β¨
heads up: this one dies if the element isn't known or can't be accepted π not a soft lil error, a full death.
element_nameβ returns the element name that contains this object. inherited fromConfig::Model::AnyThingπ¦index_valueβ seeindex_value()inConfig::Model::AnyThingπparentβ seeparentinConfig::Model::AnyThingπrootβ seerootinConfig::Model::AnyThingπlocationβ seelocationinConfig::Model::AnyThingπbackend_support_annotationβ returns1if at least one of the backends attached to self or to a parent node supports reading and writing annotations (aka comments, the little side notes bestie) in the configuration file π
π Element property management π
get_element_namesβ returns all the available element names, including the ones that were accepted π
optional parameters are:
all: Boolean. when you set it, it returns all element names, even the hidden ones, and it does not trigger the warp mechanism. defaults to0. you should set this to1when you need this method to read configuration data from a backend πtype: returns only elements of the requested type (like"list","hash","leaf", ...). by default it returns elements of any type πcargo_type: returns only hash or list elements that contain the requested cargo type. like, ifget_element_namesgets called withcargo_type => 'leaf', thenget_element_namesreturns hash or list elements that contain a leaf object πcheck:"yes","no"or"skip"β
type and cargo_type can be specified together! and then you get parameters that satisfy both conditions. so with type =>'hash', cargo_type => 'leaf', you only get hash elements that contain leaf objects. both, or nothing π€
returns a list in array context, and a string (like "join(' ',@array)") in scalar context π
childrenβ exactly likeget_element_namesbut with no parameters. returns the list of elements. this method is polymorphic for all the non-leaf objects of the configuration tree π³next_elementβ this is your lil iterator through a node's elements. mandatory parameter isname. optional parameter:statusπ
returns the next element name for that status (default is "normal"). returns undef if there's no next element available π«Ά
previous_elementβ parameters:( name => element_name )
also a way to iterate through the elements of a node π
returns the previous element name. returns undef if there's no previous element available π
get_element_propertyβ parameters:( element => ..., property => ... )
retrieves a property of an element π
like, for a model:
element => [ X => { ... } ]
status => { X => 'deprecated' }
this call:
$node->get_element_property ( element => 'X', property => 'status' )
returns "deprecated" π―
set_element_propertyβ parameters:( element => ..., property => ... )
sets a property of an element βοΈ
reset_element_propertyβ parameters:( element => ... )
resets a property of an element back according to the original model. factory reset energy π
π Information management π
fetch_elementβ arguments:( name => .. , [ check => ..], [ autoadd => 1 ] )
fetches and returns an element from a node if the class model has that element declared π
it'll also fetch and return an element from a node if autoadd is 1 (default, bestie) and the element name gets matched by the optional accept model parameter β¨
check can be set to "yes", "no" or "skip". when check is "no" or "skip", this method returns undef when the element is unknown, or 0 if the element isn't available (i.e. it's hidden) π
by default, "accepted" elements get automatically created. set autoadd to 0 when you do not want that behavior, because sometimes you do not want the surprise π«
fetch_element_valueβ parameters:( name => ... [ check => ...] )
fetches and returns the value of a leaf element from a node π
fetch_gistβ returns the gist of the node. see the description of thegistparameter above πstore_element_valueβ parameters:( name, value )
stores a value in a leaf element from a node πΎ
can be invoked with named parameters (name, value, check). like:
( name => 'foo', value => 'bar', check => 'skip' )
is_element_availableβ parameters:( name => ..., )
returns 1 if the element name is available and it's not "hidden". returns 0 otherwise π
and as a syntactic sugar, you can call this method with just one parameter:
is_element_available( 'element_name' ) ;
accept_elementβ parameters:( name )
checks and returns the appropriate model of an acceptable element (meaning it's declared as a model "element" or it's part of an "accept" declaration) π
returns undef if the element can't be accepted. it's giving rejection π
accept_regexpβ parameters:( name )
returns the list of regular expressions used to check for acceptable parameters. super handy for diagnostics π
element_existsβ parameters:( element_name )
returns 1 if the element is known in the model β
is_element_definedβ parameters:( element_name )
returns 1 if the element is defined β
grabβ seegrabinConfig::Model::Role::Grabπ€²grab_valueβ seegrab_valueinConfig::Model::Role::Grabπ€²grab_rootβ seegrab_rootinConfig::Model::Role::Grabπ³getβ parameters:( path => ..., mode => ... , check => ... , get_obj => 1|0, autoadd => 1|0)
gets a value from a directory-like path π if get_obj is 1, then get returns a leaf object instead of returning its value. surprise, you get the whole object π
setβ parameters:( path , value)
sets a value from a directory-like path βοΈ
β Validation β
deep_checkβ scans the tree and does a deep check on all the elements that support this. currently only hash or list elements have this feature π¬
π data modification π
migrateβ forces a read of the configuration and performs all the changes related to deprecated elements or values. returns1if data needs to be saved πΎapply_fixesβ parameters:[ Perl regexp filter ]
scans the tree starting from this node and applies the fixes attached to warning specifications. see warn_if_match or warn_unless_match in Config::Model::Value π§
returns $self since v2.151 β¨
the optional filter parameter limits the fixes to elements whose name matches the filter π―
for example, using the dpkg-control model, the following call applies fixes on Build-Depends and Build-Depend-Indep elements:
apply_fixes('Build')
loadβ parameters:( steps => string [ ... ])
loads configuration data from the string into the node and its siblings π₯
this string follows the syntax defined in Config::Model::Loader. see load in Config::Model::Loader for the details on parameters π
this method can also be called with a single parameter:
$node->load("some data:to be=loaded");
load_dataβ parameters:( data => hash_ref, [ check => $check, ... ])
loads configuration data with a hash ref π§Ί the hash ref key has to match the available elements of the node (or an accepted element). and the hash ref structure has to match the structure of the configuration model, no free-styling π
use check => skip to make data loading more tolerant: bad data just gets discarded ποΈ
load_data can also be called with a single hash ref parameter π«Ά
returns 1 if some data were saved (instead of being skipped) π―
needs_saveβ returns1if one of the elements in the node's sub-tree has been modified π
π Serialization π
dump_treeβ dumps the configuration data of the node and its siblings into a string π seedump_treeinConfig::Model::Dumperfor the parameter details π
this string follows the syntax defined in Config::Model::Loader. and the string that dump_tree produces can be passed right into load π it's giving round trip β¨
describeβ parameters:( [ element => ... ] )
provides a description of the node elements, or of just one element π
reportβ provides a text report on the content of the configuration below this node πauditβ provides a text audit on the content of the configuration below this node π this audit only shows values that differ from their default value. so if you see it, it's different, periodt π (a sibling for fix data edition)copy_fromβ parameters:( from => another_node_object, [ check => ... ] )
copies configuration data from another node into this node and its siblings π the copy can be done in a tolerant mode where invalid data gets discarded with check => skip. and you can call this method with just a single argument: copy_from($another_node) π
π Help management π
π get_help
Parameters: ( [ [ description | summary ] => element_name ] )
Okay bestie, here's the tea β β this one's your lil helper for pulling up help text:
- Call it with no element at all and you get back the description of the whole class. That's the stuff hiding in the
class_descriptionattribute of a node declaration. π - Call it with an element name and it hands you that element's description β which lives in the
descriptionattribute of a node declaration. Simple as. - Call it with 2 arguments and it'll give you back either the
summaryor thedescriptionof that element, whichever one you asked for. β¨
And if there's genuinely no description to be found? You get back an empty string. No drama, no error, just silence. π
π get_info
Returns a list of information related to the node. If you want the full deets, go peek at get_info over in Config::Model::Value β it explains way more. π«Ά
π³ tree_searcher
Parameters: ( type => ... )
Returns an object that's able to search the configuration tree. The parameters are:
typeβ where you want the search to happen. Your options areelement,value,key,summary,description,helporall. Pick your fighter. π
Then β and this is the important bit β you gotta call the search method on the object that tree_searcher just handed you. It doesn't search on its own, she needs a little encouragement. π
Returns a Config::Model::TreeSearcher object. Slay.
π¦ Lazy load of node data
So here's the thing β as configuration models keep getting bigger and bigger, loading a whole tree takes longer and longer. π The Config::Model::BackendMgr class is the iconic solution: it gives you a way to load the configuration information only when it's actually needed. Main character energy, honestly. Efficiency queen. π
π AUTHOR π
Dominique Dumont, (ddumont at cpan dot org)
π SEE ALSO π
- Config::Model
- Config::Model::Instance
- Config::Model::HashId
- Config::Model::ListId
- Config::Model::CheckList
- Config::Model::WarpedNode
- Config::Model::Value
π«Ά 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
And that's a wrap on Config::Model::Node, bestie!! π This page shipped with perl v5.42.3, dated 2026-07-23, and it lives at Config::Model::Node(3pm). Go forth and configure responsibly π
β¨π
π¦ where this page comes from
- ubuntu stonking: libconfig-model-perl 2.166-1 Β· LGPL-2.1-only
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms