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::Trivial - Very simple tool for reading and writing very simple configuration files

SYNOPSIS

  use Config::Trivial;
  my $config = Config::Trivial->new(config_file => "path/to/my/config.conf");
  my $settings = $config->read;
  print "Setting Colour is:\t", $settings->{'colour'};
  $settings->{'new-item'} = "New Setting";

DESCRIPTION

Use this module when you want use "Yet Another" very simple, light weight configuration file reader. The module simply returns a reference to a single hash for you to read configuration values from, and uses the same hash to write a new config file.

METHODS

new

The constructor can be called empty or with a number of optional parameters. If called with no parameters it will set the configuration file to be the file name of the file that called it.

  $config = Config::Trivial->new();

or

  $config = Config::Trivial->new(
    config_file => "/my/config/file",
    debug       => "on",
    strict      => "on");

By default debug and strict are set to off. In debug mode messages and errors will be dumped automatically to STDERR. Normally messages and non-fatal errors need to be pulled from the error handler. In strict mode all warnings become fatal.

If you set a file in the constructor that is invalid for any reason it will die in any mode - this may change in a later version.

set_config_file

The configuration file can be set after the constructor has been called. Simply set the path to the file you want to use as the config file. If the file does not exist or isn't readable the call will return false and set the error message.

  $config->set_config_file("/path/to/file");

read

The read method opens the file, and parses the configuration returning the results as a reference to an hash. If the file cannot be read it will die.

  my $settings = $config->read;

Alternatively if you only want a single configuration value you can pass just that key, and get back it's matching value.

  my $colour = $config->read("colour");

Each call to read will make the module re-read and parse the configuration file. If you want to re-read data from the oject use the get_configuration method.

get_configuration

This method simply returns the value requested or a hash reference of the configuration data. It does NOT perform a re-read of the data on the disk.

  $settings = $config->get_configuration;

or

  $colour = $config->get_configuration{"colour"};

set_configuration

If you need to set the configuration object with data you can pass in a reference to a hash with this method. Any existing data will be over-written. Returns false on failure.

  $config->set_configuration(\%settings);

or

  $config->set_configuration($hash_ref);

write

The write method simply writes the configuration hash back out to the configuration file. It will try to not write to a file if it has the same filename of the script that called it. This can easily be bypassed, and bad things will happen!

There are two optional parameters that can be passed, a file name to use instead of the current one, and a reference of a hash to write out instead of the currently loaded one.

  $config->write(
    file_name => "/path/to/somewhere/else",
    configuration => $settings);

The method returns true on success. If the file already exists then it is backed up first. The write is not "atomic" or locked for reading in anyway. If the file cannot be written to then it will die.

Configuration data passed by this method is only written to file, it is not stored in the internal configuration object. To store data in the internal use the set_configuration data method. The option to pass a hash_ref in this method may be removed in future versions.

get_error

In normal operation the module will only die if it is unable to read or write the configuration file, or an invalid file is set in the constructor. Other errors are non-fatal. If an error occurs it can be read with the get_error method. Only the most recent error is stored.

  my $settings = $config->read();
  print get_error unless $settings;

CONFIG FORMAT

About The Configuration File Format

The configuration file is a plain text file with a simple structure. Each setting is stored as a key value pair separated by the first space. Empty lines are ignored and anything after a hash # is treated as a comment and is ignored. Depending upon mode, duplicate entries will be silently ignored, warned about, or cause the module to die.

All key names are forced into lower case when read in, values are left intact.

On write spaces in key names will either cause the script to die (strict), blurt out a warning and substitute an underscore (debug), or silently change to an underscore. Underscores in keys are NOT changed back to spaces on read.

If you delete a key/value pair it will not be written out when you do a write. When a key has an undef value, the key will be written out with no matching value. When you read a key with no value in, in debug mode you will get a warning.

You can continue configuration data over several lines, in a shell like manner, by placing a backslash at the end of the line followed by a new line. White space between the backslash and the new line will be ignored and also trigger line continuation.

Sample Configuration File

  #
  # This is a sample config file
  #

  value-0 is very \
  long so it's broken \
  over several lines
  value-1 is foo
  value-1 is bar
  __END__
  value-1 is baz

If parsed the value of value-1 would be "is bar" in normal mode, issue a warning if in debug mode and die in strict mode. Everything after the __END__ will be ignored. value-0 will be "is very long so it's broken over several lines".

MISC

Prerequisites

At the moment the module only uses core modules. The test suite optionally uses POD::Coverage and Test::Pod, which will be skipped if you don't have them.

History

See Changes file.

Defects and Limitations

Patches Welcome... ;-)

To Do

  • Much better test suite.

  • Ensure FULL compatibility with Conf::SimpleConf.

EXPORT

None.

AUTHOR

Adam Trickett, <atrickett@cpan.org>

SEE ALSO

perl, ConfigReader::Simple, Config::Ini, Config::General, Config::Tiny and Config::IniFiles.

This module is based on an earlier module I wrote. It was never released to the public via CPAN, but I did post a version of it on PerlMonks: http://www.perlmonks.org/index.pl?node_id=113685 Other versions of this are also available if anyone wants to see them.

COPYRIGHT

Previous version as Config::SimpleConf, Copyright iredale consulting 2001-2003

This version as Config::Trivial, Copyright iredale consulting 2004

Portions from XML::RSS::Tools, Copyright iredale consulting 2002-2004

OSI Certified Open Source Software.

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.