问题描述
是否有在perl中存储数据库连接参数和其他全局设置的约定?类似.NET的.config文件?
Is there a convention for storing database connection parameters and other global settings in perl? similar to .NET's .config files?
背景:我继承了一个大型的基于perl的应用程序,它有一堆cgi脚本和几个后台服务其中有数据库主机名,用户名和密码硬编码。我想找一个单一的,安全的地方来存储这些,并且如果可能的话,坚持perl惯例。我没有太多的perl经验,google似乎没有导致我这样的约定。
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.
推荐答案
是我到目前为止:
创建一个名为 config.pl
的文件。将其放置在所有脚本可访问的位置,将权限降低到所有脚本读取它所需的最低要求。
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.
根据:
将config.pl的内容哈希:
Make the contents of config.pl a hash:
dbserver => 'localhost',
db => 'mydatabase',
user => 'username',
password => 'mysecretpassword',
然后在每个脚本中:
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});
这篇关于在哪里存储全局数据库连接参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!