📄 00000005.htm
字号:
<HTML><HEAD> <TITLE>BBS水木清华站∶精华区</TITLE></HEAD><BODY><CENTER><H1>BBS水木清华站∶精华区</H1></CENTER>发信人: scaner (又一个年头), 信区: Linux <BR>标 题: TCL的语法 <BR>发信站: BBS 水木清华站 (Fri Dec 22 01:52:17 2000) WWW-POST <BR> <BR>觉得里面挺科学的, 感觉比较舒服. <BR>又郁闷就无聊就这样折腾一点东西出来. <BR>share吧,大家有兴趣就看看 <BR> <BR>==== <BR>Source: <A HREF="http://dev.scriptcs.com/scripting/syntax.html">http://dev.scriptcs.com/scripting/syntax.html</A> <BR>Translator: scaner.smth-bbs <BR> <BR>1.简单语法 <BR>cmd arg arg arg <BR>A Tcl command is formed by words separated by white space. <BR>The first word is the name of the command, and the remaining <BR>words are arguments to the command. <BR>Tcl的确命令是由空格分隔的字符串组成.第一个字符串是命令 <BR>的名字, 余下的字符串是命令的参数. <BR> <BR> <BR>$foo <BR>The dollar sign ($) substitutes the value of a variable. <BR>In this example, the variable name is foo. <BR>$代表(替换)变量的值. $foo, 变量的名字就是foo <BR> <BR>[clock secounds] <BR>Square brackets execute a nested command. For example, <BR>if you want to pass the result of one command as the argument to <BR>another, you use this syntax. In this example, the nested <BR>command is clock seconds, which gives the current time in seconds. <BR>[]执行一内签的命令. 比如说, <BR>如果你想将一个命令的结果作为参数传给另一个命令, 你可以 <BR>使用这个方式. 在上面的例子中, 命令是clock seconds. <BR>这个命令用以获得秒表示的时间. <BR> <BR>"some stuff" <BR>Double quotation marks group words as a single argument <BR>to a command. Dollar signs and square brackets are interpreted <BR>inside double quotation marks. <BR>霜引号表示一组字符串作为一个参数传递给命令. 在引号中的 <BR>$, []都将被解释. <BR> <BR>{some stuff} <BR>Curly braces also group words into a single argument. <BR>In this case, however, elements within the braces are not interpreted. <BR>花括号同样是用来聚合字符串成为单一的参数的. 但是在{} <BR>中的元素将不会被解释. <BR> <BR>\ <BR>The backslash (\) is used to quote special characters. <BR>For example, \n generates a newline. The backslash also <BR>is used to "turn off" the special meanings of the dollar sign, <BR>quotation marks, square brackets, and curly braces. <BR>=用来引用特殊的字符. 比如说\n代表换行. \同样用来 <BR>关闭$,".[]{}的特殊意义. <BR> <BR>A Little Example <BR>Below is a Tcl command that prints the current time. <BR>It uses three Tcl commands: set, clock, and puts. The <BR>set command assigns the variable. The clock command <BR>manipulates time values. The puts command prints the values. <BR> <BR>set seconds [clock seconds] <BR>puts "The time is [clock format $seconds]" <BR>下面是一个tcl命令, 用来打印当前时间. <BR>他使用了三个Tcl命令:set, clock and puts. set命令 <BR>用来给变量赋值. clock命令处理时间, put命令打印 <BR>变量. <BR> <BR>Note that you do not use $ when assigning to a <BR>variable. Only when you want the value do you use $. <BR>The seconds variable isn't needed in the previous example. <BR>You could print the current time with one command: <BR> <BR>puts "The time is [clock format [clock seconds]]" <BR>请注意在赋值时是没有使用$的. 只有档你想获得 <BR>值的时候, 才使用$. 另外变量是不必须的, 你可以直接 <BR>用一条命令打印当前的时间. <BR> <BR>Grouping and Substitution <BR>The Tcl syntax is used to guide the Tcl parser <BR>through three steps: argument grouping, result <BR>substitution, and command dispatch <BR> <BR>分组于替换 <BR>Tcl的语法用来指导Tcl的解析经过三个过程, <BR>参数分组(argument grouping), 结果替换(result substitution) <BR>与命令分派(command dispatch) <BR> <BR>Argument grouping. ? <BR>Tcl needs to determine how to organize <BR>the arguments to the commands. In the simplest case, <BR>white space separates arguments. As stated earlier, <BR>the quotation marks and braces syntax is used to group <BR>multiple words into one argument. In the previous example, <BR>double quotation marks are used to group a single argument <BR>to the puts command. <BR>Tcl需要知道如何组织命令的参数. 简单的情况瞎, 空格分隔 <BR>每个参数. 正如前面的规定,引用语法用来将多个字符串 <BR>聚合成一个参数. 如前面的例子, 双引号标记被用来聚合 <BR>puts的参数. <BR> <BR>Result substitution. <BR>After the arguments are grouped, Tcl performs string <BR>substitutions. Put simply, it replaces $foo with the value <BR>of the variable foo, and it replaces bracketed commands <BR>with their result. That substitutions are done after grouping <BR>is crucial. This sequence ensures that unusual values <BR>do not complicate the structure of commands. <BR>在参数分组后, Tcl 执行字符串替换. 将$foo替换成变量 <BR>foo 的值, 同时替换[]引用命令的结果. 结果替换在参数 <BR>分组以后进行, 是非常重要的, 这样的顺序保真特别的 <BR>值不会复杂命令的结构. <BR> <BR> <BR>Command dispatch. <BR>After substitution, Tcl uses the command name as a key into a <BR>dispatch table. It calls the C procedure identified in the table, <BR>and the C procedure implements the command. You also can write <BR>command procedures in Tcl. There are simple conventions about <BR>argument passing and handling errors. <BR>结果替换以后, Tcl 用命令名子作为关键字查找派遣表. 调用 <BR>表中确定的C 过程, 这个C 过程实现了这个命令. 你也可以用 <BR>Tcl 编写命令处理过程. 那里有关于变量传递与错误处理的简单 <BR>约定. <BR> <BR>Another Example <BR>Here is another example: <BR> <BR>set i 0 <BR>while {$i < 10} { <BR> puts "$i squared = [expr $i*$i]" <BR> incr i <BR>} <BR> <BR>Here, curly braces are used to group arguments without doing <BR>any substitutions. The Tcl parser knows nothing special about <BR>the while command. It treats it like any other command. It is <BR>the implementation of the while command knows that the first <BR>argument is an expression, and the second argument is more Tcl <BR>commands. The braces group two arguments: the boolean expression <BR>that controls the loop and the commands in the loop body. <BR>这里, {}用于引用不做任何替换的参数. Tcl 解释器不知道while命令的 <BR>任何特别信息. 它只是把它当成一个普通的命令. while 命令的实现, <BR>知道第一个参数, 是一个表达式, 第二个参数是多个Tcl 命令的集合. <BR>{}聚集了两个参数: 用来控制循环的逻辑表达式与循环命令集合. <BR> <BR>We also see two math expressions: the boolean comparison <BR>and multiplication. The while command automatically evaluates <BR>its first argument as an expression. In other cases you must <BR>explicitly use the expr command to perform math evaluation. <BR>我们同时看见两个表达式, 布尔比较与乘法. while 命令自动 <BR>对第一个参数求值. 在别的情况, 你必须明确的使用expr命令 <BR>执行数字表达式. <BR> <BR>Command Dispatch <BR>Lastly, Tcl calls something else to do the hard work. <BR>We've seen that Tcl uses expr to perform math functions, <BR>puts to handle output functions, and set to assign variables. <BR>These Tcl commands are implemented by a C procedure that has <BR>registered itself with Tcl. The C command procedures take the <BR>string arguments from the Tcl command and return a new string <BR>as their result. It is very easy to write C command procedures. <BR>They can do everything from accessing databases to creating <BR>graphical user interfaces. Tcl, the language, doesn't really know <BR>what the commands do. It just groups arguments, substitutes <BR>results, and dispatches commands. <BR>最后Tcl 调用点别的东西, 来完成复杂的工作. 我们已经看见 <BR>Tcl 利用expr来执行数学运算, puts来完成输出功能, set <BR>用于变量定值. 这些Tcl 命令都是由 C过程实现的, 它们都是 <BR>由Tcl 已经注册好的. C 实现的命令从Tcl 命令中获得 <BR>字符串参数, 同时返回一个字符串作为结果返回给Tcl 命令. <BR>编写C 命令处理过程是非常简单. 它们可以做从访问数据库 <BR>到建立GUI 的任何事情. Tcl 语言本生并不知道命令完成功能. <BR>它只是执行groups arguments, substitutes results and dispatches commands <BR>这三步操作. <BR> <BR> <BR>One Last Example <BR>Here is the factorial procedure: <BR> <BR>proc fac {x} { <BR> if {$x < 0} { <BR> error "Invalid argument $x: must be a positive integer" <BR> } elseif {$x <= 1} { <BR> return 1 <BR> } else { <BR> return [expr $x * [fac [expr $x-1]]] <BR> } <BR>} <BR> <BR>Sign: z9e4+M7S1+7H17CutcTIyw== <BR>-- <BR>她在电话的另一头默默不语,久久地保持沉默, <BR>如同全世界所有的细雨落在全世界所有的草坪上. <BR> <BR> <BR> <BR> <BR> <BR>※ 修改:·scaner 於 Dec 22 01:55:41 修改本文·[FROM: 166.111.215.235] <BR>※ 来源:·BBS 水木清华站 smth.org·[FROM: 166.111.215.235] <BR><CENTER><H1>BBS水木清华站∶精华区</H1></CENTER></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -