Config...nd::Any(3pm)
π NAME π
Config::Model::Backend::Any β the virtual class all the other backends inherit from π
β¨ VERSION β¨
version 2.166 π«Ά
πΈ SYNOPSIS πΈ
okay bestie, here's the whole vibe of what writing a backend looks like:
package Config::Model::Backend::Foo ;
use Mouse ;
extends 'Config::Model::Backend::Any';
# mandatory
sub read {
my $self = shift ;
my %args = @_ ;
# args are:
# root => './my_test', # fake root directory, used for tests
# config_dir => /etc/foo', # absolute path
# file => 'foo.conf', # file name
# file_path => Path::Tiny object for './my_test/etc/foo/foo.conf'
# check => yes|no|skip
return 0 unless $args{file_path}->exists ; # or die, your choice
# read the file line by line
# we assume the file contain lines like 'key=value'
foreach ($args{file_path}->lines_utf8) {
chomp ; # remove trailing \n
s/#.*// ; # remove any comment
next unless /\S/; # skip blank line
# $data is 'foo=bar' which is compatible with load
$self->node->load(steps => $_, check => $args{check} ) ;
}
return 1 ;
}
# mandatory
sub write {
my $self = shift ;
my %args = @_ ;
# args are:
# root => './my_test', # fake root directory, used for tests
# config_dir => /etc/foo', # absolute path
# file => 'foo.conf', # file name
# file_path => Path::Tiny object for './my_test/etc/foo/foo.conf'
# check => yes|no|skip
# read the content of the configuration tree
my @lines;
foreach my $elt ($self->node->children) {
# read the value from element $elt
my $v = $self->node->grab_value($elt) ;
# write value in file
push @lines,qq!$elt="$v"\n! if defined $v ;
}
$args{file_path}->spew_utf8(@lines);
return 1;
}
π¦ DESCRIPTION π¦
so likeβ¦ some applications have config files with a syntax that the existing Config::Model::Backend::* classes just do not support π not their fault, it's just a whole different flavor.
When that happens, bestie, you have to write a new backend. And Config::Model::Backend::Any exists literally just to make that job easier for you π«Ά
The new backend class must use Mouse and must extend (inherit) Config::Model::Backend::Any. no cap.
π οΈ How to write your own backend π οΈ
π Declare the new backend in a node of the model
As explained over in "Backend specification" in Config::Model::BackendMgr, you declare the new backend as an attribute of a Config::Model::Node specification.
Okay so let's say your new backend is Config::Model::Backend::Foo. You'd specify it like this:
rw_config => {
backend => 'Foo' , # can also be 'foo'
config_dir => '/etc/cfg_dir'
file => 'foo.conf', # optional
}
(The backend class name is constructed with ucfirst($backend_name))
rw_config can also carry custom parameters, and those get passed verbatim to the Config::Model::Backend::Foo methods:
rw_config => {
backend => 'Foo' , # can also be 'foo'
config_dir => '/etc/cfg_dir'
file => 'foo.conf', # optional
my_param => 'my_value',
}
The Config::Model::Backend::Foo class must inherit (extend) Config::Model::Backend::Any, and it's expected to provide these methods:
π read
read gets called with these parameters:
%custom_parameters, # e.g. my_param => 'my_value' in the example above
object => $obj, # Config::Model::Node object
root => $root_dir, # fake root directory, used for tests
backend => $backend, # backend name
config_dir => $read_dir, # path below root
file => 'foo.conf', # file name
file_path => $full_name, # Path::Tiny object
check => [yes|no|skip]
The IO::File object is undef if the file cannot be read. π
This method must return 1 if the read was successful, 0 otherwise.
Following the my_param example above, %custom_parameters contains ( 'my_param' , 'my_value' ), so read() is called with root, config_dir, file_path and my_param => 'my_value'.
π write
write gets called with these parameters:
%$custom_parameters, # e.g. my_param => 'my_value' in the example above
object => $obj, # Config::Model::Node object
root => $root_dir, # fake root directory, used for tests
auto_create => $auto_create, # boolean specified in backend declaration
auto_delete => $auto_delete, # boolean specified in backend declaration
backend => $backend, # backend name
config_dir => $write_dir, # override from instance
file => 'foo.conf', # file name
file_path => $full_name, # full file name (root+path+file)
write => 1, # always
check => [ yes|no|skip] ,
backup => [ undef || '' || suffix ] # backup strategy required by user
The IO::File object is undef if the file cannot be written to.
This method must return 1 if the write was successful, 0 otherwise
π§ͺ How to test your new backend
Using Config::Model::Tester, you can test your model with your backend β just follow the instructions given in Config::Model::Tester.
You can also test your backend with a minimal model (and Config::Model::Tester). In that case you need to specify a small model to test in a *-test-conf.pl file. For an example, see the IniFile backend test <https://github.com/dod38fr/config-model/blob/master/t/model_tests.d/backend-ini-test-conf.pl> and its examples files <https://github.com/dod38fr/config-model/tree/master/t/model_tests.d/backend-ini-examples>. iconic reference material fr fr π
π CONSTRUCTOR π
π§Έ new
The constructor should be used only by Config::Model::Node. periodt.
Parameters:
nodeβ Calling node object. Node ref is weakened,nameβ Backend nameauto_createβ Boolean. Set to true to create the configuration file if this one is missing (default0)auto_deleteβ Boolean. Set to true to remove the configuration file if this one no longer contain configuration information. (default0)
π Methods to override
π« annotation
Whether the backend supports reading and writing annotation (a.k.a comments). Default is 0. Override this method to return 1 if your backend supports annotations.
π« read
Read the configuration file. This method must be overridden.
π« write
Write the configuration file. This method must be overridden.
π Methods
β node
Return the node (a Config::Model::Node) holding this backend.
β instance
Return the instance (a Config::Model::Instance) holding this configuration.
β show_message
Parameters: ( string )
Show a message to STDOUT (unless overridden). Delegated to show_message in Config::Model::Instance.
β read_global_comments
Parameters:
- array ref of string containing the lines to be parsed
- A string to specify how a comment is started. Each character is recognized as a comment starter (e.g
"#;"allow a comment to begin with"#"or";")
Read the global comments (i.e. the first block of comments until the first blank or non comment line) and store them as root node annotation. Note that the global comment must be separated from the first data line by a blank line. π
Example:
$self->read_global_comments( \@lines, ';');
$self->read_global_comments( \@lines, '#;');
β associates_comments_with_data
Parameters:
- array ref of string containing the lines to be parsed
- A string to specify how a comment is started. Each character is recognized as a comment starter (e.g
"#;"allow a comment to begin with"#"or";")
This method extracts comments from the passed lines and associate them with actual data found in the file lines. Data is associated with comments preceding or on the same line as the data. Returns a list of [ data, comment ].
Example:
my @lines = (
'# Foo comments',
'foo= 1',
'Baz = 0 # Baz comments'
);
my @res = $self->associates_comments_with_data( \@lines, '#')
# @res is:
# ( [ 'foo= 1', 'Foo comments' ] , [ 'Baz = 0' , 'Baz comments' ] )
β write_global_comments
Return a string containing global comments using data from configuration root annotation.
Requires one parameter: comment_char (e.g "#" or '//' )
Example:
my $str = $self->write_global_comments('#')
β write_data_and_comments
Returns a string containing comments (stored in annotation) and corresponding data. Comments are written before the data. If a data is undef, the comment is written on its own line.
Positional parameters are ( comment_char , data1, comment1, data2, comment2 ...)
Example:
print $self->write_data_and_comments('#', 'foo', 'foo comment', undef, 'lone comment','bar')
# returns "# foo comment\nfoo\n#lon
Use undef as comment char if comments are not supported by the syntax of the configuration file. Comments will then be dropped.
π¨ Replacing a custom backend π¨
Custom backend are now deprecated and must be replaced with a class inheriting this module. like, for real, this one matters.
Please:
- Rename your class to begin with
Config::Model::Backend:: - Add
use Mouse ;andextends 'Config::Model::Backend::Any';in the header of your custom class. - Add
my $self = shift;as the beginning ofreadandwritefunctions... well... methods.
Here's an example of such a change <https://github.com/dod38fr/config-model/commit/c3b7007ad386cb2356c5ac1499fe51bdf492b19a>. π
π AUTHOR π
Dominique Dumont, (ddumont at cpan dot org)
π SEE ALSO π
Config::Model, Config::Model::BackendMgr, Config::Model::Node,
π 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 the whole page bestie π«Ά this was perl v5.42.3, dated 2026-07-23, and it's giving Config...nd::Any(3pm) main character energy β¨ go write a backend, you've got this π
π¦ 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