• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 910
  • Last Modified:

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
0
gravindra1977
Asked:
gravindra1977
  • 4
  • 2
1 Solution
 
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
Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

 
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

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now