Chapter 16. Using PostgreSQL with Tcl and Tcl/Tk

Tcl is an interpreted scripting language. Tcl is an acronym for Tool Command Language and is often pronounced as "tickle." The original goal of Tcl's creator (John Ousterhout) was to create an embeddable interpreted language that could be included in many small applications. The idea was to create a language that could be embedded in applications that might not normally justify having their own language. Another example of this sort of embeddable language is Microsoft's VBA (Visual Basic for Applications). With an embedded language, you can make any application programmable (or scriptable). For example, you might have a spiffy terminal emulator that you've developed for your own use. It would be nice if you could add a scripting capability to the emulator, but that would require a ton of work. This is a perfect fit for Tcl. By embedding Tcl in your terminal emulator, you are incorporating an entire programming language in your application with very little work.

Tcl is also a general-purpose programming language. In fact, I think Tcl might just be the simplest language ever invented. (But beware, a simple language doesn't always imply simple programs; it just means the language won't get in your way.)

There are only a few rules that you have to remember:

  • Everything in Tcl is a string…everything.

  • A variable reference ($variable) is replaced by the variable value anywhere it occurs within a string.

  • A command reference ([command]) is replaced by the command value anywhere it occurs within a string.

  • If you want to suppress variable and command substitution, surround a string with curly braces.

  • If you don't want to suppress substitution, surround a string with double quotes.

If you remember those simple rules (and suspend your disbelief?it really is that simple), you'll be fluent in Tcl in no time. When you start writing Tcl applications, you'll probably use the Tcl shell as an execution environment. The Tcl shell (tclsh) is a simple shell (like bash or sh) that has been combined with the Tcl interpreter. Using tclsh, you can do all the things you would normally do in a Unix shell (such as run a program, change directories, redirect output, and so on) in addition to all the things you can do in a Tcl program.

Tcl is often combined with Tk. Tk is a graphical toolkit. Using Tk, you can create windows and widgets (graphical controls), and interact with the user in a graphical interface. You can use Tk with many different languages, but it was originally designed as a companion to Tcl. The Tcl/Tk environment includes a graphical shell called wish. The wish shell is similar to tclsh, except that it has Tk thrown in so you can build graphical shell scripts.

Tcl applications (and therefore Tcl/Tk applications) can interact with PostgreSQL database servers. The Tcl-to-PostgreSQL interface is contained in a library named libpgtcl. libpgtcl provides a small number (17) of procedures that you can call from a Tcl script. In this chapter, I'll describe each of these procedures, and you'll build a few client applications that show you how to use libpgtcl to build PostgreSQL client applications.

    Part II: Programming with PostgreSQL