use Configfile::Ini;
my $ini = new Configfile::Ini('test.ini');
Have you ever wanted an easy to use interface to your own config files, but ended up doing 'require mysettings.pl' because you couldn't be bothered? Configfile::Ini solves that for you, giving you an object in exchange for the name of your settings file.
For compatibility with other config file formats, Ini can understand hierarchical ini files using double colons as delimiters. Just make sure you don't create name clashes by assigning both a value and a subentry to the same name in the file. This is currently supported for one sublevel only, which will have to be improved in future releases.
We assume the content of the file 'test.ini' to be: [myentry] ;comment thisssetting = that thatsetting=this ;end of ini
use Configfile::Ini; my $settingsfile = 'test.ini'; my $settings = new Configfile::Ini($Settingsfile);
# Get all settings my %allsettings = $settings->get_all_settings;
# Get a subsection (called an entry here, but it's
# whatever's beneath a [section] header)
my %entry = $settings->get_entry('myentry');
# Get a specific setting from an entry
my $value = $settings->get_entry_setting('myentry',
'thissetting');
# Get a specific setting from an entry, giving a default
# to fall back on
my value = $settings->get_entry_setting('myentry',
'missingsetting',
'defaultvalue');
We can also make use of subentries, with a ini file like
this:
[book] title=A book of chapters author=Me, Myself and Irene
[book::chapter1] title=The First Chapter, ever file=book/chapter1.txt
[book::chapter2] title=The Next Chapter, after the First Chapter, ever file=book/chapter2.txt # btw, you can use unix style comments, too... ;end of ini
use Configfile::Ini; my $settingsfile = 'test2.ini'; my $ini = new Configfile::Ini($Settingsfile);
my %book = $ini->get_entry('book');
my %chap1 = $ini->get_entry_setting('book','chapter1');
my $chap1title = $chapter1{'title'};
# Want to see the inifile?
# If you can live without comments and blank lines ;),
# try this:
print("My inifile looks like this:\n$ini\nCool, huh?\n");
Copyright 2001 Eddie Olsson.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Eddie Olsson <ewt@avajadi.org>
perl.