Version 4 of Substituting Command Names

Updated 2001-10-18 15:40:49

Try this:

   set DontPrintString puts
   $DontPrintString "I told you not to print this!\n"

It prints it anyway.

Why? Because the variable DontPrintString was evaluated in the substitution pass, "puts" was substituted, and then it was evaluated.

So What? I'm not sure. Try this:

   proc PrintOrExpr {a b} {
      if { [string compare $a "print"] } {
         set cmdname expr
      } else {
         set cmdname puts
      }
      $cmdname $b
   }

This example isn't useful, but the technique has got to be good for something...

Paul Miller LV Paul, one place I use something like this is in ... hmm, what is that page name. Basically, it was a tcl script to take create csv records containing file info like ls would return. I use a variable for the output commands so that I could turn on debugging and all of a sudden the output goes elsewhere...