Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using a config file with Perl program

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
What I would like to do is utilize a simple text file with some configuration parameters for a Perl program. Items like user name, password, server, IP Address, etc.

How do I utilize the information in the config file in my program?
 
you can do something like this

--config file--
somefile.config
name=john doe
address=5555 some road
city=new york
state=new york
zip=55555

--perl script--
#!/usr/bin/perl -w

use strict;

my %config;

open CONFIGFILE, 'somefile.config';
while (<CONFIGFILE>)
{
next if /^\s+$/;
my($key, $value) = split /=/;
$config{$key} = $value;
}
close CONFIGFILE;

and then all the config entries are in the has %config
 
So after yauncin's while loop, you could do this:

print &quot;Name: $config{'name'}\n&quot;;
print &quot;Address: $config{'address'}\n;

or whatever you want. I just wanted to illustrate how you would refer to the config variables read in from the config file - in case you're not familiar with Perl hashes ;-)

If you're *not* familiar with Perl hashes, you should definitely learn about them.

perldoc perldata

is Perl's documentation on Perl data types.

HTH. Hardy Merrill
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top