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;
}