Perl Basics
Introduction to Perl
FAQ's
CGI
Regular Expressions

PC Overview
Cool Stuff
My Modules
Success Stories
Links
Perl in the News
Logos

PC Internals
About
Contact
Handy Logos
What's new

Simple Configuration Processing

See also
P-friendly

[ home ] - [ search ] - [ sitemap ]


my $FILE_LOCATION = '/home/foo';

my $CONFIG = &getConfig( conf_file => 'your_app.conf' );

foreach (keys %$CONFIG) { print "X $_ = ", $CONFIG->{$_}, "<br>\n"; }

sub getConfig (%) {
	my %args = @_;
	my %CONFIG = ();

	open(CONFIG, "<$FILE_LOCATION/conf/$args{'conf_file'}")
		or print STDERR "No conf file in $FILE_LOCATION/conf $!";

	while (<CONFIG>) {
		next if /^\#/;
		chomp;
		next unless $_; # skip empty lines
		s/\=/___EQ___/; # replace first equal sign
		my ($key, $val) = split /___EQ___/;
		$key =~ s/[\t ]//g;
		# trim leading & trailing spaces
		$val = join " ", grep { $_ } split /[\t ]/, $val;
		$CONFIG{$key} = $val;
	}

	# close CONFIG;

	\%CONFIG;

}


Related Links

  1. perl/config-files.htm (Perl)
    http://www.infocopter.com/perl/config-files.htm
    CPAN Config::IniFiles

  2. perl/simple-config.htm (Perl)
    http://www.infocopter.com/perl/simple-config.htm
    Simple Configuration Processing

home - feedback - search

$Id: foo.htm,v 1.10 2004/03/11 17:50:01 reto Exp $
© 1998-2004 reto :)