Updated 2011-06-11 04:06:18 by RLE

The way tcl works

from "Bryan's Practical Guide to Tcl Programming", vol 0, page 0.
    The way tcl works: a line of code is parsed once. Only
    once. Only ever once. Once, once, once. No exceptions at
    all. None. Zero. Zip. Nada. After substitutions are performed
    the first word becomes the command and all the other words
    become the arguments. Always. Always, always. Always.


    To avoid substitutions, place a \ before special characters,
    or place a whole string of special characters inside curly
    braces. Things in curly braces will never, ever, ever, ever
    be parsed before being passed to the command. Never, ever,
    ever. No exceptions.

    The only tricky part (the _only_ tricky part) is knowing
    that some commands will do their own parsing of arguments.
    But only *after* the line of code is parsed in the usual
    manner. Examples of these commands are "if", "expr",
    "while" and a few others.

    Unless you know for certain what you are doing, and know
    for a fact that you want something to potentially be
    evaluated twice, always, always, always provide such
    arguments in curly braces. Always. Unless you know what
    precisely what you are doing.

    When you truly understand the above, you are a tcl expert.

Reference: [1]

LV While it may look like the paragraph 'the only tricky part' contradicts the first paragraph, it doesn't. The line of code is only parsed once. However, the resulting of that parse is a command name and zero or more arguments. IF there are arguments, when the command is invoked, it may be written to perform an eval on one or more of those arguments. Most commands don't. However, a few do.

[Replace this line with the name of a wiki page which discusses the commands which perform one or more additional evals]

FW: Hum. My understanding was that things in the global scope are re-parsed every time they're run (for instance, loop bodies, bindings etc.) on the theory that in the majority of cases things in the global scope are gonna be executed once anyway. I've heard you're supposed to keep loops in procs for that reason, for instance. Am I wrong?

WHD: I think you're confusing parsing with byte-compiling. - RS: Also, loop and if bodies are compiled to bytecode when first encountered - e.g. in
 if 1 $body

the body is compiled, while
 eval $body

it is not. Bindings on the other hand are not byte-compiled, as there are often some words appended to the string. But UI reaction time in Tk is rarely a reason for complaints...