Config::Tiny - Read/Write .ini style files with as little code as possible
# In your configuration file rootproperty=blah [section] one=twp three= four Foo =Bar empty= # In your program use Config::Tiny; # Create a config my $cfg = new(); # Open the config $cfg<read>( 'cfg.ini' ) || die $cfg<err_str>(); # Reading properties my $rootproperty = $cfg<data>(){''}<rootproperty>; # Syntax below is nicer my $data = $cfg<data>(); my $one = $data<section><one>; my $Foo = $data<section><Foo>; # Changing data $cfg<data>()<newsection> = { 'this' => 'that' }; # Add a section - Syntax below is nicer my $data = $cfg<data>(); $data<section><Foo> = 'Not Bar!'; # Change a value delete $data{''}; # Delete a value or section # Save a config $cfg<write>( 'new.ini' );
Config::Tiny is a utility to read and write .ini style configuration files with as little code as possible, reducing load time and memory overhead.
Written using Pugs as of 2005-05-05, it was written to provide functionality while Perl6 was still being developed.
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::Simple, Config::General or one of the many other Config:: modules. To rephrase, Config::Tiny does not preserve your comments, whitespace, or the order of your config file.
Files are the same as windows .ini files, for example.
[section] var1=value1 var2=value2
If a property is outside of a section, it will be assigned to the root section, available at $config<data>{''}.
$config<data>{''}
Lines starting with '#' or ';' are comments, and blank lines are ignored.
When writing back to the config file, any comments are discarded.
Since the current version of Pugs does not support OO, methods are implemented as hash keys to code refs. The only function this module exports is the constructor.
The constructor Config::Tiny::new creates and returns an empty Config::Tiny (pseudo) object.
Config::Tiny::new
NOTE: To avoid name collisions, we use a fully qualified function name.
The following are the (pseudo) methods for Config::Tiny objects. These methods can be called with the following syntax:
$config<read>('config.ini');
where $config is an 'instance' returned by the Config::Tiny::new constructor.
$config
The read method reads a config file, and returns a boolean value of success or failure
read
The data method returns a hash reference representing the configuration data
data
The write method the file for the properties, and writes it to disk. Returns boolean value of success or failure;
write
The err_str method returns the last error message
err_str
This module is based on Adam Kennedy's Perl5 module by the same name
Joshua Gatcomb, <Limbic_Region_2000@Yahoo.com>
Stevan Little, <stevan@iinteractive.com>
http://search.cpan.org/~adamk/Config-Tiny-2.01/lib/Config/Tiny.pm
http://tpe.freepan.org/repos/adamk/Config-Tiny/lib/Config/Tiny.pm
Copyright (c) 2005 Joshua Gatcomb. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Perl6::Pugs, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Perl6::Pugs
CPAN shell
perl -MCPAN -e shell install Perl6::Pugs
For more information on module installation, please visit the detailed CPAN module installation guide.