Tcl

Tcl (originally from "Tool Command Language", but nonetheless conventionally rendered as "Tcl" rather than "TCL"; and pronounced like "tickle") 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.

Contents

Features

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 written entirely in Tcl.

Also functional programming can easily be done in Tcl, as higher-order functions or functional abstractions are just there, even if many people don't use them. Functional composition is as easy as this:

proc o {f g x} {$f [$g $x]}

Syntax

Very simple and consistent syntax

Tcl has a very simple syntax which is applied in a consistent way. A Tcl script consists of several commands. A command is a list of words separated by whitespace.

  word0 word1 word2 ... wordN

The first word is the name of a command, which is not built into the language, but which is in the library. The following words are arguments. So we have:

  commandName  argument1 argument2 ... argumentN

Instead of an argument, you may put another command in square brackets. The subcommand is evaluated first and the result is substituted as the argument. If you put something in curly braces as an argument, it is not evaluated but handed directly to the command as the argument.

To summarize: there is one basic construct and only the block, the curly braces and the backslash have a special meaning besides the quotes. The single equality sign (=) for example is not used at all, and the double equality sign (==) is the test for equality.

All commands have the same structure - a keyword which is followed by several parameters. A command is terminated by a newline or a semicolon. Even comments are just commands which happen to do nothing.

Tcl is not statically typed: each variable may contain integers, floats or strings.

Symbols with a special meaning

    $    variable substitution
         e.g. $argv0 might be replaced by /usr/bin/tclsh
    []   subcommand substitution
         e.g [pwd] might be replaced by /home/joe
    ""   word grouping
         e.g. "you are $user" is one word; substitution still occurs
    {}   word grouping with deferred substitution
         e.g. {you are $user} is one word, where "$user" is not replaced
    \    line continuation
    #    comment (only at the beginning of a line)

Some examples of commands

Assignments are made with the command set, no equality sign.

 set variable value


While loops are implemented by the command while which takes two arguments. The arguments are Tcl scripts. They are in curly braces to avoid execution on the first level of interpretation. Within the execution of the while command the scripts are executed.


 while { aTCLcommandWhichEvalutesToAnInteger }   { aTCLcommand
                 anotherTclCommand
                 ....
        }

If command

 if {$x < 0}   {
         set x 0
 }

Commands may have no arguments

 pwd

gives back the current working directory. With

 set wdir [pwd]

you store the string describing the working directory in the variable wdir.


A command may give back as a result a list

 glob aPattern

gives back a list of file names in the working directory whose names match aPattern.

Procedures

Procedures are defined as follows

  proc nameOfProc { argumentList }   {
           ....
           ....
         }

Associative arrays

The following code snippet creates and initializes an associative array.

set capital(France)  Paris
set capital(Italy)   Rome
set capital(Germany) Berlin
set capital(Poland)  Warsaw
set capital(Russia)  Moscow
set capital(Spain)   Madrid

To query it use and put the result on standard output use

puts $capital(Italy)

To get a list of all countries for which a capital is defined use

array names capital

The result is an unsorted

Poland Spain Russia Germany Italy France

If you like to have it sorted use

 lsort [array names capital]

GUI and Expect

The most popular Tcl extension is the Tk toolkit, which provides a graphical user interface library for a variety of operating systems. Each GUI consists of one or more frames. Each frame has a layout manager.

Another popular extension is Expect, which allows automated driving of terminal-based programs (such as passwd, ftp, telnet and command driven shells).

Examples

Echo server

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


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

# echo server that can handle multiple
# 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
     }
}

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

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

Digital clock

