Monday, November 17, 2008

Tcl Commands

Tcl stands for Tool Command Language. A command does something for you, like
output a string, compute a math expression, or display a widget on the screen.
Tcl casts everything into the mold of a command, even programming constructs
ike variable assignment and procedure definition. Tcl adds a tiny amount of
syntax needed to properly invoke commands, and then it leaves all the hard work
up to the command implementation.

The basic syntax for a Tcl command is:
command arg1 arg2 arg3 ...

The command is either the name of a built-in command or a Tcl procedure.
White space (i.e., spaces or tabs) is used to separate the command name and its
arguments, and a newline (i.e., the end of line character) or semicolon is used to
terminate a command. Tcl does not interpret the arguments to the commands
except to perform grouping, which allows multiple words in one argument, and
substitution, which is used with programming variables and nested command
calls. The behavior of the Tcl command processor can be summarized in three
basic steps:
• Argument grouping.
• Value substitution of nested commands, variables, and backslash escapes.
• Command invocation. It is up to the command to interpret its arguments.
This model is described in detail in this Chapter.
Hello, World!

Example 1–1 The “Hello, World!” example.
puts stdout {Hello, World!}
=> Hello, World!
In this example, the command is puts, which takes two arguments: an I/O
stream identifier and a string. puts writes the string to the I/O stream along
with a trailing newline character. There are two points to emphasize:
• Arguments are interpreted by the command. In the example, stdout is used
to identify the standard output stream. The use of stdout as a name is a
convention employed by puts and the other I/O commands. Also, stderr is
used to identify the standard error output, and stdin is used to identify the
standard input. Chapter 9 describes how to open other files for I/O.
• Curly braces are used to group words together into a single argument. The
puts command receives Hello, World! as its second argument.
The braces are not part of the value.
The braces are syntax for the interpreter, and they get stripped off before
the value is passed to the command. Braces group all characters, including newlines
and nested braces, until a matching brace is found. Tcl also uses double
quotes for grouping. Grouping arguments will be described in more detail later.
Variables 5

No comments: