You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Ken Peng <yh...@orange.fr> on 2013/03/07 04:00:37 UTC
About config file
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.
Re: About config file
Posted by Andy Colson <an...@squeakycode.net>.
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;
}
Re: About config file
Posted by Randolf Richardson <ra...@modperl.pl>.
> On Thursday 07 March 2013 11:00:37 Ken Peng wrote:
> > Hello,
> >
> > How do you setup config file in modperl web development?
> > I currently use the style like a package:
> > ...
> > I don't know if this is a good way. Do you have suggestions?
>
> I am not an expert here, but I think it's acceptable way. YAML is another
> alternative. Here's my example for model call validation config:
> ---
> params:
> ip:
> regex: ^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$
> default: defaults.ip
It's important to note that IPv6 addresses are excluded by that
RegEx formula. I suggest either changing "ip" to "ip4" or adding
support for IPv6 addresses as well (which is not going to be anywhere
near as straight-forward as matching a basic IPv4 pattern).
[End of reply.]
> cookie:
> max-size: 40
> min-size: 4
> result:
> OK:
> redirect: /appIndex
> set-cookie:
> auth:
> value: TT response.auth
> secure: 1
> expires: +1d
> domain: .fr.iii.la
> call_method: model
> allowed_source:
> - submit # ajax, submit, template
> - ajax
>
> This config translates into Perl structure with hashes and arrays (Dumper
> output):
> $VAR1 = {
> 'params' => {
> 'ip' => {
> 'regex' => '^\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}$',
> 'default' => 'defaults.ip'
> },
> 'cookie' => {
> 'min-size' => 4,
> 'max-size' => 40
> }
> },
> 'call_method' => 'model',
> 'allowed_source' => [
> 'submit',
> 'ajax'
> ],
> 'result' => {
> 'OK' => {
> 'set-cookie' => {
> 'auth' => {
> 'domain' => '.fr.iii.la',
> 'value' => 'TT response.auth',
> 'secure' => 1,
> 'expires' => '+1d'
> }
> },
> 'redirect' => '/appIndex'
> }
> }
> };
> --
> Anton Petrusevich
Randolf Richardson - randolf@inter-corporate.com
Inter-Corporate Computer & Network Services, Inc.
Beautiful British Columbia, Canada
http://www.inter-corporate.com/
Re: About config file
Posted by Anton Petrusevich <ca...@casus.us>.
On Thursday 07 March 2013 11:00:37 Ken Peng wrote:
> Hello,
>
> How do you setup config file in modperl web development?
> I currently use the style like a package:
> ...
> I don't know if this is a good way. Do you have suggestions?
I am not an expert here, but I think it's acceptable way. YAML is another
alternative. Here's my example for model call validation config:
---
params:
ip:
regex: ^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$
default: defaults.ip
cookie:
max-size: 40
min-size: 4
result:
OK:
redirect: /appIndex
set-cookie:
auth:
value: TT response.auth
secure: 1
expires: +1d
domain: .fr.iii.la
call_method: model
allowed_source:
- submit # ajax, submit, template
- ajax
This config translates into Perl structure with hashes and arrays (Dumper
output):
$VAR1 = {
'params' => {
'ip' => {
'regex' => '^\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}$',
'default' => 'defaults.ip'
},
'cookie' => {
'min-size' => 4,
'max-size' => 40
}
},
'call_method' => 'model',
'allowed_source' => [
'submit',
'ajax'
],
'result' => {
'OK' => {
'set-cookie' => {
'auth' => {
'domain' => '.fr.iii.la',
'value' => 'TT response.auth',
'secure' => 1,
'expires' => '+1d'
}
},
'redirect' => '/appIndex'
}
}
};
--
Anton Petrusevich
Re: About config file
Posted by Adam Prime <ad...@utoronto.ca>.
On 03/06/2013 11:21 PM, Jon wrote:
>
> Is there any reason he can't use Config::Any?
> http://search.cpan.org/~bricas/Config-Any-0.23/lib/Config/Any.pm
> <http://search.cpan.org/%7Ebricas/Config-Any-0.23/lib/Config/Any.pm>
>
>
There's no reason why he can't use any Config package, or roll his own
using json, or yaml, or whatever format he prefers.
It could be as simple as this:
package MyConfig;
use Sub::Exporter -setup => { exports => [ qw(get_config) ] };
use YAML::Syck;
my $config;
sub get_config {
if (defined($config)) {
return $config
}
$config = LoadFile('/www/etc/lolol.com/site.yaml');
return $config
}
1;
This particular version will only load the file once, so if you change
the config you need to kick the webserver, but you can tailor this, or
any solution to suit whatever your needs are.
Adam
Re: About config file
Posted by Jon <th...@gmail.com>.
Is there any reason he can't use Config::Any?
http://search.cpan.org/~bricas/Config-Any-0.23/lib/Config/Any.pm
Then he can load his json structures directly (via .json file) or load
variables from a .pl file.
Maybe I've missed the scope of the problem, but op's example immediately
made me think he's trying to build config files as classes. While you could
use moose (Or mouse, or moo, etc.) And build your config options as
parameters, e.g.:
package My::Config
use Moose;
has 'param1' => (
is => 'ro',
isa => 'Str',
default => 'Bob',
);
Then later
package main;
my $cfg = My::Config->new;
do_stuff $cfg->param1, '12';
Or whatever method invocation would make use of those variables.
Config::Any effectively gives you an OO interface to your config file
without having to build the class by hand.
Admittedly, the last project I had that used mod_perl was a long time ago,
and I've just stayed subscribed to this list because I really enjoy most of
your discussions (and typically learn a thing or two) so this probably
isn't the most canonical "mod_perl" thinking...
I think it's a good solution because it provides a "simple" interface to
complex config files. But would really like to understand the flaws in my
thinking if you're going to shoot me down.
Thanks,
Jon A
On Mar 6, 2013 8:19 PM, "Ken Peng" <yh...@orange.fr> wrote:
> 于 2013-3-7 11:15, Jie Gao 写道:
>
>> You can follow this:
>>
>> http://perl.apache.org/docs/2.**0/devel/core/coding_style.html<http://perl.apache.org/docs/2.0/devel/core/coding_style.html>
>>
>> if you are after a standard coding style.
>>
>> Regards,
>>
>
>
> Thanks. but I don't think it's something about coding style.
>
>
Re: About config file
Posted by Ken Peng <yh...@orange.fr>.
于 2013-3-7 11:15, Jie Gao 写道:
> You can follow this:
>
> http://perl.apache.org/docs/2.0/devel/core/coding_style.html
>
> if you are after a standard coding style.
>
> Regards,
Thanks. but I don't think it's something about coding style.
Re: About config file
Posted by Jie Gao <J....@sydney.edu.au>.
Hi Ken
You can follow this:
http://perl.apache.org/docs/2.0/devel/core/coding_style.html
if you are after a standard coding style.
Regards,
Jie
* Ken Peng <yh...@orange.fr> wrote:
> Date: Thu, 7 Mar 2013 11:00:37 +0800
> From: Ken Peng <yh...@orange.fr>
> To: modperl@perl.apache.org
> Subject: About config file
> User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:17.0) Gecko/20130215
> Thunderbird/17.0.3
>
> 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.
Re: About config file
Posted by Ken Peng <yh...@orange.fr>.
于 2013-3-7 11:07, Eduardo Arino de la Rubia 写道:
> For relatively simple needs, check this out:
>
> http://modperlbook.org/html/4-2-11-PerlSetVar-and-PerlAddVar.html
>
> Cheers!
Thanks. But my config file is a complicated data stru, it's a
multi-level hash, more likely the JSON.
Re: About config file
Posted by Eduardo Arino de la Rubia <ea...@gmail.com>.
Greetings!
For relatively simple needs, check this out:
http://modperlbook.org/html/4-2-11-PerlSetVar-and-PerlAddVar.html
Cheers!
On Wed, Mar 6, 2013 at 7:00 PM, Ken Peng <yh...@orange.fr> 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.
>
--
int getRandomNumber() {
return 4; // Chosen by dice throw.
// guaranteed to be random...
}