Getting Your Perl Install Ready for Oracle Database

On one of my old CD-ROM drives, I found a old piece of Perl code that I wrote for Oracle 8i. Thought I could enhance that to work with Oracle 10g and perhaps enhance it. My test server is an RHEL AS4 box and I did install Perl when I installed the OS. My script failed due to missing DBI and Oracle DBD modules. I checked for their existence to confirm the same. It took little a googling before I got it working, therefore, I decided to write a little notes here.

Here is a simple way to check if the DBI and Oracle DBD Perl modules are installed:

If you know the module name, at the command prompt:

> perl -e ‘use DBI’
> perl -e ‘use DBD::Oracle’

If the above return no error messages and return the command prompt you, the modules exist. If you see a message,  some thing like: “Can’t locate DBD/Oraccle.pm in @INC (@INC contains:……..lot of paths here……) at -e line 1. BEGIN failed–compilation aborted at -e line 1.” Then you missing those modules and you need to install them.

If you do not know the module name, copy paste the below at command prompt:

> perl -MFile::Find=find -MFile::Spec::Functions -Tlwe ‘find { wanted => sub { print canonpath $_ if /\.pm\z/ }, no_chdir => 1 }, @INC’ | grep DBI | more

Note: Manipulate the blue highlighted portion above to find other modules

Install The DBI Module

To install missing Perl modules (DBI first and then DBD::Oracle), you would need the root access. You can download the modules directly from CPAN, unzip, untar, read the README file in the module directory very carefully and then do the below (The below example shows how to install DBI module, you can extend that to install DBD::Oracle module):

>perl Makefile.PL

>make

>make test

>make install

(I am not posting the output of any of the above commands, if you read the output, you will know what it means and what you need to do)

Another way to install is, using the CAPN shell. At the command prompt, type the below:

> cpan

cpan> install DBI

The above command will install the latest version of DBI, sit back and watch the magic!

(Note: If you are using the cpan for the first time, it will prompt you to setup the CPAN environment. On most installations your can get away by hitting return key for all options/questions, it will assume defaults. Make sure to select proper continent, country and list of sites to download the Perl modules from. Now you will see the cpan prompt like below)

Before You Install DBD::Oracle Module

Apart from installing the DBI module before you install the DBD::Oracle, you must make sure that you are able to connect to the database. I would certainly test the connectivity with Sql*Plus, with a working schema/password from the OS user (root in my case). My root user does not have the Oracle specific environment variables set, so I had to set the following appropriately:

ORACLE_HOME, ORACLE_SID, TWO_TASK, PATH, LD_LIBRARY_PATH and most importantly ORACLE_USERID. ORACLE_USERID should be set to a correct username and password pair, that is active and unlocked in the database.

Example:

> export ORACLE_USERID=scott/tiger

Do make sure you have the latest version of Test::Builder module. While installing the DBD::Oracle module, if you see a bunch of error messages with “Test returned status 9 (wstat 2304, 0×900)” in them; it means your Perl install is missing the latest version of Test::Builder module. This is some thing I witnessed while installing the latest (version 1.23) DBD::Oracle module. My existing Test::Builder module was the culprit and the error messages vanished after I upgraded it.

Install the DBD::Oracle Module

The below will install the Oracle DBD module.

cpan> install DBD::Oracle

You can always download the module from CPAN and manually install as described in the Install the DBI Module section

Sample Script To Test Your Installation

#!/usr/bin/perl
#Sample script to test Perl connectivity to Oracle Database
#http://www.Oracledbahelp.com
use strict;
use DBI;
my $dbh = DBI->connect( ‘dbi:Oracle:orcl’, ‘hr’, ‘tiger’, { RaiseError => 1, AutoCommit => 0 } );
my $query = qq{ SELECT sysdate FROM DUAL };
my $outpt = $dbh->prepare( $query );
$outpt->execute();
while ( my($systemdate) = $outpt->fetchrow_array) {
print $systemdate, “\n”;
}
$dbh->disconnect();

3 Responses to “Getting Your Perl Install Ready for Oracle Database”

  1. VitalikGromovss says:

    Hey very nice blog!! Man .. Beautiful .. Amazing .. I will bookmark your blog and take the feeds also…

Leave a Reply

Dansette