TCL Learning-Process Control |

Source: Internet
Author: User

L Switch command : - mode match

Pattern matching, similar to if, ElseIf, else

First parameter: The value to be detected

Second argument: List of one or more element pairs

Look at the following three ways:


Switch supports three pattern matching methods:

-exact: Strict string comparison, default

-glob: Global match, see Stringmatch

-regexp: Regular expression matching

--: Mark option End

Note: In general, you should always use the-option to mark the end of an option to ensure that the switch command correctly recognizes the detection string

L Source command : : - one file to run another file

If a file is named: 1.txt file, there is a command

source HELLO.TCL; # then call run HELLO.TCL file

L eval command : A common building block for creating and running Tcl scripts

The command accepts multiple parameters, concatenates them with delimiters, and then processes the concatenated results as a TCL script. So this command can contain multiple commands, expand to multiple lines, include annotations, and so on.

If you write an application where the script is generated as a result of a dynamic process, then eval is the appropriate way to run the script. If the list of variables, one scenario is as follows:

Usual procedure:

But the following method does not work, cause: The variable list of the unset command is a parameter, not a separate variable name, so unset will attempt to delete a variable named a b c D.

To achieve the above purposes, you can use the following methods:

L while ,for, foreach:-and Loop command

Like other languages, TCL provides three commands for looping: The while,for and Foreach.while commands get two parameters: an expression and a TCL script. It executes the loop until the condition is false. For example, the following is a very simple example:

The same script, with a for to rewrite is:


First parameter: Init script

Second parameter: Terminating a loop expression

Third parameter: Init script

foreach is relatively simple, first of all to look at a simple example, to traverse the male, female.


A foreach must have three variables:

The first one is the variable name

The second one is the list

The third one is the Tcl script that makes up the loop body.

The foreach command can also process multiple lists in parallel, each of which requires a separate set of variables. no corresponding variable is set to an empty string .


L break,continue: -- loop control

Break: Terminate Loop

Continue: Terminates the current loop and continues the next cycle

L proc,return: à process basis

Proc---Commands for creating parameters

Plus--Process name

{a B}--The two parameters that need to be passed

Return--is the returned value, NULL if none

See the implementation of factorial function

L Local variables and global variables

Local variables are only valid in the process, and you must add the Global keyword if you want to make it work for the global.

L Number of parameters and default parameters


Note: The default parameter can only be placed later, provided the parameter is provided as the main, otherwise with the default parameters.

args: It is more than one parameter.

From the above example, we can see the use of args and how to traverse them.

L upvar: -- Pass reference call

Tcl only supports value invocation of parameters. When the TCL command is called, the value of the parameter is copied and then passed to the command. This is true even if the argument comes from a variable. Because the TCL interpreter will replace the parameter with its value before executing the command. Therefore, in the following example, the variable that the sum command obtains is a copy of the value stored in a and B.

Sum $a $b

Tcl also does not support pointers and reference types, so at first glance it is not possible to write a procedure to change the value of a variable that already exists. However, the name of the variable is also a string value, which can also be stored in a variable. Therefore, by requiring multiple rounds of substitution, you can simulate the behavior of the reference, for example:

In the example above, the TCL interpreter replaces $y with its string value x. Then executes the SET command, parses its arguments into the name of a variable, and returns the value stored in the variable. Tcl in the Upvar command, you can implement the same behavior as a reference call.

The Upvar command provides a common mechanism for variables outside the context of the current procedure. Can be used to access global variables, namespace variables, and local variables within procedures in other activities. The most common use is to simulate the behavior of reference calls, which are particularly useful for arrays. If a is an array, it cannot be passed to the MyProc process as MyProc $a, because there is no value for the entire array; only the values for each array element. However, you can pass the name of the array to the procedure, such as MyProc A, and then use the Upvar command to access the elements of the array during the procedure.

The following is a simple example of using Upvar in a procedure to output the contents of an array.

When PrintArray is called, the name of the array is given as an argument. The Upvar command allows the procedure to access the array through variable a. The first parameter of Upvar is a variable that is visible to the calling environment of the procedure. The first parameter of Upvar is a variable that is visible to the calling environment of the procedure. This can be a global variable, as in this example, a namespace variable, or an internal variable that invokes a procedure.

The second parameter is the name of a local variable.

The Upvar command redirects the return of the local variable A to a variable named name in the calling environment. In this example, when PrintArray reads a, it reads the elements of the info global variable. If PrintArray writes to a, it modifies info. PrintArray uses the array names command to get a list of all the elements in the array. They are then sorted by Lsort, and the elements are output sequentially.

The first variable name of the Upvar command defaults to the context of the caller of the current procedure, but can also access variables of any level in the call stack, including global variables.

Example: Upvar #0 other X

Using global variables other can be accessed through the local variable x.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

TCL Learning-Process Control |

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.