2011-02-27 63 views
2

在perl中是否存在數據庫連接參數和其他全局設置的約定?類似於.NET的.config文件?在哪裏存儲全局數據庫連接參數

背景:我已經繼承了一個大型的基於perl的應用程序,它有一堆cgi腳本和幾個後臺服務,所有這些服務都具有硬編碼的數據庫主機名,用戶名和密碼。我希望找到一個安全的地方來存儲這些地方,並且如果可能的話,還要堅持perl慣例。我對perl沒有太多的經驗,而且谷歌似乎並沒有把我引向這個約定。

+1

看看配置''::命名空間在CPAN例如Config :: Multi,Config :: Settings,Config :: IniFiles等http://search.cpan.org/search?query=Config%3A%3A&mode=module – jfs 2011-02-27 22:07:02

回答

6

這是我到目前爲止有:

創建一個名爲config.pl。將其放置在所有腳本均可訪問的位置,從而降低所有腳本讀取它所需的最低限度的權限。

this guide on perlmonks

讓config.pl哈希的內容:

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