Re: Where to put Settings-Variables?
- From: "Bart Van der Donck" <bart@xxxxxxxxxx>
- Date: 30 Mar 2006 06:29:10 -0800
Bernd Schneider wrote:
[...]
But now the application is getting a bit more complex with some scripts
being only run from the command line and having nothing to do with the
web-interface yet still sharing some common library.
So I decided to put some stuff into modules.
The problem that I now come up with is:
Where do I put the variables?
Before I just declared them as:
my $mysql_host = 'localhost';
Now I have several packages and they in turn use these variables as well.
I now use a self-written script which reads the variables from a
textfile. And I put all this into a Module called Settings.pm.
So my call to a setting would be:
$Settings::value{'key'}
e.g.
$Settings::value{'mysql_host'}
But I am not too sure if this is the right way.
Do you have any hints to to deal with these settings properly?
I think various approaches are possible, as long as the design is
secure, practical, maintainable, easily adaptable and CPU-friendly.
I'm using the following construction that I once made. Has always
worked fine for me, is easily expandable and keeps everything in one
hash.
Main script (script.pl):
#!/usr/bin/perl
use loadvars;
use strict;
use warnings;
use somemodule;
loadvars::init;
print "hash from main script:\n";
my ($aa, $ab);
print " $aa = $ab\n" while ($aa, $ab) = each %CONFIG;
somemodule::somesub;
Module to load vars (loadvars.pm):
package loadvars;
require Exporter;
@ISA = qw(Exporter);
@EXPORT = qw(%CONFIG);
@EXPORT_OK = qw(%CONFIG);
sub init {
%CONFIG =
(
host => 'localhost',
user => 'john',
pass => 'G5JKm5Kg',
);
}
1
Module invoked by main script (somemodule.pm):
package somemodule;
BEGIN { import loadvars }
sub somesub {
print "hash from somemodule:\n";
my ($ac, $ad);
print " $ac = $ad\n" while ($ac, $ad) = each %CONFIG;
}
1
If you're working in a typical UNIX/CGI environment, don't forget to
protect your directory from *.pm readouts from the web. If chmod can
not be used, you could do something like this (.htaccess directive):
<Files ~ "\.pm">
Order allow,deny
Deny from all
</Files>
(Obviously, this concern also applies to variables loaded from text
files)
--
Bart
.
- References:
- Where to put Settings-Variables?
- From: Bernd Schneider
- Where to put Settings-Variables?
- Prev by Date: Re: Arbitrarily Many Nested Loops
- Next by Date: Re: file renamer... request feedback
- Previous by thread: Re: Where to put Settings-Variables?
- Next by thread: Re: Where to put Settings-Variables?
- Index(es):
Relevant Pages
|