***** infoCopter.com/perl *****

Simple Configuration Processing



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

© reto :)