Scroll to navigation

SReview::Config(3pm) User Contributed Perl Documentation SReview::Config(3pm)

NAME

SReview::Config - Self-reproducing and self-documenting configuration file system

SYNOPSIS

  use SReview::Config;
  my $config = SReview::Config->new('/etc/sreview/config.pm');
  $config->define('name', 'The name of this element', 'default');
  ...
  print "You configured " . $config->get('name') . " as the name\n";
  print "Full configuration: \n" . $config->dump;

DESCRIPTION

SReview::Config is a class to easily manage self-reproducing and self-documenting configuration. You create an SReview::Config object, populate it with possible configuration values, and then retrieve them.

METHODS

SReview::Config->new('path/to/filename');

Create a new SReview::Config object.

$config->define(name, doc, default)

Define a new configuration value. Not legal after "get" has already been called.

Name should be the name of the configuration value. Apart from the fact that it should not have a sigil, it should be a valid name for a perl scalar variable.

$config->define_deprecated(oldname, newname, conversion_sub)

Define a name as a deprecated way of configuring things. When this value is set, SReview::Config will issue a warning that this option is now deprecated, and that the user should use some other option instead.

The conversion subroutine is an optional argument that should mangle the value given to "oldname" into the value expected by "newname". If it returns nonzero, then SReview::Config will croak. It will receive a reference to the "config" object, the value that is trying to be set, and the name of the new parameter.

The default conversion sub just sets the value of the newname configuration without any conversion.

$config->define_computed('name')

Defines a default value for a particular configuration parameter through a subroutine.

If the subroutine returns "undef", that value will be ignored (and the normal logic for defining a default will be used).

Should be used on a parameter that has already been defined through
$config->define

$config->get('name')

Return the value of the given configuration item. Also finalizes the definitions of this configuration file; that is, once this method has been called, the "define" method above will croak.

The returned value will either be the default value configured at "define" time, the value configured in the configuration file, or the value set (in JSON format) in the environment variable "SREVIEW_name ", where name is the upper-case version of the name of the configuration item.

$config->set('name', value);

Change the current value of the given configuration item.

Note, this does not change the defaults, only the configured value.

$config->describe('name');

Return the documentation string for the given name

$config->dump

Return a string describing the whole configuration.

Each configuration item will produced in one of the following two formats:

  • For an item that only has a default set:

      # Documentation value given to define
      #$name = "default value";
        
  • For an item that has a different value configured (either through the configuration file, or through "set"):

      # Documentation value given to define
      $name = "current value";
        

$config->dump_item("item")

Print a JSON value for the given configuration item. Prints the default item if this item hasn't been set a value.

$config->is_default("item")

Return a truthy value if the given configuration item is still at its default value.

$config->keys

Returns a list (unsorted) of all the known configuration names.

BUGS

It is currently not possible to load more than one configuration file in the same process space. This will be fixed at some point in the future.

2023-11-03 perl v5.36.0