What are the test automation tools that use perl as the scripting language??

Perl (programming language): What is the best way to store common database connection parameters for use with DBI?

  • I am re-factoring a code base that includes a number of Perl scripts that are executed on the linux command line (not a web app). They use DBI to manipulate data in MySQL and MSSQL and all the connection params (host, user, password, db name, etc) are hard-coded. It's time to start storing the connection parameters in some sort of shared library. This could be as simple as a module that defines hashes with db parameters, then including it in each script via 'use'. The new module could also wrap DBI functionality, although the priority is simply to remove the hard-coding. Other possibilities include storing the parameters in JSON, YAML, or Config::IniFiles (standard .ini config files). I'm wondering if anyone has some suggestions on best practices or existing modules for this. Thanks

  • Answer:

    You might want to consider using DBIx::NamedDSN in this case. This allows you to maintain complete set of database names, hosts, usernames and passwords in a separate config file. Once you have the config file ready, just set the path in your perl applications(scripts/packages) and use DBIx::NamedDSN's 'connect'ion method for connection to any database. Remember, DBIx::NamedDSN returns a regular DBI db handle.

Daya Sagar Nune at Quora Visit the source

Was this solution helpful to you?

Other answers

I suggest storing configuration data like this in a human-readable text file. JSON, YAML, XML and ini files are all fine, just choose a format that you're comfortable with, document it and stick with it. A useful practise is to have different configuration files for each environment/host and then create a softlink to the appropriate file from a generic one, e.g. link config.yaml to config-production-myhost.yaml.

Andy Murdoch

We store all credentials essentially as you first suggest, in a module, and we do wrap DBI functionality into that module (we have some added functions that we use for some shortcut queries, like getDBSimpleValue, getDBArrayValues, getDBHashValue).  We have specific names for different connections, and so one just uses the module and calls my $dbh = &dbConnect($connectionName); to connect. I haven't read any "best practice" guide to doing this, but after trying a number of different options (many of which you noted), we went to this and have been happy with the choice.

Joe Emison

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.