Tcl只有一種語法: command arg arg ....windows
這種設計真的是很是好,簡潔,快速!app
它和不少程序設計語言不同,它沒有關鍵詞!譬如if, switch在Tcl中也是命令。dom
初次體驗Tcl的感受是:利用Tcl來構建一些東西的時候,只作兩件事情:一,調用命令;二,直接給參數或者用替換的方法給參數。 僅有的一種語法在有了「可替換「這個特性後,果真強大了不少!可見,好的設計,真的是至簡的設計。你沒法再用更少的東西來達到相同的目的了。ui
Tcl也是支持結構化的,好比c中的function,能夠對應爲Tcl中的proc(procedure的簡稱)。設計
proc procName args... {unix
main bodyorm
}server
這樣就至關於定義了一個procedure。其實,它仍是個命令,這個命令作的事情的,當procName被調用時,執行mainbody.ip
如下給出我初步試驗的腳本和結果:get
#test tcl built-in commands puts "======================start==========================" #print "running" after 5 seconds puts "waiting for 1 second ..." after 1000 puts "running ..." #append var(="Hello") with " " and "World!" set var {} append var "Hello" " " "World!" puts ${var} #test command 'for', 'if' and 'incr' #output the odd number between [1:10] for {set i 1} {$i <= 10} {incr i} { if {$i%2 != 0} {puts $i} } #command substitution set var [expr 1+2+3] puts $var #file puts [file attributes tmp.tcl] puts [file atime tmp.tcl] puts [file channels *err] puts [file dirname ~] puts [file dirname ~/Desktop] #file copy tmp.tcl commandtest.tcl set flag [file executable commandtest.tcl] if {$flag == 1} { puts "commandtest.tcl is executable" } if {$flag == 0} { puts "commandtest.tcl is not executable" } file mkdir testdir1 testdir2 testdir3 puts [file normalize commandtest.tcl] puts [file separator] #test 'proc' and 'cd' proc mypwd args { return [file normalize ./] } puts [mypwd] set current_dir [file normalize ./] cd /home/chenqi/Desktop puts [mypwd] cd ${current_dir} puts [mypwd] #test pwd puts [pwd] #test 'file stat', 'array' and 'foreach' file stat commandtest.tcl var_stat_array foreach {field value} [array get var_stat_array] { puts "field: $field; value: $value" } puts $::tcl_platform(platform) #test 'switch' switch $::tcl_platform(platform) { unix { puts "the platform is unix" } windows { puts "the platform is windows" } } #test 'exec' puts "executing 'uname -r' command" puts [exec uname -r] #test time puts [time { puts [exec find /home/chenqi/MyPro/CFiles/ -name *.c] }] puts "===================end=================================" ======================start========================== waiting for 1 second ... running ... Hello World! 1 3 5 7 9 6 -group root -owner root -permissions 00644 1336199519 stderr / ~ commandtest.tcl is executable /home/chenqi/MyPro/ShellScript/tcl/commandtest.tcl / /home/chenqi/MyPro/ShellScript/tcl /home/chenqi/Desktop /home/chenqi/MyPro/ShellScript/tcl /home/chenqi/MyPro/ShellScript/tcl field: mtime; value: 1336203195 field: atime; value: 1336203234 field: gid; value: 0 field: nlink; value: 1 field: mode; value: 33261 field: type; value: file field: ctime; value: 1336203195 field: uid; value: 0 field: ino; value: 2360375 field: size; value: 1724 field: dev; value: 2056 unix the platform is unix executing 'uname -r' command 2.6.32-21-generic /home/chenqi/MyPro/CFiles/temp/bufsiz.c /home/chenqi/MyPro/CFiles/Chapter-1/Hello.c /home/chenqi/MyPro/CFiles/POSIX_Threads/autobakup.c /home/chenqi/MyPro/CFiles/POSIX_Threads/thread1.c /home/chenqi/MyPro/CFiles/virtual_process/virt_proc.c /home/chenqi/MyPro/CFiles/virtual_process/test.c /home/chenqi/MyPro/CFiles/virtual_process/hash.c /home/chenqi/MyPro/CFiles/virtual_process/pid.c /home/chenqi/MyPro/CFiles/IPC/semaphores/cfunc.c /home/chenqi/MyPro/CFiles/IPC/pipes/named_pipes/client.c /home/chenqi/MyPro/CFiles/IPC/pipes/named_pipes/server.c /home/chenqi/MyPro/CFiles/IPC/pipes/named_pipes/tmp/procB.c /home/chenqi/MyPro/CFiles/IPC/pipes/named_pipes/tmp/procA.c /home/chenqi/MyPro/CFiles/IPC/pipes/named_pipes/server_with_queue.c /home/chenqi/MyPro/CFiles/IPC/pipes/pipes_as_stdio/pipe_as_stdout.c /home/chenqi/MyPro/CFiles/IPC/pipes/pipes_as_stdio/pipe_as_stdin.c /home/chenqi/MyPro/CFiles/IPC/pipes/unnamed_pipes/procB.c /home/chenqi/MyPro/CFiles/IPC/pipes/unnamed_pipes/procA.c /home/chenqi/MyPro/CFiles/IPC/signals/test.c /home/chenqi/MyPro/CFiles/IPC/signals/getpidbyname.c /home/chenqi/MyPro/CFiles/IPC/signals/test2.c /home/chenqi/MyPro/CFiles/programming-pearls/chapter1/qsort.c /home/chenqi/MyPro/CFiles/programming-pearls/chapter1/mergesort.c /home/chenqi/MyPro/CFiles/programming-pearls/chapter1/tmp/test.c /home/chenqi/MyPro/CFiles/programming-pearls/chapter1/bitmap_sort.c /home/chenqi/MyPro/CFiles/programming-pearls/chapter1/generate_random.c 4776 microseconds per iteration ===================end=================================