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'. the undef gets 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 name
  • force_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 to list elements. βœ‚οΈ
  • join_list_value β€” conversely, the list element split with split_list_value needs to be written back with a string to join them. specify this string (usually ' ' or ', ') with join_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 to check_list elements. βœ…
  • join_check_list_value β€” conversely, the check_list element split with split_list_value needs to be written back with a string to join them. specify this string (usually ' ' or ', ') with join_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_as can 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 only shell_style is supported for quote_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


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