quick.gif

space2.gif

space2.gif

space2.gif

space2.gif

space2.gif

space2.gif

space2.gif

   

space.gif

   

space.gif

  ../images/main/bullet_green_ball.gif Introduction

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. Originally "born out of frustration" according to the author with programmers devising their own (poor quality) languages intended to be embedded into applications, Tcl quickly gained wide acceptance on its own and is generally thought to be easy to learn, but powerful in competent hands. It is most commonly used for rapid prototyping, scripted applications, GUIs and testing. Tcl is also used for CGI scripting.

   

space.gif

The combination of Tcl and the Tk GUI toolkit is referred to as Tcl/Tk.

   

space.gif

Some of the main features of TCL are

   

space.gif

  • Everything is a command, including language structures. They are in Polish notation.
  • Everything can be dynamically redefined and overridden.
  • All data types can be manipulated as strings, including code.
  • Extremely simple syntactic rules.
  • Event-driven interface to sockets and files. Time based and user defined events are also possible.
  • Flexible scope, with variable visibility restricted to lexical (static) scope by default, but uplevel and upvar allowing procs to interact with the enclosing functions' scopes.
  • Simple exception handling using exception code returned by all command executions.
  • Readily extensible, via C, C++, Java, and Tcl.
  • Interpreted language, code can be created and modified dynamically, but still fast due to compiling into bytecode.
  • Full Unicode (3.1) support, first released 1999.
  • Platform independent: Win32, UNIX, Linux, Mac, etc.
   

space.gif

  ../images/main/bullet_green_ball.gif TCL Basics

In this section we will look at the basic's of TCL.

   

space.gif

   

space.gif

  ../images/main/bulllet_4dots_orange.gif Scripts, commands, and words

Tcl is a string-based, interpreted command language. Its simplicity in syntax and common sensical approach to semantics makes this an easy language to learn and become proficient in. This short tutorial will concentrate on the most basic concepts to get you started as quickly as possible.

   

space.gif

A Tcl script consists of one or more commands, separated by newlines or semicolons.

   

space.gif

command arg1 arg2 arg3 ...

   

space.gif

   

space.gif

Commands are the basic execution elements. A command is followed by zero or more words which are parameters or arguments, each separated by white space or tabs.

   

space.gif

ex: puts stdout "My first Tcl script."

   

space.gif

= > My first Tcl script.

   

space.gif

   

space.gif

  ../images/main/bulllet_4dots_orange.gif Evaluating a command
   

space.gif

Two step process: parsing and execution.

   

space.gif

  • Parsing: Tcl does not apply any meaning to the value of the words. It only performs simple string operations, e.g. variable substitutions. Tcl only makes one pass for substitutions (each character is scanned exactly once). The result of one substitution is not scanned for further substitutions.
  • Execution: meaning is applied to the arguments of the command. Tcl checks to see if the command is defined, assumed to be the first word in the sequence of words, and locates a command procedure to execute.
   

space.gif

Note: arguments are quoted by default -- if you want evaluation, you must ask for it explicitly

For example:

   

space.gif

set a 5

set b a+8

   

space.gif

The first command assigns the string 5 to variable a. The second command takes the string a+8 and stores it as the new value for b. For y to take on the value 13, you must explicitly evaluate it, as in:

   

space.gif

set a 5

set b [expr $a+8]

   

space.gif

Each pair of brackets invokes an additional evaluation. The one thing you must remember about Tcl is that it does just what you think it will. The evaluation model is very straightforward. There is a single command and zero or more arguments. Those arguments may in turn be commands with arguments that must be evaluated. The commands' return values become the argument to the original command to be evaluated.

   

space.gif

  ../images/main/bulllet_4dots_orange.gif Variable,command and backslash substitutions
   

space.gif

State is maintained in Tcl through the use of variables, and are both declared and instantiated with the "set" command.

   

space.gif

example:

   

space.gif

set loopCounter 0

   

space.gif

   

space.gif

Note that a separate variable declaration is not necessary. Variable values are retrieved using the dollar sign, which will be familiar to shell programmers and Perl developers, but there are slightly different rules associated with when and when not to use the dollar sign.

   

space.gif

example:

   

space.gif

if

   

space.gif

($loopCounter > 10) {

   

space.gif

do something

   

space.gif

}

   

space.gif

  • $ variable substitution: variables are not declared -- they are all of type string of arbitrary length. Variable substitution can occur anywhere in a word (e.g. button .b$num).
  • [ ] command substitution: everything inside the brackets is evaluated as a Tcl script.
  • \ backslash substitution: escape for $, newline, quotes, etc.
   

space.gif

  ../images/main/bulllet_4dots_orange.gif Quoting and comments
   

space.gif

  • double quotes: spaces, tabs, newlines, semicolons treated as ordinary characters (normal $, command, and backslash substitution occur as usual).
  • curly braces: all special characters lose their meaning (no substitutions take place). Braces defer evaluation.
  • comments: if first non-blank character is a '#', everything up to the newline is a comment. If '#' occurs anywhere else, it is treated as an ordinary character.
   

space.gif

   

space.gif

   

space.gif

   

space.gif

space2.gif

space2.gif

space2.gif

space2.gif

space2.gif

  

Copyright 1998-2014

Deepak Kumar Tala - All rights reserved

Do you have any Comment? mail me at:deepak@asic-world.com