Where to put Settings-Variables?

B

Bernd Schneider

Hello!

I am developing a small web-based application (customer-frontend).

It used to be quite small so that I keeped everything in one file
(index.pl). In the head I defined all the variables etc. and that was it.

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?

Thanks in advance!
 
J

jussi.mononen-asdf

Bernd Schneider said:
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.

It is much better than using 'our'. You have them stashed
in their own namespace and thus reduced the possibility
to access them by accident.

You could create a configuration/settings object and
access configuration through method calls.

Avoid global parameters in larger programs as they make
the maintenance task really difficult.

/jUSSi
 
B

Bart Van der Donck

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)
 
M

Michael Zawrotny

Bernd Schneider wrote:

[ snip ]
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';
[ snip ]

I would put them in a config file and use one of the many config file
parsing modules on CPAN (e.g. Config::Simple).


Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,147
Latest member
CarenSchni
Top