Pragana's Tcl Guide



This is not a tentative to compete with the many excellent explanations available about this theme. I want just to clarify some points that makes most of us (mortals) do many mistakes when developing scripts in Tcl.
Please look at this link for an extensive list of tutorials and documents otherwise available.

Tcl can confuse many people because of wrong understanding of how it parses each command before execution. We have a natural inclination to replace one expression by it's equivalent when doing careless coding and this could be dangerous.
Let us consider this simple example:

        set r rildo
        
        set s1 "$r pragana"
        set s2 {$r pragana}
The variables s1 and s2 will have very distinct values, because of value subsititution for $r in the first of them. So, if you assign each of them to another variable, like:
        set sn1 $s1
        set sn2 $s2
total substitution will not occur, and in the sn2 we will have a $r as part of the string.
There are several commands to manage this. First, to force another round of string substitution, we can use subst. Optionally, we can put the switches -nobackslashes,-novariables, and -nocommands to control which kind of substitutions we want to be done. By default all of them are done.
In our examples, we could do:
        set sn1 [subst $s1]
        set sn2 [subst $s2]
To make all replacements we wanted to.

Other interesting commands to explore are eval, upvar, and uplevel, but let us leave them for another opportunity... 


Back Home