Another example using Tk (from A simple A/D clock (http://mini.net/tcl/2563.html)) and timer events, a digital clock in three lines of code:

 proc every {ms body} {eval $body; after $ms [info level 0]}
 pack [label .clock -textvar time]
 every 1000 {set ::time [clock format [clock sec] -format %H:%M:%S]} ;# RS

Explaination: the first line defines a command, "every", which re-schedules an action ('body') every 'ms' milliseconds; the second creates a label whose content is bound to the variable 'time'; the third line arranges so that the variable 'time' is updated to formatted local time every second.


List of content of associative array

In an array tcl_platform, platform-specific properties are kept. A list of the names of the properties is obtained by

array names tcl_platform

The following snippet lists them together with their values

 foreach i [array names tcl_platform] {
    puts [ concat $i= $tcl_platform($i) ]
 }


If the properties should be sorted

 foreach i [lsort [array names tcl_platform]] {
    puts [ concat $i= $tcl_platform($i) ]
 }

This demonstrates how commands may be nested. In fact they may be nested to any depth.

Intersection of two sets

The filter procedure returns those elements of the list where the script returns TRUE:

proc filter {list script} {
  set res {}
  foreach e $list {if {[uplevel 1 $script $e]} {lappend res $e}}
  set res
}

The in procedure is shorthand for list inclusion:

proc in {list e} {expr {[lsearch -exact $list $e]>=0}}

Testing:

% filter {a b c} {in {b c d}}
b c

Factorial

proc ! x {expr {$x<2? 1: $x*[! [incr x -1]]}}

This demonstrates that any string can be a command name, the ?: operator as from C is available in Tcl too, and recursion is easily possible, although Tcl has no tail call optimisation.

External links

de:Tcl eo:Tcl es:TCL fr:Tool Command Language it:Tcl/Tk la:Tcl nl:Programmeertaal TCL pl:Tcl ru:Tcl fi:TCL sv:Tcl tr:Tcl zh:Tcl

Navigation

  • Art and Cultures
    • Art (https://academickids.com/encyclopedia/index.php/Art)
    • Architecture (https://academickids.com/encyclopedia/index.php/Architecture)
    • Cultures (https://www.academickids.com/encyclopedia/index.php/Cultures)
    • Music (https://www.academickids.com/encyclopedia/index.php/Music)
    • Musical Instruments (http://academickids.com/encyclopedia/index.php/List_of_musical_instruments)
  • Biographies (http://www.academickids.com/encyclopedia/index.php/Biographies)
  • Clipart (http://www.academickids.com/encyclopedia/index.php/Clipart)
  • Geography (http://www.academickids.com/encyclopedia/index.php/Geography)
    • Countries of the World (http://www.academickids.com/encyclopedia/index.php/Countries)
    • Maps (http://www.academickids.com/encyclopedia/index.php/Maps)
    • Flags (http://www.academickids.com/encyclopedia/index.php/Flags)
    • Continents (http://www.academickids.com/encyclopedia/index.php/Continents)
  • History (http://www.academickids.com/encyclopedia/index.php/History)
    • Ancient Civilizations (http://www.academickids.com/encyclopedia/index.php/Ancient_Civilizations)
    • Industrial Revolution (http://www.academickids.com/encyclopedia/index.php/Industrial_Revolution)
    • Middle Ages (http://www.academickids.com/encyclopedia/index.php/Middle_Ages)
    • Prehistory (http://www.academickids.com/encyclopedia/index.php/Prehistory)
    • Renaissance (http://www.academickids.com/encyclopedia/index.php/Renaissance)
    • Timelines (http://www.academickids.com/encyclopedia/index.php/Timelines)
    • United States (http://www.academickids.com/encyclopedia/index.php/United_States)
    • Wars (http://www.academickids.com/encyclopedia/index.php/Wars)
    • World History (http://www.academickids.com/encyclopedia/index.php/History_of_the_world)
  • Human Body (http://www.academickids.com/encyclopedia/index.php/Human_Body)
  • Mathematics (http://www.academickids.com/encyclopedia/index.php/Mathematics)
  • Reference (http://www.academickids.com/encyclopedia/index.php/Reference)
  • Science (http://www.academickids.com/encyclopedia/index.php/Science)
    • Animals (http://www.academickids.com/encyclopedia/index.php/Animals)
    • Aviation (http://www.academickids.com/encyclopedia/index.php/Aviation)
    • Dinosaurs (http://www.academickids.com/encyclopedia/index.php/Dinosaurs)
    • Earth (http://www.academickids.com/encyclopedia/index.php/Earth)
    • Inventions (http://www.academickids.com/encyclopedia/index.php/Inventions)
    • Physical Science (http://www.academickids.com/encyclopedia/index.php/Physical_Science)
    • Plants (http://www.academickids.com/encyclopedia/index.php/Plants)
    • Scientists (http://www.academickids.com/encyclopedia/index.php/Scientists)
  • Social Studies (http://www.academickids.com/encyclopedia/index.php/Social_Studies)
    • Anthropology (http://www.academickids.com/encyclopedia/index.php/Anthropology)
    • Economics (http://www.academickids.com/encyclopedia/index.php/Economics)
    • Government (http://www.academickids.com/encyclopedia/index.php/Government)
    • Religion (http://www.academickids.com/encyclopedia/index.php/Religion)
    • Holidays (http://www.academickids.com/encyclopedia/index.php/Holidays)
  • Space and Astronomy
    • Solar System (http://www.academickids.com/encyclopedia/index.php/Solar_System)
    • Planets (http://www.academickids.com/encyclopedia/index.php/Planets)
  • Sports (http://www.academickids.com/encyclopedia/index.php/Sports)
  • Timelines (http://www.academickids.com/encyclopedia/index.php/Timelines)
  • Weather (http://www.academickids.com/encyclopedia/index.php/Weather)
  • US States (http://www.academickids.com/encyclopedia/index.php/US_States)

Information

  • Home Page (http://academickids.com/encyclopedia/index.php)
  • Contact Us (http://www.academickids.com/encyclopedia/index.php/Contactus)

  • Clip Art (http://classroomclipart.com)
Toolbox
Personal tools