A Tcl script is basically this line repeated again and again with different commands and arguments
command ARG1 ARG2 ... ARGnThe arguments are separated by white space, that is, blanks or tabs. For example,
set X 2has two arguments. Not all white space separates arguments. Here are the exceptions.
set X "Hellow orld"
set Y {Hellow orld}
set Z Hellow\ orld
Any given command may require a fixed number of arguments or may accept a variable number of arguments.
The command line pattern shown above applies not just to simple commands but also to control structures. Examples:
while CONDITION BODYBODY contains a sequence of command lines to be executed repeatedly and CONDITION is the expression that tests whether the repetition is to happen again.
In fact, there is no noticeable difference between control structures and user-defined procedures while could have been defined, and could be redefined, by you. To do so, you will need to understand how procedures are defined, how their name spaces are controlled, and how exceptions work. That will take several chapters.
proc NAME PARAMETERS BODYwhere NAME is the new procedure's name, PARAMETERS is a list of the new procedure's parameters and BODY is the sequence of the statements that make up the body of the new procedure. For example, here is the definition of a procedure named hello which takes no arguments and prints "Hello World!"
proc hello {} {puts "Hello World!"}
procedure_name ARG1 ARG2 ... ARGnHere, for example, is an execution of hello which takes no arguments.
hello
Since the first word in a Tcl command line can be a procedure or a built-in command, I will use the words "command" and "procedure" almost interchangeably.
Tcl is interpreted. The interpretor
Here is a weird example to make the point.
X + 1This would be the invocation of a procedure named X with two arguments if it is on a line by itself. In other contexts, it might be either
Skip these on first reading if you like.
It is possible to put multiple commands on one line of text if the commands are separated by semicolons.
Because # only signals a comment if it appears in place of a command name, comments usually appear on lines by themselves. Because semicolons signal the beginning of a new command, you can put a comment at the end of a command line by using ;# as a prefix for it.
Tcl works with lists a lot. Compared to a language like Lisp, the concept "list" in Tcl is somewhat murky but you will have a good idea of what is meant if you think of a command line as an example of a list. The first element of this list is the command name and subsequent elements are the arguments.
When using Tcl interactively, if a command requires arguments, you can execute a command without any arguments to see a short explanation of how it should be used. For example,
% set wrong # args: should be "set varName ?newValue?"
Some commands are really families of actions where the action is determined by the first argument. (See below in Action Families.) If you invoke such a command with an inappropriate first argument, you will get a list of the possibilities. For example,
% info ? bad option "?": should be args, body, cmdcount, commands, complete, default, exists, globals, level, library, locals, patchlevel, procs, script, tclversion, or vars % info tclversion 8.0
Jun 17, 1998