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.0AMG: Another way of writing that:% interp alias "" unknown "" expr unknownThis gives us a wonderfully useless way of doing stuff we could do all along:
% puts moo
moo
% {[puts moo]}
moo
% {[{[puts moo]}]}
mooGood 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
}Tcl syntax help - Arts and crafts of Tcl-Tk programming - Category Command
