Free Newsletters:
DatabaseJournal  
DBANews
Database Journal
Search Database Journal:
 
HOME News MS SQL Oracle DB2 Access MySQL PostgreSQL PHP SQL Etc Scripts Links Discussion
internet.com

» HOME
» NEWS
» FEATURES
» SERIES
MS SQL
Oracle
MS Access
MySQL
DB2
» RESOURCES
Products
Scripts
Links
» DISCUSSION
» TECH JOBS

Marketplace Partners
Be a Marketplace Partner




internet.commerce
Be a Commerce Partner
Data Center Solutions
Holiday Gift Ideas
Shop Online
Computer Hardware
Promotional Golf
Promotional Products
Imprinted Promotions
Phone Cards
Web Hosting Directory
KVM Switch over IP
Hurricane Shutters
Car Donations
Boat Donations
Auto Insurance Quote




MySpace Joins eBay, Yahoo in Open Profile Push

News Corp. Unit Under Fire for Ties to Hacker

Are Non-PC Devices Hurting 'Net Innovation?

internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


Linked Data Planet Conference & Expo

CA ERwin® Data Modeler Proven database design and modeling. Efficiently analyze, design and deploy effective database solutions. Whitepaper: Manage SQL Server Deployments
Try it free: CA ERwin® Data Modeler


Solaris 8 Migration Assistant
Rapidly move your Solaris 8 application environments to new systems running Solaris 10 with the Solaris 8 Migration Assistant. Reduce migration risk while taking advantage of increased performance, reliability and security of the latest SPARC hardware platforms and Solaris 10 OS. »

 
Sun Eco Innovation: Good for Business, Good for the Environment
A complete solution to help you optimize and refresh your datacenter while properly recycling equipment and eliminating eWaste, including money-saving promotions to lower hardware acquisition costs. »

 
Sun Eco Innovation: Power Calculators
Power consumption has increasingly become a priority in customer's minds when purchasing new systems or storage. Sun's Power Calculators provide data on power consumption of Sun products allowing IT managers to better plan the power requirements in the datacenter to achieve better energy and cost savings. »

 
Optimize the Web Tier: Consolidate to Get More Performance in Less Space and Lower Power Consumption
Expansion in the Web tier is generally accomplished by adding more servers whenever extra capacity is needed. As the pool of servers grows larger, however, the complexity of the environment can grow exponentially. »

Production Manager (hands on)
Aquent
US-MA-Cambridge

Justtechjobs.com Post A Job | Post A Resume
Oracle
May 5, 2008
Programming, Perl + Oracle
By Sean Hull

1. Introduction

Perl is a powerful scripting language that has grown into a fully-fledged programming language, as good as Java (many would argue better), and though not as efficient, at least as functional and powerful as C.  It can meet your day-to-day scripting needs as a database administrator, and if used prudently, and responsibly, as a language to build large web-based applications.  

Here's a quick introduction to using Perl with Oracle.  Take a look and I'm sure you'll see it can be an ally to you for many of your day-to-day needs.

2. Overview

When you write Perl code, and want to talk to a database, you use DBI, the database interaction library.  It includes drivers for all of the popular databases so you can write programs that talk to multiple different types of databases, or port your Perl code later, with ease.  

The DBI is quite mature, so you can expect full functionality, power, and performance when you use it.  Follow these guidelines for a first look at how it works.

a. connect

The first thing your Perl script must do is open a connection to your Oracle database.  Here's how you do that:

my $dbh = DBI->connect 
  ('dbi:Oracle:orcl', 'sean', 'hull') || die $DBI::errstr;

In this example, the Oracle SID is "orcl", username is "sean", and password is "hull".  Perl returns a database handle, which you'll use in your Perl code to interact with the database.

b. prepare

This is where you give Oracle your SQL query to parse.  If you've used bind variables, and run this query previously, chances are Oracle will already have it cached, and this step will only involve Oracle finding the cached version.

$sql = 'select username from users where id = :a';
$sth = $dbh->prepare ($sql);

c. bind

Here's the binding step.  This is where you tell Oracle what values you want to use for your variables embedded in your sql:

$sth->bind_param(1, '12345');

d. execute

The execute phase looks like the following:

$sth->execute || die $DBI::errstr;

e. fetch

There are a bunch of ways to fetch rows into arrays, array references, hashes, and other Perl data structures.  We'll just use an array for simplicities sake:

@row = $sth->fetchrow_array;

f. other

There are also the expected compliment of calls such as:

$dbh->commit;
$dbh->rollback;

