GetRc - A Module for reading configuration files
use GetRc; my $file = GetRc->new ("file_name"); $file->ifs('\s*:\s*'); my $result_get = $file->getrc(\%input); my $newfile = GetRc->new ("new_file_name"); $newfile->ofs(' = '); my $result_wri = $newfile->writerc(\%input); my $updatefile = GetRc->new ("update_file_name"); $updatefile->ifs('\s*:\s*'); $updatefile->ofs(' = '); my $result_upd = $upadtefile->updaterc(\%input);
This perl library provides reading, writing and updating configuration files which is outside your Perl script.
The current version of GetRc.pm is available at
http://rodney.alert.sk/perl/
To install this package, just change to the directory in which this file is found and type the following:
perl Makefile.PL make make test make install
This will copy GetRc.pm to your perl library directory for use by all perl scripts. You probably must be root to do this. Now you can load the GetRc routines in your Perl scripts with the line:
use GetRc;
0.23
$file = GetRc->new($filename);
This creates a new GetRC object, using $filename, where $filename specified (may be relative) path to filename.
For 'filename' looking in directories defined in $file->find_path. Default find_path contain:
### actual directory "./", ### home directory (getpwuid($>))[7]."/", ### program directory by File::Basename::fileparse() $dirname, ### parent directory "../", ### default config directory "/usr/local/etc/"
You may redefine find_path with push, pop, shift and unshift methods.
$retval = $file->getrc(\%input);
Fetch file content to %input.
$retval = $file->writerc(\%input);
Write %input to 'filename' each entry per line.
$file = GetRc->new("filename"); $retval = $file->updaterc(\%input);
Update specified file with %input. Get configuration fields from "filename", update by %input and write to "filename".
$file->ifs($ifs);
Definition Input Fields Separator. Default is used '\s*=\s*'. You may use regex in this piece.
$file->ofs($ofs);
Definition Output Fields Separator. Default is used ' = '. Don't use regex.
$file->configure ( ifs => '\s*=\s*', ofs => ' : ', debug => 1, find_path => $array_ref, .... ); $file->find_path->push("value"); $file->find_path->pop(); $file->find_path->unshift("value"); $file->find_path->shift(); my @PATH = $file->find_path();
Return codes from getrc,writerc,updaterc:
0 = if everything is O.K -1 = if 'filename' doesn't exist -2 = if can't read 'filename' -3 = if file lock timeouted -4 = if can't open file for writing
Return codes from other functions ( methods ? ) is value
#!/usr/bin/perl
$file = GetRc->new (".pinerc"); $file->configure( ifs => '\s*=\s*', );
$file->getrc(\%input);
foreach ( keys %input ) { print "$_ -> $input{$_}\n"; }
If $object->debug is set then print some debug informations on STDERR $object->debug(1); ## default is 0 - no debug info
Copyright 1997-2000 Jan 'Kozo' Vajda <Jan.Vajda@alert.sk>. All rights reserved. It may be used and modified freely, but I do request that this copyright notice remain attached to the file. You may modify this module as you wish, but if you redistribute a modified version, please attach a note listing the modifications you have made.
Address bug reports and comments to: Jan.Vajda@alert.sk
Thanks very much to:
for patience
for inspiration
for permanent discontent
for background noise
for some suggestions & solutions
for my perl book :-{))
for many suggestions and bug fixes.
IniConf , perl(1)
To install GetRc, copy and paste the appropriate command in to your terminal.
cpanm
cpanm GetRc
CPAN shell
perl -MCPAN -e shell install GetRc
For more information on module installation, please visit the detailed CPAN module installation guide.