.Config file in perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting .Config file in perl
# 1  
Old 02-27-2012
.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.
# 2  
Old 02-27-2012
how your config file look like ?
# 3  
Old 02-27-2012
Code:
[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

# 4  
Old 02-27-2012
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?
# 5  
Old 02-27-2012
Code:
 
#!/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
# 6  
Old 02-27-2012
@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
Code:
$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
# 7  
Old 02-27-2012
IniConf

Hi,

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

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,
Code:
print $mline1;

output should be
Code:
Care and its affiliate

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

Cheers,
RangaSmilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read Data from Config file using Perl

Hi All, Can anyone please explain me how to read data from config file in Perl. Suppose i have a config file named cfile. The data in config file is name=parth lname=mittal user=2007 hostname=fluoride username=parthmittal password=XXXXXX account=unix url=www.unix.com ... (2 Replies)
Discussion started by: parthmittal2007
2 Replies

2. Shell Programming and Scripting

Read from config file and use it in perl program

Hi, I want to configure some values in config file like below work_dir /home/work csv_dir /home/csv sql_dir /home/sqls reportfirst yes and i want to store each value in variable to use it further in my my perl program ?? any thought on this(i am new to perl) ? ... (2 Replies)
Discussion started by: raghavendra.nsn
2 Replies

3. Shell Programming and Scripting

PERL on windows accessing variables from a config file

Folks, I'm a perl moron, so please speak very slowly. : ) I'm modifying a build script that starts up an apache server. Now there is a .config file that hardcodes an old webserver path like this c:\oldWebserver. Now I don't want that hardcoded value, rather wish to use an... (3 Replies)
Discussion started by: MarkoRocko
3 Replies

4. Shell Programming and Scripting

Shell script that will compare two config files and produce 2 outputs 1)actual config file 2)report

Hi I am new to shell scripting. There is a requirement to write a shell script to meet follwing needs.Prompt reply shall be highly appreciated. script that will compare two config files and produce 2 outputs - actual config file and a report indicating changes made. OS :Susi linux ver 10.3. ... (4 Replies)
Discussion started by: muraliinfy04
4 Replies

5. Shell Programming and Scripting

Parsing config-file (perl)

Hi, i'm trying to parse a config file that have alot of rows similar to this one: Example value value value What i want to do is to split and save the row above in a hash, like this: Example = value value value Basically i want to split on the first whitespace after the first... (3 Replies)
Discussion started by: mikemikemike
3 Replies

6. Shell Programming and Scripting

SOLVED: reading config file in a perl script

Hi! I have a need to do this in Perl. script.pl -config file The script would be doing a wget/LWP on a URL which is defined in the config file. So when I run the script it should return either one of these conditions - 1) OK with exit status 0. Should also print "wget URL" 2)... (6 Replies)
Discussion started by: jacki
6 Replies

7. Shell Programming and Scripting

parsing config file to create new config files

Hi, I want to use a config file as the base file and parse over the values of country and city parameters in the config file and generate separate config files as explained below. I will be using the config file as mentioned below: (config.txt) country:a,b city:1,2 type:b1... (1 Reply)
Discussion started by: clazzic
1 Replies

8. Shell Programming and Scripting

Perl config file Help

Hi In My config file i have 12.122.12.111:2000 A host and a port number. To read the config file i have written in a perl script like this my %config; open my $config, '<','config' or die "Failed to read file :$!"; Its running fine in HP-UX but showing error in solaris... (2 Replies)
Discussion started by: Harikrishna
2 Replies

9. Shell Programming and Scripting

Perl config file Help

HI I have a config file (ip.config)that has the following fields with colon as delimeter. yahoo.com:1919 ( host and port number) rediffmail.com:2020 gmail.com:2121 How to read it in a perl script. I need to parse the host and port number in the perl script using split... (6 Replies)
Discussion started by: Harikrishna
6 Replies

10. Shell Programming and Scripting

Perl config file Help

Hi can anyone help me understanding the following code my %config; open my $config, '<', 'config.txt' or die $!; while(<$config>) { chomp; (my $key, my @value) = split /=/, $_; $config{$key} = join '=', @value; } (5 Replies)
Discussion started by: Harikrishna
5 Replies
Login or Register to Ask a Question