Tcl

This is an old revision of this page, as edited by 193.98.144.2 (talk) at 01:30, 27 February 2002 (added digital clock example). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Tcl (originally from "Tool command language", but nonetheless usually lowercased) is a scripting language created by John Ousterhout that is generally thought to be easy to learn, but powerful in the right hands. Some of its features:

  • Everything is a command, including language structures.
  • Everything can be dynamically replaced and overridden.
  • All data types can be handled as if they were strings, including code.
  • Extremely simple syntactical rules, which everything follows.
  • Heavily event-driven.
  • Dynamic scope.
  • High extensibility (with C, Java and Tcl)

While Tcl itself does not provide an object oriented framework the language itself can be extended to provide new functionality as that is required. Indeed, many C extensions exist that provide OO funcionality, including the powerful XOTcl and more traditional incr Tcl. Some Tcl-based OO extensions also exist.

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

A simple example, demonstrating event-based computing, follows.


# Simple echo-server that can handle multiple connections.

# This procedure is called when a client connects to the server
proc newConnection {sock addr port} {
    fconfigure $sock -blocking no -buffering line

    # handleData should be called with $sock as the parameter when data can
    # be read from the socket
    fileevent $sock readable "handleData $sock"

    return
}


proc handleData {sock} {
    set line [gets $sock]
    if {($line == "") && [eof $sock]} {
	close $sock
    } else {
	puts $sock $line
    }

    return
}


socket -server newConnection 20000
vwait forever

Another example using Tk (from A simple A/D clock, 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: