Category:Tcl: Difference between revisions

→‎Language Syntax: Describe key commands
(→‎Language Syntax: More detail on effective syntax)
(→‎Language Syntax: Describe key commands)
Line 57:
** After-execution callback scripts are evaluated in the global scope.
* Commands cannot discover how their arguments were quoted.
===Key Commands===
The following commands are simply normal commands, and can be renamed, deleted, traced, etc., just like any other command, but they are also used in virtually all Tcl scripts and so overriding their behavior is typically an indication of code that is likely to fail. (People do that anyway, but they are almost always careful to ensure that the existing semantics of the commands with these names are still supported.)
 
'''set''' ''varName'' ?''value''?
:Sets a named variable to a value and returns the current value of the variable. If ''value'' is omitted, reads from the variable.
'''expr''' ''arg''...
:Concatenates the ''arg''uments and evaluates them as an expression.
'''if''' ''expr'' ?'''then'''? ''script'' ?'''elseif''' ''expr'' ?'''then'''? ''script'' ...? ?'''else'''? ?''script''?
:Evaluates expressions in order until one of them yields a true value, and then evaluate the associated script, or evaluate the script from the '''else''' clause otherwise. The '''then''' and '''else''' keyword-arguments are both optional, but it is conventional to always include the '''else''' for readability ('''then''' only tends to be used with multiline conditions). Arbitrarily many '''elseif''' clauses are allowed.
'''switch''' ?''options''? ''value'' ''body''<br>'''switch''' ?''options''? ''value'' ''val1'' ''script'' ?''val2'' ''script'' ...?
:Finds the first ''val''n that matches ''value'' (the default matching rule is exact equality, this is overrideable using the ''options'') and evaluate its script. The final ''val'' can be '''default''' to supply a catch-all case, and if a ''script'' is a '''-''' then the ''script'' from the following clause is used. If a single ''body'' is supplied, it is interpreted as a list of clauses.
'''while''' ''expr script''
:While the expression evaluates to true, evaluate the ''script''.
'''for''' ''init expr incr script''
:Evaluate the ''init'' script, and then while the expression evaluates to true, evaluate the ''script'', evaluating the ''incr'' script after each iteration. This is very similar to [[C]]'s <code>for</code> keyword.
'''foreach''' ''varName list script''<br>
'''foreach''' ''varNameList list'' ?''varNameList list'' ...? ''script''
:Evaluate the ''script'' for each value in ''list'', setting ''varName'' to that value. In the more general case, there is more than one ''list'' and there are multiple variable names per list allowing striding.
'''break'''
:Make the current loop finish executing early.
'''continue'''
:Make the next iteration of the current loop start early.
'''error''' ''message'' ...
:Generate an error exception. The additional optional arguments allow finer control over the exception.
'''eval''' ''arg''...
:Concatenate the arguments and evaluate the resulting string as a script.
'''proc''' ''name formalArgs body''
:Define a new command called ''name'' that pushes a new stack frame, accepts arguments and binds them to the list of local variable names given in ''formalArgs'' and then evaluates ''body''.
'''return''' ?''options''? ?''value''?
:Return from the current stack frame with the given ''value'' (or the empty string if that's omitted). The ''options'' allow for greater control of the underlying exception semantics used.
'''catch''' ''body'' ?''varName''? ?''optVarName''?
:Evaluate the ''body'' script, and return the exception status produced (e.g., 0 for no exception). If ''varName'' is given, the result or error message is stored in it. If ''optVarName'' is present (from Tcl 8.5 onwards) then a dictionary characterizing the exception status is stored in it.
'''upvar''' ?''level''? ''otherVarName localVarName'' ?''otherVarName localVarName'' ...?
:Bind each of the ''otherVarName'' variables (as looked up at stack level ''level'', or the parent stack frame of the current procedure if that is omitted) to the corresponding ''localVarName''. Following this, the two refer to the same variable until the termination of the current stack frame.
'''uplevel''' ?''level''? ''arg''...
:Concatenate the arguments and evaluate them as a script in the stack frame given by ''level'' (or the stack frame that called the current procedure if that is omitted). Due to syntactic ambiguities, it is recommended that the ''level'' always be specified explicitly.
 
==Language Semantics==
Anonymous user