Forum Moderators: coopster & phranque

Message Too Old, No Replies

perl query

         

bigshow

1:20 pm on Aug 30, 2005 (gmt 0)

10+ Year Member



I'm a novice, what’s the easiest way to do the following in perl?

I have a configuration file called setup.ini, which contains something like this:
Val1=abc
Val2=def
Val3=ghi

In perl, how would I read in the ini file and assign the values?

A working example would be good.

thanks

ChadSEO

4:33 pm on Aug 30, 2005 (gmt 0)

10+ Year Member



Bigshow,

I would probably do something like this:

open(IN,"input") or die "Couldn't open input: $!\n";
while(<IN>) {
chomp;
/^([^=]+)=(.*)$/;
$config{$1} = $2;
}
close(IN);

You then have a hash called %config populated with all your key/value pairs. So $config{'Val1'} = 'abc' in your example.

Chad

bennymack

5:14 pm on Aug 30, 2005 (gmt 0)

10+ Year Member



Config::Tiny

KevinADC

5:24 pm on Aug 30, 2005 (gmt 0)

10+ Year Member



Config::Tiny is a great suggestion. There is also Storable, ( [perldoc.perl.org...] ) which is a core perl module and should already be installed. Both are very easy to use.

lexipixel

1:56 pm on Aug 31, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




I have a configuration file called setup.ini, which contains something like this:

Val1=abc
Val2=def
Val3=ghi

In perl, how would I read in the ini file and assign the values?

You say "something like this"... If you can use standard Perl syntax in your ini file, ie-


#
Val1 = 'abc';
Val2 = 'def';
Val3 = 'ghi';
#

Then you could just use require in your main program...


#
require 'setup.ini';
#

(P.S. - don't forget to CHMOD the ini file to 755 or it won't work).