PERL (Practical Extraction and Report Language) has had strong acceptance in the IT community since the introduction of version 1.0 in 1987. PERL is used widely on UNIX systems, especially Linux, for text manipulation and parsing scenarios that are too intensive for shell commands, but not complex enough for a language like C. Not being able to use shell commands for these actions makes them less portable across different implementations of UNIX. Many organizations today use PERL to handle common tasks associated with web servers and web interfaces to corporate databases. PERL is extremely flexible and has a large support base in the USENET community, which frequently posts "modules" that can be modified for any number of wide ranging tasks. These modules greatly reduce the amount of code a developer needs to implement because they are widely available for anyone's use. This community also offers many resources available to those just beginning to learn PERL.
Because PERL is easily portable across operating systems, several initiatives have been undertaken to develop modules that enable developers to use the ODBC interface, thus enhancing PERL's portability across corporate databases. DataDirect Connect® for ODBC allows PERL applications not only to be completely portable, but also highly scalable. DataDirect Connect for ODBC enables applications to achieve better performance through its unique wire protocol technology and network optimizations.
This tutorial provides information necessary for using DataDirect Connect for ODBC with PERL, including configuration instructions and a sample PERL application.
DBI is the Database Interface module for the PERL programming language. The DBD::ODBC PERL module is available at http://dbi.perl.org/ and provides a transparent interface to any ODBC-compliant database rather than a particular database. In addition to increasing portability, this module offers PERL developers a wide degree of flexibility when developing PERL applications and insulates the application from any subsequent changes to databases.
After downloading and installing the DBD::ODBC module, you will need to install the DataDirect Connect for ODBC drivers and edit the odbc.ini file.
Download DataDirect Connect for ODBC from http://web.datadirect.com. Select DataDirect Connect for ODBC from Products on the navigation bar at the top of the main page. Then, click the Try icon at the top of the product page. You can also view the DataDirect Connect for ODBC documentation from the product page, including the installation guide. After downloading the ODBC drivers and documentation, refer to the installation guide to install the drivers.
You will need to create a data source that the PERL application can call to establish a connection to the database. On UNIX, this is done through the system information file, usually named odbc.ini. A sample odbc.ini is part of the DataDirect Connect for ODBC installation. Complete instructions on setting environment variables and creating a data source are provided in the DataDirect Connect for ODBC Reference that comes with the product.
After completing the procedures for setting up odbc.ini, edit the SQL Server section of the file so that, at a minimum, it contains the following entries:
is the DataDirect Connect for ODBC installation directory;
represents the driver release number;
is the name of the target database; and
are the IP address and port of the target database, for example, 184.108.40.206,1433.
Use the following procedure to enable PERL and the DBD::ODBC module to communicate correctly with ODBC drivers on all UNIX platforms. Modification requests have been submitted to the author of the module so that the following procedure should be unnecessary in the future.
This file contains the following two lines by default:
You need to relocate these lines so that they precede the line:
Modify them to read:
If you do not move them to precede the 'unixodbc' entry in the Makefile.PL script, then the script assumes that you are using a generic UNIX ODBC Driver Manager and the Makefile will not generate correctly.
The file also contains:
Change this to:
Run the command:
If you receive an error similar to
Run theto compile the DBD::ODBC module source code.
Issueto write the module to the PERL directories.
This simple application (sample_app.pl) enables you to query and enter employee information. The application establishes a connection to a SQL Server database and, based on information entered at the prompts, either queries a table to retrieve employee information or enters employee information into the database. This application is meant to demonstrate in a simplified manner how to connect to a database and enter or retrieve information using ODBC with PERL.
For this application to work, you will need to create a table. In this example, the table name is employee. The Create statement for this table is straightforward:
The sample application is named sample_app.pl and can be created in a text editor. This command-prompt application allows you either to query employee information or to enter employee information.
NOTE: You will need to modify the connect string to include a data source that is on your machine (such as SQL Server Wire Protocol) as well as an appropriate user ID and password for your database.
In this annotation section, the code sample is followed by an explanatory paragraph.
This code states which modules are used in the sample application. For sample_app.pl, the DBI module enables the application to get information from the SQL Server database through ODBC.
This is the database connection. The parenthetical statement indicates that DBI will be used for ODBC, and which data source, user ID, and password will be used to connect. You will need to modify the code to use an existing data source that you have created, as well as the user ID and password that you use to connect to the database.
These two statements are prepares for the SQL statements that the application uses.
is to retrieve any employee information that matches the input from the user.
is to insert the employee data that the user enters from a series of prompts.
This code declares and defines a subroutine that strips any leading and trailing white space from the variables in the application.
This code prompts the user to enter the function that should be performed, assigns the input to a variable, and strips the variable of any trailing white space.
to search employee information, this code takes the user input and assigns it to
The first prepared statement for
This code reads the result set from the array, strips the results of any leading or trailing white space, and prints the results on the screen.
If there are no matching results from the query, this code prints out a message stating that there were no matches. It then drops the connection.
to insert employee information, then this section of code prompts the user to enter the associated employee information, assigns each input to a variable, and strips it of any leading or trailing white spaces.
to insert the data is then executed with the associated parameters. The statement finishes and the connection is dropped.
Refer to the instructions for executing the application in your particular PERL environment. If you are running WinActive's PERL implementation on Windows, change to the directory where the application resides and enter the following line at the command prompt:
The following prompt is displayed,
and presses ENTER to insert employee information.
The following prompts are then displayed (shown with sample responses):
The application then updates the database with the appropriate information. To query the database and view the information, run the application again, this time entering 1 to search employee information:
At the prompt, enter the name of the employee described in the previous step to view the information in the database.
PERL is a highly portable language that provides a simple and flexible way to develop applications that are too intensive for shell commands, but do not need a complex language structure like C. For developers to deliver truly portable applications, DataDirect Connect for ODBC provides consistent implementations and performance across all major operating systems and databases. Because PERL applications are often used in Web-based scenarios, they need to be highly scalable and fast. DataDirect Connect for ODBC delivers both performance and scalability through the use of its unique wire protocol architecture. This architecture also does not require database client libraries for connecting to the database, easing deployment of the applications across environments. DataDirect Connect for ODBC is the fastest, most comprehensive suite of ODBC drivers for all major databases.