.Config file in perl

Hi can anybody help me in how to read .config file in Perl, which module is used, and some help in coding.

how your config file look like ?

[head]
line1=Care and its affiliate
line2=WNB00901,Total number of transactions
line3=Quarterly Report Period Dates:
line4=Description of Service Type,Approved  transactions,Denied transactions    

[output]
file=Report
debug=1
destdir=.
name=aaa
auth=bbb
host=N0123451

[log]
file=Rpt_log

[archive]
file=CommRpt

[parms]
empgroup=0123,4567
um03list=AB,CD,EF,GH,IJ,KL
  1. What do you want to do to this config file?
  2. What's your desired output? Please clarify and be specific about your requirements. To "read" a config file you don't need special modules. Open a file handle and read each line in a loop.
  3. And is this a continuation of your previous thread?
 
#!/bin/perl
open FILE, "config.txt" or die $!;
while (<FILE>) {
    chomp;                  # no newline
    s/#.*//;                # no comments
    s/^\s+//;               # no leading white
    s/\s+$//;               # no trailing white
    next unless length;     # anything left?
    my ($var, $value) = split(/\s*=\s*/, $_, 2);
    $User_Preferences{$var} = $value;
}
close FILE;
print $User_Preferences{"line1"} . "\n";
print $User_Preferences{"line2"} . "\n";
print $User_Preferences{"line3"} . "\n";
print $User_Preferences{"line4"} . "\n";
print $User_Preferences{"file"} . "\n";
print $User_Preferences{"debug"} . "\n";
print $User_Preferences{"host"} . "\n";     

Recipe 8.16. Reading Configuration Files

@balajesuri
I have the configuration file, i want to read it by making objects like suppose i have a object named obj
so if i write

$line1 = $obj->get('head.line1');

The output should be Care and its affilate

---------- Post updated at 04:33 AM ---------- Previous update was at 04:28 AM ----------

$obj = ?->new()
$obj->read("Configuration file path")

? denotes which module should be used
So if i write
$line2 = $obj->get('log.file');I should get the output as Rpt_log

Hope you might clear now

Hi,

If you use IniConf module in perl, the below are the sample code.

my $mCfg = IniConf->new( -file => $gIniFile );
 
my $mSection = "head";
my @mParms = $mCfg->Parameters( $mSection );
foreach my $mItem ( @mParms )
{
     (my $tmpS = $mCfg->val( $mSection, $mItem )) =~ s/^\s+|\s+$)//g;
      eval '$m' . "$mItem='$tmpS'";
}
 

consider with you input file, you have one section in the ini file called head and have four parameter(line1,line2,line3 and line4).
Here we are trying to create runtime variable with the parameter name and prefix with 'm'
you have your values in the below variables,

  1. mline1
  2. mline2
  3. mline3
  4. mline4
    i have added the prefix 'm' that denotes my variable. you have to declare all the variables when you run with strict.

If you print the mline1 variable like below,

print $mline1;

output should be

Care and its affiliate

this is the value you have configured under head section for line1 parameter in config file.

Cheers,
Ranga:)

Any other method if possible

Hi,

what's your requirement exactly? unless we know your requirement exactly, we are unble to give solution which what you are expecting.

Any way!!!

Here is an another way, Go through the below link.

http://search.cpan.org/~tlinden/Config-General-2.50/General.pm

Cheers,
Ranga:)

Hi all
I developed the code

use Config::Simple;
  
$config = new Config::Simple();
$config->read('new.cfg'); #read the confg file

$line1 = $config->param("head.line1");
$line3 = $config->param(head.line3);
$outfile = $
config->param("parms.empgroup");

print  "$line1 \n $line3 \n $outfile \n";

For the $line1 and $line2 i get the correct result but for the
$outfile i get the reffrence of the array. The output i get is as follow

Care and its affiliate
Quarterly Report Period Dates:
ARRAY(0x1c0cb5c)

But for the array reference i should get
0123,4567

Please help me about this matter.

---------- Post updated 02-28-12 at 12:16 AM ---------- Previous update was 02-27-12 at 02:05 PM ----------

Means to say the the expected output should be like this
Care and its affiliate
Quarterly Report Period Dates:
0123,4567

Please add the above code and let me know the output and also use some other parameter name.