2.3 Running Perl DBIWould this book be complete without a "Hello World" example? Of course not, so here goes! Our very simple Perl DBI script (in Example 2-1) will simply connect to the orcl Oracle database as the scott user, run through a straightforward SQL cursor on the DUAL table via a prepared statement, and then print out the result before logging off. We'll run the same script on both Unix and Win32 to demonstrate Perl's operating system independence: Example 2-1. Our first Perl DBI script, HelloWorld.pl#!perl -w use strict; use DBI; # Connect to Oracle database, making sure AutoCommit is # turned off and potential errors are raised. my $dbh = DBI->connect( 'dbi:Oracle:orcl', 'scott', 'tiger', { RaiseError => 1, AutoCommit => 0 } ); # Create the SQL. my $sql = qq{ SELECT 'Hello World' FROM DUAL }; # Prepare the SQL and execute. my $sth = $dbh->prepare( $sql ); $sth->execute( ); # Fetch output rows into array, plus prepare a # print formatter for the results. while ( my($helloWorldString) = $sth->fetchrow_array) { # Print out the result. print $helloWorldString, "\n"; } $dbh->disconnect( ); # Disconnect Example 2-1 may look a little scary, but after reading Appendix B, you'll quickly be able to reduce it to the following: #!perl -w use strict; use DBI; my $dbh = DBI->connect( 'dbi:Oracle:orcl', 'scott', 'tiger', { RaiseError => 1, AutoCommit => 0 } ); print $dbh->selectrow_array(qq{ SELECT 'Hello World' FROM DUAL }); $dbh->disconnect( ); Note the following about Example 2-1:
Before we run this script under either Unix or Win32, we need to do the following:
We should now be ready to run the HelloWorld.pl Perl script under both Unix and Win32. 2.3.1 Running a Perl Script on UnixFollow these steps to run a Perl script on a Unix system:
2.3.2 Running a Perl Script on Win32Follow these steps to run a Perl script on a Win32 system:
Now we can break out the bubbly! 2.3.3 DBI by ProxyOne of DBD::Oracle's major limitations is its reliance on the presence of at least Oracle client libraries for successful compilation. Indeed, here's what the Version 1.12 DBD::Oracle README file has to say:
Figure 2-3. HelloWorld.pl running under Win32 and UnixIf you have an Oracle server but no client machines possessing on-board Oracle software, this is a problem. It's also a problem if you have a client firewall that DBD::Oracle fails to break through. Fear not, for there is a potential solution at hand — Jochen Wiedmann's amazing DBI::ProxyServer and DBD::Proxy module set, which comes automatically within the DBI tarball. For DBI 1.20, you can read about both modules here:
(Try searching on http://search.cpan.org if these version-specific documents have been superseded.) The idea is to set up a proxy server daemon, dbiproxy, that runs on your Oracle server machine. On your remote clients, you use DBD::Proxy instead of DBD::Oracle. This module connects across the network to the dbiproxy daemon, which passes through the SQL requests to a server-configured DBD::Oracle driver, thereby allowing proxy access to the Oracle database. This setup is displayed in Figure 2-4. In order to use the ProxyServer system over a network, we need to install several Perl packages:
You can find these at http://www.cpan.org/authors/id/JWIED. Figure 2-4. DBI:ProxyServer and DBD::Proxy architecture |