如何避免每个perl文件中的数据库连接信息

时间:2013-10-04 07:31:42

标签: database perl

我在我的web项目中使用了多个perl脚本,我通过jquery ajax调用调用它。每个perl脚本都包含mysql数据库连接信息(user_name,密码,数据库名称)。

有没有更好的方法,这样我就可以避免每个perl文件中的数据库连接信息,并且可以全局存储,而不会出现任何安全问题。

2 个答案:

答案 0 :(得分:2)

config.pl

{ 
  # sql credentials
  user => "sqluser", pass => "123", dsn => "mysql..",
}

index.pl和其他脚本:

my $CFG = do "config.pl";
print "$CFG->{user} $CFG->{pass} ...\n";

答案 1 :(得分:2)

更多perlish选项:

Config.pm

package Config;

sub get_config {
  return { 
    # sql credentials
    user => "sqluser", pass => "123", dsn => "mysql..",
  };
}
1; #return true from modules

index.pl和其他脚本:

#make sure the current folder is in the lib path
#(if you have all in the same folder).
use FindBin; 
use lib $FindBin::Bin;

require Config;
my $cfg = Config->get_config();
print "$cfg->{user} $cfg->{pass} ...\n";

但我猜你得到了这个概念和TMTOWTDI。