Updated 2016-05-27 20:00:12 by pooryorick

Magic Names are values that have special meaning to the interpreter or a command, and affect the behaviour of those things. It's useful to be aware of them since they don't necessarily occur explicitly in a script, and might therefore be easy to overlook when troubleshooting. For example, $auto_path, whose value might depend on the value of an environment variable, or which might be set somewhere in a script, can lead to not quite the expected packages being loaded.

The tclvars enumerates variable special to the Tcl interpreter. Below are other values to be aware of.

Environment variables  edit

May be used by exec.

Variables  edit

Some other magic variable names for Tcl are $args, $argv, and $argv0, as well as the global arrays $env and $tcl_platform.

Values  edit

Used by package, via tclPkgUnknown
stdin, stdout, and stderr
Used by open

Canvas tags may be almost any strings - except: all, current, or decimal integers, and substrings like && , !, or || have special meaning.

Literals  edit

Used by tclsh

Bad magic (taboo names)  edit

Someone wrote in comp.lang.tcl:
 I am calling the file button.tcl ...

Donald Porter replied: Don't do that.

Don't give your wish scripts the same name as any of the widgets provided by Tk. It will cause problems with wish trying to apply options for those widgets to your program as a whole. Try the file name example.tcl.

Jeffrey Hobbs explained: It is not OK to name any primary Tcl file (the one that is launched by wish) after one of the widget classes, due to the way that the X resource option db stuff works. It will make the primary application of class Button in this case, and then things will be goofy after that ...

Are there potential problems if the file was named after Tcl files such as auto.tcl, init.tcl, package.tcl, etc. ?

Bryan Oakley: no. The issue has to do with widget class names rather than file names. If your application file is the same name as a widget class madness will ensue.


Tk creates ::. Therefore, creating a procedure named ::. will run afoul of Tk.


DKF: Remember that images are also commands, so calling them things like "open" is a bad idea. Absolutely do not create an image called "." as that causes really bad things to happen. The easiest way to work around this, of course, is to let Tk pick the names for all the images it creates, and for you to store those names in an array indexed by the names that you prefer. Like this, you can be sure you won't hit things unexpectedly. (Also it means that you can use the same "names" to refer to different images in different contexts just by changing the name of the lookup array, which can make programming easier. You could even use read traces to populate the arrays on-demand...

LV: Most Tcl and Tk commands create objects by the name provided by the developer/user, without regard for whether an item by that name already exists. This is as it should be, in most cases, since you don't want to have to use unset before every set, now do you ?