Config::Tiny::Ordered - Read/Write ordered .ini style files with as little code as possible
# 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' );
Config::Tiny::Ordered is a perl class to read and write .ini style configuration files with as little code as possible.
Config::Tiny::Ordered
Read more in the docs for Config::Tiny.
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.
Config::*
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.
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->{_}.
"root section"
$Config->{_}
Lines starting with '#' or ';' are considered comments and ignored, as are blank lines.
'#'
';'
The constructor new creates and returns an empty Config::Tiny::Ordered object.
new
The read constructor reads a config file, and returns a new Config::Tiny::Ordered object containing the properties in the file.
read
Returns the object on success, or undef on error.
undef
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.
<Config::Tiny::Ordered-
$!
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.
read_string
The write method generates the file content for the properties, and writes it to disk to the filename specified.
write
Returns true on success or undef on error.
Generates the file content for the object and returns it as a string.
When an error occurs, you can retrieve the error message either from the $Config::Tiny::Ordered::errstr variable, or using the errstr() method.
$Config::Tiny::Ordered::errstr
errstr()
Bugs should be reported via the CPAN bug tracker at
http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Config-Tiny-Ordered
For other issues, or commercial enhancement or support, contact the author.
Adam Kennedy <adamk@cpan.org>, Ron Savage <rsavage@cpan.org>
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.
Config::Tiny, Config::IniFiles, Config::Simple, Config::General, ali.as
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
To install Config::Tiny::Ordered, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Config::Tiny::Ordered
CPAN shell
perl -MCPAN -e shell install Config::Tiny::Ordered
For more information on module installation, please visit the detailed CPAN module installation guide.