4 Jan 2006 minorityreport   » (Apprentice)

Using Guile Scheme to write resilient CGI scripts.

I've been using Guile for scripting purposes for a while now. It's quite fun to use and very powerful. Guile is a full implementation of the Scheme programming language, which is a member of the Lisp family of languges.

More recently I've been using it to write CGI, or rather scripts that operate both in CGI mode and from the command line.

vandalism is a guile script that plots the incidence of vandalism on any one of (currently) nine different Wikipedias, between 2001 and the present day. It's written in 100% guile with the guile-dbi module connecting to a live replication of Wikipedia's mysql databases.

It's written mainly in a functional style--the strategy is to construct the xhtml document in text fragments which are appended together. Once assembled the document is sent onwards to the http client.

If the script fails, it sends a suitable error document according to whether it's operating in development mode or production mode--in either case the error message is carefully filtered to as to avoid accidentally displaying the mysql username and password which may appear in some of the guile structures related to the database. In development mode, a sophisticated lazy-dispatch technique is used to capture the stack at the point of failure and thus deliver backtrace information, which is not normally available in guile when operating in script mode. A great help in development!

Another feature I thought I'd build in to the script is the ability to produce a html page listing its own source. Executing the script from the command line as ./vandalism --source >vandalism_source.html will produce an up-to-date listing of the source and all related data files. As you may see, the source listing isn't particularly pretty yet, but give it time.

I'm particularly pleased with the way in which Scheme S-expressions can be used to represent xhtml. It's much nicer than all that stuff with the angle brackets:

(define (html-table-header columns legend)
      (html:attr (list (cons 'colspan columns)))

and here's another one that I like:

(define (html-validation-button)
      (html:attr '((src . "http://www.w3.org/Icons/valid-xhtml10")
        (alt . "Valid XHTML 1.0 Transitional")
        (height . 31)(width . 88))))))

I could just write this all as plain html with embedded variables and whatnot, but I'd probably forget the closing tags, and in any case it wouldn't look as pretty. Of course you can do this kind of thing in other languages too, but Scheme and other languages in the Lisp family tend to encourage it.

Latest blog entries     Older blog entries

New Advogato Features

New HTML Parser: The long-awaited libxml2 based HTML parser code is live. It needs further work but already handles most markup better than the original parser.

Keep up with the latest Advogato features by reading the Advogato status blog.

If you're a C programmer with some spare time, take a look at the mod_virgule project page and help us with one of the tasks on the ToDo list!