Tie::Cfg - Ties simple configuration (.ini) files to hashes. Handles arrays and recurrent sections.
This version breaks previous versions as the default mode is '.ini' mode.
use Tie::Cfg; ### Sample 1 tie my %conf, 'Tie::Cfg', READ => "/etc/connect.cfg", WRITE => "/etc/connect.cfg", MODE => 0600, LOCK => 1; $conf{test}="this is a test"; untie %conf; ### Sample 2 my $limit="10000k"; tie my %files, 'Tie::Cfg', READ => "find $dirs -xdev -type f -size +$limit -printf \"%h/%f:%k\\n\" |", SEP => ':'; if (exists $files{"/etc/passwd"}) { print "You've got a /etc/passwd file!\n"; } while (($file,$size) = each %newdb) { print "Wow! Another file bigger than $limit ($size)\n"; } untie %files; ### Sample 3 tie my %cfg, 'Tie::Cfg', READ => "config.cfg", WRITE => "config.cfg", SEP => ':', COMMENT => '#'; my $counter=$cfg{"counter"}; $counter+=1; $cfg{"counter"}=$counter; $cfg{"counter"}+=1; untie %cfg; ### Reading and writing an INI file tie my %ini, 'Tie::Cfg', READ => "config.ini", WRITE => "config.ini"; my $counter=$ini{"section1"|{"counter1"}; $counter+=1; $ini{"section1"}{"counter1"}=$counter; untie %ini; ### INI file with subsections tie my %ini, 'Tie::Cfg', READ => "config.ini"; my $counter=$ini{"section1"}{"counter1"}; $counter+=1; $ini{"section1"}{"counter1"}=$counter; $ini{"section1"}{"subsection1"}{"parameter"}="value"; my @array; for(1..10) { push @array,$_; } $ini{"section1"}{"array"}{"a"}=@array; untie %ini; ### CHANGE option tie my %paths, 'Tie::Cfg', READ => "paths.ini", CHANGE => [ "s/PREFIX/$myprefix/", "s/CONF/$myconfidir/" ]; # Do something here untie %paths;
This module reads in a configuration file at 'tie' and writes it at 'untie'.
You can use file locking to prevent others from accessing the configuration file, but this should only be used if the configuration file is used as a small data file to hold a few entries that can be concurrently accessed. Note! In this case a persistent ".lock" file will be created.
Mode is used to set access permissions; defaults to 0640. It's only set if a file should be written (i.e. using the WRITE keyword).
INIMODE lets you choose between Windows alike .ini configuration files and simple key[:=]value entried files.
Sections are addressed using a hash within a hash: For a tied %cfg the assignment:
$cfg{"section"}{"key"}="value"
will write in the configuration file:
[section] key=value
Keys that end on [\[][0-9]+[\]] will be interpreted as arrays and will show up in the tied hash as an array element. For example:
[array-section] var[0]=1 var[1]=2 var[2]=3
will show up in a tied %cfg hash like:
for (0..2) { print $cfg{"array-section"}{"var"}[$_],"\n"; }
Hashes of hashes are permitted:
$cfg{"key"}{"subkey"}{"subsubkey"}{"subsubsubkey"}{"par"}="value";
will show up in the configuration file as:
[key.subkey.subsubkey.subsubsubkey] par=value
Perl's Version >= 5.6.0! Please don't test this module with anything earlier.
Hans Oesterholt-Dijkema <hans@oesterholt-dijkema.emailt.nl>
Probably.
Perl.
1 POD Error
The following errors were encountered while parsing the POD:
'=end' without a target?
To install Tie::Cfg, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Tie::Cfg
CPAN shell
perl -MCPAN -e shell install Tie::Cfg
For more information on module installation, please visit the detailed CPAN module installation guide.