Main Page | See live article | Alphabetical index

Tcl

Tcl (originally from "Tool Command Language", but nonetheless conventionally rendered as "Tcl") is a scripting language created by John Ousterhout that is generally thought to be easy to learn, but powerful in the right hands. It is most commonly used for rapid prototyping, scripted applications, GUIs and testing. The name is commonly pronounced as "tickle". Tcl's features include:

While Tcl itself does not provide an object oriented framework, the language itself can be extended to provide new features as required. Indeed, many C extensions have been written to provide OO functionality, including the XOTcl and incr Tcl packages. Other OO extensions, including Snit, are coded entirely in Tcl.

The most popular Tcl extension is the Tk toolkit, which allows one to write portable graphical user interfaces for a variety of operating systems.

A simple working example, demonstrating event-based handling of a socket, follows.


  1. !/bin/sh
  2. next line restarts using tclsh in path \\
exec tclsh $0 ${1+"$@"}

  1. echo server that can handle multiple
  2. simultaneous connections.

proc newConnection { sock addr port } {
    
    # client connections will be handled in
    # line-buffered, non-blocking mode
    fconfigure $sock -blocking no -buffering line

# call handleData when socket is readable fileevent $sock readable [ list handleData $sock ]

}

proc handleData { sock } {

    puts $sock [ gets $sock ]
    if { [ eof $sock ] } {
       close $sock
    }
}

  1. handle all connections to port given
  2. as argument when server was invoked
  3. by calling newConnection
set port [ lindex $argv 0 ] socket -server newConnection $port

  1. enter the event loop by waiting
  2. on a dummy variable that is otherwise
  3. unused.
vwait forever

Another example using Tk (from A simple A/D clock) and timer events, a digital clock in six lines of code:
proc every {ms body} {
    eval $body
    after $ms [list every $ms $body]
}
pack [label .clock -textvar time]
every 1000 {set ::time [clock format [clock sec] -format %H:%M:%S]} ;# RS

External links: