发布新日志

  • [转]使用Tcl扩展包cwind进行界面自动化测试

    2009-07-23 23:17:34

    cwind是一个控制界面操作的Tcl扩展包,它可以模拟键盘、鼠标的操作,捕获界面信息,控制界面窗口等,类似于QARUN等软件,通过cwind库可以利用tcl脚本方便的实现QARUN的功能,因此可以用于各种GUI测试。

    cwind扩展包命令介绍:

    to simulate a left click on the mouse
    ::cwind::lclick

    to simulate a right click on the mouse
    ::cwind::rclick

    to simulate a middle click on the mouse
    ::cwind::mclick

    to set the mouse position
    ::cwind::setpos
    where the starting position (0 0) is the upper left corner

    to get the mouse position
    ::cwind::getpos

    to get the cursor postion based on the position of the foreground window
    ::cwind::getwpos

    to set the cursor postion based on the position of the foreground window
    ::cwind::setwpos

    In version 1.3

    to send a message to the foreground window.
    ::cwind::sendmessage ?? ??
    Message name is the windows's message to be sent.
    Wparam and lparam are additional value.

    to post a message to the foreground window.
    ::cwind::postmessage ?? ??
    Message name is the windows's message to be posted.
    Wparam and lparam are additional value.

    In version 1.2

    I've expanded the specification of some commands for MDI.

    to put in active a window.
    ::cwind::show ?-exact?
    Search into the list of active window's name and set in foreground this window.
    Window's name is treated as a glob-style. pattern.
    Window's name is specified in list form,
    The first element is a parent window name and the optional second element is a MDI child window name.

    to get the status of a window (Maximized, Minimized or Normal).
    ::cwind::state ?-exact?
    Window's name is treated as a glob-style. pattern.
    Window's name is specified in list form,
    The first element is a parent window name and the optional second element is a MDI child window name.

    to have a list of all opened windows.
    ::cwind::wlist ?-exact? ??
    If Window's name is omitted, Will show you a Tcl list with all the window's name.
    If Window's name is specified, Will show you a Tcl list with all the MDI child window's name.
    Window's name is treated as a glob-style. pattern.

    In version 1.1

    Due to confilcts with the Tcl command "list",
    I've renamed the command ::cwind::list to ::cwind::wlist.
    ::cwind::wlist

    to get the status of a window (Maximized, Minimized or Normal).
    ::cwind::state ?-exact?
    Window's name is treated as a glob-style. pattern.

    to restore the foreground window at the original size.
    ::cwind::restore

    to minimize the foreground window.
    ::cwind::minimize

    to maximize the foreground window.
    ::cwind::maximize

    to minimize all the windows.
    ::cwind::minimizeAll

    to wait a specific window on the foreground.
    ::cwind::waitwind ??
    Max wait is in seconds and the default value is 5.
    Window's name is treated as a glob-style. pattern.

    In version 1.0

    to send a text to the foreground window.
    ::cwind::send ...
    Send the arguments, separated by spaces.
    If you want send the spaces, enclose your text into { and }.

    to put in active a window.
    ::cwind::show ?-exact?
    Search into the list of active window's name and set in foreground this window.
    Window's name is treated as a glob-style. pattern.

    to get the active window name.
    ::cwind::gettext
    Get the test of the foreground window.

    to get the content of the clipboard.
    ::cwind::getcb

    to put strings into the clipboard.
    ::cwind::putcb

    to have a list of all opened windows.
    ::cwind::list
    Will show you a Tcl list with all the window's name.

    to set the sleep time.
    ::cwind::sleeptime ??
    Time is to ask to the system to wait before each character sent.
    A sort of delay in milliseconds.

    一些特殊键的用法:

    如果要使用像ctrl + c这样的特殊键,必须用下面的字符串序列来表示

    "|CTRL+| c |CTRL-|"

    特殊键的名字对应关系如下:

    "ALT+" press ALT
    "ALT-" release ALT
    "ALTL+" press left ALT key
    "ALTL-" release left ALT key
    "ALTR+" press right ALT key
    "ALTR-" release right ALT key
    
    "CTRL+" press CTRL
    "CTRL-" release CTRL
    "CTRLL+" press left CTRL key
    "CTRLL-" release left CTRL key
    "CTRLR+" press right CTRL key
    "CTRLR-" release right CTRL key
    
    "SHIFT+" press SHIFT
    "SHIFT-" release SHIFT
    "SHIFTL+" press left SHIFT key
    "SHIFTL-" release left SHIFT key
    "SHIFTR+" press right SHIFT key
    "SHIFTR-" release right SHIFT key
    
    "TAB" press the tabulation key
    "RET" press the return key
    "ESC" press the escape key
    
    "BACK" press the backward key
    "DEL" press the delete key
    "INS" press the insert key
    "HELP" press the help key
    
    "LEFT" send the cursor to the left
    "RIGHT" send the cursor to the right
    "UP" send the cursor to up
    "DOWN" send the cursor to down
    "PGUP" press the page up key
    "PGDN" press the page down key
    "HOME" press the home key
    "END" press the end key
    
    "F1" press the function key F1
    "F2" press the function key F2
    "F3" press the function key F3
    "F4" press the function key F4
    "F5" press the function key F5
    "F6" press the function key F6
    "F7" press the function key F7
    "F8" press the function key F8
    "F9" press the function key F9
    "F10" press the function key F10
    "F11" press the function key F11
    "F12" press the function key F12
    
    "NUM0" press the 0 on the key pad
    "NUM1" press the 1 on the key pad
    "NUM2" press the 2 on the key pad
    "NUM3" press the 3 on the key pad
    "NUM4" press the 4 on the key pad
    "NUM5" press the 5 on the key pad
    "NUM6" press the 6 on the key pad
    "NUM7" press the 7 on the key pad
    "NUM8" press the 8 on the key pad
    "NUM9" press the 9 on the key pad
    
    "NUM*" press the * on the key pad
    "NUM+" press the + on the key pad
    "NUM-" press the - on the key pad
    "NUM," press the , on the key pad
    "NUM/" press the / on the key pad
    
    "SNAP" press the print key
    "APPS" press the application key
    "KANJI" press the kanji key
    "CONV" press the convert key
    "NCONV" press the nonconvert key
    

    下面是一段演示程序,演示了打开一个记事本,输入hello world,打开about窗口,然后拷贝窗口到剪贴板,打开画笔,把界面抓图复制到画图程序。下面程序中使用的窗口名是按照英文操作系统的,对于中文操作系统,需要修改其中的窗口名,例如*WordPad修改为记事本的窗口名,*Paint修改为画笔的中文窗口名。

    package require cwind
    
    exec $env(COMSPEC) /c start wordpad.exe &
    ::cwind::waitwind {*WordPad} 10
    ::cwind::send {Hello World!} |RET|
    after 1000
    ::cwind::send |CTRL+| a |CTRL-|
    after 1000
    ::cwind::send |CTRL+| c |CTRL-|
    after 1000
    set clip [::cwind::getcb]
    after 1000
    ::cwind::send |END| |CTRL+| v |CTRL-|
    after 1000
    ::cwind::send |ALT+| h |ALT-| a
    after 1000
    ::cwind::send |ALT+| |SNAP| |ALT-|
    
    exec $env(COMSPEC) /c start mspaint.exe &
    ::cwind::waitwind {*Paint} 10
    ::cwind::send |ALT+| e |ALT-| p
    
    exit
    
    =================================
    参考资料   http://blog.csdn.net/oceanheart/archive/2008/05/24/2476874.aspx
     
  • TCL catch 详解

    2009-07-06 22:05:57

    catch - 执行脚本并捕获错误

    语法

    catch script. ?resultVarName? ?optionsVarName?

    描述

    catch命令用来防止出现错误而导致脚本执行终止,catch命令可以调用Tcl解释器去执行脚本,并且能够正常返回。
    如果脚本产生一个错误,catch将返回一个非0的整数,如果没有捕获到错误就返回0TCL_OK,Tcl还定义了四种异常代码:1TCL_ERROR)、2TCL_RETURN)、3TCL_BREAK)和4TCL_CONTINUE)。当执行脚本产生错误时就返回TCL_ERROR,其它的异常由returnbreakcontinue命令产生。
    如果给出了resultVarName变元,当返回1时,存储在resultVarName中的为错误信息,如果返回0,存储在resultVarName中的为脚本运行结果。
    如果给出了optionsVarName变元,变量里面包含有-code-level两个条目,如果返回代码不是TCL_RETURN时,-level0-code为返回的异常代码,当返回代码为TCL_RETURN时,-level-code为其它的值,详细解释见return命令。
    当返回TCL_ERROR时,三个额外的条目将会添加到optionsVarName中:-errorinfo-errorcode-errorline-errorinfo条目是产生错误的信息,-errorcode条目是关于错误的一些额外信息,存储为列表方式,-errorline指出了错误发生的位置。-errorinfo-errorcode条目都是最近发生的错误并且可以使用::errorInfo::errorCode

    示例

    catch命令可以在if命令中使用。
    if { [catch {open $someFile w} fid] } {
        puts stderr "Could not open $someFile for writing\n$fid"
        exit 1
    }
    
    return命令中有更多的关于catch的示例。
  • FreeWrap:TCL/TK的脚本和二进制文件打包成应用程序‏

    2009-07-06 22:03:54

     FreeWrap可以把TCL/TK的脚本和二进制文件打包成应用程序,FreeWrap将所有的文件组合成一个单独的可执行文件。

        FreeWrap的原理是把脚本和tcl/tk解释器和库文件都打包在一个文件当中,做成一个可执行程序。生成的可执行文件实际上是一个压缩包,里面包含有需要使用的所有内容。不同的版本对应不同的tcl/tk版本,由于原始的tcl/tk版本只包括一些基本的库,所以如果需要使用更多的库,需要额外添加到文件中,还需要注意添加的方法和调用的顺序,如果是二进制的库就更麻烦了,需要额外的添加一段脚本。

        FreeWrap的可执行程序本身就是一个shell,如果修改了可执行程序的名字,在windows下把freeWrap修改为shell,再运行则是一个tcl/tk的shell。

        FreeWrap使用非常方便,就是一条命令,语法参考如下。

     

        语法:freewrap dir/test.tcl [-debug] [-f FileLoadList] [-forcewrap] [-i ICOfile] [-o OutFile][-p] [-w WrapStub] File1 ... FileN

     

        参数:

        dir/test.tcl      TCL/TK脚本主文件目录

        File1 ... FileN   需要打包在可执行程序里面的文件,用空格间隔

        -debug            在打包的时候打开一个可以查看调试信息的窗口

        -f                需要打包的在命令后面的文件名详单

        -forcewrap        当freeWrap应用程序名被修改后,强制freeWrap程序以打包程序来运行

        -i                指定生成的可执行应用程序的图标

        -o                指定生成的可执行应用程序的名称

        -p                创建一个freeWrap格式的程序包而不是创建一个可执行程序

        -w                生成跨平台的可执行文件

        

        参数详解:

        dir/test.tcl      

        TCL/TK脚本主文件目录,命令紧接着的默认为主脚本,其它的文件都为额外添加的文件。

        File1 ... FileN   

        可以是任意的文件,但是要注意在生成的可执行程序包中的访问方式。比如你在命令中添加的file路径为C:\myfile\lib.tcl,那么在在打包程序中的目录结构是如下:

                    \myfile\lib.tcl

                    test.tcl

    所以在添加库文件时要十分小心,要保证在脚本中调用的文件为source \myfile\lib.tcl而不是source lib.tcl。

        -f                

        可以罗列需要打包的文件路径名到一个txt文件当中,运行命令后自动添加,避免命令过长。比如有3个文件C:\lib1.tcl、C:\lib2.tcl和D:\lib3.tcl,那么就可以在一个txt文件中写入上面的全路径,注意一个文件路径名占用一行。在调用的时候就可以正常添加所需的文件了。

        -forcewrap        

        简单点说就是默认的应用程序为freewrap,如果由于需要程序名称修改成了shell,那么就可以使用 freewrap dir/test.tcl -forcewrap来打包一个程序,如果没有-forcewrap则是调用一个shell。

        -i

        使用-i选项去指定生成的可执行程序使用的图标。比如freewrap test.tcl -i test.ico,ico文件需要满足一下三种条件之一:1、16*16 16位色 2、32*32 16位色 3、32*32 2位色,如果是其他ico文件格式,在生成可执行文件时还是会采用默认的图标。

        -o

        指定生成的可执行应用程序的名称,比如freewrap test.tcl -o my.exe,生成的可执行程序就是my.exe而不是test.exe。

        -w

        生成跨平台的可执行文件,生成的时候需要有目标平台的freewarp,举例如下:

    在windows下生成可以在linux下运行的打包应用程序:freewrap test.tcl -w freewrap

    在linux下生成可以在windows下运行的打包应用程序:freewrap test.tcl -w freewrap.exe

    如果需要freewrap,可以在http://sourceforge.net/中找到,目前的最新版本为6.4,支持tcl/tk8.5.0

  • a Python package for Tcl

    2009-07-06 22:02:37

    This package allows the execution of Python code from a Tcl interpreter, as in:
    package require tclpython 4
    set interpreter [python::interp new]
    $interpreter exec {print("Hello World")}
    puts [$interpreter eval 3/2.0]
    python::interp delete $interpreter
    
    which outputs:
    Hello World
    1.5
    

    and, starting with version 4.0, execution of Tcl code from Python interpreters, as in:

    package require tclpython 4
    set interpreter [python::interp new]
    puts [$interpreter eval {tcl.eval('clock format [clock seconds]')}]
    python::interp delete $interpreter
    

    It works by creating one or more embedded Python interpreters and sending them strings to evaluate or execute. Modules can be loaded in the Python interpreter as if it were the real Python program.

    The commands created by the package are very simple:

    • python::interp new
    • python::interp delete interpreter
    • interpreter eval script.
    • interpreter exec script.

    As expected, eval returns the Python interpreter result or reports an error at the Tcl level when the Python interpreter itself fails in executing the code string, while exec simply lets the Python interpreter execute the code and returns nothing or eventually report an error.

    You can create several Python interpreters, if the tclpython package was linked against a Python library compiled with threads support, otherwise only 1 Python interpreter can exist at a time.

    Notes:

    • for Python 2.2 and above, the automatic import site on a new thread is suppressed because it otherwise makes the program hang (you can still use import site in your code, as described at the top of the Lib/site.py file).
    • on a Redhat system, use the tclpython rpm on my homepage.
    • tclpython is used by the moodss system monitoring software (information on my homepage) to allow its modules to be written in Python as well as Tcl and Perl.

    Upgrading from tclpython version 2

    In tclpython version 2, only the eval command was available to an interpreter. Some such invocations, generally that return nothing, need to be converted to the exec command, as the following examples show:

    • $interpreter eval {print("Hello World")} ;# version 2
      $interpreter exec {print("Hello World")} ;# version 3 or 4
      
    • $interpreter eval {def initialize(): pass} ;# version 2
      $interpreter exec {def initialize(): pass} ;# version 3 or 4
      
    • # version 2:
      set exists [$interpreter eval "try: type(initialize) == FunctionType\nexcept: 0"]
      # version 3 or 4:
      $interpreter exec "try: result = (type(initialize) == FunctionType)\nexcept: result = 0"
      set exists [$interpreter eval result]
      

    Send your comments, complaints, ... to jfontain@free.fr.
    My homepage is at http://jfontain.free.fr/.

  • TCL eval 使用详解

    2009-07-06 22:01:17

    eval命令本身使用非常简单,但是用处非常大,如果需要动态的构造命令,那么必须使用eval命令。

        eval命令参考:http://blog.csdn.net/dulixin/archive/2008/03/27/2223978.aspx

        命令格式:eval arg ?arg ...?

        如果是一个参数,那么相当于把这个参数当作命令来执行,如果有多个参数,eval命令会把多个参数以concat命令风格连接起来然后再执行命令。

        举一个最简单的例子:

        % set cmd "puts \"This is a tcltk example\""

        puts "This is a tcltk example"

        % eval $cmd

        This is a tcltk example

        一般在动态脚本中,主要是由脚本片断组成,脚本片断一般是一个变量,根据实际情况进行变量修改来达到执行不同脚本的目的。

        % set a puts

        puts

        % set b stdout

        stdout

        % set c "haha"

        haha

        % eval $a $b $c

        haha

  • TCL 实例: 让一个命令运行一定的时间‏

    2009-07-06 21:59:31

    [root@ntp tcl]# vi timed-run.tcl

    exec tclsh "$0" ${1+"$@"}

    #
    # run a program for a given amount of time,
    # aborting after the number of seconds
    #
    # Usage:
    # tclsh timed-run.tcl 20 long_running_program  program_args
    #
    # Author: Jeff Hobbs, based off shorter example by Don Libes
    #

    # This is required to declare that we will use Expect
    package require Expect

    proc usage {} {
        puts stderr "usage: $::argv0 <timeInSecs> <program>  <program args> "
        exit 1
    }


    if {$argc < 2} { usage }

    # timeout value is first to be passed in
    set timeout [lindex $argv 0]
    if {![string is integer -strict $timeout]} { usage }

    # program and args are the rest
    set cmd [lrange $argv 1 end]

    # invoke the cmd with spawn
    eval spawn $cmd

    # just call expect and wait for it to timeout or eof to occur
    expect




    运行结果:

    [root@ntp tcl]# ./timed-run.tcl
    usage: ./timed-run.tcl <timeInSecs> <program>  <program args>

    以下调用 vmstat 运行时间长1秒,自然退出
    [root@ntp tcl]# ./timed-run.tcl  9 vmstat
    spawn vmstat
       procs                      memory      swap          io     system      cpu
     r  b  w   swpd   free   buff  cache   si   so    bi    bo   in    cs us sy id
     0  0  0      0 835304  43340  90120    0    0     0     4   83    40  0  0 100

    以下调用 vmstat 运行时间长9秒,超时退出
    [root@ntp tcl]# ./timed-run.tcl  9 vmstat 1
    spawn vmstat 1
       procs                      memory      swap          io     system      cpu
     r  b  w   swpd   free   buff  cache   si   so    bi    bo   in    cs us sy id
     1  0  0      0 835304  43340  90120    0    0     0     4   83    40  0  0 100
     0  0  0      0 835304  43340  90120    0    0     0     0 1019    33  0  0 100
     1  0  0      0 835300  43340  90120    0    0     0     0 1024    48  0  0 100
     0  0  0      0 835980  43340  90108    0    0     0   272 1046    88  0  0 100
     0  0  0      0 835980  43340  90108    0    0     0     0 1029    57  0  0 100
     0  0  0      0 835980  43340  90108    0    0     0     0 1035    59  0  0 100
     0  0  0      0 835980  43340  90108    0    0     0     0 1048    91  1  1 98
     0  0  0      0 835980  43340  90108    0    0     0     0 1031    51  0  0 100
     0  0  0      0 835980  43340  90108    0    0     0    12 1023    54  0  0 100
     1  0  0      0 835980  43340  90108    0    0     0     0 1016    38  0  0 100
    [root@ntp tcl]#


    [root@ntp tcl]# cat timed-run.tcl
    #!/bin/sh
    # \
    exec tclsh "$0" ${1+"$@"}
    优点:
    1,二进制文件的位置不需要填入,它可以在你的 shell 查找路径中的任何地方。
    2,突破了#!只有30个字符的限制
    3,再shell中, 第2行是注释,第3行不是.但是在tcl中, \是连接符号,下一行依旧是注释.所以只有shell有机会执行第3行.exec 语句导致 shell 停止处理而启动 tclsh 来重新处理整个脚本。当 tclsh启动时,因为第二行的反斜线导致第三行被作为第二行注释的一部分,它把所有三行都作为注释对待。


    exec tclsh "$0" ${1+"$@"}
    很多地方是这样表达:
    exec wish "$0" "$@"

    [root@ntp tcl]# vi test

    #!/bin/sh
    # \
    echo hello
    echo $0
    echo ${1+"$@"}
    echo "$@"

    [root@ntp tcl]# ./test 1 2 3
    hello
    ./test
    1 2 3
    1 2 3

    可见两者没有太大的区别.

    $::argv0 应该是TCL在双引号中调用系统参数的方法?
  • 如何在Tcl中加载包

    2009-07-06 21:57:10

    先说一下 ActiveTcl,一般来说,安装完ActiveTcl,会有一个lib的文件夹,在这个文件夹下面会有很多很常用到的一些Tcl或者Tk的包,例如:bwidget, treectrl 等等。 一般来说,要用这些包,只需要运行 package require Bwidget 就可以,但是很多情况下,也许我们对于这些包,有些特定的目录来放置,那么如何来Load这些包呢?
     
    在Tcl,Tk中,有一个变量来控制查找包,就是auto_path, 默认情况下,如果在ActiveTcl环境下,初始值是
    C:/Tcl/lib/tcl8.4 C:/Tcl/lib C:/Tcl/lib/tcllib1.8 C:/Tcl/lib/tklib0.4 C:/Tcl/lib/tk8.4
    这里我是在Wish中运行:set auto_path
    如果现在我们有一个blt的包放置在D:/lib下面,那么我们需要运行:
    lappend auto_path D:/lib
    把需要加入的包所属的文件夹加入到变量auto_path中,这样我们就可以正确运行 package require blt.

  • Tcl 内建命令

    2009-07-05 23:26:33

    Tcl 内建命令

    Tcl 内建命令
    Built-in commands 内建的命令

    Tcl提供了下面描述的内建函数。
    ... 表示参数不定

    append varName value
    append varName value value value ...
    将那一大堆value附加到varName后面。如果变量不存在,会新
    建一个。
    例子:
    set i "aaa"
    append i "bbb" "ccc"
    //i = aaabbbccc


    array subcommand arrayName
    array subcommand arrayName arg ...
    这是一组用于向量操作的命令。第二个参数是子命令名。

    假设:
    set a(1) 1111
    set a(2) 2222
    set a(three) 3333
    一下均以它为例子(tclsh在中运行)。

    array names arrayName
    返回一个数组元素名字的列表。
    tclsh>array names a
    1 2 three

    array size arrayName
    返回数组的元素个数。
    tclsh>array size a
    3

    下面是用于遍历的命令
    arrry startsearch arrayName
    初始化一次遍历,返回一个遍历标示(searchId)在下面的命令
    是中使用。

    array nextelement arrayName searchId
    返回下一个数组中的元素。如果没有返回一个空串。

    array anymore arrayName searchId
    返回 1 表示还有更多的元素。0 表示没有了。

    array donesearch arrayName searchId
    结束该次遍历。

    array nextelement arrayName searchId
    返回下一个元素。

    tclsh>array startsearch a
    s-1-a
    tclsh>array nextelement a s-1-a
    1111
    tclsh>array nextelement a s-1-a
    2222
    tclsh>array anymore a s-1-a
    1
    tclsh?array nextelement a s-1-a
    3333
    tclsh>array donesearch a s-1-a

    注意可以同时并发多个遍历。

    break
    跳出最近的循环。

    case string in patList body ...
    case string patList body ...
    case string in {patList body ...}
    case string {patList body ...}
    分支跳转。
    例如:
    case abc in {a b} {puts 1} default {puts 2} a* {puts 3}
    return 3.

    case a in {
    {a b} {format 1}
    default {format 2}
    a* {format 3}
    }
    returns 1.

    case xyz {
    {a b}
    {format 1}
    default
    {format 2}
    a*
    {format 3}
    }
    returns 2.
    注意default不可以放在第一位。支持shell文件名风格的匹配
    符。

    catch command
    catch command varName
    用于阻止由于错误而导致中断执行。执行command, 每次都返
    回TCL_OK, 无论是否有错误发生。如有错误发生返回1 ,反之返回0
    。如果给了varName这被置为错误信息。注意varName是已经存在的
    变量。

    cd
    cd dirName
    转换当前工作目录。如dirName未给出则转入home目录。

    close fileId
    关闭文件描述符。

    concat arg ...
    将参数连接产生一个表。
    concat a b {c d e} {f {g h}}
    return `a b c d e f {g h}'

    continue
    结束该次循环并继续循环。

    eof fileId
    如fileId以结束 返回1,反之返回 0。

    error message
    error message info
    error message info code
    返回一个错误,引起解释器停止运行。info用于初始化全局变
    量errorInfo。code被付给errorCode。

    eval arg ...
    将所有的参数连起来作为命令语句来执行。

    exec arg ...
    仿佛是在shell下执行一条命令。
    exec ls --color
    exec cat /etc/passwd > /tmp/a

    exit
    exit returnCode
    中断执行。

    expr arg
    处理表达式。
    set a [expr 1+1]
    //a=2

    file subcommand name
    一组用于文件处理的命令。
    file subcommand name arg ...

    file atime name
    返回文件的最近存取时间。

    file dirname name
    返回name所描述的文件名的目录部分。

    file executable name
    返回文件是否可被执行。

    file exists name
    返回1 表示文件存在,0 表示文件不存在。

    file extension name
    返回文件的扩展名。

    file isdirectory name
    判断是否为目录。

    file isfile name
    判断是否为文件。

    file lstat name varName
    以数组形式返回。执行lstat系统函数。存储在varName。

    file mtime name
    文件的最近修改时间。

    file owned name
    判断文件是否属于你。

    file readable name
    判断文件是否可读。

    file readlink name
    都出符号连接的真正的文件名。

    file rootname name
    返回不包括最后一个点的字符串。

    file size name
    返回文件的大小。

    file stat name varName
    调用stat内和调用,以数组形式存在varName中。

    file tail name
    返回最后一个斜线以后的部分。

    file type name
    返回文件类型file, directory, characterSpecial,
    blockSpecial, fifo, link, 或
    socket。

    file writable name
    判断文件是否可写。

    flush fileId
    立即处理由fileId描述的文件缓冲区。

    for start test next body
    for循环。同C总的一样。

    for {set i 1} {$i < 10} {incr i} {puts $i}

    foreach varname list body
    类似于C Shell总的foreach或bash中的for..in...

    format formatString
    format formatString arg ...
    格式化输出,类似于C中的sprintf。
    set a [format "%s %d" hello 100]
    //a="hello 100"

    gets fileId
    gets fileId varName
    从文件中读出一行。
    set f [open /etc/passwd r]
    gets $f

    glob filename ...
    glob -nocomplain filename ...
    使用C Shell风格的文件名通配规则,对filename进行扩展。
    ls /tmp
    a b c

    tclsh>glob /tmp/*
    a b c
    当加上参数 -nocomplain 时,如文件列表为空则发生一个错
    误。

    global varname ...
    定义全局变量。

    if test trueBody
    if test trueBody falseBody
    if test then trueBody
    if test then trueBody else falseBody
    条件判断,是在没什么说的。

    incr varName
    incr varName increment
    如果没有incremnet,将varName加一,反之将varName加
    上increment。

    set i 10
    incr i
    //i=11
    incr i 10
    //i=21

    info subcommand
    info subcommand arg ...
    取得当前的Tcl解释器的状态信息。

    info args procname
    返回由procname指定的命令(你自己创建的)的参数列表。
    如:
    proc ff { a b c } {puts haha}
    info args ff
    //return "a b c" 

    info body procname
    返回由procname指定的命令(你自己创建的)的函数体。
    如:
    proc ff { a b c } {puts haha}
    info body ff
    //return "puts haha" 

    info cmdcount
    返回当前的解释器已经执行的命令的个数。

    info commands
    info commands pattern
    如果不给出模式,返回所有的命令的列表,内建和自建的。
    模式是用C Shell匹配风格写成的。

    info complete command
    检查名是否完全,有无错误。

    info default procname arg varname
    procname的参数arg,是否有缺省值。

    info exists varName
    判断是否存在该变量。

    info globals
    info globals pattern
    返回全局变量的列表,模式同样是用C Shell风格写成的。

    info hostname
    返回主机名。

    info level
    info level number
    如果不给参数number则返回当前的在栈中的绝对位置,参
    见uplevel中的描述。如加了参数number,则返回一个列表包
    含了在该level上的命令名和参数。

    info library
    返回标准的Tcl脚本的可的路径。实际上是存在变量
    tcl_library中。

    info locals
    info locals pattern
    返回locale列表。

    info procs
    info procs pattern
    返回所有的过程的列表。

    info script.
    返回最里面的脚本(用 source 来执行)的文件名。

    info tclversion
    返回Tcl的版本号。

    info vars
    info vars pattern
    返回当前可见的变量名的列表。

    下面是一些用于列表的命令,范围可以是end。

    join list
    join list joinString
    将列表的内容连成一个字符串。

    lappend varName value ...
    将value加入列表varName中。

    lindex list index
    将list视为一个列表,返回其中第index个。列表中的第一个
    元素下标是0。
    lindex "000 111 222" 1
    111

    linsert list index element ...
    在列表中的index前插入element。

    list arg ...
    将所有的参数发在一起产生一个列表。
    list friday [exec ls] [exec cat /etc/passwd]

    llength list
    返回列表中元素的个数。
    set l [list sdfj sdfjhsdf sdkfj]
    llength $l
    //return 3

    lrange list first last
    返回列表中从frist到last之间的所有元素。
    set l [list 000 111 222 333 444 555]
    lrange $l 3 end
    //return 333 444 555

    lreplace list first last
    lreplace list first last element ...
    替换列表中的从first到last的元素,用element。
    set l [list 000 111 222 333 444 555]
    lreplace $l 1 2 dklfj sdfsdf dsfjh jdsf
    000 dklfj sdfsdf dsfjh jdsf 333 444 555

    lsearch -mode list pattern
    在列表中搜索pattern,成功返回序号,找不到返回-1。
    -mode : -exact 精确
    -glob shell的通配符
    -regexp 正则表达式

    lsearch "111 222 333 444" 111
    //return 0
    lsearch "111 222 333 444" uwe
    //return 1

    lsort -mode list
    排列列表。
    -mode : -ascii
    -dictionary 与acsii类似,只是不区分大小写
    -integer 转化为整数再比较
    -real 转化为浮点数再比较
    -command command 执行command来做比较

    open fileName
    open fileName access
    打开文件,返回一个文件描述符。
    access
    r w a r+ w+ a+
    定义与C中相同。如文件名的第一个字符为|表示一管道的形式
    来打开。
    set f [open |more w]
    set f [open /etc/pass r]

    proc name args body
    创建一个新的过程,可以替代任何存在的过程或命令。

    proc wf {file str} {
    puts -nonewline $file str
    flush $file
    }

    set f [open /tmp/a w]
    wf $f "first line\n"
    wf $f "second line\n"
    在函数末尾可用 return 来返回值。

    puts -nonewline fileId string
    向fileId中写入string,如果不加上 -nonewline 则自动产
    生一个换行符。

    pwd
    返回当前目录。

    read fileId
    read fileId numBytes
    从fileId中读取numBytes个字节。

    regexp ?switches? exp string ?matchVar? ?subMatchVar
    subMatchVar ...?
    执行正则表达式的匹配。
    ?switches? -nocase 不区分大小写
    -indices 返回匹配区间
    如:
    regexp ^abc abcjsdfh
    //return 1
    regexp ^abc abcjsdfh a
    //return 1
    puts $a
    //return abc

    regexp -indices ^abc abcsdfjkhsdf a
    //return 1
    puts $a
    //return "0 2"


    regsub ?switchs? exp string subSpec varName
    执行正则表达式的替换,用subSpec的内容替换string中匹配exp
    的部分。
    ?switchs? -all 将所有匹配的部分替换,缺省子替换第一
    个,返回值为替换的个数。
    -nocase 不区分大小写。
    如:
    regsub abc abcabcbac eee b
    //return 1
    puts $b
    //return "eeeabcabc"

    regsub -all abc abcabcabc eee b
    //return 3
    puts $b
    //return "eeeeeeeee"


    return
    立即从当前命令中返回。
    proc ff {} {
    return friday
    }

    set a [ff]
    //a = "friday"

    scan string `format' varname ...
    从string中安format来读取值到varname。

    seek fileId offset ?origin?
    移动文件指针。
    origin: start current end
    offset从哪里开始算起。

    set varname ?value?
    设置varname用value,或返回varname的值。如果不是在一
    个proc命令中则生成一个全局变量。

    source fileName
    从filename中读出内容传给Tcl解释起来执行。

    split string ?splitChars?
    将string分裂成列表。缺省以空白为分隔符,也可通
    过splitChars来设定分隔符

    string subcommand arg ...
    用于字符串的命令。

    string compare string1 string2
    执行字符串的比较,按 C strcmp 的方式。返回 -1, 0, or 1。

    string first string1 string2
    在string1种查找string2的定义次出现的位置。未找到返回-1。

    string length string
    返回字符串string的长度。

    string match pattern string
    判断string是否能匹配pattern。pattern是以shell文件名的
    统配格式来给出。

    string range string first last
    返回字符串string中从first到last之间的内容。

    string tolower string
    将string转换为小写。

    string toupper string
    将string转换为大写。

    string trim string
    将string的左右空白去掉。

    string trimleft string
    将string的左空白去掉。

    string trimright string
    将string的右空白去掉。

    tell fileId
    返回fileId的文件指针位置。

    time command
    执行命令,并计算所消耗的时间。
    time "ls --color"
    some file name
    503 microseconds per iteration

    trace subcommand
    trace subcommand arg ...
    监视变量的存储。子命令定义了不少,但目前只实现了
    virable。
    trace variable name ops command
    name 为变量的名字。
    ops 为要监视的操作。
    r 读
    w 写
    u unset
    command 条件满足时执行的命令。
    以三个参数来执行 name1 name2 ops
    name1时变量的名字。当name1为矢量时,name2为下标,ops
    为执行的操作。

    例如:
    proc ff {name1 name2 op} {
    puts [format "%s %s %s" name1 name2 op]
    }
    set a hhh
    trace variable a r {ff}
    puts $a
    //return "a r\nhhh"

    unknown cmdName
    unknown 并不是 Tcl 的一部分,当 Tcl 发现一条不认识的命
    令时会看看是否存在 unknown命令,如果有,则调用它,没有则出
    错。

    如:
    #!/usr/bin/tclsh
    proc unknown {cwd args} {
    puts $cwd
    puts $args
    }
    //下面是一条错误命令
    sdfdf sdf sdkhf sdjkfhkasdf jksdhfk
    //return "sdfdf sdf sdkhf sdjkfhkasdf jksdhfk"

    unset name ...
    删除一个或多个变量(标量或矢量)。

    uplevel command ...
    将起参数连接起来(象是在concat中)。最后在由level所指
    定的上下文中来执行。如果level是一个整数,给出了在栈中的距
    离(是跳到其它的命令环境中来执行)。
    缺省为1(即上一层)。
    如:
    #!/usr/bin/tcl
    proc ff {} {
    set a "ff" //设置了局部的a
    -------------------------
    }
    set a "global"
    ff
    puts $a
    //return "global"

    再看下一个:
    #!/usr/bin/tcl
    proc ff {} {
    uplevel set a "ff" //改变上一级栈中的a
    -------------------------------------
    }
    set a global
    ff
    puts $a
    //return "ff"
    如果level是以#开头后接一个整数,则level指出了在栈中的
    绝对位置。如#0表示了顶层(top-level)。
    a b c 分别为三个命令,下面是它们之间的调用关系,
    top-level -> a -> b -> c -> uplevel level
    绝对位置: 0 1 2 3
    当level为 1 或 #2 都是在 b 的环境中来执行。
    3 或 #0 都是在 top-level 的环境中来执行。

    upvar ?level? otherVar myVar ?otherVar myVar ...?
    在不同的栈中为变量建立连接。这里的level与uplevel中
    的level是同样风格的。
    例如:
    #!/usr/bin/tcl
    proc ff {name } {
    upvar $name x
    set x "ff"
    }
    set a "global"
    ff a
    puts $a
    //return "ff"

    while test body
    举个例子吧:
    set x 0
    while {$x<10} {
    puts "x is $x"
    incr x
    }

    Built-in variables 内建的变量
    下名的全局变量是由 Tcl library 自动来管理的。一般是只
    读的。

    env
    环境变量数组。
    如:
    puts $env(PATH)
    // return /bin:/usr/bin:/usr/X11R6/bin

    errorCode
    当错误发生时保存了一些错误信息。用下列格式来存储:
    CHILDKILLED pid sigName msg
    当由于一个信号而被终止时的信息。
    CHILDSTATUS pid code
    当一个子程序以非0值退出时的格式。
    CHILDSUSP pid sigName msg
    当一个子程序由于一个信号而被终止时的格式。
    NONE
    错误没有附加信息。
    UNIX errName msg
    当一个内核调用发生错误时使用的格式。

    errorInfo
    包含了一行或多行的信息,描述了错误发生处的程序和信息。

    原文的作者也是Tcl的缔造者 John Ousterhout
    (`ouster@sprite.berkeley.edu')写于伯克利分校。
    Tcl 名字空间
    namespace
    创建和操纵命令和变量的上下文(content)。

    简介:
    一个名字空间是一个命令和变量的集合,通过名字空间的封装来
    保证他们不会影响其它名字空间的变量和命令。 Tcl 总是维护了一
    个全局名字空间 global namespace 包含了所有的全局变量和命令。

    namespace eval允许你创建一个新的namespace。
    例如:
    namespace eval Counter {
    namespace export Bump
    variable num 0

    proc Bump {} {
    variable num//声明局部变量
    incr num
    }
    }
    名字空间是动态的,可变的。
    例如:
    namespace eval Counter {
    variable num 0//初始化
    proc Bump {} {
    variable num
    return [incr num]
    }
    }

    //添加了一个过程
    namespace eval Counter {
    proc test {args} {
    return $args
    }
    }

    //删除test
    namespace eval Counter {
    rename test ""
    }
    引用:
    set Counter::num
    //return 0
    也可以用下面的方式添加:
    proc Foo::Test {args} {return $args}
    或在名字空间中移动:
    rename Foo::Test Bar::Test
Open Toolbar