1

I am puzzled by the backslash operator as it is used in the following example:

foreach op {+ - * / %} {proc $op {a b} "expr {\$a $op \$b}"}

When the backslashes are within the curly braces, keyboard input (in prefix notation) is executed. When I delete the backslashes the error message is "can't read "a": no such variable".

Googling this just told me, over and over, about escape characters and line continuation. The backslash operator in this context is different: How is it that \$a accepts keyboard input? Should I have been googling something other than "TCL backslash operator"?

1
  • A little comment aside, in newer Tcl versions you already have all of those procs in the ::tcl::mathop namespace and could just import them.
    – schlenk
    Jan 1, 2015 at 13:07

1 Answer 1

5

Your observation (or the language you are using to describe it) is incorrect. The backslash does not accept keyboard input. It is used as an escape character, nothing more. There's nothing special about its behavior in this context.

Let's start by looking at what the code is trying to accomplish. It's trying to create procs like this:

proc + {a b} {
    expr {$a + $b}
}

proc - {a b} {
    expr {$a - $b}
}

... and so on. Only, it's doing it in a loop where the operators and proc name are in variables.

If you split the original code across a few lines it might be easier to visualize what's going on:

foreach op {+ - * / %} {
    proc $op {a b} "expr {\$a $op \$b}"
}

Notice that inside the loop is an almost normal proc definition. However, instead of using curly braces to define the body of the proc (proc $op {a b} {...}) it's using double quotes (proc $op {a b} "..."). Because it is using double quotes, everything in the body of the proc is evaluated at the time that the foreach command runs. That is why you get the error about no such variable named "a". "a" isn't defined in the foreach loop, and neither is "b". Only "op" is defined.

To prevent $a from being substituted at the time the foreach command runs, the backslash is used to escape the special meaning of $. Thus, "expr {\$a $op \$b}" becomes expr {$a + $b} (or -, or *, etc).

Why is this being done? Imagine if the proc statement used more traditional curly braces:

foreach op {+ - * / %} {
    proc $op {a b} {
        expr {$a $op $b}
    }

In this case, $a and $b are just normal variables inside a normal procedure. So far so good. However, there's a problem. Notice $op? The proc has no idea what $op is, so if you actually run the above code and then try to use one of the new procs that was created, you'd get an error about an unknown variable named op.

So, there's a conflict: you want $a and $b to be literally $a and $b, but you want $op to be replaced with the value of $op from the loop. Right? So, in this case the person who wrote the code decided to use double quotes instead of curly braces to define the proc -- perfectly legal, though somewhat uncommon. This means that every variable in the body of the proc will get evaluated at the time the foreach runs. This is great for $op, but not great for $a and $b. So, to keep those variables un-expanded at the time the foreach runs, the dollar sign is escaped.

Thus, when the foreach runs, it creates a proc with a body that looks like expr {$a + $b}, expr {$a - $b}, and so on.

7
  • A way to make this more explicit is using format to create the body of the proc. You could write: foreach op {+ - * / %} { proc $op [format {expr {$a %s $b}} $op] }
    – schlenk
    Jan 1, 2015 at 13:05
  • Thanks, Bryan and schlenk, the mechanism behind the code is a little clearer now. What is still nagging me (and Google has, so far, been less than helpful with), is: how is it that TCL, after the above proc as been entered, then sits waiting for keyboard input? And how does it know to store that input first in "a", then "b", the "op" variables?
    – Otto Hunt
    Jan 1, 2015 at 20:58
  • @user901750: order is important. The first argument goes to the first parameter, the second to the second, and so on. Jan 1, 2015 at 23:01
  • So does TCL direct the first character of keyboard input to the innermost curly braces that follow the "proc %op"? If so, then does the third character entered then fill the "op" variable which is outside of any curly braces? Finally, does the "$" token (inside another set of curly braces) prevent keyboard substitution?
    – Otto Hunt
    Jan 2, 2015 at 0:10
  • @user901750: no. Read this page, it literally explains everything about the Tcl parsing rules on one page: tcl.tk/man/tcl8.5/TclCmd/Tcl.htm . The short answer is that tcl breaks a command line up into "words". The first "word" is the command to run, the next "word" is the first argument to the command, the second "word" goes to the second argument, and so on. A "word" can be more than one literal word, which you control with quoting (curly braces and double quotes, mostly). Jan 2, 2015 at 0:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.