Creating configuration files with perl

Code:

#!/usr/bin/perl
use Config::Simple;
$cfg = new Config::Simple(syntax=>'ini');
$cfg->param('auth.user', 'sherzodR');
$cfg->param('auth.pass', 'mypass');
$cfg->write("new.cfg");

File structure:

#cat new.cfg
; Config::Simple 4.58
; Tue Jul  7 11:20:58 2015

[auth]
pass=mypass
user=sherzodR

Reading the values:

#!/usr/bin/perl
use Config::Simple;
$cfg = new Config::Simple();
$cfg->read('new.cfg');
$user = $cfg->param("auth.user");
$pass=$cfg->param("auth.pass");
printf ("Username:%s  Password:%s\n", $user,$pass);

On running:

 #./testconfig.pl
Username:sherzodR  Password:mypass

Reading and then updating the values:

use Config::Simple;
$cfg = new Config::Simple();
$cfg->read('new.cfg');
$user = $cfg->param("auth.user");
$pass=$cfg->param("auth.pass");
printf ("Username:%s  Password:%s\n", $user,$pass);


$cfg->param('auth.user', 'newuser');
$cfg->write("new.cfg");

On running:

#./testconfig.pl
Username:sherzodR  Password:mypass
[[email protected]] ~/scripts #./testconfig.pl
Username:newuser  Password:mypass

You are reading this post on Joel G Mathew’s tech blog. Joel's personal blog is the Eyrie, hosted here.