Updated 2014-09-06 10:07:20 by gp


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
 % pow(2,20)

AMG: Another way of writing that:
 % interp alias "" unknown "" expr

This gives us a wonderfully useless way of doing stuff we could do all along:
 % puts moo
 % {[puts moo]}
 % {[{[puts moo]}]}

Good times, good times.

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

AMG: It's not possible for a [regexp] to spot a valid [expr] expression. Maybe you can write one to detect candidate [expr]s, but things get really funny when the user creates his/her own math functions.

AMG: At the bottom of What kinds of variable names can be used in Tcl, RS writes about a neat idea begging to be implemented as an [unknown] proc. To motivate you to read further, I'll copy his example usage:
 foreach digit [0-9] {
    puts hello,$digit