There are even some other interesting methods for controlling autocommit, handling LOB data, metadata, errors, and other interesting stuff.

3. An Administrators Ally

Now that you understand the fundamentals of how Perl talks to Oracle, let's take a look at what you can use it for.

a. pdba toolkit

This toolkit was written by Andy Duncan and Jared Still to compliment their excellent O'Reilly title "Perl for Oracle DBAs".  Some of the things this toolkit can help you with include getting the DDL for your db objects, killing sessions, managing user accounts, and more.

b. cronjobs

As a DBA, we all use cron to schedule jobs that do database work, backups and so forth.  You may have had your fill of shell scripts, with all their quirks and idiosyncrasies.    Enter Perl to save the day.  It's much more powerful for database access, and more importantly more efficient since you can create handles, potentially multiple sessions to multiple databases, perform work in parallel with different statement handles, and on and on.

c. alertlog monitoring

Every DBA knows about watching the alert.log file for errors.  Now you can consider using Perl to help you with that task.  Perl includes a rich regular expression implementation, which will come in handy when searching for errors in your logfile.

d. using with nagios

Nagios is a great open source monitoring system which a lot of Unix administrators use to watch networks, system uptimes, load averages, disk usage, and now Oracle!  From monitoring that your database is accessible to tablespace usage, metrics or really anything you can write a query for, monitoring Oracle just became a lot easier!

4. Web Applications

There are lots of choices these days for building web applications, from Microsofts proprietary solutions, to PHP and Java.  Perl's advantages include mod_Perl, described below, plus all the advantages you've grown to love about Perl, such as powerful regular expressions, and a huge compliment of support packages and libraries to choose from.  

a. Using with Apache

Out of the box, Apache can run Perl code as CGIs.  Essentially Apache asks the OS to run the program, and whatever it returns, Apache then returns to the client.  This allows for all sorts of dynamic web application building, and provides a powerful way to build internet applications.

b. Using mod_Perl

It's important to make sure your Apache server is configured to use mod_Perl.  This brings the Perl engine into Apache for efficiency, caches previously executed Perl scripts so they don't have to be compiled at call time, and then thirdly it will cache connections to your database.  So, when your scripts make the DBI->connect call, Apache will first check if it already has a free handle open to the target database, and give that to you if it does.  

For Oracle especially, this is extremely important.  That's because Oracle uses processes for each new session that is open (on Unix at least, on Windows it uses threads).  These processes are rather expensive to open, resource & cpu-wise, so you want to open them ahead of time, and keep them open.  mod_Perl gives you this power.

5. Conclusion

Perl has a strong and vibrant following in the open source world; nd for good reason.  It has the tremendous support of modules and libraries to extend its functionality, a great community behind it, and the power and flexibility to perform as both a scripting language for cronjobs and monitoring, as well as a fully-fledged web-based development platform.   

» See All Articles by Columnist Sean Hull

Tools:
Add databasejournal.com to your favorites
Add databasejournal.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed

Oracle Archives

Download: SQL Backup & DBA Best Practices eBook.
Flash Demo: Learn how IBM Information Server Blade is easy to manage, highly scalable and efficient.
Data Sheet: IBM Information Server Blade
HP eBook: Using Business Service Management (BSM) to Manage Your Business Applications
Quest Whitepaper: Improving Oracle Database Performance Using Real-Time Visual Diagnostics


Latest Forum Threads
Oracle Forum
Topic By Replies Updated
GET DATA FROM .DBF FILE, ORACLE 9i revelation 5 May 5th, 10:55 AM
Could not locate Java runtime. Oracle installation error revelation 0 April 10th, 12:06 AM
Database Backup junOOni 4 March 20th, 06:28 AM
Helpme to How to Write Text File intelram_18 1 March 17th, 02:54 PM







JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Microsoft Article: HyperV-The Killer Feature in WinServer ‘08
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Win Server ‘08
HP eBook: Putting the Green into IT
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
Avaya Article: Setting Up a SIP A/S Development Environment
IBM Article: How Cool Is Your Data Center?
Microsoft Article: Managing Virtual Machines with Microsoft System Center
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Video: Are Multi-core Processors Here to Stay?
On-Demand Webcast: Five Virtualization Trends to Watch
HP Video: Page Cost Calculator
Intel Video: APIs for Parallel Programming
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Sun Download: Solaris 8 Migration Assistant
Sybase Download: SQL Anywhere Developer Edition
Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
Red Gate Download: SQL Compare Pro 6
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
IBM Article: Collaborating in the High-Performance Workplace
HP Demo: StorageWorks EVA4400
Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES