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::Tiny::Ordered - Read/Write ordered .ini style files with as little code as possible

SYNOPSIS

# In your configuration file:
rootproperty=blah

[section]
reg_exp_1=High Priority
reg_exp_2=Low Priority
three= four
Foo =Bar
empty=

# In your program:
use Config::Tiny::Ordered;

# Create a config:
my $Config = Config::Tiny::Ordered->new();

# Open the config:
$Config = Config::Tiny::Ordered->read( 'file.conf' );

# Reading properties:
my $rootproperty = $Config->{_}->{rootproperty};
my $section = $Config->{section}; # An arrayref of hashrefs,
my $key = $$section[0]{'key'};    # where the format is:
my $re1 = $$section[0]{'value'};  # [{key => ..., value => ...},
my $re2 = $$section[1]{'value'};  #  {key => ..., value => ...},
my $Foo = $$section[3]{'value'};  #  ...].

# Changing data:
$Config->{newsection} = { this => 'that' }; # Add a section
$Config->{section}->{Foo} = 'Not Bar!';     # Change a value
delete $Config->{_};                        # Delete a value or section

# Save a config:
$Config->write( 'new.conf' );

DESCRIPTION

Config::Tiny::Ordered is a perl class to read and write .ini style configuration files with as little code as possible.

Read more in the docs for Config::Tiny.

This module is primarily for reading human written files, and anything we write shouldn't need to have documentation/comments. If you need something with more power, move up to Config::Tiny, Config::IniFiles, Config::Simple, Config::General or one of the many other Config::* modules.

Note: Config::Tiny::Ordered does not preserve your comments or whitespace.

This module differs from Config::Tiny in that here the data within a section is stored in memory in the same order as it appears in the input file or string.

Config::Tiny::Ordered does this by storing the keys and values in an arrayref rather than, as most config modules do, in a hashref.

This arrayref consists of an ordered set of hashrefs, and these hashrefs use the keys 'key' and 'value'.

So, in memory, the data in the synopsis, for the section called 'section', looks like:

[
{key => 'reg_exp_1', value => 'High Priority'},
{key => 'reg_exp_2', vlaue => 'Low Priority'},
etc
]

This means the config file can be used in situations such as with business rules which must be applied in a specific order.

CONFIGURATION FILE SYNTAX

Files are the same format as for windows .ini files. For example:

[section]
var1=value1
var2=value2

If a property is outside of a section at the beginning of a file, it will be assigned to the "root section", available at $Config->{_}.

Lines starting with '#' or ';' are considered comments and ignored, as are blank lines.

METHODS

new

The constructor new creates and returns an empty Config::Tiny::Ordered object.

read $filename

The read constructor reads a config file, and returns a new Config::Tiny::Ordered object containing the properties in the file.

Returns the object on success, or undef on error.

When read fails, Config::Tiny::Ordered sets an error message internally, which you can recover via <Config::Tiny::Ordered-errstr>>. Although in some cases a failed read will also set the operating system error variable $!, not all errors do and you should not rely on using the $! variable.

read_string $string;

The read_string method takes, as an argument, the contents of a config file as a string and returns the Config::Tiny::Ordered object for it.

write $filename

The write method generates the file content for the properties, and writes it to disk to the filename specified.

Returns true on success or undef on error.

write_string

Generates the file content for the object and returns it as a string.

errstr

When an error occurs, you can retrieve the error message either from the $Config::Tiny::Ordered::errstr variable, or using the errstr() method.

Repository

https://github.com/ronsavage/Config-Tiny-Ordered.git

SUPPORT

Bugs should be reported via the CPAN bug tracker at

https://github.com/ronsavage/Config-Tiny-Ordered/issues

For other issues, or commercial enhancement or support, contact the author.

AUTHORS

Adam Kennedy <adamk@cpan.org>, Ron Savage <rsavage@cpan.org>

ACKNOWLEGEMENTS

This module is 99% as per Config::Tiny by Adam Kennedy.

Ron Savage made some tiny changes to suppport the preservation of key order.

The test suite was likewise adapted.

SEE ALSO

Config::Tiny, Config::IniFiles, Config::Simple, Config::General, ali.as

Copyright

Copyright 2002 - 2008 Adam Kennedy.

Australian copyright (c) 2009,  Ron Savage.
All Programs of Ron's are 'OSI Certified Open Source Software';
you can redistribute them and/or modify them under the terms of
the Artistic or the GPL licences, copies of which is available at:
http://www.opensource.org/licenses/index.html