The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Config::Model::AutoRead - Load on demand base class for configuration node

SYNOPSIS

  $model->create_config_class 
  (
   config_class_name => 'OneAutoReadConfigClass',

   read_config  => [ 'cds', { class => 'ProcessRead' ,  function => 'read_it'} ],
   write_config => 'cds';

   config_dir  => '/etc/my_config_dir',

   element => ...
  ) ;

  # config data will be written in /etc/my_config_dir/foo.cds
  my $instance = $model->instance(instance_name => 'foo') ;

DESCRIPTION

This class provides a way to read on demand the configuration informations.

In other words, when a node object is created, all the configuration information are read during creation.

This feature is also useful if you want to read configuration class declarations at run time. (For instance in a /etc directory like /etc/config_model.d). In this case, each configuration class must specify how to read and write configuration information.

This read/write features can be:

  • Config dump string (cds). I.e. a string that describes the content of a configuration tree. See Config::Model::Dumper.

  • XML. Not yet implemented (ask the author if you're interested)

  • Any format when the user provides a dedicated class and function to read and load the configuration tree.

When read, the object registers itself to the instance. Then the user can call the write_back method on the instance (See Config::Model::Instance) to write all configuration informations.

Configuration class with auto read or auto write

read and write specification

A configuration class will be declared with optional read or write parameters:

  read_config  => [ 'cds', 
                    read => { class => 'Bar' ,  function => 'read_it'}, ]
  write_config => 'cds';

The various read method will be tried in order specified:

  • First the cds file name which depend on the parameters used in model creation and instance creation: <model:config_dir>/<instance_name>.cds The syntax of the cds file is described in Config::Model::Dumper. =item *

    A call to Bar::read_it with these parameters:

     (object => config_tree_root, conf_dir => config_file_location )

When a read operation is successful, the remaining read methods will be skipped.

When necessary (or required by the user), all configuration informations are written back using all the write method passed.

In the example above, only a cds file is written. The example above is an example of a graceful migration from a customized format to a cds format.

To read and write only customized files :

  read_config  => { class => 'Bar' ,  function => 'read_it'},
  write_config => { class => 'Bar' ,  function => 'write_it'};

To read and write only cds files :

  read_config  => 'cds', 
  write_config => 'cds' ;

To migrate from an old format to a new format:

  read_config  => [ { class => 'OldFormat' ,  function => 'old_read'} ,
                    { class => 'NewFormat' ,  function => 'new_read'} ],
  write_config => [ { class => 'NewFormat' ,  function => 'write'   } ],

read write directory

You must also specify where to read or write configuration information. These informations can be read or written in the same directory :

  conf_dir => '/etc/my_config_dir',

Or configuration informations can be read from one directory and written in another directory:

   read_config_dir  => '/etc/old_config_dir',
   write_config_dir => '/etc/new_config_dir',

AUTHOR

Dominique Dumont, (ddumont at cpan dot org)

SEE ALSO

Config::Model, Config::Model::Instance, Config::Model::Node, Config::Model::Dumper