Version 9 of unknown

Updated 2005-03-29 12:54:35 by LES

http://www.purl.org/tcl/home/man/tcl8.4/TclCmd/unknown.htm

The unknown procedure is called if a command is not known to the interpreter. The default unknown handler tries to find a sourcable file to define the command in question, or, in interactive mode, tries to redirect the command to the operating system. For examples how to put unknown to other uses, see Radical language modification, or Let unknown know


As hinted above, manipulation of unknown can be a powerful metaprogramming technique, giving Tcl radically new behavior. unknown is implemented as a proc; among other reasons, it does elaborate string-handling that'd be tedious in C. One might imagine, though, in a "Small Tcl" context, construction of a minimal C-coded unknown implementation that both provides sufficient functionality for embedded situations (autoloading?) and occupies severely constrained memory.

GPS: A very useful unknown is:

 % proc unknown args {expr $args}
 % 1 + 2
 3
 % pow(2,20) 
 1048576.0

LES suggests this tiny improvement for anyone interested in starting to write a more sophisticated unknown procedure:

 set  unknown_original  [ info body  unknown ]

 proc  unknown  args          {

         if          [ regexp  {^[0-9]+\s*[/*+-]\s*[0-9]+}  $args ]          {
                 return [ expr $args ]
         }  

         if          [ regexp  {^pow\s*\([0-9]+,[0-9]+\)}  $args ]          {
                 return [ expr $args ]
         }  

         eval $::unknown_original
 }

Tcl syntax help - Arts and crafts of Tcl-Tk programming - Category Command