Version 1 of Empty interpreter

Updated 2008-07-13 12:04:18 by lars_h

An empty interpreter is a Tcl interpreter where there are neither commands nor variables.

The following creates an empty slave interp empty for you to experiment with:

 interp create -safe empty
 empty eval {namespace delete ::}

(The namespace delete trick here is by DGP, first published on the Braintwisters page.) empty now exhibits the following behaviour:

 % empty eval {info patchlevel}
 invalid command name "info"
 % empty eval {namespace exists ::}
 invalid command name "namespace"

and so on.

What good is such an interpreter? Well, it is still possible to create aliases in it:

 % empty alias list list {[list] in empty interp}    
 list
 % empty eval {list a b c}
 {[list] in empty interp} a b c

Hence empty interpreters can be used as config file parsers, for config files that follow Tcl syntax: For every allowed "config file command" you create that command as an alias to something in the main interpreter which actually does what that command is supposed to do. Then source or interp eval the config file in the empty interpreter, to have it safely processed.

How can I source the file when the source command is gone? you may ask? Well, as it turns out, the source command (and a few others) aren't really gone, since they were interp hidden from the namespace delete that emptied the interpreter:

 % empty hidden
 file socket open unload pwd glob exec encoding fconfigure load source exit cd
 % empty invokehidden source my-config-file
 couldn't read file "my-config-file": no such file or directory