Handling Unknown Commands

The unknown command is called when another command is executed which is not known to the interpreter. Rather than simply issuing an error message, this gives you the opportunity to handle the problem and recover in an intelligent way. For example, you could attempt to re-evaluate the arguments as an expression. This would allow you to be able to evaluate expressions without using the expr command.

set a [1+1]

To make unknown do what you want, simply define it as a procedure. The list of arguments is available as a parameter to the unknown command. Here is a definition of unknown which supports expression evaluation without having to specify the expr command:

proc unknown {args} {
    expr $args
}

By default, Tcl comes with a definition for unknown that does a number of things such as attempt history substitution. I will only go into detail on the most useful action that unknown takes—retrieving procedure definitions from libraries.

Get Exploring Expect now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.