Version 2 of Substituting Command Names

Updated 2001-10-18 15:14:41

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 going to answer that right away (mainly because I can't, yet). Instead, try this:

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

(well, this is the idea. I can't get it to run yet -- I'm still working on learning TCL)

This has got to be useful for something...

Paul Miller


Comments? Add 'em here: