How to write a perl script which reads the configuration file in the mentioned format

Hi,
I need a perl script which reads the configuration file in the following format

config.ini

[DEV]
<Key1>=<Value1>
<Key2>=<Value2>
....

[QA]
<Key1>=<Value1>
<Key2>=<Value2>
.....

[PRD>
<Key1>=<Value1>
<Key2=<Value2>
....

DEV,QA or PRD and config file path and name would be passed as an arguments to this perl script. The script should read that particular section of the Key, Value pairs and store them, so that I can use these in rest of my perl script.
Please provide me with the perl code... preferably without using any of the existing modules. This perl script will be run on SunOs unix operating system.

Thanks for the help and I appreciate the same
gravindra1977Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Adam314Commented:
The Config::IniFiles might be easier, but not using any modules, here is some code.  All of the parameters are stored in the %config hash.
#Get passed parameters
my $Filename = shift;
my $Section = shift;
die "Usage: $0 <Filename> <Section>\n" unless ($Filename and $Section);
 
#Read config file
my %config;
my $CurSection;
open(IN,"<$Filename") or die "Could not open $Filename: $!\n";
while(<IN>) {
	if(/\[(.*)\]/) {$CurSection=$1;}
	elsif(/(.*?)=(.*)/ and ($CurSection eq $Section)) {$config{$1}=$2;}
}
close(IN);

Open in new window

0
FishMongerCommented:
Why don't you want to use a module that is designed for this purpose?

Please take a look at the Config::INI::Simple module and if it doesn't suit your needs, we can come up with other options.
http://search.cpan.org/~kirsle/Config-INI-Simple-0.01/lib/Config/INI/Simple.pm
0
gravindra1977Author Commented:
Adam,
I tried your logic and it works fine. But in my config file I have something like this

REPORT_PATH=$BASE_DIR/Reports

I have defined $BASE_DIR in my perl script as a some directory.[/home/gravi].  When I read the config values from the configuration file , this $BASE_DIR does not get evaluated to the appropriate value. i.e., the value should /home/gravi/Reports where as value is stored as $BASE_DIR/Reports and hence the perl script fails. How can I acheive this?
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

FishMongerCommented:
REPORT_PATH=$BASE_DIR/Reports

Are all of your <Key>=<Value> pairs configured in that manor?
0
FishMongerCommented:
Also, is it consistent in each section, is

REPORT_PATH=$BASE_DIR/Reports

in each of the sections?
0
FishMongerCommented:
See if this adjustment to Adams code does what you need.

use Data::Dumper;
my %paths = (
                        '$BASE_DIR' => '/home/gravi',
                        '$some_var' =>  'some_value'
                       );

while(<IN>) {
    if(/\[(.*)\]/) {$CurSection=$1;}
    elsif( m!(.*?)=([^/]+)/(.*)! and ($CurSection eq $Section) ) {
       $config{$1}="$paths{$2}/$3";
    }
}
print Dumper \%config;
0
Adam314Commented:
Here is another way to substitute variables:

my %paths = (
    'BASE_DIR' => '/home/gravi',
    'some_var' =>  'some_value'
    );
 
my %config;
my $CurSection;
while(<DATA>) {
	if(/\[(.*)\]/) {$CurSection=$1;}
	elsif((my ($key, $val) = m!(.*?)=(.*)!) and ($CurSection eq $Section) ) {
		$config{$key}=$val;
		$config{$key} =~ s/\$([\w_]+)/$paths{$1}/g;
	}
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.