Perl and the Oracle DBAStill
Perl has made possible the creation of DBA utilities that previously required more traditional languages such as C. Complexdata structures unavailable in shell scripting as well as more complex control structures make it an ideal scripting languagefor utility use. The fact that many Perl scripts can work on UNIX, Linux and Win32 without modification is also a valuableasset. Many a DBA must support Oracle on multiple platforms. No wonder Perl is referred to as the “Swiss Army Knife” of programming languages.Though there are many features that separate Perl from the more traditional scripting tools, there are two features of DBD::Oracle that may be particularly attractive to a DBA.
Multiple connections to multiple databases.
The ability to work with LONG data types.When faced with querying data from another database in SQL*Plus while maintaining the current session databaseconnection, it is necessary to use a database link. While this usually works well its intended use in applications, it issomewhat unwieldy for the DBA maintaining many databases. The database connections may not be known until runtime. Itis very convenient to simply be able to open another connection to another database.Another limitation of SQL*Plus and PL/SQL is when dealing with LONG data types. PL/SQL can manipulate a LONGcolumn only up to 32k in length. Using Perl with DBI it is possible to manipulate LONG data types without resorting to C.Though modern applications should be using the more modern LOB data types, there are still many applications that rely onthe long data type.Here are a few of the many features of Perl that make it a powerful addition to the DBA tool box:
Powerful command line argument parsing
Direct control of many OS features and protocols such as ockets, ftp, ssh, file handling, semaphores and IPC
Interaction with the user
The Nitty Gritty
Let’s take a look at a simple but complete Perl script makes a connection to a database and queries the DBA_USERSview. Though this is a relatively short script at ~120 lines, it is fairly robust. The following elements are included in thisscript:
Parses command line arguments
Creates a database connection
Creates calls to Oracle to create a cursor, execute it and fetch data.
Displays a usage screen if there is an error on the command line
Will produce either a CSV output or a formatted report
Includes documentation available via the
scriptThe first 16 lines of the script appear in Example 1. If you are unfamiliar with Unix systems, line 1 may appear somewhatstrange. This known as the
in shell scripting, and is used to inform the Unix kernel which shell is used toexecute a script, in this case the shell is Perl. The
tells Perl to include warnings.Lines 5-10 make up some simple internal documentation. This can be viewed by using the
command. Many Perlmodules contain extensive internal documentation. To view the documentation for the sample script you would use thecommand
.At lines 12-15 the Perl modules required for this script are loaded. Notice that DBD::Oracle is not mentioned. The DBI willautomatically load the correct driver at the time the database connection is made.
1: #!/usr/bin/perl -w
www.rmoug.orgRMOUG Training Days 2005