Last Update: 05 February 2009
Product: StarSQL
Version: 5.x
Article ID: SQV00SQ013
Perl, or Practical Extraction and Reporting Language, is an open source programming language. It is often used to create CGI programs and other web scripts, and is particularly useful for Web forms. StarSQL can be used with Perl, DBI (database interface module for Perl), and the DBD::ODBC module to access DB2 databases.
This document explains how to set up and use StarSQL and unixODBC with Perl on UNIX platforms. The same concepts should apply to using Perl on Windows. This document also provides instructions on how to create and run a CGI application written in Perl on an Apache HTTP Server.
Run all UNIX commands as root user.
Most UNIX distributions include Perl, and you can run the following command to verify that it is installed:
# perl -v
If Perl is not installed, obtain it from either the UNIX distribution media, your UNIX vendor, or the Comprehensive Perl Archive Network, http://www.cpan.org (browse the binary distributions). You may also download ActivePerl from http://www.activestate.com.
Some UNIX distributions supply DBI and DBD::ODBC. However, in many cases you will need to build and install DBI and DBD::ODBC from source, which may be downloaded from http://www.cpan.org. To determine whether DBI and DBD::ODBC are installed, run the following commands:
# perl -e 'use DBI;'
# perl -e 'use DBD::ODBC;'
# perl -MDBD::ODBC -e 'print %DBD::ODBC::VERSION;'If DBI and DBD::ODBC are not installed, follow the procedures below to build both modules.
The 32-bit version of StarSQL should only be used with the 32-bit versions of perl, DBI, and DBD::ODBC. Use StarSQL (64-bit) with the 64-bit versions of perl, DBI, and DBD::ODBC.
HP supplies two different binaries for perl in /opt/perl_32/bin: perl-static and perl-dynamic. The default (symbolic link from /usr/bin/perl) is perl-static, which has SHLIB_PATH disabled. For best results, we recommend using perl-dynamic instead of perl-static.
This solution explains how to set up and use StarSQL and unixODBC with Perl on UNIX platforms. The steps that you need to perform, which are explained in detail in the sections that follow, are:
# perl Makefile.PL
# make
# make test
# make install
Set the DBI_DSN, DBI_USER, and DBI_PASS environment variables to values that the DBD::ODBC tests can use to verify the database connection during the build process. Run the following commands, replacing mydsn, myuser, and mypassword with the appropriate values for the ODBC data source, DB2 userID, and DB2 password, respectively.
(using csh)
# setenv DBI_DSN dbi:ODBC:mydsn
# setenv DBI_USER myuser
# setenv DBI_PASS mypassword
# perl Makefile.PL -o $STARSQL/odbc
# make
# make test
# make install
# perl -e 'use DBI;'
# perl -e 'use DBD::ODBC;'
# perl -MDBD::ODBC -e 'print $DBD::ODBC::VERSION;'
If the modules were built successfully, the output from the last command will be the version of the DBD::ODBC module, such as shown in the following example:
# perl -MDBD::ODBC -e 'print $DBD::ODBC::VERSION;' 1.13#
The following short program will fetch the contents of a table and display the result set.
use strict;
use DBI;
my $dbh = DBI->connect('dbi:ODBC:mydsn', 'myuser', 'mypassword',
{AutoCommit => 0});
my $sql = qq/select * from MYTABLE/; # the query to execute
my $sth = $dbh->prepare($sql); # prepare the query
$sth->execute(); # execute the query
my @row;
while (@row = $sth->fetchrow_array) { # retrieve one row
print join(", ", @row), "\n";
}
$dbh->disconnect if ($dbh);
# perl test.pl
The procedure outlined below was tested with the Apache2 HTTP Server running on Linux.
The apache environment must be configured with the location of the unixODBC Driver Manager to use with StarSQL, whether it be an existing, supported unixODBC installation or the one included with StarSQL.
32 bit: export LD_LIBRARY_PATH=/usr/share/starsql/odbc/lib
64 bit: export LD_LIBRARY_PATH=/usr/share/starsql64/odbc/lib
#!/usr/bin/perl -w
use DBI;
# it is recommended that the application set the environment variable HOME
$ENV{HOME} = '/var/www';
# declare the content-type for the web server to use
print "content-type: text/html\n\n";
print '<html><head><title>Basic CGI</title><head><body>';
my $dbh = DBI->connect('dbi:ODBC:mydsn', 'myuser', 'mypassword',
{AutoCommit => 0});
my $sql = qq/select * from MYTABLE/;
# the query to execute
my $sth = $dbh->prepare($sql);
$sth->execute;
print '<table border="1">';
# table headings are SQL column names
print "<tr><th>$sth->{NAME}->[0]</th><th>$sth->{NAME}->[1]</th></tr>";
while (my @row = $sth->fetchrow_array) {
print "<tr><td>$row[0]</td><td>$row[1]</td></tr>\n";
}
print " </table>\n";
print " </body></html>\n";# a disconnect statement is required
$dbh->disconnect;
# chmod 755 webtest.pl
If the Web application cannot locate Perl, use the command " which perl" to determine the installed location of Perl and modify the first line of the sample Web application (webtest.pl) if it is a location other than /usr/bin.
For errors related to the Web server, examine the Web server error log (i.e., /var/log/httpd/error_log).
To assist with troubleshooting problems with Perl applications running on IIS, run the application in debug mode. Rename the perl file to nph-<appname>.pl and run the application. "nph" stands for "no-parsed-header" script, and it is a directive to run in debug mode which will produce more useful error messages.
If you encounter an error related to the database or the database queries, enable ODBC tracing by editing /usr/local/etc/odbcinst.ini (on UNIX/Linux) or use the Windows ODBC tracing tool (in the ODBC Data Source Administrator). After reproducing the problem, examine the ODBC trace log.
The information in technical documents comes without any warranty or applicability for a specific purpose. The author(s) or distributor(s) will not accept responsibility for any damage incurred directly or indirectly through use of the information contained in these documents. The instructions may need to be modified to be appropriate for the hardware and software that has been installed and configured within a particular organization. The information in technical documents should be considered only as an example and may include information from various sources, including IBM, Microsoft, and other organizations.