2

I have written a perl script where I will be connecting to database, for which I'm using this statement

my $dbh = DBI->connect(
    "DBI:mysql:database=test;host=localhost;mysql_socket=/var/run/mysqld/mysqld.sock",
    "root", "password", {'RaiseError' => 1});

As I don't want any information to be hardcoded, I want to use properties file where I can list the above details (e.g., database, host, mysql_socket) and read the details of properties file from the script. How can I write the properties file and read the details from perl script?

3

4 Answers 4

2

I had issues using perl-Config-Simple and decided to use Config::Properties instead. If you are experiencing the same, then you can try the following.

Make sure you have the Config::Properties installed. The following are several examples of how to install from command line, depending on the OS you are using, you'll want to use the appropriate choice:

  1. cpan Config::Properties
  2. cpan install Config::Properties
  3. yum install perl-Config-Properties

The code:

#!/usr/bin/perl
use strict;
use warnings;
use Config::Properties;

open my $cfh, '<', './foo.properties' or die "unable to open property file";
my $properties = Config::Properties->new();
$properties->load($cfh);
my $dbName = $properties->getProperty('database.name');
my $dbUser = $properties->getProperty('database.user');

The property file:

database.name=somedb
database.user=someuser

Once you have the values in the variables, put them into your connection string and you should be good to go.

1

There are a lot of CPAN modules that helps you to achieve this task. I like Config::Simple, for example:

#!/usr/bin/perl
use strict;
use warnings;
use Config::Simple;
...
my $cfg = new Config::Simple('myapp.ini');
my $user = $cfg->param('database.user');
my $connection_str = $cfg->param('database.connection');
#...

and the file myapp.ini:

[database]
connection="DBI:mysql:database=test;host=localhost;mysql_socket=/var/run/mysqld/mysqld.sock"
user=root
;...

You can install the module from the terminal/command prompt using:

cpan install Config::Simple

or

yum install perl-Config-Simple
7
  • Can't locate object method "new" via package "Config::Simple" (perhaps you forgot to load "Config::Simple"?)...I'm getting this error
    – Rudra
    Jun 10, 2013 at 11:40
  • @Vishu — So load Config::Simple. perldoc.perl.org/perlintro.html#Using-Perl-modules
    – Quentin
    Jun 10, 2013 at 11:46
  • @Quentin: I did this-use Config::Simple;
    – Rudra
    Jun 10, 2013 at 11:53
  • @Vishu Do you have it installed?
    – TLP
    Jun 10, 2013 at 12:10
  • @TLP I can't have it installed. Any other alternate solution
    – Rudra
    Jun 10, 2013 at 12:15
0
var temp;
if($ENV eq "Prod"){
#Prod Configurations
temp = "Prod";
}
else{
# Stage and Test Confgurations
temp = "Stage";
}
0

My brick and mortar solution, exports one value from a properties file:

#!/usr/bin/perl
use strict;
use warnings;

my $line;
foreach $line (<STDIN>) {
  chomp($line);
  if(my $match = $line =~ /^(.*)=(.*)$/){
    my $key = $1;
    my $value = $2;
    if ($ARGV[0] eq $key) {
      print "$value\n";
      exit 0;
    }
  }
}

Usage: perl script.pl mykey < file.properties

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.