Chapter 14. Using PostgreSQL with Perl

The Perl language has been called the "toolbox for Unix." If you are an experienced Perl programmer, you already know three things about the language: It's extremely useful, it's notoriously difficult to master, and it gives you a new way to write completely incomprehensible code. If you are not already a Perl programmer, you should be forewarned that I won't try to teach you the basics of Perl programming in this chapter. But that doesn't mean that you won't be able to get anything useful from this chapter. If you don't already know Perl, read this chapter once without paying too much attention to the syntactical details?they won't make a lot of sense the first time through. Then, read through the client applications again, trying them out as you go. You'll be surprised at how quickly you can make sense of the examples if you don't get too hung up on the unusual syntax.

There are two ways to connect to a PostgreSQL database from a Perl application[1]: pgsql_perl5 (also known as the Pg module) and the DBI module. The pgsql_perl5 interface is a Perl binding for the libpq API. If you are already comfortable with the libpq API, you will find pgsql_perl5 very familiar.

[1] I'll use the terms application, script, and program interchangeably in this chapter. They all mean the same thing in Perl: a series of statements that does something?hopefully something useful.

In this chapter, I'll focus on the DBI module. DBI provides a portable interface to a variety of database systems. When you use the DBI module within a Perl application, you can move from database to database with few if any changes to your code. The architecture of the DBI module is similar in structure to JDBC (the Java database API) and ODBC.

    Part II: Programming with PostgreSQL