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:

For requesting a page you send a command GET url_to_get protocol
finishing your input with a blank line.

The server replies in the same fashion, with some information (irrelevant
for us in that case), followed by a blank line and the requested content.

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:

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.