where to store global database connection paramete

2020-07-24 03:51发布

问题:

Is there a convention for storing database connection parameters and other global settings in perl? similar to .NET's .config files?

Background: I've inherited a large perl-based application, which has a bunch of cgi scripts, and several background services, all of which have database hostnames, usernames and passwords hard-coded. I'd like to find a single, secure place to store these, and also to stick to perl conventions if possible. I've not much experience with perl, and google doesn't seem to lead me to a convention for this.

回答1:

This is what I've got so far:

Create a file called config.pl. Place it in a location accessible to all scripts, reducing permissions down the bare minimum required for all scripts to read it.

As per this guide on perlmonks:

Make the contents of config.pl a hash:

dbserver   => 'localhost',
db         => 'mydatabase',
user       => 'username',
password   => 'mysecretpassword',

Then in each script:

use strict;

# The old way....
#my $dbh = DBI->connect("DBI:mysql:database=mydatabase;host=localhost", "username", "mysecretpassword");

# The new way
my %config = do '/path/to/config.pl';    
my $dbh = DBI->connect("DBI:mysql:database=".$config{db}.";host=".$config{dbserver}."", $config{user}, $config{password});