where to store global database connection parameters

谁说我不能喝 提交于 2019-12-21 20:27:49

问题


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});


来源:https://stackoverflow.com/questions/5135966/where-to-store-global-database-connection-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!