sub foo { my $o = { q{debug} => 0, ## debug 0|1. q{db} => SQL_DB, ## sql db. q{host} => SQL_SERVER, ## sql server. q{port} => SQL_PORT, ## sql server port. q{user} => SQL_USER, ## sql user. q{pass} => SQL_PASS, ## sql user pass. @_ }; ## do your safety checks... my $dsn = q{DBI:mysql:database=} . $o->{'db'} . q{;host=} . $o->{'host'} . q{;port=} . $o->{'port'}; my $objSQL = eval { DBI->connect( $dsn, $o->{'user'}, $o->{'pass'}, { 'PrintError' => 0, 'RaiseError' => 0 } ); }; if ( ( $@ ) || ( ! defined $objSQL ) ) { ## maybe we only want to show the error if in debug?? print STDERR $DBI::errstr if ( $o->{'debug'} ); return undef; } DBI->install_driver( q{mysql} ); return $objSQL; }