Re: About config file

Andy Colson <[email protected]>
Newsgroups gmane.comp.apache.mod-perl
Message-ID <[email protected]>
On 3/6/2013 9:00 PM, Ken Peng wrote:
> Hello,
>
> How do you setup config file in modperl web development?
> I currently use the style like a package:
>
> package Myconfig;
>
> sub new {
>      my $class = shift;
>      my $option = { key1 => 'foo', key2 => 'bar', ... };
>      bless $option,$class;
> }
>
> 1;
>
>
> Then in the modperl program:
>
> use Myconfig;
>
> my $conf = Myconfig->new;
> my $opt1 = $conf->{key1};
> my $opt2 = $conf->{key2};
> ...
>
>
> I don't know if this is a good way. Do you have suggestions?
>
> Thanks.

Here's how I do it.


use Config::General qw(ParseConfig);
use constant CONFIG => '/pub/maps/maps.conf';
my $cfgstamp;  # when config timestamp changes, reload
reloadConfig();

sub reloadConfig
{
	my $x = (stat(CONFIG))[9];
	if ($x != $cfgstamp)
	{
		%config = ParseConfig( -ConfigFile => CONFIG);
		$cfgstamp = $x;
		my $h = hostname;
		#print STDERR "reloadConfig: host = $h\n";
		if (exists($config{$h}->{debug}))
		{
			$config{debug} = $config{$h}->{debug};
		}
		if (exists($config{$h}->{reload}))
		{
			$config{reload} = $config{$h}->{reload};
		}
		if (exists($config{$h}->{persistdb}))
		{
			$config{persistdb} = $config{$h}->{persistdb};
		}


		if ($config{persistdb})
		{
			opendb();
		}
	}
}


sub handler
{
	$web = AWeb->new(shift);
	if ($config{updating})
	{
		$web->content_type("text/plain");
		$web->print("The maps are being updated and should be back soon");
		reloadConfig();
		return Apache2::Const::OK;
	}
... etc, etc,

... then finally
	if ($config{reload})
	{
		reloadConfig();
	}
	if (not $config{persistdb})
	{
		closedb();
	}
	return Apache2::Const::OK;
}
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.