in reply to How to pass hash site variables into sql connection using perl?
use strict; use warnings; use Getopt::Long; use DBI; GetOptions("site=s" => \my $site) or die "Error in command line arguments\n"; defined $site or die "usage: $0 site_name\n"; my %site_map = ( site1 => { host => 'host1', db=>'db1', user=>'user1', pw=>'pw1' }, site2 => { host => 'host2', db=>'db2', user=>'user2', pw=>'pw2' }, site3 => { host => 'host3', db=>'db3', user=>'user3', pw=>'pw3' }, ); my $dbh = dbconnect($site); sub dbconnect { my $s = shift; die "bad site $s" unless $site_map{$s}; my $dsn = "DBI:mysql:database=$site_map{$s}{db};host=$site_map{$s} +{host}"; my $dbh = DBI->connect($dsn, $site_map{$s}{user}, $site_map{$s}{pw +}) or die "ERROR: can't connect to database server site $s"; return $dbh; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How to pass hash site variables into sql connection using perl?
by finddata (Sexton) on Mar 30, 2017 at 06:03 UTC | |
by huck (Prior) on Mar 30, 2017 at 06:25 UTC | |
by poj (Abbot) on Mar 30, 2017 at 06:21 UTC | |
| A reply falls below the community's threshold of quality. You may see it by logging in. |