Friday, March 22, 2013

Scripting with Perl

Perl is very good scripting language since it has lot of in-built modules to do lot of complex tasks that you would not be able to do with shell scripts. For example, if you want to insert data in to mysql data store or if you want to connect to a AMQP channel and get messages, you can use inbuilt modules in perl.

In this blog, I'm summarizing some very useful functionalities that I was able to do with perl.
  1. Read a config file and assign values to variables from the file 
  2. Connect to MySQL data store
  3. Execute a query in MySQL data store
  4. Creating a directory
  5. Go to working directory
  6. Replace a string in a file with another string
1. Read a config file and assign values to variables from the file

Suppose name of the configuration file I have is "config.ini". In here we use , Config::Simple perl module. If I have a parameter called "host" in my config file, below is how I read the config file and assign the value for host parameter to a defined variable in the perl script.

$cfg is a hash map. It will contain all the parameters that are defined in that config file.

2. Connect to MySQL data store

In here we use, DBI and DBD::mysql perl modules.        


In here, $host is where your MySQL server is running,  $userid is the database user and $passwd is the password for the given db user. Once you execute this, you will connect to the MySQL store that is running in the given host with given user name and password.


3. Execute a query in MySQL data store

Once you connect to the MySQL data store, you can execute queries.   

In here, $connection is the connection object that you get, when you connect to MySQL data store.


4. Creating a directory
To create a directory, you can use File::Path module of perl.
In here, it creates a directory called IUGateway and another directory called UBMoD inside IUGateway directory.

5. Go to working directory


Suppose you want to do a maven build by going to your source directory. Below is how you can jump to whatever directory that you wish.




6. Replace a string in a file with another string

There are lot of occasions where you want replace strings in a file with some other string. Here is a example how you can do it using perl.

If you have only single instance of that string, here is what you can use.     


Here $user_substr is the original string in the file. $http_running_user is the value that the original string will be replaced. When you replacing, make sure you don't have special characters such as ":", "/" inside that string. $httpd_conf_location is the location of the file. In perl you can run any command that you run in the command line inside "system".


If you have multiple occurrences of the same string and you want to replace all the occurrences, here is the command that you can use.     


All the above commands are extracted by googling. It took me lot of time since I'm very new to perl. I hope this post will be helpful for someone like me.

No comments: