Pragana's Tcl Guide


Old notes


CGI programming with tcl

When someone thinks of a dynamic web page, with database access for instance, they never ask me to do it. He don't like perl stuff, so let us find some one else. I can only laugh at this thinking, because tcl is my secret weapon for doing cgi programming and dynamic content. Yes, that's right! And my code is so small and simple, that when they look at it think I'm a kind of genius ;-) and of course, I'm not.
You can do almost everything with tcl, even substitute perl for cgi programming and many things perl is not very able to do.

Let us summarize what's needed to create a cgi program. There's nothing mysterious about them, anyhow. When a browser asks a web server for a page, usually a .html page, it talks to the server with a protocol known as HTTP. There are several commands in this protocol, but we will concentrate our talks in just two of them, GET and POST. You may want to get some feeling on how this works, so don't be scared to do yourself a telnet:

~ $ telnet localhost 80
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Now you can give a simple page request like:
GET http://localhost/index.html HTTP/1.0
and it will reply with a header followed by the requested html page:
HTTP/1.1 200 OK
Date: Tue, 08 Feb 2000 10:32:15 GMT
Server: Apache/1.3.9 (Unix)
Last-Modified: Mon, 07 Feb 2000 20:23:34 GMT
ETag: "300ac-b40-389f29c6"
Accept-Ranges: bytes
Content-Length: 2880
Connection: close
Content-Type: text/html

<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
...

The most important things here are:

Easy, isn't it?

A cgi program is just like that, changing the static html page for a program that generally returns a variable html script, by printing it to the standard output (stdout). Of course, your program shall return a valid html page, and additionally return the Content-Type for the stuff generated. In this first article, let us study a simple "real time clock" web page done with tcl commands. As most of my articles, I prefer to show you the code first and then comment about it, so here is it:

#!/usr/local/bin/tclsh8.2

set page {Content-type: text/html

<html>
<head>
<title>Real time clock in tcl</title>
</head>
<body>
<h1>Don't lose your time</h1><br>

<p>
Local time: [clock format [clock seconds] -format %H:%M]
</p>
<hr>
</body>
</html>
}

puts [subst $page]

The code is very simple indeed. First, the Content-Type stuff to say that our page have test/html content (will be part of the header), then the blank line¸ and at last our html page. You may even use Netscape Composer to write your page, if you want so (I prefer vim). You may notice that the html code have also some embedded tcl commands.
How can this work? Well, the last step in our program is to do a subst processing for substituting every variable and command embedded into the page script for their real values, what's done by the tcl interpreter. So, take care on quoting \$ \[ \{ and other special tcl characters to not having them interpreted by the tcl command (subst).

To test this program, save the code above as clock-cgi (or any other name, of course) and move to your cgi-bin directory (/var/lib/apache/cgi-bin). Dont' forget to chmod it so it become executable, and edit the first line to reflect your tcl interpreter (don't need to be 8.2, even a 7.4 will do). Then, point your browser to http://localhost/cgi-bin/clock-cgi or whatever and reload to get the time updated.

That's all fellows. Happy hacking!
In the next time, I will comment more on this and introduce how to do some simple database accessing from cgi programs with tcl.


Back Home