For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > March 2006 > Where to put Settings-Variables?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Where to put Settings-Variables?
Bernd Schneider

2006-03-30, 3:59 am

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!
jussi.mononen-asdf@asdf-comptel.com

2006-03-30, 3:59 am

Bernd Schneider <bernd_schneid@web.de> wrote:
>
> 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

--
remove -asdf and asdf- for email address

Bart Van der Donck

2006-03-30, 7:00 pm

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

Michael Zawrotny

2006-03-30, 7:00 pm

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

--
Michael Zawrotny
Institute of Molecular Biophysics
Florida State University | email: zawrotny@sb.fsu.edu
Tallahassee, FL 32306-4380 | phone: (850) 644-0069
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com