next up previous contents
Next: SQL*Loader Up: Oracle Programming Previous: The C Interface: OCI   Contents

Regarding PERL

If you poke your head into $ORACLE_HOME you'll notice a PERL directory. If you dig around you'll find that you've got a pretty decent PERL setup at your fingertips. In 10g (10.1.0) you've got PERL 5.6.1, plus a wide variety of applicable modules including: mod_perl, URI, Apache, LWP, RPC, and more. Of possibly more interest are the inclusion of the OraPERL module and DBI with the DBD for Oracle! Proof that Oracle doesn't hate UNIX admins. A quick look at CPAN, however, will tell you that the OraPERL module is intended only for compatability with PERL 4 apps and "any new development should use DBI directly."

The included version of the Oracle DBD is 1.12, in Oracle 10.1.0. Its increadably easy to use and most SAs will probly find the DBD interface far more "homey" than PL/SQL.

To use the Oracle distribution of PERL you'll need to modify 2 enviromental variables. Firstly you'll need to add the Oracle library directory to LD_LIBRARY_PATH if you don't have it included in the run time linkers configuration (ldconfig in Linux, crle in Solaris). Secondly you'll need to put the PERL module directories in your PERL5LIB variable so that they are included in INC. If you fail to add these your likely to get a slew of errors.

$ export LD_LIBRARY_PATH=/u01/app/oracle/product/10.1.0/db_1/lib32
$ export PERL5LIB=/u01/app/oracle/product/10.1.0/db_1/perl/lib/site_\

If you do, however, forget to set LD_LIBRARY_PATH you'll notice interestingly that the Oracle DBD uses the OCI.

Once you've got things setup, you can use PERL and the DBI like you'd expect. If your new to the DBI I'd strongly suggest picking up the excellent book Programming the PERL DBI from O'Reilly. (Insidently, Tim Bunce who co-wrote Programming the PERL DBI is also the author of the Oracle DBI.)

Here's a simple example of using the PERL DBI provided with Oracle10g:

# Example PERL DBI/DBD Oracle Example on Oracle 10g

use DBI;

my $dbname = "testdb";	## DB Name from tnsnames.ora
my $user = "ben";
my $passwd = "passwd";

#### Connect to the database and return a database handle
$dbh = DBI->connect("dbi:Oracle:${dbname}", $user, $passwd);

        print("Connected as user $user\n");
} else {
        print("Failed to connect!\n");
#### Prepare and Execute a SQL Statement Handle
my $sth = $dbh->prepare("SELECT owner,table_name,num_rows FROM all_tables");


print("All tables - Got rows:\n");
while(@row = $sth->fetchrow_array()){
print("Select Done!...");
#### Disconnect
} else {
        print("Failed to disconnect\n");

In the above script we're grabbing 3 columns from the Data Dictionary's ALL_TABLES system table. We connect, grab the rows and output them, and then disconnect from the database when we're done.

If you have trouble connecting to the database, remember that you need to connect through the listener (you can connect locally, but it's pretty figity) and ensure that you can properly tnsping the database before freaking out about your script.

The output looks like this (several rows removed for clarity):

bash-2.05$ ./ 
Connected as user ben
All tables - Got rows:
Owner   TableName       NumRows
-----   ---------       -------
SYS     DUAL    1
SYS     PLAN_TABLE$     
Select Done!...Disconnected

For more information on usign the DBI please refer to CPAN and/or Programming the PERL DBI.

next up previous contents
Next: SQL*Loader Up: Oracle Programming Previous: The C Interface: OCI   Contents