use DataBaseDriver; ... use constant CITIES => qw(lon par); use constant APPLICATIONS => qw(test prod); # build dispatch table for database handles my %db_handle; for my $city (CITIES) { for my $app (APPLICATIONS) { $db_handle{$city}{$app} = DataBaseDriver->new or die "'$city:$app' driver creation failed: ", DataBaseDriver->error_msg; } } ... # use database handle dispatch table my $msg = '...'; my $city = '...'; my $app = '...'; my $dbh = $db_handle{$city}{$app} or die "'$city:$app' driver access failed"; $dbh->store_to_database($msg); ...