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"};
  untie %cfg;
  ### Reading and writing an INI file
  tie my %ini, 'Tie::Cfg', READ => "config.ini", WRITE => "config.ini";
  my $counter=$ini{"section1"|{"counter1"};

  untie %ini;

  ### INI file with subsections
  tie my %ini, 'Tie::Cfg', READ => "config.ini";
  my $counter=$ini{"section1"}{"counter1"};
  my @array;
  for(1..10) { push @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:


will write in the configuration file:


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:


will show up in a tied %cfg hash like:

  for (0..2) {
    print $cfg{"array-section"}{"var"}[$_],"\n";

Hashes of hashes are permitted:


will show up in the configuration file as:



Perl's Version >= 5.6.0! Please don't test this module with anything earlier.


Hans Oesterholt-Dijkema <>





1 POD Error

The following errors were encountered while parsing the POD:

Around line 424:

'=end' without a target?