in reply to Seeking efficient and safe way to store database connection information
I usually just write a little MyDB module that has one method: connect. Then any script that wants to use the database just does:
use MyDB; my $dbh = MyDB::connect();
This method will also let you add connection pooling later without changing any code other than the MyDB module.
Here's a sample MyDB module with a connect method that lets you override any of the default connect parameters (note: I just rewrote a good chunk of this as I posted it, and haven't tested it, so...):
package MyDB; use DBI; my %Default = ( database => 'mydatabase', driver => 'mysql', hostname => 'localhost', port => 3306, username => 'someuser', password => 'topsecret', options => { AutoCommit => 1, RaiseError => 0, } ); sub connect { my %params = @_; my $connect_str=sprintf("dbi:%s:database=%s;hostname=%s;port=%s", $params{driver} || $Default{driver}, $params{database} || $Default{database}, $params{hostname} || $Default{hostname}, $params{port} || $Default{port}); return DBI->connect($connect_str, $params{username} || $Default{username}, $params{password} || $Default{password}, $params{options} || $Default{options} ); } 1;
Brad
|
|---|