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