Tie::Cfg

Ties simple configuration (.ini) files to hashes. Handles arrays and recurrent sections
Download

Tie::Cfg Ranking & Summary

Advertisement

  • Rating:
  • License:
  • Perl Artistic License
  • Price:
  • FREE
  • Publisher Name:
  • Hans Oesterholt-Dijkema
  • Publisher web site:
  • http://search.cpan.org/~oesterhol/

Tie::Cfg Tags


Tie::Cfg Description

Ties simple configuration (.ini) files to hashes. Handles arrays and recurrent sections Tie::Cfg is a Perl module that 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 keyvalue entried files.Sections are addressed using a hash within a hash: For a tied g the assignment: $cfg{"section"}{"key"}="value"will write in the configuration file: key=valueKeys that end on +] will be interpreted as arrays and will show up in the tied hash as an array element. For example: var=1 var=2 var=3will show up in a tied g 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: par=valueSYNOPSIS 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 g, 'Tie::Cfg', READ => "config.cfg", WRITE => "config.cfg", SEP => ':', COMMENT => '#'; my $counter=$cfg{"counter"}; $counter+=1; $cfg{"counter"}=$counter; $cfg{"counter"}+=1; untie g; ### 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 => ; # Do something here untie %paths; Requirements: · Perl Limitations: · This version breaks previous versions as the default mode is '.ini' mode.


Tie::Cfg Related Software