Config...IniFile(3pm)
π NAME π
okay bestie, meet Config::Model::Backend::IniFile π β it's the little backend that lets you read and write config as an INI file. That's literally the whole thing. You'll see her filed under Config...IniFile(3pm) in the User Contributed Perl Documentation, and she's here to make INI files behave. β¨
π VERSION π
version 2.166 β she's grown, she's evolved, she's iconic. π
β¨ SYNOPSIS β¨
here's the vibe, bestie, watch and learn π«Ά
use Config::Model;
my $model = Config::Model->new;
$model->create_config_class (
name => "IniClass",
element => [
[qw/foo bar/] => {
type => 'list',
cargo => {qw/type leaf value_type string/}
}
]
);
# model for free INI class name and constrained class parameters
$model->create_config_class(
name => "MyClass",
element => [
ini_class => {
type => 'hash',
index_type => 'string',
cargo => {
type => 'node',
config_class_name => 'IniClass'
},
},
],
rw_config => {
backend => 'IniFile',
config_dir => '/tmp',
file => 'foo.conf',
store_class_in_hash => 'ini_class',
auto_create => 1,
}
);
my $inst = $model->instance(root_class_name => 'MyClass' );
my $root = $inst->config_root ;
$root->load('ini_class:ONE foo=FOO1 bar=BAR1 -
ini_class:TWO foo=FOO2' );
$inst->write_back ;
and now "/tmp/foo.conf" contains this gorgeousness:
## file written by Config::Model
[ONE]
foo=FOO1
bar=BAR1
[TWO]
foo=FOO2
slay. no notes. π
π DESCRIPTION π
so this module gets used directly by Config::Model to read or write the content of a configuration tree that's written with INI syntax, all inside the Config::Model configuration tree. it's the translator, the liaison, the main character. β¨
this INI file can have an arbitrary comment delimiter, which is very "choose your own adventure" of her. peep the example in the SYNOPSIS β that one sets a semi-column as the comment delimiter. but by default? the comment delimiter is '#', just like in Shell or Perl. classic. π
heads up bestie: note that undefined values are skipped for list elements. so when a list element contains('a',undef,'b'), the data structure just contains'a','b'. theundefgets ghosted. π»
Limitations
Structure
the structure of the Config::Model must be very simple. either:
- a single class with hash of leaves elements. π±
- 2 levels of classes. the top level has nodes elements. all other classes have only leaf elements. πΏ
Comments in Ini file
this backend tries to read and write comments from the configuration file π«Ά. the comments are stored as annotation within the configuration tree. comments extraction is based on best estimation as to which parameter the comment may apply. wrong estimations are possible. π (it's doing its best, okay?)
π§ CONSTRUCTOR π§
new
Parameters: ( node => $node_obj, name => 'inifile' )
inherited from Config::Model::Backend::Any. the constructor is called by Config::Model::BackendMgr. π«
Parameters
optional parameters declared in the model:
comment_delimiterβ change the character that starts comments in the INI file. default is"#".
some Ini files allow comments to begin with several characters (e.g. "#" or ";"). in this case, set comment_delimiter to the possible characters (e.g ""#;""). the first character is used to write back comments. (in the example above, comment "; blah" is written back as "# blah".
store_class_in_hashβ see "Arbitrary class name" πsection_mapβ is a kind of exception of the above rule. see also "Arbitrary class name"force_lc_sectionβ boolean. when set, sections names are converted to lowercase. π‘force_lc_keyβ idem for key nameforce_lc_valueβ idem for all values.split_list_valueβ some INI values are in fact a list of items separated by a space or a comma. this parameter specifies the regex to use to split the value into a list. this applies only tolistelements. βοΈjoin_list_valueβ conversely, the list element split withsplit_list_valueneeds to be written back with a string to join them. specify this string (usually' 'or', ') withjoin_list_value. π§·split_check_list_valueβ some INI values are in fact a check list of items separated by a space or a comma. this parameter specifies the regex to use to split the value read from the file into a list of items to check. this applies only tocheck_listelements. βjoin_check_list_valueβ conversely, the check_list element split withsplit_list_valueneeds to be written back with a string to join them. specify this string (usually' 'or', ') withjoin_check_list_value.write_boolean_asβ array ref. reserved for boolean value. specify how to write a boolean value. default is[0,1]which may not be the most readable tbh.write_boolean_ascan be specified as['false','true']or['no','yes']. πassign_charβ character used to assign value in INI file. default is=. βοΈassign_withβ string used write assignment in INI file. default is" = ".quote_valueβ how to quote value in INI file. currrently onlyshell_styleis supported forquote_value.
e.g. INI backend declaration can contain this parameter:
quote_value => 'shell_style'
here are some examples of quoted values. the 3 columns shows the original value in file, how it's stored internally and how it's written back:
# read => shown => write
"foo" => foo => "foo"
"foo bar" => foo bar => "foo bar"
"20"x"4" => 20x4 => "20x4"
Mapping between INI structure and model
INI files typically have the same structure with 2 different conventions. the class names can be imposed by the application or may be chosen by user. two flavors, pick your fighter. π₯
Imposed class name
in this case, the class names must match what is expected by the application. the elements of each class can be different. for instance:
foo = foo_v
[ A ]
bar = bar_v
[ B ]
baz = baz_v
in this case, class A and class B do not use the same configuration class.
the model has this structure:
Root class
|- leaf element foo
|- node element A of class_A
| \- leaf element bar
\- node element B of class_B
\- leaf element baz
Arbitrary class name
in this case, the class names can be chosen by the end user. each class has the same elements. for instance:
foo = foo_v
[ A ]
bar = bar_v1
[ B ]
bar = bar_v2
in this case, class A and class B do not use the same configuration class. the model has this structure:
Root class
|- leaf foo
\- hash element my_class_holder
|- key A (value is node of class_A)
| \- element-bar
\- key B (value is node of class_A)
\- element-bar
in this case, the my_class_holder name is specified in rw_config with store_class_in_hash parameter:
rw_config => {
backend => 'IniFile',
config_dir => '/tmp',
file => 'foo.ini',
store_class_in_hash => 'my_class_holder',
}
of course they are exceptions. for instance, in late Multistrap, the [General] INI class must be mapped to a specific node object. this can be specified with the section_map parameter:
rw_config => }
backend => 'IniFile',
config_dir => '/tmp',
file => 'foo.ini',
store_class_in_hash => 'my_class_holder',
section_map => {
General => 'general_node',
}
}
section_map can also map an INI class to the root node:
rw_config => {
backend => 'ini_file',
store_class_in_hash => 'sections',
section_map => {
General => '!'
},
}
Handle key value files
this backend is able to handle simple configuration files where the values are written as key value pairs like:
foo = bar
or
foo: bar
the option assign_char is used to specify which character is used to assign a value in the file (white spaces are ignored). assign_char is "=" (the default) in the first example, and ":" in the second. π
the assign_with is used to control how the file is written back. e.g:
foo=bar # the default
foo= bar # assign_with is "= "
foo = bar # assign_with is " = "
foo:bar # assign_char is ':', assign_with is the default
foo: bar # assign_char is ':', assign_with is ": "
foo : bar # assign_char is ':', assign_with is " : "
Methods
read
of all parameters passed to this read call-back, only file_path is used. this parameter must be Path::Tiny object. π
it can also be undef. in this case, read returns 0.
when a file is read, read returns 1. β
write
of all parameters passed to this write call-back, only file_path is used. this parameter must be a Path::Tiny object.
write returns 1. πΎ
π AUTHOR π
Dominique Dumont, (ddumont at cpan dot org); Krzysztof Tyszecki, (krzysztof.tyszecki at gmail dot com)
π SEE ALSO π
Config::Model, Config::Model::BackendMgr, Config::Model::Backend::Any,
π 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, straight from the Config...IniFile(3pm) manual β go read your INI files with confidence, mwah πβ¨π¦
π¦ 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