Commonly encountered problems in Tcl

Purpose: to discuss various common Tcl problems as well as their solutions/workarounds/rationalizations.


One marvelous source of information on this topic is Cameron Laird's Frequently Made Mistakes(tm) document at http://www.phaseit.net/claird/comp.lang.tcl/fmm.html

Use this page for additional problems, alternative solutions, etc.


Recently David Cuthbert mailto:[email protected] stated [L1 ] that recently several students have been learning Tcl and he has found that "[t]he biggest stumbling points have been

1. Scoping, which is backwards from C:

    int i = 0;                      set i 0

    void f() {                      proc f {} {
        i = 2; /* legal */              set i 2 ;# illegal
        if(1) { int j = 0; }            if 1 { set j 0 }
        ++j; /* illegal */              incr j  ;# legal
    }                               }

(DKF: The above use of Tcl is legal but not useful; the [set] creates a new local variable called i instead of reusing the global one. Of course, an initial command that reads from the variable will fail...)

2. upvar, and the use of variable names as pointers.

RS: Well, if that isn't the most straightforward implementation of "call by name"...

3. Reducing the use of exec vs. built-in Tcl functions. (They tend to revert to things they know, as anyone would expect.)

RS: Just make it a habit to test the same script on Unix and Windows...


See also: