Tcl(3) Tcl(3)
______________________________________________________________________________
NAME
Tcl - overview of tool command language facilities
______________________________________________________________________________
INTRODUCTION
Tcl stands for ``tool command language'' and is pronounced ``tickle.''
It is actually two things: a language and a library. First, Tcl is a
simple textual language, intended primarily for issuing commands to
interactive programs such as text editors, debuggers, illustrators, and
shells. It has a simple syntax and is also programmable, so Tcl users
can write command procedures to provide more powerful commands than
those in the built-in set.
Second, Tcl is a library package that can be embedded in application
programs. The Tcl library consists of a parser for the Tcl language,
routines to implement the Tcl built-in commands, and procedures that
allow each application to extend Tcl with additional commands specific
to that application. The application program generates Tcl commands
and passes them to the Tcl parser for execution. Commands may be genâ
erated by reading characters from an input source, or by associating
command strings with elements of the application's user interface, such
as menu entries, buttons, or keystrokes. When the Tcl library receives
commands it parses them into component fields and executes built-in
commands directly. For commands implemented by the application, Tcl
calls back to the application to execute the commands. In many cases
commands will invoke recursive invocations of the Tcl interpreter by
passing in additional strings to execute (procedures, looping commands,
and conditional commands all work in this way).
An application program gains three advantages by using Tcl for its comâ
mand language. First, Tcl provides a standard syntax: once users know
Tcl, they will be able to issue commands easily to any Tcl-based appliâ
cation. Second, Tcl provides programmability. All a Tcl application
needs to do is to implement a few application-specific low-level comâ
mands. Tcl provides many utility commands plus a general programming
interface for building up complex command procedures. By using Tcl,
applications need not re-implement these features. Third, Tcl can be
used as a common language for communicating between applications. â
Inter-application communication is not built into the Tcl core â
described here, but various add-on libraries, such as the Tk toolkit, â
allow applications to issue commands to each other. This makes it posâ â
sible for applications to work together in much more powerful ways than â
was previously possible.
This manual page focuses primarily on the Tcl language. It describes
the language syntax and the built-in commands that will be available in
any application based on Tcl. The individual library procedures are
described in more detail in separate manual pages, one per procedure.
INTERPRETERS
The central data structure in Tcl is an interpreter (C type
``Tcl_Interp''). An interpreter consists of a set of command bindings,
a set of variable values, and a few other miscellaneous pieces of
state. Each Tcl command is interpreted in the context of a particular
interpreter. Some Tcl-based applications will maintain multiple interâ
preters simultaneously, each associated with a different widget or porâ
tion of the application. Interpreters are relatively lightweight
structures. They can be created and deleted quickly, so application
programmers should feel free to use multiple interpreters if that simâ
plifies the application. Eventually Tcl will provide a mechanism for
sending Tcl commands and results back and forth between interpreters,
even if the interpreters are managed by different processes.
DATA TYPES
Tcl supports only one type of data: strings. All commands, all arguâ
ments to commands, all command results, and all variable values are
strings. Where commands require numeric arguments or return numeric
results, the arguments and results are passed as strings. Many comâ
mands expect their string arguments to have certain formats, but this
interpretation is up to the individual commands. For example, arguâ
ments often contain Tcl command strings, which may get executed as part
of the commands. The easiest way to understand the Tcl interpreter is
to remember that everything is just an operation on a string. In many
cases Tcl constructs will look similar to more structured constructs
from other languages. However, the Tcl constructs are not structured
at all; they are just strings of characters, and this gives them a difâ
ferent behavior than the structures they may look like.
Although the exact interpretation of a Tcl string depends on who is
doing the interpretation, there are three common forms that strings
take: commands, expressions, and lists. The major sections below disâ
cuss these three forms in more detail.
BASIC COMMAND SYNTAX
The Tcl language has syntactic similarities to both the Unix shells and
Lisp. However, the interpretation of commands is different in Tcl than
in either of those other two systems. A Tcl command string consists of
one or more commands separated by newline characters or semi-colons.
Each command consists of a collection of fields separated by white
space (spaces or tabs). The first field must be the name of a command,
and the additional fields, if any, are arguments that will be passed to
that command. For example, the command set a 22 has three fields: the
first, set, is the name of a Tcl command, and the last two, a and 22,
will be passed as arguments to the set command. The command name may
refer either to a built-in Tcl command, an application-specific command
bound in with the library procedure Tcl_CreateCommand, or a command
procedure defined with the proc built-in command. Arguments are passed
literally as text strings. Individual commands may interpret those
strings in any fashion they wish. The set command, for example, will
treat its first argument as the name of a variable and its second arguâ
ment as a string value to assign to that variable. For other commands
arguments may be interpreted as integers, lists, file names, or Tcl
commands.
Command names should normally be typed completely (e.g. no abbreviaâ â
tions). However, if the Tcl interpreter cannot locate a command it â
invokes a special command named unknown which attempts to find or creâ â
ate the command. For example, at many sites unknown will search â
through library directories for the desired command and create it as a â
Tcl procedure if it is found. The unknown command often provides autoâ â
matic completion of abbreviated commands, but usually only for commands â
that were typed interactively. It's probably a bad idea to use abbreâ â
viations in command scripts and other forms that will be re-used over â
time: changes to the command set may cause abbreviations to become â
ambiguous, resulting in scripts that no longer work.
COMMENTS
If the first non-blank character in a command is #, then everything
from the # up through the next newline character is treated as a comâ
ment and ignored. When comments are embedded inside nested commands
(e.g. fields enclosed in braces) they must have properly-matched braces
(this is necessary because when Tcl parses the top-level command it
doesn't yet know that the nested field will be used as a command so it
cannot process the nested comment character as a comment).
GROUPING ARGUMENTS WITH DOUBLE-QUOTES
Normally each argument field ends at the next white space, but double-
quotes may be used to create arguments with embedded space. If an
argument field begins with a double-quote, then the argument isn't terâ
minated by white space (including newlines) or a semi-colon (see below
for information on semi-colons); instead it ends at the next double-
quote character. The double-quotes are not included in the resulting
argument. For example, the command set a "This is a single argument"
will pass two arguments to set: a and This is a single argument.
Within double-quotes, command substitutions, variable substitutions,
and backslash substitutions still occur, as described below. If the
first character of a command field is not a quote, then quotes receive
no special interpretation in the parsing of that field.
GROUPING ARGUMENTS WITH BRACES
Curly braces may also be used for grouping arguments. They are similar
to quotes except for two differences. First, they nest; this makes
them easier to use for complicated arguments like nested Tcl command
strings. Second, the substitutions described below for commands, variâ
ables, and backslashes do not occur in arguments enclosed in braces, so
braces can be used to prevent substitutions where they are undesirable.
If an argument field begins with a left brace, then the argument ends
at the matching right brace. Tcl will strip off the outermost layer of
braces and pass the information between the braces to the command withâ
out any further modification. For example, in the command set a {xyz a
{b c d}} the set command will receive two arguments: a and xyz a {b c
d}.
When braces or quotes are in effect, the matching brace or quote need
not be on the same line as the starting quote or brace; in this case
the newline will be included in the argument field along with any other
characters up to the matching brace or quote. For example, the eval
command takes one argument, which is a command string; eval invokes the
Tcl interpreter to execute the command string. The command eval {
set a 22 set b 33 } will assign the value 22 to a and 33 to
b.
If the first character of a command field is not a left brace, then
neither left nor right braces in the field will be treated specially
(except as part of variable substitution; see below).
COMMAND SUBSTITUTION WITH BRACKETS
If an open bracket occurs in a field of a command, then command substiâ
tution occurs (except for fields enclosed in braces). All of the text
up to the matching close bracket is treated as a Tcl command and exeâ
cuted immediately. Then the result of that command is substituted for
the bracketed text. For example, consider the command set a [set b]
When the set command has only a single argument, it is the name of a
variable and set returns the contents of that variable. In this case,
if variable b has the value foo, then the command above is equivalent
to the command set a foo Brackets can be used in more complex ways.
For example, if the variable b has the value foo and the variable c has
the value gorp, then the command set a xyz[set b].[set c] is equivalent
to the command set a xyzfoo.gorp A bracketed command may contain multiâ â
ple commands separated by newlines or semi-colons in the usual fashion. â
In this case the value of the last command is used for substitution. â
For example, the command set a x[set b 22 expr $b+2]x is equivalent to â
the command set a x24x If a field is enclosed in braces then the brackâ
ets and the characters between them are not interpreted specially; they
are passed through to the argument verbatim.
VARIABLE SUBSTITUTION WITH $
The dollar sign ($) may be used as a special shorthand form for substiâ
tuting variable values. If $ appears in an argument that isn't
enclosed in braces then variable substitution will occur. The characâ
ters after the $, up to the first character that isn't a number, letâ
ter, or underscore, are taken as a variable name and the string value
of that variable is substituted for the name. For example, if variable â
foo has the value test, then the command set a $foo.c is equivalent to â
the command set a test.c â
There are two special forms for variable substitution. If the next â
character after the name of the variable is an open parenthesis, then â
the variable is assumed to be an array name, and all of the characters â
between the open parenthesis and the next close parenthesis are taken â
as an index into the array. Command substitutions and variable substiâ â
tutions are performed on the information between the parentheses before â
it is used as an index. For example, if the variable x is an array â
with one element named first and value 87 and another element named 14 â
and value more, then the command set a xyz$x(first)zyx is equivalent to â
the command set a xyz87zyx If the variable index has the value 14, then â
the command set a xyz$x($index)zyx is equivalent to the command set a â
xyzmorezyx For more information on arrays, see VARIABLES AND ARRAYS â
below. â
The second special form for variables occurs when the dollar sign is â
followed by an open curly brace. In this case the variable name conâ â
sists of all the characters up to the next curly brace. Array referâ â
ences are not possible in this form: the name between braces is â
assumed to refer to a scalar variable. For example, if variable foo â
has the value test, then the command set a abc${foo}bar is equivalent â
to the command set a abctestbar Variable substitution does not occur in
arguments that are enclosed in braces: the dollar sign and variable
name are passed through to the argument verbatim.
The dollar sign abbreviation is simply a shorthand form. $a is comâ
pletely equivalent to [set a]; it is provided as a convenience to
reduce typing.
SEPARATING COMMANDS WITH SEMI-COLONS
Normally, each command occupies one line (the command is terminated by
a newline character). However, semi-colon (``;'') is treated as a comâ
mand separator character; multiple commands may be placed on one line
by separating them with a semi-colon. Semi-colons are not treated as
command separators if they appear within curly braces or double-quotes.
BACKSLASH SUBSTITUTION
Backslashes may be used to insert non-printing characters into command
fields and also to insert special characters like braces and brackets
into fields without them being interpreted specially as described
above. The backslash sequences understood by the Tcl interpreter are
listed below. In each case, the backslash sequence is replaced by the
given character:
\b Backspace (0x8).
\f Form feed (0xc).
\n Newline (0xa).
\r Carriage-return (0xd).
\t Tab (0x9).
\v Vertical tab (0xb).
\{ Left brace (``{'').
\} Right brace (``}'').
\[ Open bracket (``['').
\] Close bracket (``]'').
\$ Dollar sign (``$'').
\<space> Space (`` ''): doesn't terminate argument.
\; Semi-colon: doesn't terminate command.
\" Double-quote.
\<newline> Nothing: this joins two lines together into a sinâ
gle line. This backslash feature is unique in that
it will be applied even when the sequence occurs
within braces.
\\ Backslash (``\'').
\ddd The digits ddd (one, two, or three of them) give
the octal value of the character. Null characters
may not be embedded in command fields; if ddd is
zero then the backslash sequence is ignored (i.e.
it maps to an empty string).
For example, in the command set a \{x\[\ yz\141 the second argument to
set will be ``{x[ yza''.
If a backslash is followed by something other than one of the options
described above, then the backslash is transmitted to the argument
field without any special processing, and the Tcl scanner continues
normal processing with the next character. For example, in the command
set \*a \\\{foo The first argument to set will be \*a and the second
argument will be \{foo.
If an argument is enclosed in braces, then backslash sequences inside
the argument are parsed but no substitution occurs (except for backâ
slash-newline): the backslash sequence is passed through to the arguâ
ment as is, without making any special interpretation of the characters
in the backslash sequence. In particular, backslashed braces are not
counted in locating the matching right brace that terminates the arguâ
ment. For example, in the command set a {\{abc} the second argument to
set will be \{abc.
This backslash mechanism is not sufficient to generate absolutely any
argument structure; it only covers the most common cases. To produce
particularly complicated arguments it is probably easiest to use the
format command along with command substitution.
COMMAND SUMMARY
[1] A command is just a string.
[2] Within a string commands are separated by newlines or semi-
colons (unless the newline or semi-colon is within braces or
brackets or is backslashed).
[3] A command consists of fields. The first field is the name of
the command. The other fields are strings that are passed to
that command as arguments.
[4] Fields are normally separated by white space.
[5] Double-quotes allow white space and semi-colons to appear within
a single argument. Command substitution, variable substitution,
and backslash substitution still occur inside quotes.
[6] Braces defer interpretation of special characters. If a field
begins with a left brace, then it consists of everything between
the left brace and the matching right brace. The braces themâ
selves are not included in the argument. No further processing
is done on the information between the braces except that backâ
slash-newline sequences are eliminated.
[7] If a field doesn't begin with a brace then backslash, variable,
and command substitution are done on the field. Only a single
level of processing is done: the results of one substitution
are not scanned again for further substitutions or any other
special treatment. Substitution can occur on any field of a
command, including the command name as well as the arguments.
[8] If the first non-blank character of a command is a #, everything
from the # up through the next newline is treated as a comment
and ignored.
EXPRESSIONS
The second major interpretation applied to strings in Tcl is as expresâ â
sions. Several commands, such as expr, for, and if, treat one or more â
of their arguments as expressions and call the Tcl expression procesâ â
sors (Tcl_ExprLong, Tcl_ExprBoolean, etc.) to evaluate them. The operâ â
ators permitted in Tcl expressions are a subset of the operators perâ â
mitted in C expressions, and they have the same meaning and precedence â
as the corresponding C operators. Expressions almost always yield â
numeric results (integer or floating-point values). For example, the â
expression 8.2 + 6 evaluates to 14.2. Tcl expressions differ from C â
expressions in the way that operands are specified, and in that Tcl â
expressions support non-numeric operands and string comparisons. â
A Tcl expression consists of a combination of operands, operators, and â
parentheses. White space may be used between the operands and operaâ â
tors and parentheses; it is ignored by the expression processor. Where â
possible, operands are interpreted as integer values. Integer values â
may be specified in decimal (the normal case), in octal (if the first â
character of the operand is 0), or in hexadecimal (if the first two â
characters of the operand are 0x). If an operand does not have one of â
the integer formats given above, then it is treated as a floating-point â
number if that is possible. Floating-point numbers may be specified in â
any of the ways accepted by an ANSI-compliant C compiler (except that â
the ``f'', ``F'', ``l'', and ``L'' suffixes will not be permitted in â
most installations). For example, all of the following are valid â
floating-point numbers: 2.1, 3., 6e4, 7.91e+16. If no numeric interâ â
pretation is possible, then an operand is left as a string (and only a â
limited set of operators may be applied to it). â
Operands may be specified in any of the following ways: â
[1] â
As an numeric value, either integer or floating-point. â
[2] â
As a Tcl variable, using standard $ notation. The variable's â
value will be used as the operand. â
[3] â
As a string enclosed in double-quotes. The expression parser â
will perform backslash, variable, and command substitutions on â
the information between the quotes, and use the resulting value â
as the operand â
[4] â
As a string enclosed in braces. The characters between the open â
brace and matching close brace will be used as the operand withâ â
out any substitutions. â
[5] â
As a Tcl command enclosed in brackets. The command will be exeâ â
cuted and its result will be used as the operand. â
Where substitutions occur above (e.g. inside quoted strings), they are â
performed by the expression processor. However, an additional layer of â
substitution may already have been performed by the command parser â
before the expression processor was called. As discussed below, it is â
usually best to enclose expressions in braces to prevent the command â
parser from performing substitutions on the contents. â
For some examples of simple expressions, suppose the variable a has the â
value 3 and the variable b has the value 6. Then the expression on the â
left side of each of the lines below will evaluate to the value on the â
right side of the line: 3.1 + $a 6.1 2 + â
"$a.$b" 5.6 4*[llength "6 2"] 8 {word one} < "word â
$a" 0 â
The valid operators are listed below, grouped in decreasing order of â
precedence: â
- ~ ! â
Unary minus, bit-wise NOT, logical NOT. None of â
these operands may be applied to string operands, â
and bit-wise NOT may be applied only to integers. â
* / % â
Multiply, divide, remainder. None of these operâ â
ands may be applied to string operands, and remainâ â
der may be applied only to integers. â
+ - â
Add and subtract. Valid for any numeric operands. â
<< >> â
Left and right shift. Valid for integer operands â
only. â
< > <= >= â
Boolean less, greater, less than or equal, and â
greater than or equal. Each operator produces 1 if â
the condition is true, 0 otherwise. These operaâ â
tors may be applied to strings as well as numeric â
operands, in which case string comparison is used. â
== != â
Boolean equal and not equal. Each operator proâ â
duces a zero/one result. Valid for all operand â
types. â
& â
Bit-wise AND. Valid for integer operands only. â
^ â
Bit-wise exclusive OR. Valid for integer operands â
only. â
| â
Bit-wise OR. Valid for integer operands only. â
&& â
Logical AND. Produces a 1 result if both operands â
are non-zero, 0 otherwise. Valid for numeric operâ â
ands only (integers or floating-point). â
|| â
Logical OR. Produces a 0 result if both operands â
are zero, 1 otherwise. Valid for numeric operands â
only (integers or floating-point). â
x?y:z â
If-then-else, as in C. If x evaluates to non-zero, â
then the result is the value of y. Otherwise the â
result is the value of z. The x operand must have â
a numeric value. â
See the C manual for more details on the results produced by each operâ â
ator. All of the binary operators group left-to-right within the same â
precedence level. For example, the expression 4*2 < 7 evaluates to 0. â
The &&, ||, and ?: operators have ``lazy evaluation'', just as in C, â
which means that operands are not evaluated if they are not needed to â
determine the outcome. For example, in $v ? [a] : [b] only one of [a] â
or [b] will actually be evaluated, depending on the value of $v. â
All internal computations involving integers are done with the C type â
long, and all internal computations involving floating-point are done â
with the C type double. When converting a string to floating-point, â
exponent overflow is detected and results in a Tcl error. For converâ â
sion to integer from string, detection of overflow depends on the â
behavior of some routines in the local C library, so it should be â
regarded as unreliable. In any case, overflow and underflow are generâ â
ally not detected reliably for intermediate results. â
Conversion among internal representations for integer, floating-point, â
and string operands is done automatically as needed. For arithmetic â
computations, integers are used until some floating-point number is â
introduced, after which floating-point is used. For example, 5 / 4 â
yields the result 1, while 5 / 4.0 5 / ( [string length "abcd"] + 0.0 ) â
both yield the result 1.25. â
String values may be used as operands of the comparison operators, â
although the expression evaluator tries to do comparisons as integer or â
floating-point when it can. If one of the operands of a comparison is â
a string and the other has a numeric value, the numeric operand is conâ â
verted back to a string using the C sprintf format specifier %d for â
integers and %g for floating-point values. For example, the expresâ â
sions "0x03" > "2" "0y" < "0x12" both evaluate to 1. The first comparâ â
ison is done using integer comparison, and the second is done using â
string comparison after the second operand is converted to the string â
``18''.
In general it is safest to enclose an expression in braces when enterâ
ing it in a command: otherwise, if the expression contains any white
space then the Tcl interpreter will split it among several arguments.
For example, the command expr $a + $b results in three arguments being
passed to expr: $a, +, and $b. In addition, if the expression isn't
in braces then the Tcl interpreter will perform variable and command
substitution immediately (it will happen in the command parser rather
than in the expression parser). In many cases the expression is being
passed to a command that will evaluate the expression later (or even
many times if, for example, the expression is to be used to decide when
to exit a loop). Usually the desired goal is to re-do the variable or
command substitutions each time the expression is evaluated, rather
than once and for all at the beginning. For example, the command for
{set i 1} $i<=10 {incr i} {...}*** WRONG *** is probably intended to
iterate over all values of i from 1 to 10. After each iteration of the
body of the loop, for will pass its second argument to the expression
evaluator to see whether or not to continue processing. Unfortunately,
in this case the value of i in the second argument will be substituted
once and for all when the for command is parsed. If i was 0 before the
for command was invoked then for's second argument will be 0<=10 which
will always evaluate to 1, even though i's value eventually becomes
greater than 10. In the above case the loop will never terminate.
Instead, the expression should be placed in braces: for {set i 1}
{$i<=10} {incr i} {...}*** RIGHT *** This causes the substitution of
i's value to be delayed; it will be re-done each time the expression is
evaluated, which is the desired result.
LISTS
The third major way that strings are interpreted in Tcl is as lists. A
list is just a string with a list-like structure consisting of fields
separated by white space. For example, the string Al Sue Anne John is
a list with four elements or fields. Lists have the same basic strucâ
ture as command strings, except that a newline character in a list is
treated as a field separator just like space or tab. Conventions for
braces and quotes and backslashes are the same for lists as for comâ
mands. For example, the string a b\ c {d e {f g h}} is a list with
three elements: a, b c, and d e {f g h}. Whenever an element is
extracted from a list, the same rules about braces and quotes and backâ
slashes are applied as for commands. Thus in the example above when
the third element is extracted from the list, the result is d e {f g h}
(when the field was extracted, all that happened was to strip off the
outermost layer of braces). Command substitution and variable substiâ
tution are never made on a list (at least, not by the list-processing
commands; the list can always be passed to the Tcl interpreter for
evaluation).
The Tcl commands concat, foreach, lappend, lindex, linsert, list, â
llength, lrange, lreplace, lsearch, and lsort allow you to build lists,
extract elements from them, search them, and perform other list-related
functions.
REGULAR EXPRESSIONS
Tcl provides two commands that support string matching using egrep- â
style regular expressions: regexp and regsub. Regular expressions are â
implemented using Henry Spencer's package, and the description of reguâ â
lar expressions below is copied verbatim from his manual entry. â
A regular expression is zero or more branches, separated by ``|''. It â
matches anything that matches one of the branches. â
A branch is zero or more pieces, concatenated. It matches a match for â
the first, followed by a match for the second, etc. â
A piece is an atom possibly followed by ``*'', ``+'', or ``?''. An â
atom followed by ``*'' matches a sequence of 0 or more matches of the â
atom. An atom followed by ``+'' matches a sequence of 1 or more â
matches of the atom. An atom followed by ``?'' matches a match of the â
atom, or the null string. â
An atom is a regular expression in parentheses (matching a match for â
the regular expression), a range (see below), ``.'' (matching any sinâ â
gle character), ``^'' (matching the null string at the beginning of the â
input string), ``$'' (matching the null string at the end of the input â
string), a ``\'' followed by a single character (matching that characâ â
ter), or a single character with no other significance (matching that â
character). â
A range is a sequence of characters enclosed in ``[]''. It normally â
matches any single character from the sequence. If the sequence begins â
with ``^'', it matches any single character not from the rest of the â
sequence. If two characters in the sequence are separated by ``-'', â
this is shorthand for the full list of ASCII characters between them â
(e.g. ``[0-9]'' matches any decimal digit). To include a literal ``]'' â
in the sequence, make it the first character (following a possible â
``^''). To include a literal ``-'', make it the first or last characâ â
ter. â
If a regular expression could match two different parts of a string, it â
will match the one which begins earliest. If both begin in the same â
place but match different lengths, or match the same length in differâ â
ent ways, life gets messier, as follows. â
In general, the possibilities in a list of branches are considered in â
left-to-right order, the possibilities for ``*'', ``+'', and ``?'' are â
considered longest-first, nested constructs are considered from the â
outermost in, and concatenated constructs are considered leftmost- â
first. The match that will be chosen is the one that uses the earliest â
possibility in the first choice that has to be made. If there is more â
than one choice, the next will be made in the same manner (earliest â
possibility) subject to the decision on the first choice. And so â
forth. â
For example, ``(ab|a)b*c'' could match ``abc'' in one of two ways. The â
first choice is between ``ab'' and ``a''; since ``ab'' is earlier, and â
does lead to a successful overall match, it is chosen. Since the ``b'' â
is already spoken for, the ``b*'' must match its last possibilityâthe â
empty stringâsince it must respect the earlier choice. â
In the particular case where no ``|''s are present and there is only â
one ``*'', ``+'', or ``?'', the net effect is that the longest possible â
match will be chosen. So ``ab*'', presented with ``xabbbby'', will â
match ``abbbb''. Note that if ``ab*'' is tried against ``xabyabbbz'', â
it will match ``ab'' just after ``x'', due to the begins-earliest rule. â
(In effect, the decision on where to start the match is the first â
choice to be made, hence subsequent choices must respect it even if â
this leads them to less-preferred alternatives.)
COMMAND RESULTS
Each command produces two results: a code and a string. The code
indicates whether the command completed successfully or not, and the
string gives additional information. The valid codes are defined in
tcl.h, and are:
TCL_OK This is the normal return code, and indiâ
cates that the command completed successâ
fully. The string gives the command's
return value.
TCL_ERROR Indicates that an error occurred; the string
gives a message describing the error. In â
addition, the global variable errorInfo will â
contain human-readable information describâ â
ing which commands and procedures were being â
executed when the error occurred, and the â
global variable errorCode will contain â
machine-readable details about the error, if â
they are available. See the section BUILT- â
IN VARIABLES below for more information.
TCL_RETURN Indicates that the return command has been
invoked, and that the current procedure (or
top-level command or source command) should
return immediately. The string gives the
return value for the procedure or command.
TCL_BREAK Indicates that the break command has been
invoked, so the innermost loop should abort
immediately. The string should always be
empty.
TCL_CONTINUE Indicates that the continue command has been
invoked, so the innermost loop should go on
to the next iteration. The string should
always be empty.
Tcl programmers do not normally need to think about return codes, since
TCL_OK is almost always returned. If anything else is returned by a
command, then the Tcl interpreter immediately stops processing commands
and returns to its caller. If there are several nested invocations of
the Tcl interpreter in progress, then each nested command will usually
return the error to its caller, until eventually the error is reported
to the top-level application code. The application will then display
the error message for the user.
In a few cases, some commands will handle certain ``error'' conditions
themselves and not return them upwards. For example, the for command
checks for the TCL_BREAK code; if it occurs, then for stops executing
the body of the loop and returns TCL_OK to its caller. The for command
also handles TCL_CONTINUE codes and the procedure interpreter handles
TCL_RETURN codes. The catch command allows Tcl programs to catch
errors and handle them without aborting command interpretation any furâ
ther.
PROCEDURES
Tcl allows you to extend the command interface by defining procedures.
A Tcl procedure can be invoked just like any other Tcl command (it has
a name and it receives one or more arguments). The only difference is
that its body isn't a piece of C code linked into the program; it is a
string containing one or more other Tcl commands. See the proc command
for information on how to define procedures and what happens when they
are invoked.
VARIABLES - SCALARS AND ARRAYS
Tcl allows the definition of variables and the use of their values â
either through $-style variable substitution, the set command, or a few â
other mechanisms. Variables need not be declared: a new variable will â
automatically be created each time a new variable name is used. â
Tcl supports two types of variables: scalars and arrays. A scalar â
variable has a single value, whereas an array variable can have any â
number of elements, each with a name (called its ``index'') and a â
value. Array indexes may be arbitrary strings; they need not be â
numeric. Parentheses are used refer to array elements in Tcl commands. â
For example, the command set x(first) 44 will modify the element of x â
whose index is first so that its new value is 44. Two-dimensional â
arrays can be simulated in Tcl by using indexes that contain multiple â
concatenated values. For example, the commands set a(2,3) 1 set a(3,6) â
2 set the elements of a whose indexes are 2,3 and 3,6. â
In general, array elements may be used anywhere in Tcl that scalar â
variables may be used. If an array is defined with a particular name, â
then there may not be a scalar variable with the same name. Similarly, â
if there is a scalar variable with a particular name then it is not â
possible to make array references to the variable. To convert a scalar â
variable to an array or vice versa, remove the existing variable with â
the unset command. â
The array command provides several features for dealing with arrays, â
such as querying the names of all the elements of the array and searchâ â
ing through the array one element at a time.
Variables may be either global or local. If a variable name is used
when a procedure isn't being executed, then it automatically refers to
a global variable. Variable names used within a procedure normally
refer to local variables associated with that invocation of the proceâ
dure. Local variables are deleted whenever a procedure exits. The
global command may be used to request that a name refer to a global
variable for the duration of the current procedure (this is somewhat
analogous to extern in C).
BUILT-IN COMMANDS
The Tcl library provides the following built-in commands, which will be
available in any application using Tcl. In addition to these built-in
commands, there may be additional commands defined by each application,
plus commands defined as Tcl procedures. In the command syntax
descriptions below, words in boldface are literals that you type verbaâ
tim to Tcl. Words in italics are meta-symbols; they serve as names for
any of a range of values that you can type. Optional arguments or
groups of arguments are indicated by enclosing them in question-marks.
Ellipses (``...'') indicate that any number of additional arguments or
groups of arguments may appear, in the same format as the preceding
argument(s).
append varName value ?value value ...?
Append all of the value arguments to the current value of variâ â
able varName. If varName doesn't exist, it is given a value â
equal to the concatenation of all the value arguments. This â
command provides an efficient way to build up long variables â
incrementally. For example, ``append a $b'' is much more effiâ â
cient than ``set a $a$b'' if $a is long.
array option arrayName ?arg arg ...?
This command performs one of several operations on the variable â
given by arrayName. ArrayName must be the name of an existing â
array variable. The option argument determines what action is â
carried out by the command. The legal options (which may be â
abbreviated) are: â
array anymore arrayName searchId â
Returns 1 if there are any more elements left to be proâ â
cessed in an array search, 0 if all elements have already â
been returned. SearchId indicates which search on arrayâ â
Name to check, and must have been the return value from a â
previous invocation of array startsearch. This option is â
particularly useful if an array has an element with an â
empty name, since the return value from array nextelement â
won't indicate whether the search has been completed. â
array donesearch arrayName searchId â
This command terminates an array search and destroys all â
the state associated with that search. SearchId indiâ â
cates which search on arrayName to destroy, and must have â
been the return value from a previous invocation of array â
startsearch. Returns an empty string. â
array names arrayName â
Returns a list containing the names of all of the eleâ â
ments in the array. If there are no elements in the â
array then an empty string is returned. â
array nextelement arrayName searchId â
Returns the name of the next element in arrayName, or an â
empty string if all elements of arrayName have already â
been returned in this search. The searchId argument â
identifies the search, and must have been the return â
value of an array startsearch command. Warning: if eleâ â
ments are added to or deleted from the array, then all â
searches are automatically terminated just as if array â
donesearch had been invoked; this will cause array nexâ â
telement operations to fail for those searches. â
array size arrayName â
Returns a decimal string giving the number of elements in â
the array. â
array startsearch arrayName â
This command initializes an element-by-element search â
through the array given by arrayName, such that invocaâ â
tions of the array nextelement command will return the â
names of the individual elements in the array. When the â
search has been completed, the array donesearch command â
should be invoked. The return value is a search identiâ â
fier that must be used in array nextelement and array â
donesearch commands; it allows multiple searches to be â
underway simultaneously for the same array.
break This command may be invoked only inside the body of a loop comâ
mand such as for or foreach or while. It returns a TCL_BREAK
code to signal the innermost containing loop command to return
immediately.
case string ?in? patList body ?patList body ...?
case string ?in? {patList body ?patList body ...?}
Match string against each of the patList arguments in order. If
one matches, then evaluate the following body argument by passâ
ing it recursively to the Tcl interpreter, and return the result
of that evaluation. Each patList argument consists of a single
pattern or list of patterns. Each pattern may contain any of
the wild-cards described under string match. If a patList arguâ
ment is default, the corresponding body will be evaluated if no
patList matches string. If no patList argument matches string
and no default is given, then the case command returns an empty
string.
Two syntaxes are provided. The first uses a separate argument
for each of the patterns and commands; this form is convenient
if substitutions are desired on some of the patterns or comâ
mands. The second form places all of the patterns and commands â
together into a single argument; the argument must have proper â
list structure, with the elements of the list being the patterns â
and commands. The second form makes it easy to construct multi- â
line case commands, since the braces around the whole list make â
it unnecessary to include a backslash at the end of each line. â
Since the patList arguments are in braces in the second form, no â
command or variable substitutions are performed on them; this â
makes the behavior of the second form different than the first â
form in some cases. â
Below are some examples of case commands: case abc in {a b} â
{format 1} default {format 2} a* {format 3} will return 3, case â
a in { {a b} {format 1} default {format 2} a* {format 3} } â
will return 1, and case xyz { {a b} {format 1} default â
{format 2} a* {format 3} } will return 2.
catch command ?varName?
The catch command may be used to prevent errors from aborting
command interpretation. Catch calls the Tcl interpreter recurâ
sively to execute command, and always returns a TCL_OK code,
regardless of any errors that might occur while executing comâ
mand. The return value from catch is a decimal string giving
the code returned by the Tcl interpreter after executing comâ
mand. This will be 0 (TCL_OK) if there were no errors in comâ
mand; otherwise it will have a non-zero value corresponding to
one of the exceptional return codes (see tcl.h for the definiâ
tions of code values). If the varName argument is given, then
it gives the name of a variable; catch will set the value of the
variable to the string returned from command (either a result or
an error message).
cd ?dirName?
Change the current working directory to dirName, or to the home â
directory (as specified in the HOME environment variable) if â
dirName is not given. If dirName starts with a tilde, then â
tilde-expansion is done as described for Tcl_TildeSubst. â
Returns an empty string. This command can potentially be disâ â
ruptive to an application, so it may be removed in some applicaâ â
tions. â
close fileId â
Closes the file given by fileId. FileId must be the return â
value from a previous invocation of the open command; after this â
command, it should not be used anymore. If fileId refers to a â
command pipeline instead of a file, then close waits for the â
children to complete. The normal result of this command is an â
empty string, but errors are returned if there are problems in â
closing the file or waiting for children to complete.
concat arg ?arg ...?
This command treats each argument as a list and concatenates
them into a single list. It permits any number of arguments.
For example, the command
concat a b {c d e} {f {g h}} will return a b c d e f {g h} as
its result.
continue
This command may be invoked only inside the body of a loop comâ
mand such as for or foreach or while. It returns a TCL_CONâ
TINUE code to signal the innermost containing loop command to
skip the remainder of the loop's body but continue with the next
iteration of the loop.
eof fileId
Returns 1 if an end-of-file condition has occurred on fileId, 0 â
otherwise. FileId must have been the return value from a previâ â
ous call to open, or it may be stdin, stdout, or stderr to refer â
to one of the standard I/O channels.
error message ?info? ?code?
Returns a TCL_ERROR code, which causes command interpretation to
be unwound. Message is a string that is returned to the appliâ
cation to indicate what went wrong.
If the info argument is provided and is non-empty, it is used to
initialize the global variable errorInfo. errorInfo is used to
accumulate a stack trace of what was in progress when an error
occurred; as nested commands unwind, the Tcl interpreter adds
information to errorInfo. If the info argument is present, it
is used to initialize errorInfo and the first increment of
unwind information will not be added by the Tcl interpreter. In
other words, the command containing the error command will not
appear in errorInfo; in its place will be info. This feature is
most useful in conjunction with the catch command: if a caught
error cannot be handled successfully, info can be used to return
a stack trace reflecting the original point of occurrence of the
error: catch {...} errMsg set savedInfo $errorInfo ... error
$errMsg $savedInfo
If the code argument is present, then its value is stored in the â
errorCode global variable. This variable is intended to hold a â
machine-readable description of the error in cases where such â
information is available; see the section BUILT-IN VARIABLES â
below for information on the proper format for the variable. If â
the code argument is not present, then errorCode is automatiâ â
cally reset to ``NONE'' by the Tcl interpreter as part of proâ â
cessing the error generated by the command.
eval arg ?arg ...?
Eval takes one or more arguments, which together comprise a Tcl
command (or collection of Tcl commands separated by newlines in
the usual way). Eval concatenates all its arguments in the same
fashion as the concat command, passes the concatenated string to
the Tcl interpreter recursively, and returns the result of that
evaluation (or any error generated by it).
exec arg ?arg ...?
This command treats its arguments as the specification of one or â
more UNIX commands to execute as subprocesses. The commands â
take the form of a standard shell pipeline; ``|'' arguments sepâ â
arate commands in the pipeline and cause standard output of the â
preceding command to be piped into standard input of the next â
command. â
Under normal conditions the result of the exec command consists â
of the standard output produced by the last command in the pipeâ â
line. If any of the commands in the pipeline exit abnormally or â
are killed or suspended, then exec will return an error and the â
error message will include the pipeline's output followed by â
error messages describing the abnormal terminations; the errorâ â
Code variable will contain additional information about the last â
abnormal termination encountered. If any of the commands writes â
to its standard error file, then exec will return an error, and â
the error message will include the pipeline's output, followed â
by messages about abnormal terminations (if any), followed by â
the standard error output. â
If the last character of the result or error message is a newâ â
line then that character is deleted from the result or error â
message for consistency with normal Tcl return values. â
If an arg has the value ``>'' then the following argument is â
taken as the name of a file and the standard output of the last â
command in the pipeline is redirected to the file. In this sitâ â
uation exec will normally return an empty string. â
If an arg has the value ``<'' then the following argument is â
taken as the name of a file to use for standard input to the â
first command in the pipeline. If an argument has the value â
``<<'' then the following argument is taken as an immediate â
value to be passed to the first command as standard input. If â
there is no ``<'' or ``<<'' argument then the standard input for â
the first command in the pipeline is taken from the applicaâ â
tion's current standard input. â
If the last arg is ``&'' then the command will be executed in â
background. In this case the standard output from the last comâ â
mand in the pipeline will go to the application's standard outâ â
put unless redirected in the command, and error output from all â
the commands in the pipeline will go to the application's stanâ â
dard error file. â
Each arg becomes one word for a command, except for ``|'', â
``<'', ``<<'', ``>'', and ``&'' arguments, and the arguments â
that follow ``<'', ``<<'', and ``>''. The first word in each â
command is taken as the command name; tilde-substitution is perâ â
formed on it, and the directories in the PATH environment variâ â
able are searched for an executable by the given name. No â
``glob'' expansion or other shell-like substitutions are perâ â
formed on the arguments to commands. â
exit ?returnCode? â
Terminate the process, returning returnCode to the parent as the â
exit status. If returnCode isn't specified then it defaults to â
0.
expr arg
Calls the expression processor to evaluate arg, and returns the
result as a string. See the section EXPRESSIONS above.
file option name ?arg arg ...?
Operate on a file or a file name. Name is the name of a file; â
if it starts with a tilde, then tilde substitution is done â
before executing the command (see the manual entry for â
Tcl_TildeSubst for details). Option indicates what to do with â
the file name. Any unique abbreviation for option is acceptâ â
able. The valid options are: â
file atime name â
Return a decimal string giving the time at which file â
name was last accessed. The time is measured in the â
standard UNIX fashion as seconds from a fixed starting â
time (often January 1, 1970). If the file doesn't exist â
or its access time cannot be queried then an error is â
generated. â
file dirname name â
Return all of the characters in name up to but not â
including the last slash character. If there are no â
slashes in name then return ``.''. If the last slash in â
name is its first character, then return ``/''. â
file executable name â
Return 1 if file name is executable by the current user, â
0 otherwise. â
file exists name â
Return 1 if file name exists and the current user has â
search privileges for the directories leading to it, 0 â
otherwise. â
file extension name â
Return all of the characters in name after and including â
the last dot in name. If there is no dot in name then â
return the empty string. â
file isdirectory name â
Return 1 if file name is a directory, 0 otherwise. â
file isfile name â
Return 1 if file name is a regular file, 0 otherwise. â
file lstat name varName â
Same as stat option (see below) except uses the lstat â
kernel call instead of stat. This means that if name â
refers to a symbolic link the information returned in â
varName is for the link rather than the file it refers â
to. On systems that don't support symbolic links this â
option behaves exactly the same as the stat option. â
file mtime name â
Return a decimal string giving the time at which file â
name was last modified. The time is measured in the â
standard UNIX fashion as seconds from a fixed starting â
time (often January 1, 1970). If the file doesn't exist â
or its modified time cannot be queried then an error is â
generated. â
file owned name â
Return 1 if file name is owned by the current user, 0 â
otherwise. â
file readable name â
Return 1 if file name is readable by the current user, 0 â
otherwise. â
file readlink name â
Returns the value of the symbolic link given by name â
(i.e. the name of the file it points to). If name isn't â
a symbolic link or its value cannot be read, then an â
error is returned. On systems that don't support symâ â
bolic links this option is undefined. â
file rootname name â
Return all of the characters in name up to but not â
including the last ``.'' character in the name. If name â
doesn't contain a dot, then return name. â
file size name â
Return a decimal string giving the size of file name in â
bytes. If the file doesn't exist or its size cannot be â
queried then an error is generated. â
file stat name varName â
Invoke the stat kernel call on name, and use the variable â
given by varName to hold information returned from the â
kernel call. VarName is treated as an array variable, â
and the following elements of that variable are set: â
atime, ctime, dev, gid, ino, mode, mtime, nlink, size, â
type, uid. Each element except type is a decimal string â
with the value of the corresponding field from the stat â
return structure; see the manual entry for stat for â
details on the meanings of the values. The type element â
gives the type of the file in the same form returned by â
the command file type. This command returns an empty â
string. â
file tail name â
Return all of the characters in name after the last â
slash. If name contains no slashes then return name. â
file type name â
Returns a string giving the type of file name, which will â
be one of file, directory, characterSpecial, blockSpeâ â
cial, fifo, link, or socket. â
file writable name â
Return 1 if file name is writable by the current user, 0 â
otherwise. â
The file commands that return 0/1 results are often used in conâ â
ditional or looping commands, for example: â
if {![file exists foo]} then {error {bad file name}} else {...}
flush fileId
Flushes any output that has been buffered for fileId. FileId â
must have been the return value from a previous call to open, or â
it may be stdout or stderr to access one of the standard I/O â
streams; it must refer to a file that was opened for writing. â
This command returns an empty string.
for start test next body
For is a looping command, similar in structure to the C for
statement. The start, next, and body arguments must be Tcl comâ
mand strings, and test is an expression string. The for command
first invokes the Tcl interpreter to execute start. Then it
repeatedly evaluates test as an expression; if the result is
non-zero it invokes the Tcl interpreter on body, then invokes
the Tcl interpreter on next, then repeats the loop. The command
terminates when test evaluates to 0. If a continue command is
invoked within body then any remaining commands in the current
execution of body are skipped; processing continues by invoking
the Tcl interpreter on next, then evaluating test, and so on.
If a break command is invoked within body or next, then the for
command will return immediately. The operation of break and
continue are similar to the corresponding statements in C. For
returns an empty string.
foreach varname list body
In this command, varname is the name of a variable, list is a
list of values to assign to varname, and body is a collection of
Tcl commands. For each field in list (in order from left to
right), foreach assigns the contents of the field to varname (as
if the lindex command had been used to extract the field), then
calls the Tcl interpreter to execute body. The break and conâ
tinue statements may be invoked inside body, with the same
effect as in the for command. Foreach returns an empty string.
format formatString ?arg arg ...?
This command generates a formatted string in the same way as the
C sprintf procedure (it uses sprintf in its implementation).
FormatString indicates how to format the result, using % fields
as in sprintf, and the additional arguments, if any, provide
values to be substituted into the result. All of the sprintf
options are valid; see the sprintf man page for details. Each
arg must match the expected type from the % field in formatâ
String; the format command converts each argument to the correct
type (floating, integer, etc.) before passing it to sprintf for
formatting. The only unusual conversion is for %c; in this case
the argument must be a decimal string, which will then be conâ
verted to the corresponding ASCII character value. Format does
backslash substitution on its formatString argument, so backâ
slash sequences in formatString will be handled correctly even
if the argument is in braces. The return value from format is
the formatted string.
gets fileId ?varName?
Reads the next line from the file given by fileId and discards â
the terminating newline character. If varName is specified, â
then the line is placed in the variable by that name and the â
return value is a count of the number of characters read (not â
including the newline). If the end of the file is reached â
before reading any characters then -1 is returned and varName is â
set to an empty string. If varName is not specified then the â
return value will be the line (minus the newline character) or â
an empty string if the end of the file is reached before reading â
any characters. An empty string will also be returned if a line â
contains no characters except the newline, so eof may have to be â
used to determine what really happened. If the last character â
in the file is not a newline character, then gets behaves as if â
there were an additional newline character at the end of the â
file. FileId must be stdin or the return value from a previous â
call to open; it must refer to a file that was opened for readâ â
ing.
glob ?-nocomplain? filename ?filename ...?
This command performs filename globbing, using csh rules. The
returned value from glob is the list of expanded filenames. If â
-nocomplain is specified as the first argument then an empty â
list may be returned; otherwise an error is returned if the â
expanded list is empty. The -nocomplain argument must be proâ â
vided exactly: an abbreviation will not be accepted.
global varname ?varname ...?
This command is ignored unless a Tcl procedure is being interâ
preted. If so, then it declares the given varname's to be
global variables rather than local ones. For the duration of
the current procedure (and only while executing in the current
procedure), any reference to any of the varnames will be bound
to a global variable instead of a local one.
history ?option? ?arg arg ...?
Note: this command may not be available in all Tcl-based appliâ
cations. Typically, only those that receive command input in a
typescript form will support history. The history command perâ
forms one of several operations related to recently-executed
commands recorded in a history list. Each of these recorded
commands is referred to as an ``event''. When specifying an
event to the history command, the following forms may be used:
[1] A number: if positive, it refers to the event with that
number (all events are numbered starting at 1). If the
number is negative, it selects an event relative to the
current event (-1 refers to the previous event, -2 to the
one before that, and so on).
[2] A string: selects the most recent event that matches the
string. An event is considered to match the string
either if the string is the same as the first characters
of the event, or if the string matches the event in the
sense of the string match command.
The history command can take any of the following forms:
history
Same as history info, described below. â
history add command ?exec?
Add the command argument to the history list as a new
event. If exec is specified (or abbreviated) then the
command is also executed and its result is returned. If
exec isn't specified then an empty string is returned as
result.
history change newValue ?event?
Replace the value recorded for an event with newValue.
Event specifies the event to replace, and defaults to the
current event (not event -1). This command is intended
for use in commands that implement new forms of history
substitution and wish to replace the current event (which
invokes the substitution) with the command created
through substitution. The return value is an empty
string.
history event ?event?
Returns the value of the event given by event. Event
defaults to -1. This command causes history revision to
occur: see below for details.
history info ?count?
Returns a formatted string (intended for humans to read)
giving the event number and contents for each of the
events in the history list except the current event. If
count is specified then only the most recent count events
are returned.
history keep count
This command may be used to change the size of the hisâ
tory list to count events. Initially, 20 events are
retained in the history list. This command returns an
empty string.
history nextid
Returns the number of the next event to be recorded in
the history list. It is useful for things like printing
the event number in command-line prompts.
history redo ?event?
Re-execute the command indicated by event and return its
result. Event defaults to -1. This command results in
history revision: see below for details.
history substitute old new ?event?
Retrieve the command given by event (-1 by default),
replace any occurrences of old by new in the command
(only simple character equality is supported; no wild
cards), execute the resulting command, and return the
result of that execution. This command results in hisâ
tory revision: see below for details.
history words selector ?event?
Retrieve from the command given by event (-1 by default)
the words given by selector, and return those words in a
string separated by spaces. The selector argument has
three forms. If it is a single number then it selects
the word given by that number (0 for the command name, 1
for its first argument, and so on). If it consists of
two numbers separated by a dash, then it selects all the
arguments between those two. Otherwise selector is
treated as a pattern; all words matching that pattern (in
the sense of string match) are returned. In the numeric
forms $ may be used to select the last word of a command.
For example, suppose the most recent command in the hisâ
tory list is
format {%s is %d years old} Alice [expr $ageInMonths/12]
Below are some history commands and the results they
would produce:
_______ ______
history words $ [expr $ageInMonths/12]
history words 1-2{%s is %d years old} Alice
history words *a*o*{%s is %d years old} [expr $ageInMonths/12]
History words results in history revision: see below for details.
The history options event, redo, substitute,
and words result in ``history revision''.
When one of these options is invoked then the current event
is modified to eliminate the history command and replace it with
the result of the history command.
For example, suppose that the most recent command in the history
list is
set a [expr $b+2]
and suppose that the next command invoked is one of the ones on
the left side of the table below. The command actually recorded in
the history event will be the corresponding one on the right side
of the table.
_____________ ________________
history redo set a [expr $b+2]
history s a b set b [expr $b+2]
set c [history w 2]set c [expr $b+2]
History revision is needed because event specifiers like -1 â
are only valid at a particular time: once more events have been â
added to the history list a different event specifier would be â
needed. â
History revision occurs even when history is invoked â
indirectly from the current event (e.g. a user types a command â
that invokes a Tcl procedure that invokes history): the â
top-level command whose execution eventually resulted in a â
history command is replaced. â
If you wish to invoke commands like history words without â
history revision, you can use history event to save the â
current history event and then use history change to â
restore it later. â
if expr1 ?then? body1 elseif expr2 ?then? body2 elseif ... ?else? ?bodyN?
The if command evaluates expr1 as an expression (in the â
same way that expr evaluates its argument). The value of the â
expression must be numeric; if it â
is non-zero then body1 is executed by passing it to the â
Tcl interpreter. â
Otherwise expr2 is evaluated as an expression and if it is non-zeroâ
then body2 is executed, and so on. â
If none of the expressions evaluates to non-zero then bodyN is â
executed. â
The then and else arguments are optional â
``noise words'' to make the command easier to read. â
There may be any number of elseif clauses, including zero. â
BodyN may also be omitted as long as else is omitted too. â
The return value from the command is the result of the body scriptâ
that was executed, or an empty string â
if none of the expressions was non-zero and there was no bodyN. â
incr varName ?increment?
Increment the value stored in the variable whose name is varName.â
The value of the variable must be integral. â
If increment is supplied then its value (which must be an â
integer) is added to the value of variable varName; otherwise â
1 is added to varName. â
The new value is stored as a decimal string in variable varName â
and also returned as result. â
info option ?arg arg ...?
Provide information about various internals to the Tcl interpreter.
The legal option's (which may be abbreviated) are:
info args procname
Returns a list containing the names of the arguments to procedure
procname, in order. Procname must be the name of a
Tcl command procedure.
info body procname
Returns the body of procedure procname. Procname must be
the name of a Tcl command procedure.
info cmdcount
Returns a count of the total number of commands that have been invoked
in this interpreter.
info commands ?pattern?
If pattern isn't specified, returns a list of names of all the
Tcl commands, including both the built-in commands written in C and
the command procedures defined using the proc command.
If pattern is specified, only those names matching pattern
are returned. Matching is determined using the same rules as for
string match.
info complete command
Returns 1 if command is a complete Tcl command in the sense ofâ
having no unclosed quotes, braces, brackets or array element names,â
If the command doesn't appear to be complete then 0 is returned.â
This command is typically used in line-oriented input environmentsâ
to allow users to type in commands that span multiple lines; if theâ
command isn't complete, the script can delay evaluating it until additionalâ
lines have been typed to complete the command. â
info default procname arg varname
Procname must be the name of a Tcl command procedure and arg
must be the name of an argument to that procedure. If arg
doesn't have a default value then the command returns 0.
Otherwise it returns 1 and places the default value of arg
into variable varname.
info exists varName
Returns 1 if the variable named varName exists in the
current context (either as a global or local variable), returns 0
otherwise.
info globals ?pattern?
If pattern isn't specified, returns a list of all the names
of currently-defined global variables.
If pattern is specified, only those names matching pattern
are returned. Matching is determined using the same rules as for
string match.
info level ?number?
If number is not specified, this command returns a number
giving the stack level of the invoking procedure, or 0 if the
command is invoked at top-level. If number is specified,
then the result is a list consisting of the name and arguments for the
procedure call at level number on the stack. If number
is positive then it selects a particular stack level (1 refers
to the top-most active procedure, 2 to the procedure it called, and
so on); otherwise it gives a level relative to the current level
(0 refers to the current procedure, -1 to its caller, and so on).
See the uplevel command for more information on what stack
levels mean.
info library
Returns the name of the library directory in which standard Tclâ
scripts are stored. â
The default value for the library is compiled into Tcl, but itâ
may be overridden by setting the TCL_LIBRARY environment variable.â
If there is no TCL_LIBRARY variable and no compiled-in value thenâ
and error is generated. â
See the library manual entry for details of the facilities
provided by the Tcl script library.
Normally each application will have its own application-specific
script library in addition to the Tcl script library; I suggest that
each application set a global variable with a name like
$app_library (where app is the application's name) â
to hold the location of that application's library directory.
info locals ?pattern?
If pattern isn't specified, returns a list of all the names
of currently-defined local variables, including arguments to the
current procedure, if any.
Variables defined with the global and upvar commands â
will not be returned. â
If pattern is specified, only those names matching pattern
are returned. Matching is determined using the same rules as for
string match.
info procs ?pattern?
If pattern isn't specified, returns a list of all the
names of Tcl command procedures.
If pattern is specified, only those names matching pattern
are returned. Matching is determined using the same rules as for
string match.
info script
If a Tcl script file is currently being evaluated (i.e. there is aâ
call to Tcl_EvalFile active or there is an active invocationâ
of the source command), then this command returns the nameâ
of the innermost file being processed. Otherwise the command returns anâ
empty string. â
info tclversion
Returns the version number for this version of Tcl in the form x.y,
where changes to x represent major changes with probable
incompatibilities and changes to y represent small enhancements and
bug fixes that retain backward compatibility.
info vars ?pattern?
If pattern isn't specified,
returns a list of all the names of currently-visible variables, including
both locals and currently-visible globals.
If pattern is specified, only those names matching pattern
are returned. Matching is determined using the same rules as for
string match.
join list ?joinString?
The list argument must be a valid Tcl list. â
This command returns the string â
formed by joining all of the elements of list together with â
joinString separating each adjacent pair of elements. â
The joinString argument defaults to a space character. â
lappend varName value ?value value ...?
Treat the variable given by varName as a list and append â
each of the value arguments to that list as a separate â
element, with spaces between elements. â
If varName doesn't exist, it is created as a list with elements â
given by the value arguments. â
Lappend is similar to append except that the values â
are appended as list elements rather than raw text. â
This command provides a relatively efficient way to build up â
large lists. For example, ``lappend a $b'' is much â
more efficient than ``set a [concat $a [list $b]]'' when â
$a is long. â
lindex list index â
Treats list as a Tcl list and returns the index'th element â
from it (0 refers to the first element of the list). â
In extracting the element, lindex observes the same rules â
concerning braces and quotes and backslashes as the Tcl command â
interpreter; however, variable â
substitution and command substitution do not occur. â
If index is negative or greater than or equal to the number â
of elements in value, then an empty â
string is returned. â
linsert list index element ?element element ...? â
This command produces a new list from list by inserting all â
of the element arguments just before the indexth â
element of list. Each element argument will become â
a separate element of the new list. If index is less than â
or equal to zero, then the new elements are inserted at the â
beginning of the list. If index is greater than or equal â
to the number of elements in the list, then the new elements are â
appended to the list. â
list arg ?arg ...?
This command returns a list comprised of all the args. Braces
and backslashes get added as necessary, so that the index command
may be used on the result to re-extract the original arguments, and also
so that eval may be used to execute the resulting list, with
arg1 comprising the command's name and the other args comprising
its arguments. List produces slightly different results than
concat: concat removes one level of grouping before forming
the list, while list works directly from the original arguments.
For example, the command
list a b {c d e} {f {g h}}
will return
a b {c d e} {f {g h}}
while concat with the same arguments will return
a b c d e f {g h}
llength list â
Treats list as a list and returns a decimal string giving â
the number of elements in it. â
lrange list first last â
List must be a valid Tcl list. This command will â
return a new list consisting of elements â
first through last, inclusive. â
Last may be end (or any â
abbreviation of it) to refer to the last element of the list. â
If first is less than zero, it is treated as if it were zero. â
If last is greater than or equal to the number of elements â
in the list, then it is treated as if it were end. â
If first is greater than last then an empty string â
is returned. â
Note: ``lrange list first first'' does not always produce the â
same result as ``lindex list first'' (although it often does â
for simple fields that aren't enclosed in braces); it does, however,â
produce exactly the same results as ``list [lindex list first]'' â
lreplace list first last ?element element ...? â
Returns a new list formed by replacing one or more elements of â
list with the element arguments. â
First gives the index in list of the first element â
to be replaced. â
If first is less than zero then it refers to the first â
element of list; the element indicated by first â
must exist in the list. â
Last gives the index in list of the last element â
to be replaced; it must be greater than or equal to first. â
Last may be end (or any abbreviation of it) to indicate â
that all elements between first and the end of the list should â
be replaced. â
The element arguments specify zero or more new arguments to â
be added to the list in place of those that were deleted. â
Each element argument will become a separate element of â
the list. â
If no element arguments are specified, then the elements â
between first and last are simply deleted. â
lsearch list pattern â
Search the elements of list to see if one of them matches â
pattern. â
If so, the command returns the index of the first matching â
element. â
If not, the command returns -1. â
Pattern matching is done in the same way as for the string match â
command. â
lsort list â
Sort the elements of list, returning a new list in sorted â
order. â
ASCII sorting is used, with the result in increasing order. â
open fileName ?access?
Opens a file and returns an identifier â
that may be used in future invocations â
of commands like read, puts, and close. â
FileName gives the name of the file to open; if it starts with â
a tilde then tilde substitution is performed as described for â
Tcl_TildeSubst. â
If the first character of fileName is ``|'' then the â
remaining characters of fileName are treated as a command â
pipeline to invoke, in the same style as for exec. â
In this case, the identifier returned by open may be used â
to write to the command's input pipe or read from its output pipe.â
The access argument indicates the way in which the file â
(or command pipeline) is to be accessed. â
It may have any of the following values: â
r â
Open the file for reading only; the file must already exist.â
r+ â
Open the file for both reading and writing; the file must â
already exist. â
w â
Open the file for writing only. Truncate it if it exists. If it doesn'tâ
exist, create a new file. â
w+ â
Open the file for reading and writing. Truncate it if it exists.â
If it doesn't exist, create a new file. â
a â
Open the file for writing only. The file must already exist, and the fileâ
is positioned so that new data is appended to the file. â
a+ â
Open the file for reading and writing. If the file doesn't exist,â
create a new empty file. â
Set the initial access position to the end of the file. â
Access defaults to r. â
If a file is opened for both reading and writing, then seek â
must be invoked between a read and a write, or vice versa (this â
restriction does not apply to command pipelines opened with open).â
When fileName specifies a command pipeline and a write-only accessâ
is used, then standard output from the pipeline is directed to theâ
current standard output unless overridden by the command. â
When fileName specifies a command pipeline and a read-only accessâ
is used, then standard input from the pipeline is taken from the â
current standard input unless overridden by the command. â
proc name args body
The proc command creates a new Tcl command procedure,
name, replacing
any existing command there may have been by that name. Whenever the
new command is invoked, the contents of body will be executed
by the Tcl interpreter. Args specifies the formal arguments to the
procedure. It consists of a list, possibly empty, each of whose
elements specifies
one argument. Each argument specifier is also a list with either
one or two fields. If there is only a single field in the specifier,
then it is the name of the argument; if there are two fields, then
the first is the argument name and the second is its default value.
braces and backslashes may be used in the usual way to specify
complex default values.
When name is invoked, a local variable
will be created for each of the formal arguments to the procedure; its
value will be the value of corresponding argument in the invoking command
or the argument's default value.
Arguments with default values need not be
specified in a procedure invocation. However, there must be enough
actual arguments for all the
formal arguments that don't have defaults, and there must not be any extra
actual arguments. There is one special case to permit procedures with
variable numbers of arguments. If the last formal argument has the name
args, then a call to the procedure may contain more actual arguments
than the procedure has formals. In this case, all of the actual arguments
starting at the one that would be assigned to args are combined into
a list (as if the list command had been used); this combined value
is assigned to the local variable args.
When body is being executed, variable names normally refer to
local variables, which are created automatically when referenced and
deleted when the procedure returns. One local variable is automatically
created for each of the procedure's arguments.
Global variables can only be accessed by invoking
the global command.
The proc command returns the null string. When a procedure is
invoked, the procedure's return value is the value specified in a
return command. If the procedure doesn't execute an explicit
return, then its return value is the value of the last command
executed in the procedure's body.
If an error occurs while executing the procedure
body, then the procedure-as-a-whole will return that same error.
puts ?-nonewline? ?fileId? string
Writes the characters given by string to the file given â
by fileId. â
FileId must have been the return â
value from a previous call to open, or it may be â
stdout or stderr to refer to one of the standard I/O â
channels; it must refer to a file that was opened for â
writing. â
If no fileId is specified then it defaults to stdout. â
Puts normally outputs a newline character after string, â
but this feature may be suppressed by specifying the -nonewline â
switch. â
Output to files is buffered internally by Tcl; the flush
command may be used to force buffered characters to be output.
pwd
Returns the path name of the current working directory.
read ?-nonewline? fileId
read fileId numBytes â
In the first form, all of the remaining bytes are read from the fileâ
given by fileId; they are returned as the result of the command. â
If the -nonewline switch is specified then the last â
character of the file is discarded if it is a newline. â
In the second form, the extra argument specifies how many bytes to read;
exactly this many bytes will be read and returned, unless there are fewer than
numBytes bytes left in the file; in this case, all the remaining
bytes are returned.
FileId must be stdin or the return
value from a previous call to open; it must
refer to a file that was opened for reading.
regexp ?-indices? ?-nocase? exp string ?matchVar? ?subMatchVar subMatchVar ...?
Determines whether the regular expression exp matches part or
all of string and returns 1 if it does, 0 if it doesn't.
See REGULAR EXPRESSIONS above for complete information on the
syntax of exp and how it is matched against string.
If the -nocase switch is specified then upper-case
characters in string
are treated as lower case during the matching process.
The -nocase switch must be specified before exp and
may not be abbreviated.
If additional arguments are specified after string then they
are treated as the names of variables to use to return
information about which part(s) of string matched exp.
MatchVar will be set to the range of string that
matched all of exp. The first subMatchVar will contain
the characters in string that matched the leftmost parenthesized
subexpression within exp, the next subMatchVar will
contain the characters that matched the next parenthesized
subexpression to the right in exp, and so on.
Normally, matchVar and the subMatchVars are set to hold
the matching characters from string.
However, if the -indices switch is specified then each variable
will contain a list of two decimal strings giving the indices
in string of the first and last characters in the matching
range of characters.
The -indices switch must be specified before the exp
argument and may not be abbreviated.
If there are more subMatchVar's than parenthesized
subexpressions within exp, or if a particular subexpression
in exp doesn't match the string (e.g. because it was in a
portion of the expression that wasn't matched), then the corresponding
subMatchVar will be set to ``-1 -1'' if -indices
has been specified or to an empty string otherwise.
regsub ?-all? ?-nocase? exp string subSpec varName
This command matches the regular expression exp against
string using the rules described in REGULAR EXPRESSIONS
above.
If there is no match, then the command returns 0 and does nothing
else.
If there is a match, then the command returns 1 and also copies
string to the variable whose name is given by varName.
When copying string, the portion of string that
matched exp is replaced with subSpec.
If subSpec contains a ``&'' or ``\0'', then it is replaced
in the substitution with the portion of string that
matched exp.
If subSpec contains a ``\n'', where n is a digit
between 1 and 9, then it is replaced in the substitution with
the portion of string that matched the n-th
parenthesized subexpression of exp.
Additional backslashes may be used in subSpec to prevent special
interpretation of ``&'' or ``\0'' or ``\n'' or
backslash.
The use of backslashes in subSpec tends to interact badly
with the Tcl parser's use of backslashes, so it's generally
safest to enclose subSpec in braces if it includes
backslashes.
If the -all argument is specified, then all ranges in
string that match exp are found and substitution is
performed for each of these ranges; otherwise only the first
matching range is found and substituted.
If -all is specified, then ``&'' and ``\n''
sequences are handled for each substitution using the information
from the corresponding match.
If the -nocase argument is specified, then upper-case
characters in string are converted to lower-case before
matching against exp; however, substitutions specified
by subSpec use the original unconverted form of string.
The -all and -nocase arguments must be specified
exactly: no abbreviations are permitted.
rename oldName newName
Rename the command that used to be called oldName so that it
is now called newName. If newName is an empty string
(e.g. {}) then oldName is deleted. The rename command
returns an empty string as result.
return ?value?
Return immediately from the current procedure
(or top-level command or source command),
with value as the return value. If value is not specified,
an empty string will be returned as result.
scan string format varname1 ?varname2 ...?
This command parses fields from an input string in the same fashion
as the C sscanf procedure. String gives the input to
be parsed and format indicates how to parse it, using %
fields as in sscanf. All of the sscanf options are valid;
see the sscanf man page for details. Each varname gives
the name of a variable; when a field is scanned from string,
the result is converted back into a string and assigned to the
corresponding varname. The only unusual conversion is for
%c. For %c conversions a single character value is
converted to a decimal string, which is then assigned to the
corresponding varname;
no field width may be specified for this conversion. â
seek fileId offset ?origin? â
Change the current access position for fileId. â
The offset and origin arguments specify the position at â
which the next read or write will occur for fileId. â
Offset must be a number (which may be negative) and origin â
must be one of the following: â
start â
The new access position will be offset bytes from the startâ
of the file. â
current â
The new access position will be offset bytes from the currentâ
access position; a negative offset moves the access positionâ
backwards in the file. â
end â
The new access position will be offset bytes from the end ofâ
the file. A negative offset places the access position beforeâ
the end-of-file, and a positive offset places the access positionâ
after the end-of-file. â
The origin argument defaults to start. â
FileId must have been the return â
value from a previous call to open, or it may be stdin, â
stdout, or stderr to refer to one of the standard I/O â
channels. â
This command returns an empty string. â
set varname ?value?
Returns the value of variable varname.
If value is specified, then set
the value of varname to value, creating a new variable
if one doesn't already exist, and return its value.
If varName contains an open parenthesis and ends with a â
close parenthesis, then it refers to an array element: the charactersâ
before the open parenthesis are the name of the array, and the charactersâ
between the parentheses are the index within the array. â
Otherwise varName refers to a scalar variable. â
If no procedure is active, then varname refers to a global
variable.
If a procedure is active, then varname refers to a parameter
or local variable of the procedure, unless the global command
has been invoked to declare varname to be global.
source fileName
Read file fileName and pass the contents to the Tcl interpreter
as a sequence of commands to execute in the normal fashion. The return
value of source is the return value of the last command executed
from the file. If an error occurs in executing the contents of the
file, then the source command will return that error.
If a return command is invoked from within the file, the remainder of
the file will be skipped and the source command will return
normally with the result from the return command.
If fileName starts with a tilde, then it is tilde-substituted
as described in the Tcl_TildeSubst manual entry.
split string ?splitChars?
Returns a list created by splitting string at each character
that is in the splitChars argument.
Each element of the result list will consist of the
characters from string between instances of the
characters in splitChars.
Empty list elements will be generated if string contains
adjacent characters in splitChars, or if the first or last
character of string is in splitChars.
If splitChars is an empty string then each character of
string becomes a separate element of the result list.
SplitChars defaults to the standard white-space characters.
For example,
split "comp.unix.misc" .
returns "comp unix misc" and
split "Hello world" {}
returns "H e l l o { } w o r l d".
string option arg ?arg ...?
Perform one of several string operations, depending on option.
The legal options (which may be abbreviated) are:
string compare string1 string2
Perform a character-by-character comparison of strings string1 and
string2 in the same way as the C strcmp procedure. Return
-1, 0, or 1, depending on whether string1 is lexicographically
less than, equal to, or greater than string2.
string first string1 string2
Search string2 for a sequence of characters that exactly match
the characters in string1. If found, return the index of the
first character in the first such match within string2. If not
found, return -1.
string index string charIndex â
Returns the charIndex'th character of the string â
argument. A charIndex of 0 corresponds to the first â
character of the string. â
If charIndex is less than 0 or greater than â
or equal to the length of the string then an empty string isâ
returned. â
string last string1 string2
Search string2 for a sequence of characters that exactly match
the characters in string1. If found, return the index of the
first character in the last such match within string2. If there
is no match, then return -1.
string length string â
Returns a decimal string giving the number of characters in string.â
string match pattern string
See if pattern matches string; return 1 if it does, 0
if it doesn't. Matching is done in a fashion similar to that
used by the C-shell. For the two strings to match, their contents
must be identical except that the following special sequences
may appear in pattern:
* Matches any sequence of characters in string,
including a null string.
? Matches any single character in string.
[chars] Matches any character in the set given by chars. If a sequence
of the form
x-y appears in chars, then any character
between x and y, inclusive, will match.
\x Matches the single character x. This provides a way of
avoiding the special interpretation of the characters
*?[]\ in pattern.
string range string first last â
Returns a range of consecutive characters from string, startingâ
with the character whose index is first and ending with theâ
character whose index is last. An index of 0 refers to theâ
first character of the string. Last may be end (or any â
abbreviation of it) to refer to the last character of the string.â
If first is less than zero then it is treated as if it were zero, andâ
if last is greater than or equal to the length of the string thenâ
it is treated as if it were end. If first is greater thanâ
last then an empty string is returned. â
string tolower string â
Returns a value equal to string except that all upper caseâ
letters have been converted to lower case. â
string toupper string â
Returns a value equal to string except that all lower caseâ
letters have been converted to upper case. â
string trim string ?chars? â
Returns a value equal to string except that any leading â
or trailing characters from the set given by chars are â
removed. â
If chars is not specified then white space is removed â
(spaces, tabs, newlines, and carriage returns). â
string trimleft string ?chars? â
Returns a value equal to string except that any â
leading characters from the set given by chars are â
removed. â
If chars is not specified then white space is removed â
(spaces, tabs, newlines, and carriage returns). â
string trimright string ?chars? â
Returns a value equal to string except that any â
trailing characters from the set given by chars are â
removed. â
If chars is not specified then white space is removed â
(spaces, tabs, newlines, and carriage returns). â
tell fileId â
Returns a decimal string giving the current access position in â
fileId. â
FileId must have been the return â
value from a previous call to open, or it may be stdin, â
stdout, or stderr to refer to one of the standard I/O â
channels. â
time command ?count?
This command will call the Tcl interpreter count
times to execute command (or once if count isn't
specified). It will then return a string of the form
503 microseconds per iteration
which indicates the average amount of time required per iteration,
in microseconds.
Time is measured in elapsed time, not CPU time.
trace option ?arg arg ...?
Cause Tcl commands to be executed whenever certain operations areâ
invoked. At present, only variable tracing is implemented. The â
legal option's (which may be abbreviated) are: â
trace variable name ops command â
Arrange for command to be executed whenever variable name â
is accessed in one of the ways given by ops. Name may â
refer to a normal variable, an element of an array, or to an arrayâ
as a whole (i.e. name may be just the name of an array, with noâ
parenthesized index). If name refers to a whole array, thenâ
command is invoked whenever any element of the array is â
manipulated. â
Ops indicates which operations are of interest, and consists ofâ
one or more of the following letters: â
r â
Invoke command whenever the variable is read.â
w â
Invoke command whenever the variable is written.â
u â
Invoke command whenever the variable is unset. Variablesâ
can be unset explicitly with the unset command, orâ
implicitly when procedures return (all of their local variablesâ
are unset). Variables are also unset when interpreters areâ
deleted, but traces will not be invoked because there is noâ
interpreter in which to execute them. â
When the trace triggers, three arguments are appended to â
command so that the actual command is as follows: â
command name1 name2 op â
Name1 and name2 give the name(s) for the variable â
being accessed: if the variable is a scalar then name1 â
gives the variable's name and name2 is an empty string; â
if the variable is an array element then name1 gives the â
name of the array and name2 gives the index into the array;â
if an entire array is being deleted and the trace was registeredâ
on the overall array, rather than a single element, then name1â
gives the array name and name2 is an empty string. â
Op indicates what operation is being performed on the â
variable, and is one of r, w, or u as â
defined above. â
Command executes in the same context as the code that invokedâ
the traced operation: if the variable was accessed as part of aâ
Tcl procedure, then command will have access to the same â
local variables as code in the procedure. This context may beâ
different than the context in which the trace was created.â
If command invokes a procedure (which it normally does) thenâ
the procedure will have to use upvar or uplevel if it â
wishes to access the traced variable. â
Note also that name1 may not necessarily be the same as the nameâ
used to set the trace on the variable; differences can occur ifâ
the access is made through a variable defined with the upvarâ
command. â
For read and write traces, command can modify â
the variable to affect the result of the traced operation.â
If command modifies the value of a variable during a â
read or write trace, then the new value will be returned as theâ
result of the traced operation. â
The return value from command is ignored except that â
if it returns an error of any sort then the traced operationâ
is aborted with an error message saying that the access was deniedâ
(this mechanism can be used to implement read-only variables, forâ
example). â
For write traces, command is invoked after the variable's â
value has been changed; it can write a new value into the variableâ
to override the original value specified in the write operation.â
To implement read-only variables, command will have to restoreâ
the old value of the variable. â
While command is executing during a read or write trace, tracesâ
on the variable are temporarily disabled. â
This means that reads and writes invoked by â
command will occur directly, without invoking command â
(or any other traces) again. â
When an unset trace is invoked, the variable has already beenâ
deleted: it will appear to be undefined with no traces. â
If an unset occurs because of a procedure return, then theâ
trace will be invoked in the variable context of the procedureâ
being returned to: the stack frame of the returning procedureâ
will no longer exist. â
Traces are not disabled during unset traces, so if an unset traceâ
command creates a new trace and accesses the variable, theâ
trace will be invoked. â
If there are multiple traces on a variable they are invokedâ
in order of creation, most-recent first. â
If one trace returns an error, then no further traces are â
invoked for the variable. â
If an array element has a trace set, and there is also a traceâ
set on the array as a whole, the trace on the overall arrayâ
is invoked before the one on the element. â
Once created, the trace remains in effect either until theâ
trace is removed with the trace vdelete command described â
below, until the variable is unset, or until the interpreterâ
is deleted. â
Unsetting an element of array will remove any traces on thatâ
element, but will not remove traces on the overall array. â
This command returns an empty string. â
trace vdelete name ops command â
If there is a trace set on variable name with the â
operations and command given by ops and command, â
then the trace is removed, so that command will never â
again be invoked. â
Returns an empty string. â
trace vinfo name â
Returns a list containing one element for each trace â
currently set on variable name. â
Each element of the list is itself a list containing two â
elements, which are the ops and command associated â
with the trace. â
If name doesn't exist or doesn't have any traces set, thenâ
the result of the command will be an empty string. â
unknown cmdName ?arg arg ...? â
This command doesn't actually exist as part of Tcl, but Tcl will â
invoke it if it does exist. â
If the Tcl interpreter encounters a command name for which there â
is not a defined command, then Tcl checks for the existence of â
a command named unknown. â
If there is no such command, then the interpeter returns an â
error. â
If the unknown command exists, then it is invoked with â
arguments consisting of the fully-substituted name and arguments â
for the original non-existent command. â
The unknown command typically does things like searching â
through library directories for a command procedure with the nameâ
cmdName, or expanding abbreviated command names to full-length, â
or automatically executing unknown commands as UNIX sub-processes.â
In some cases (such as expanding abbreviations) unknown will â
change the original command slightly and then (re-)execute it. â
The result of the unknown command is used as the result for â
the original non-existent command. â
unset name ?name name ...? â
Remove one or more variables. â
Each name is a variable name, specified in any of the â
ways acceptable to the set command. â
If a name refers to an element of an array, then that â
element is removed without affecting the rest of the array. â
If a name consists of an array name with no parenthesized â
index, then the entire array is deleted. â
The unset command returns an empty string as result. â
An error occurs if any of the variables doesn't exist. â
uplevel ?level? command ?command ...?
All of the command arguments are concatenated as if they had
been passed to concat; the result is then evaluated in the
variable context indicated by level. Uplevel returns
the result of that evaluation. If level is an integer, then
it gives a distance (up the procedure calling stack) to move before
executing the command. If level consists of # followed by
a number then the number gives an absolute level number. If level
is omitted then it defaults to 1. Level cannot be
defaulted if the first command argument starts with a digit or #.
For example, suppose that procedure a was invoked
from top-level, and that it called b, and that b called c.
Suppose that c invokes the uplevel command. If level
is 1 or #2 or omitted, then the command will be executed
in the variable context of b. If level is 2 or #1
then the command will be executed in the variable context of a.
If level is 3 or #0 then the command will be executed
at top-level (only global variables will be visible).
The uplevel command causes the invoking procedure to disappear
from the procedure calling stack while the command is being executed.
In the above example, suppose c invokes the command
uplevel 1 {set x 43; d}
where d is another Tcl procedure. The set command will
modify the variable x in b's context, and d will execute
at level 3, as if called from b. If it in turn executes
the command
uplevel {set x 42}
then the set command will modify the same variable x in b's
context: the procedure c does not appear to be on the call stack
when d is executing. The command ``info level'' may
be used to obtain the level of the current procedure.
Uplevel makes it possible to implement new control
constructs as Tcl procedures (for example, uplevel could
be used to implement the while construct as a Tcl procedure).
upvar ?level? otherVar myVar ?otherVar myVar ...?
This command arranges for one or more local variables in the currentâ
procedure to refer to variables in an enclosing procedure call orâ
to global variables. â
Level may have any of the forms permitted for the uplevel â
command, and may be omitted if the first letter of the first otherVarâ
isn't # or a digit (it defaults to 1). â
For each otherVar argument, upvar makes the variable â
by that name in the procedure frame given by level (or at â
global level, if level is #0) accessible â
in the current procedure by the name given in the corresponding â
myVar argument. â
The variable named by otherVar need not exist at the time of the â
call; it will be created the first time myVar is referenced, just likeâ
an ordinary variable. â
Upvar may only be invoked from within procedures. â
Neither otherVar or myVar may refer to an element of an â
array. â
Upvar returns an empty string. â
The upvar command simplifies the implementation of call-by-name â
procedure calling and also makes it easier to build new control constructsâ
as Tcl procedures. â
For example, consider the following procedure: â
proc add2 name { â
upvar $name x â
set x [expr $x+2] â
} â
Add2 is invoked with an argument giving the name of a variable, â
and it adds two to the value of that variable. â
Although add2 could have been implemented using uplevel â
instead of upvar, upvar makes it simpler for add2 â
to access the variable in the caller's procedure frame. â
while test body
The while command evaluates test as an expression â
(in the same way that expr evaluates its argument). â
The value of the expression must be numeric; if it is non-zero â
then body is executed by passing it to the Tcl interpreter. â
Once body has been executed then test is evaluated â
again, and the process repeats until eventually test â
evaluates to a zero numeric value. Continue â
commands may be executed inside body to terminate the current â
iteration of the loop, and break â
commands may be executed inside body to cause immediate â
termination of the while command. The while command â
always returns an empty string. â
BUILT-IN VARIABLES
The following global variables are created and managed automatically by
the Tcl library. Except where noted below, these variables should norâ
mally be treated as read-only by application-specific code and by
users.
env
This variable is maintained by Tcl as an array whose elements â
are the environment variables for the process. Reading an eleâ â
ment will return the value of the corresponding environment â
variable. Setting an element of the array will modify the corâ â
responding environment variable or create a new one if it â
doesn't already exist. Unsetting an element of env will remove â
the corresponding environment variable. Changes to the env â
array will affect the environment passed to children by commands â
like exec. If the entire env array is unset then Tcl will stop â
monitoring env accesses and will not update environment variâ â
ables. â
errorCode â
After an error has occurred, this variable will be set to hold â
additional information about the error in a form that is easy to â
process with programs. errorCode consists of a Tcl list with â
one or more elements. The first element of the list identifies â
a general class of errors, and determines the format of the rest â
of the list. The following formats for errorCode are used by â
the Tcl core; individual applications may define additional forâ â
mats. â
CHILDKILLED pid sigName msg â
This format is used when a child process has been killed â
because of a signal. The second element of errorCode â
will be the process's identifier (in decimal). The third â
element will be the symbolic name of the signal that â
caused the process to terminate; it will be one of the â
names from the include file signal.h, such as SIGPIPE. â
The fourth element will be a short human-readable message â
describing the signal, such as ``write on pipe with no â
readers'' for SIGPIPE. â
CHILDSTATUS pid code â
This format is used when a child process has exited with â
a non-zero exit status. The second element of errorCode â
will be the process's identifier (in decimal) and the â
third element will be the exit code returned by the â
process (also in decimal). â
CHILDSUSP pid sigName msg â
This format is used when a child process has been susâ â
pended because of a signal. The second element of errorâ â
Code will be the process's identifier, in decimal. The â
third element will be the symbolic name of the signal â
that caused the process to suspend; this will be one of â
the names from the include file signal.h, such as SIGTâ â
TIN. The fourth element will be a short human-readable â
message describing the signal, such as ``background tty â
read'' for SIGTTIN. â
NONE â
This format is used for errors where no additional inforâ â
mation is available for an error besides the message â
returned with the error. In these cases errorCode will â
consist of a list containing a single element whose conâ â
tents are NONE. â
UNIX errName msg â
If the first element of errorCode is UNIX, then the error â
occurred during a UNIX kernel call. The second element â
of the list will contain the symbolic name of the error â
that occurred, such as ENOENT; this will be one of the â
values defined in the include file errno.h. The third â
element of the list will be a human-readable message corâ â
responding to errName, such as ``no such file or direcâ â
tory'' for the ENOENT case. â
To set errorCode, applications should use library procedures â
such as Tcl_SetErrorCode and Tcl_UnixError, or they may invoke â
the error command. If one of these methods hasn't been used, â
then the Tcl interpreter will reset the variable to NONE after â
the next error. â
errorInfo
After an error has occurred, this string will contain one or
more lines identifying the Tcl commands and procedures that were
being executed when the most recent error occurred. Its conâ
tents take the form of a stack trace showing the various nested
Tcl commands that had been invoked at the time of the error.
AUTHOR
John Ousterhout, University of California at Berkeley
(ouster@sprite.berkeley.edu)
Many people have contributed to Tcl in various ways, but the following
people have made unusually large contributions:
Bill Carpenter
Peter Da Silva
Mark Diekhans
Karl Lehenbauer
Mary Ann May-Pumphrey
Tcl(3)
tcl67.txt
tcl67.txt