2012-01-10

tcllib 1.14 is out

 我現在才注意到,在 2011.12.13,tcllib 已經釋出了新版 v1.14

Overview
========

21 new packages in 7 modules
30 changed packages in 24 modules
8 internally changed packages in 8 modules
328 unchanged packages in 89 modules
393 packages, total in 107 modules, total

2012-01-08

Coroutines

在 8.6 引進新的 Non-Recursive Engine (NRE) 之後,帶進了新的功能 Coroutines。不過 Coroutine 對我而言是很陌生的東西,所以做一下初學筆記。

在看過網路上對於 Coroutine 的介紹以後,我想可以簡單的總結如下:

Croutine 是自 1960 年代就已經現身的多工實做技術,原理相當單純,在 Coroutine 的 "yield" 是屬於程式語言層面,透過特定技巧或機制,讓原本循序執行的陳述指令,得以做出交錯執行的結果,可以說是 Windows 3.X 「合作式多工」的基礎概念(十分類似)。

用簡單的一句話來說 Coroutine,就是可以暫時中斷,之後再繼續執行的程序。


下面是從 Tcl 8.6 manual page copy 出來的例子:

coroutine accumulator apply {{} {
    set x 0
    while 1 {
        # yield $x: 丟資料並且把主控權交給呼叫者
        incr x [yield $x]
    }
}}

for {set i 0} {$i < 10} {incr i} {
    puts "$i -> [accumulator $i]"
}

manual page 裡還有其它的例子幫助理解,希望我對 Coroutines 的理解是正確的。

Wub

Wub
wub - Wub pure tcl HTTP1.1 server, client and tool suite


雖然 Wub 發展有一段時間了,不過我是最近才發現的。

截錄自 Tcler Wiki 的介紹:

Wub is a web-server written in pure-Tcl. It runs the Wiki you're now reading. It absolutely requires Tcl 8.6 and tracks the HEAD closely. It should help in creating highly-dynamic (and portable) web applications. It is the successor in spirit of Tclhttpd, aiming to preserve the best of it while using the bleeding-edge latest Tcl facilities to simplify and extend.


Tcler Wiki 就是使用 Wub 架起來的,非常好的 working example。

2012-01-07

備份 (on Windows)

跟之前一樣的程式,只是加上 GUI 表示目前正在備份中,加上練習 thread 使用的部份。

#!/usr/bin/tclsh
#
# This file is used to backup my homepage
#

package require Tk
package require Thread

set ::gThread [thread::create {thread::wait} ]

label .greetings -text "Now backup my homepage..." -bd 4 -relief ridge
pack .greetings -fill both

wm title . "Backup"

thread::send -async $::gThread { 
    set backupfile $env(HOME)
    append backupfile "\\My Documents"
    append backupfile "\\Homepage-"
    append backupfile [clock format [clock seconds] -format %Y%m%d]
    append backupfile ".7z"

    set backupdir $env(HOME)
    append backupdir "\\My Documents"
    append backupdir "\\public_html"

    set fileExist [file exists $backupfile]
    if {$fileExist > 0} {
        puts "Now try to remove old backup file."
        file delete $backupfile
    }

    set var [list 7z a $backupfile $backupdir]
    exec {*}$var        
} result


vwait result
exit