Re: Where to put Settings-Variables?



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

.



Relevant Pages

  • Installation of software, and security. . .
    ... installation in Windows and various package managers. ... A setup.exe program coded by some third party such as Real Networks ... A .msi Microsoft Installer package is unpacked, and a script coded by ...
    (Bugtraq)
  • Re: goto &Package::func destroying @_?!
    ... >>script, but maybe the description will trigger something from someone.... ... code the 'Core' package uses another package as a base that has a custom ... package Foo; ... if it really is a problem with goto&. ...
    (comp.lang.perl.misc)
  • Tcl application deployment
    ... Tcl interpretator and any packages it uses. ... As far as I know, no OS provides way to load dynamic libraries using ... There is no way to fix bugs in some package used by application, ... all script files which come with application should be ...
    (comp.lang.tcl)
  • Re: ActiveX Script errors suppressed
    ... I've never actually tried to call GetExecutionErrorInfo in an ActiveX ... >etc. ActiveX Script only has the variant type. ... >change the variant to a true string. ... Dynamic Properties task instead in the main package. ...
    (microsoft.public.sqlserver.dts)
  • QUESTION: shared libraries
    ... how would I create a package easy-to-install for a new user? ... we could use a script to install the package. ... I have seen some scripts executing from a ... Gouvernement du Canada / Government of Canada ...
    (comp.os.linux.development.apps)